var scroll = new Object();
scroll.init = function() {
	// 
	this.content = new scrollableContent();
	this.area = new scrollArea(this.content.getContentHeight(),this.content.getHeight(),5,this.content);
	var self = this;
	jQuery('#btnOpen').click(function() {
		
		// Получаем размер окна
		var aPageSize = getPageSize();

		// Получаем текущий скролл
		var aPageScroll = getPageScroll();

		// Получаем размер объекта
		var aPopupOffset = [jQuery('#popup').width(),jQuery('#popup').height()];


		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.onclick = function () {jQuery('#btnClose').click(); return false;}
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';

		objOverlay.style.width = '100%';
//		document.body.insertBefore(objOverlay, document.body.firstChild);
		document.body.appendChild(objOverlay);

		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.display = 'block';

		// Расчитываем координаты
		var nTop = aPageScroll[1] + (aPageSize[3] - aPopupOffset[1]) / 2;
		jQuery('#popup').css('top',nTop + 'px');

		// Отображаем
		document.getElementById('popup').style.zIndex = 20000;
		jQuery('#popup').css('visibility','visible');

		self.area.show();
		return false;
	})
	jQuery('#btnClose').click(function() {
		jQuery('#overlay').remove();
		jQuery('#popup').css('visibility','hidden');
		return false;
	})
	jQuery('div.topBtn').click(scroll.Up);
	jQuery('div.botBtn').click(scroll.Down);
	
}
scroll.scrollDown = function() {

}
scroll.scrollUp = function() {
}
// ---------------------------------------------------------------------------------
// Контрол для работы с полем контента
// ---------------------------------------------------------------------------------
scrollableContent = function() {
	this.area = jQuery('#popup .news').get(0);
	this.contentHeight = 0;
	this.height = jQuery('#popup .news').height();
	this.percent = 0;

	this.calculateHeight();
}
scrollableContent.prototype.scrollUp = function (percent) {
	this.percent -= percent;
	if (this.percent < 0) {
		this.percent = 0;
	}
	this.reDraw();
}
scrollableContent.prototype.scrollDown = function (percent) {
	this.percent += percent;
	if (this.percent > 100) {
		this.percent = 100;
	}
	this.reDraw();

}
scrollableContent.prototype.setupPercent = function (percent) { 
	this.percent = percent;
	if (this.percent > 100) {
		this.percent = 100;
	}
	if (this.percent < 0) {
		this.percent = 0;
	}
	this.reDraw();

}
scrollableContent.prototype.calculateHeight = function () {
	var i = 0;
	var height = 0;
	var result = 0;
	for (i = 0; i < this.area.childNodes.length; i++) {
		var row = this.area.childNodes[i];
		if (row.tagName && row.className == 'newItem') {
			height += jQuery(row).height() + 33;
		}
	}
	var offset = jQuery('#popup .news').offset();
	this.contentHeight = height - offset['top'];
	if (offset['top'] > height) {
		this.contentHeight = offset['top'];
	} else {
		this.contentHeight = height - offset['top'];
	}
}
scrollableContent.prototype.reDraw = function () {
	var i = 0;
	while (!this.area.childNodes[i].tagName) { 
		i++;
	}
	if (this.area.childNodes[i].tagName) {
		// Расчет высоты
		this.area.childNodes[i].style.marginTop="-" + this.getOffset() + 'px';
	}
}
scrollableContent.prototype.getOffset = function () {
	//alert('Height = ' + this.height);
	//alert('Percent = ' + this.percent);
	//alert(parseInt((this.height * this.percent) / 100));
	return parseInt((this.contentHeight * this.percent) / 100);
}
scrollableContent.prototype.getHeight= function () {
	return this.height;
}
scrollableContent.prototype.getContentHeight= function () {
	return this.contentHeight;
}
// ---------------------------------------------------------------------------------
// Контрол для работы со скроллом
// ---------------------------------------------------------------------------------

function scrollArea(zoneContentHeight,zoneHeight,step,content) {
	this.content = content;
	this.percent = 0;
	this.scrollStep = step;
	this.height = jQuery('div#scrolling .scrollArea').height();
	this.pointerHeight = parseInt((zoneHeight / zoneContentHeight) * this.height);
	this.height -= this.pointerHeight;
	var offset = jQuery('div#scrolling .scrollArea').offset();
	this.offsetPointer = 0;
	this.top = offset['top'];
	jQuery('div#scrolling .scrollArea .scrollPointer').css('height',this.pointerHeight + 'px');
	var self = this;

	jQuery('div#scrolling .topBtn a').click(function() {
		self.scrollUp();
		return false;
	});
	jQuery('div#scrolling .botBtn a').click(function() {
		self.scrollDown();
		return false;
	});
	// 
	this.startMove = false;
	// Подвешиваем обработки для захвата скролла
	jQuery('div#scrolling .scrollArea').click(function (){
		self.startMove = true;

	});
	jQuery('div#scrolling .scrollArea').mousemove(function (e){
		if (!self.startMove) {
			return false;
		}
//		jQuery('#x').append('<h1> true  : ' + (e.pageY - self.top ) + '</h1>');
		// Определяем текущую позицию 
		self.setupByHeight(e.pageY - self.top - self.offsetPointer);
		// Ставим процент
		return false;
	});
	jQuery('div#scrolling .scrollArea').mouseout(function (){
//		jQuery('#x').html('<h1> false </h1>');
		self.startMove = false;
		return false;
	});
	jQuery('div#scrolling .scrollArea .scrollPointer').mousedown(function (e){
		// Определяем текущую позицию 
//		jQuery('#x').html('<h1> true </h1>');
		var offset = jQuery('div#scrolling .scrollArea .scrollPointer').offset()
		self.offsetPointer = e.pageY - offset['top'];
		self.startMove = true;

	});
	jQuery('div#scrolling .scrollArea .scrollPointer').mouseup(function (){
//		jQuery('#x').html('<h1> false </h1>');
		self.startMove = false;
	});
	jQuery('div#scrolling .scrollArea ').click(function (){
//		jQuery('#x').html('<h1> false </h1>');
		self.startMove = false;
	});
}
scrollArea.prototype.show = function () {
	var offset = jQuery('div#scrolling .scrollArea').offset();
	this.top = offset['top'];

}
/**
*  
*/
scrollArea.prototype.setupPercent = function (percent) {
	this.percent = percent;
	if (this.percent > 100) {
		this.percent = 100;
	}
	if (this.percent < 0) {
		this.percent = 0;
	}
	this.reDraw();
	this.content.setupPercent(this.percent);
}
/**
*  Устанавливает процент по смещению
*/
scrollArea.prototype.setupByHeight = function (offset) {
	var percent = parseInt((offset / this.height) * 100);
	this.setupPercent(percent);
}
scrollArea.prototype.scrollDown = function () {
	this.percent += this.scrollStep;
	if (this.percent > 100) {
		this.percent = 100;
	}
	this.content.setupPercent(this.percent);
	this.reDraw();
}
scrollArea.prototype.scrollUp = function () {
	this.percent -= this.scrollStep;
	if (this.percent < 0) {
		this.percent = 0;
	}
	this.content.setupPercent(this.percent);
	this.reDraw();
}
scrollArea.prototype.reDraw = function () {
//	alert(this.height);
//	alert(this.percent);
	jQuery('div#scrolling .scrollArea .scrollPointer').css('margin-top',this.getOffset() + 'px');
}
scrollArea.prototype.getOffset = function () {
	return parseInt((this.height * this.percent) / 100);
}



function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;
	var xScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}


jQuery(document).ready(scroll.init);


