/**
 * @author Lart
 */

function cr(name){	return document.createElement(name);}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	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;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
}




 var OLGgalery=Class.create();
 
 OLGgalery.prototype ={
 	
	mainDiv:'',
	imListUl:'',
	divImgList:'',
	scrollDiv:'',
	bgDiv:'',
	bigImg:'',
	bigImgDiv:'',
	
	bgAlert:'#262626',
	bgOpacity:'0.8',
	parentElement:'',
	imgList:[],
	imgCount:0,
	imgSelectedIndex:0,
	phpthumb:PHPTHUMB,
	closeImgSrc:'/images/close.gif',
	nextImgSrc:'/images/next.gif',
	prevImgSrc:'/images/prev.gif',
	divCurItem:'',
	loadingImg:'',
	bigImgVisibled:false,
	loadingImgVisibled:false,
	DragStarted:false,
	mX:0,
	mY:0,
	mX1:0,
	mY1:0,
	mPos:[],
	scrollBarX:0,
	imListUlWidth:0,
	initialize:function(){
		this.loadingImg=new Image();
		this.loadingImg.src=loadingBarUrl;
		//this.imL
		if (typeof arguments[0]=='string')
			this.parentElement=$(arguments[0])
		else
			this.parentElement=arguments[0];
		this.options=Object.extend({
			width:573,
			height:100,
			scrollBarHeight:19,
			imgWidth:100,
			imgHeight:100,
			imgPadding:2
		},arguments[1] || '');
		
		this.MouseDown=this.initDrag.bindAsEventListener(this);
		this.MouseMove=this.moveDrag.bindAsEventListener(this);
		this.MouseUp=this.endDrag.bindAsEventListener(this);
		
		this.mainDiv=cr('div');
		this.mainDiv.style.width=this.options.width+'px';
		this.mainDiv.style.height=(this.options.height+this.options.scrollBarHeight+this.options.imgPadding*2)+'px';
		this.mainDiv.style.overflow='hidden';
		this.mainDiv.style.position='relative';
		this.mainDiv.style.border='1px solid #000';

		this.imListUl=cr('ul');
		this.imListUl.style.height=this.options.height+'px';
		this.imListUl.style.position='absolute';
		this.imListUl.className='Galery';
		
		this.scrollDivBg=cr('div');
		this.scrollDivBg.style.width='100%';
		this.scrollDivBg.style.height='17px';
		this.scrollDivBg.style.backgroundColor="#000";
		this.scrollDivBg.style.position = 'relative';
		Event.observe(this.scrollDivBg,'click',this.clickScrollBar.bindAsEventListener(this));
		this.scrollDivBg.style.top = (this.options.height + this.options.imgPadding * 2) + 'px';
		this.scrollDiv=cr('div');
		this.scrollDivCenter=cr('div');
		 
		this.scrollDivCenter.style.height='17px';
		this.scrollDivCenter.className="Galery_center_Scroll";
		
			
			this.scrollDiv.style.height = (this.options.scrollBarHeight) + 'px';
			var l=cr('div');l.style.width='7px';l.style.height='17px';
			l.className='Galery_left_Scroll';
			
			this.scrollDiv.appendChild(l);
			this.scrollDiv.appendChild(this.scrollDivCenter);
			l=cr('div');l.style.width='7px';l.style.height='17px';
			l.className='Galery_right_Scroll';
			this.scrollDiv.appendChild(l);
			this.scrollDiv.style.position = 'absolute';
			this.scrollDiv.style.left="0px";
		
		this.scrollDivBg.appendChild(this.scrollDiv);
		Event.observe(this.scrollDiv,'mousedown',this.MouseDown);
		
		this.mainDiv.appendChild(this.imListUl);
		this.mainDiv.appendChild(this.scrollDivBg);
		//this.mainDiv.appendChild(this.scrollDiv);
		this.parentElement.appendChild(this.mainDiv);
		
		
		this.bgDiv=cr('div');
		this.bgDiv.style.display='none';
		this.bgDiv.style.zIndex='98';
		
		this.bigImg=cr('img');
		this.bigImg.id='bimg';
		this.bigImg.style.position='absolute';
		this.closeImg=cr('img');
		this.closeImg.src=this.closeImgSrc;
		this.closeImg.style.position='absolute';
		this.closeImg.style.right='0px';
		this.closeImg.style.top='0px';
		this.closeImg.style.display='none';
		this.closeImg.style.cursor='pointer';
		
		this.nextImg=cr('img');
		this.nextImg.src=this.nextImgSrc;
		this.nextImg.style.position='absolute';
		this.nextImg.style.right='24px';
		this.nextImg.style.top='5px';
		this.nextImg.style.display='none';
		this.nextImg.style.cursor='pointer';
		
		this.divCurItem=cr('div');
		this.divCurItem.style.display='none';
		this.divCurItem.style.position='absolute';
		this.divCurItem.style.right='40px';
		this.divCurItem.style.top='5px';
		this.divCurItem.style.color='#000';
		this.divCurItem.style.backgroundColor='#EEE';
		this.divCurItem.style.fontWeight='bold';
		this.divCurItem.style.width='38px';
		this.divCurItem.style.height='15px';
		this.divCurItem.style.border='none';
		this.divCurItem.innerHTML='0 / 0';
		this.divCurItem.style.cursor='pointer';
		this.divCurItem.style.textAlign='center';
		
		this.prevImg=cr('img');
		this.prevImg.src=this.prevImgSrc;
		this.prevImg.style.position='absolute';
		this.prevImg.style.right='79px';
		this.prevImg.style.top='5px';
		this.prevImg.style.display='none';
		this.prevImg.style.cursor='pointer';
		
		this.bigImgDiv=cr('div');
		this.bigImgDiv.style.position='absolute';
		this.bigImgDiv.style.zIndex='99';
		this.bigImgDiv.style.overflow='hidden';
		this.bigImgDiv.style.backgroundColor='#FFF';
		
		this.bigImgDiv.appendChild(this.bigImg);
		this.bigImgDiv.appendChild(this.closeImg);
		this.bigImgDiv.appendChild(this.nextImg);
		this.bigImgDiv.appendChild(this.prevImg);
		this.bigImgDiv.appendChild(this.divCurItem);
		
		Event.observe(this.bigImg,'load',this.bigImgLoaded.bind(this));
		Event.observe(this.bigImg,'click',this.showNextImg.bindAsEventListener(this));
		Event.observe(this.closeImg,'click',this.closeBigImg.bindAsEventListener(this));
		Event.observe(this.nextImg,'click',this.showNextImg.bindAsEventListener(this));
		Event.observe(this.prevImg,'click',this.showPrevImg.bindAsEventListener(this));
		Event.observe(window,'load',this.afterLoad.bind(this));
		
	},
	afterLoad:function(){
			this.mPos=Position.cumulativeOffset(this.mainDiv);	
	},
	insertImg:function(src){
		this.imgList[this.imgCount]=cr('img');
		//this.imgList[this.imgCount].src=this.phpthumb+'?src='+src+'&w='+this.options.imgWidth+'&h='+this.options.imgHeight+'&zc=1';
		var name=src.match(/(.*?)\.(...)$/);
		this.imgList[this.imgCount].src=name[1]+'_100.'+name[2];
		this.imgList[this.imgCount].style.cursor='pointer';
		Event.observe(this.imgList[this.imgCount],'mouseover',this.imgOver.bindAsEventListener(this));
		Event.observe(this.imgList[this.imgCount],'mouseout',this.imgOut.bindAsEventListener(this));
		Event.observe(this.imgList[this.imgCount],'click',this.imgClick.bindAsEventListener(this));
		var li=cr('li');
		li.style.cssFloat="left";
		li.style.position='relative';
		li.appendChild(this.imgList[this.imgCount]);
		this.imListUl.appendChild(li);
		this.imgCount++;
		this.imListUlWidth=((this.options.imgWidth+10+(this.options.imgPadding*2))*this.imgCount);
		this.imListUl.style.width=this.imListUlWidth+'px';
		this.calcScrollBarWidth();
	},
	calcScrollBarWidth:function(){
		
		var widthPercent=Math.floor(this.options.width*100/((this.options.imgWidth+1+this.options.imgPadding*2)*this.imgCount));
		this.scrollBarWidth=Math.floor(this.options.width*widthPercent/100);
		this.scrollDivCenter.style.width=(this.scrollBarWidth-14)+'px';
		if (this.scrollBarWidth>=this.options.width){
			this.scrollDiv.style.display='none';
		}else{
			this.scrollDiv.style.display='block';
		}
		this.scrollDiv.style.width=this.scrollBarWidth+'px';
	},
	initDrag:function(event){
		//alert(this.options.width);
		this.DragStarted=true;
		this.mX=Event.pointerX(event);
		Event.observe(document,'mouseup',this.MouseUp);
		Event.observe(document,'mousemove',this.MouseMove);
		
		document.body.ondrag = function () { return false; };
    	document.body.onselectstart = function () { return false; };
		Event.stop(event);
	},
	moveDrag:function(event){
		this.mX1=Event.pointerX(event);
		if (this.mX1>(this.mPos[0]+this.options.width) || this.mX1<this.mPos[0])
			return;
		var razn=this.mX1-this.mX;
		this.mX=this.mX1;
		this.scrollBarX=this.scrollBarX+razn;
		if ((this.scrollBarX + this.scrollBarWidth) >= this.options.width) {
			this.scrollBarX = (this.options.width - this.scrollBarWidth);
		}else if (this.scrollBarX<=0)
			this.scrollBarX=0;
		
		this.scrollDiv.style.left=this.scrollBarX+'px';
		var scrollBarXPercent=Math.floor(this.scrollBarX*100/this.options.width);
		this.imListUl.style.left=(0-Math.floor(this.imListUlWidth*scrollBarXPercent/100))+'px';
	},
	endDrag:function(event){
		Event.stopObserving(document,'mouseup',this.MouseUp);
		Event.stopObserving(document,'mousemove',this.MouseMove);
	},
	clickScrollBar:function(event){
		if (this.scrollDiv.style.display=='none')return;
		this.moveDrag(event);
	},
	imgOver:function(event){
		var img=Event.findElement(event,'li');
		var p1=this.options.imgPadding-1;
		var p2=this.options.imgPadding+1;
		img.style.margin='0px';
		img.style.paddingLeft=p1+'px';
		img.style.paddingTop=p1+'px';
		img.style.paddingBottom=p2+'px';
		img.style.paddingRight=p2+'px';
	},
	imgOut:function(event){
		var img=Event.findElement(event,'li');
		img.style.padding='0px';
		img.style.margin=this.options.imgPadding+'px';
	},
	imgClick:function(event){
		document.body.appendChild(this.bgDiv);
		var img=Event.findElement(event,'img');
		var i=0;
		for(i=0; i < this.imgCount; i++)
			if (img == this.imgList[i]) this.imgSelectedIndex = i;
		//var src=img.src.match(/\?src=(.*?)&/im);
		//var src=img.src.match(/\d*?src=(.*?)&/im);
		//alert(img.src);
		var name=img.src.match(/(.*?)\.(...)$/im);
		//alert(name)
		var n=name[1].match(/(.*?)_(\d*)$/im);
		var src=n[1]+'.'+name[2];
		//alert(src);
		this.pageScroll=getPageScroll();
		this.pageSize=getPageSize();
		this.bgDiv.style.backgroundColor=this.bgAlert;
		Element.setOpacity(this.bgDiv,this.bgOpacity);
		
		this.bgDiv.style.position='absolute';
		
		var pageScroll=getPageScroll();
		var pageSize=getPageSize();
		
		this.bgDiv.style.width=pageSize[0]+'px';
		this.bgDiv.style.height=pageSize[1]+'px';
		this.bgDiv.style.top='0px';
		this.bgDiv.style.left='0px';
		this.bgDiv.style.display='block';
		this.closeImg.style.display='none';
		this.nextImg.style.display='none';
		this.prevImg.style.display='none';
		this.bigImg.style.display='none';
		this.divCurItem.style.display='none';
		
		document.body.appendChild(this.loadingImg);
		this.loadingImg.style.position="absolute";
		this.loadingImg.style.zIndex='100';
	
		this.loadingImgVisibled=true;
		//alert(this.loadingImg.id)
		var ImgSize={width:loadingBarWidth,height:loadingBarHeight}
		var ImgWidth=Math.round(ImgSize.width-ImgSize.width/2);
		var ImgHeight=Math.round(ImgSize.height-ImgSize.height/2);
		var ImgTop=this.pageScroll[1]+(this.pageSize[3]/2-ImgHeight/2);
		var ImgLeft=this.pageScroll[0]+(this.pageSize[2]/2-ImgWidth/2);
		this.loadingImg.style.top=ImgTop+'px';
		this.loadingImg.style.left=ImgLeft+'px';
		
		
		this.loadingImg.style.display='block';
		
		Event.observe(this.bgDiv,'click',this.closeBigImg.bindAsEventListener(this));
		
		this.bigImg.src=src;//this.phpthumb+'?src='+src[1]+'&w=800&h=600';
	},
	closeBigImg:function(event){
		//window.clearInterval(this.tId);
		if (this.bigImgVisibled)
			document.body.removeChild(this.bigImgDiv);
		if (this.loadingImgVisibled){
			document.body.removeChild(this.loadingImg);
			document.body.removeChild(this.bgDiv);
		}
		this.bgDiv.style.display='none';
		this.bigImgVisibled=false;
		this.loadingImgVisibled=false;
	},
	bigImgLoaded:function(){
		if (this.loadingImgVisibled)
			document.body.removeChild(this.loadingImg);
		else 
			return;
		this.loadingImgVisibled=false;
		this.bigImgVisibled=true;
		this.loadingImg.style.display='none';
		this.pageScroll=getPageScroll();
		this.pageSize=getPageSize();
		document.body.appendChild(this.bigImgDiv);
		this.bigImgSize=this.bigImg.getDimensions();
		this.bigImgDivWidth=Math.round(this.bigImgSize.width-this.bigImgSize.width/2);
		this.bigImgDivHeight=Math.round(this.bigImgSize.height-this.bigImgSize.height/2);
		this.bigImgDivTop=this.pageScroll[1]+(this.pageSize[3]/2-this.bigImgDivHeight);
		this.bigImgDivLeft=this.pageScroll[0]+(this.pageSize[2]/2-this.bigImgDivWidth);
		
		this.bigImgDiv.style.width=this.bigImgDivWidth+'px';
		this.bigImgDiv.style.height=this.bigImgDivHeight+'px';
		this.bigImgDiv.style.top=this.bigImgDivTop+'px';
		this.bigImgDiv.style.left=this.bigImgDivLeft+'px';
		this.bigImg.style.display='none';
		//this.tId=window.setInterval(this.slowShow.bind(this),1);
		this.slowShow();
		this.divCurItem.innerHTML = (this.imgSelectedIndex + 1) + ' / ' + this.imgCount;
		
	},
	slowShow:function(){
/*		if (this.bigImgDivWidth < this.bigImgSize.width+10) {
			this.bigImgDivWidth = this.bigImgDivWidth + 6;
			this.bigImgDivLeft=this.bigImgDivLeft-3;
			this.bigImgDiv.style.width = this.bigImgDivWidth + 'px';
			this.bigImgDiv.style.left = this.bigImgDivLeft + 'px';
		}
		if (this.bigImgDivHeight < this.bigImgSize.height+10 && this.bigImgDivWidth >= this.bigImgSize.width+10) {
			this.bigImgDivHeight = this.bigImgDivHeight + 6;
			this.bigImgDivTop=this.bigImgDivTop-3;
			this.bigImgDiv.style.height = this.bigImgDivHeight + 'px';
			this.bigImgDiv.style.top = this.bigImgDivTop + 'px';
		}
		
		if (this.bigImgDivWidth>=this.bigImgSize.width+10 && this.bigImgDivHeight >= this.bigImgSize.height+10){*/


			this.bigImgDiv.style.width=(this.bigImgSize.width+10)+'px';
			this.bigImgDiv.style.height=(this.bigImgSize.height+10)+'px';
			this.bigImg.style.left='5px';
			this.bigImg.style.top='5px';
			
			this.bigImg.style.clear='both';
			this.bigImg.style.display='inline';
			this.closeImg.style.display='inline';
			this.closeImg.style.clear='both';
			this.nextImg.style.display='inline';
			this.nextImg.style.clear='both';
			this.divCurItem.style.display='inline';
			this.divCurItem.style.clear='both';
			this.prevImg.style.display='inline';
			this.prevImg.style.clear='both';
			//window.clearInterval(this.tId);



		},

		// added by Magician

	showNextImg:function(event){
		if (this.imgSelectedIndex < (this.imgCount - 1)){
			this.imgSelectedIndex++;
			var name = this.imgList[this.imgSelectedIndex].src.match(/(.*?)\.(...)$/im);
			var n = name[1].match(/(.*?)_(\d*)$/im);
			var src = n[1]+'.'+name[2];
			this.bigImg.src = src;
			document.body.appendChild(this.loadingImg);
			this.loadingImgVisibled=true;
			this.loadingImg.style.display="block";
		}
	},
	showPrevImg:function(event){
		if (this.imgSelectedIndex > 0){
			this.imgSelectedIndex--;
			var name = this.imgList[this.imgSelectedIndex].src.match(/(.*?)\.(...)$/im);
			var n = name[1].match(/(.*?)_(\d*)$/im);
			var src = n[1]+'.'+name[2];
			this.bigImg.src = src;
			document.body.appendChild(this.loadingImg);
			this.loadingImgVisibled=true;
			this.loadingImg.style.display="block";
		}
	}

//	}
 }

