
var PG = {};

function $(id){
	return document.getElementById(id);
}
function $$(element,parentTag){
	parentTag = parentTag||document; 
	return parentTag.getElementsByTagName(element);
}

PG.init = function(){
	var uls = $$('ul');
	var pgids = [];
	for(var u=0; u<uls.length; u++){
		if(uls[u].className=='gallery'){
			var gname = uls[u].id.split('$')[0];
			var re = new RegExp(gname);
			if(pgids.join('|').search(re)==-1 ) {
				var gwidth = parseInt(uls[u].style.width);
				var gheight = parseInt(uls[u].style.height);
				pgids.push(gname);
				eval('_'+gname+' = new PG.photoGallery("'+uls[u].id+'",'+gwidth+','+gheight+')');
				eval('_'+gname).init(uls[u].id);
			}
		}
	}
}

PG.photoGallery = function(gid, width, height){
	var d = document;
	var gida = gid.split('$');//[name,thumbs,captions,controls,autoplay,speed]
	this.name = '_'+gida[0];
	var thumbheight = parseInt(gida[1]);
	this.thumbs = thumbheight>0? true:false;
	this.captions = gida[2];
	this.captioncontrols = this.captions==1? gida[3]:0;
	var gallery = this.gallery = $(gid);
	var wrapper = this.wrapper = d.createElement('div');
	wrapper.style.width = width+'px';
	wrapper.style.height = height+'px';
	wrapper.style.textAlign = 'left';
	wrapper.className = 'wrapper';
	gallery.parentNode.insertBefore(wrapper, gallery);
	gallery.style.display = 'none';
	var fullsize = d.createElement('div');
	fullsize.style.width = width+'px';
	fullsize.style.height = height-thumbheight-(thumbheight==0?0:8)-(this.captioncontrols==0?0:15)+'px';
	fullsize.className = 'fullsize';
	wrapper.appendChild(fullsize);
	this.pause = d.createElement('div');
	this.pause.style.position = 'absolute';
	this.pause.style.width = width+'px';
	this.pause.style.top = (Math.ceil(parseInt(fullsize.style.height)/2) - 30) + 'px';
	this.pause.className = 'pause';
	fullsize.appendChild(this.pause);
	this.prev = d.createElement('div');
	this.prev.style.position = 'absolute';
	this.prev.style.width = Math.ceil(parseInt(width*.25)) + 'px';
	this.prev.style.top = (Math.ceil(parseInt(fullsize.style.height)/2) - 50) + 'px';
	this.prev.style.left = 0;
	this.prev.className = 'prev';
	fullsize.appendChild(this.prev);
	this.next = d.createElement('div');
	this.next.style.position = 'absolute';
	this.next.style.width = Math.ceil(parseInt(width*.25)) + 'px';
	this.next.style.top = (Math.ceil(parseInt(fullsize.style.height)/2) - 50) + 'px';
	this.next.style.right = 0;
	this.next.className = 'next';
	fullsize.appendChild(this.next);
	this.image = d.createElement('div');
	this.image.style.width = width+'px';
	this.image.style.height = height-thumbheight-(thumbheight==0?0:8)-(this.captioncontrols==0?0:15)+'px';
	this.image.className = 'image';
	fullsize.appendChild(this.image);
	if(this.captions==1){
		this.caption = d.createElement('div');
		this.caption.style.position = 'absolute';
		this.caption.style.width = width+'px';
		this.caption.style.bottom = 0;
		this.caption.className = 'caption';
		fullsize.appendChild(this.caption);
		var h3 = d.createElement('h3');
		this.caption.appendChild(h3);
		var p = d.createElement('p');
		this.caption.appendChild(p);
	}
	if(this.thumbs){
		var thumbnails = d.createElement('div');
		thumbnails.style.width = width+'px';
		thumbnails.style.height = thumbheight+8+'px';//+15
		thumbnails.classname = 'thumbnails';
		wrapper.appendChild(thumbnails);
		this.left = d.createElement('div');
		this.left.style.height = thumbheight/2+14+'px';
		this.left.style.width = '20px';
		this.left.style.paddingTop = thumbheight/2-14+'px';
		this.left.className = 'slideleft';
		thumbnails.appendChild(this.left);
		this.right = d.createElement('div');
		this.right.style.height = thumbheight/2+14+'px';
		this.right.style.width = '20px';
		this.right.style.paddingTop = thumbheight/2-14+'px';
		this.right.className = 'slideright';
		thumbnails.appendChild(this.right);
		var slidearea = d.createElement('div');
		slidearea.style.position = 'relative';
		slidearea.style.height = thumbheight+4+'px';
		slidearea.style.width = width-40+'px';
		slidearea.style.overflow = 'hidden';
		slidearea.className = 'slidearea';
		thumbnails.appendChild(slidearea);
		this.slider = d.createElement('div');
		this.slider.id = this.name + '_slider'
		this.slider.style.position = 'absolute';
		this.slider.style.height = thumbheight+4+'px';
		this.slider.style.left = 0;
		this.slider.className = 'slider';
		slidearea.appendChild(this.slider);
	}
	if(this.captioncontrols==1){
		this.toggle = d.createElement('div');
		this.toggle.style.height = '15px';
		this.toggle.style.width = Math.ceil(width*.95)+'px';
		this.toggle.className = 'togglecaptions';
		wrapper.appendChild(this.toggle);
	}	
	this.auto = gida[4];
	this.speed = gida[5];
	this.infoShow = gida[2];
	this.width = width;
	//parameters
	this.infoSpeed = this.imgSpeed = 10; 
	this.thumbOpacity = 70;
	this.navHover = 90;
	this.navOpacity = 0;
	this.scrollSpeed = 5;
	this.spacing = 5;
	this.letterbox = '#000';
	this.active = '#fff';
	this.currentSlide = 0;
	this.slidesArray = [];
};

PG.photoGallery.prototype = {
	init:function(gal){ //id for ul "gallery"
			gal = $(gal);
		var slidesList = $$('li',gal), i = 0, sliderWidth = 0;
		this.slidesCount = slidesList.length;
		if(this.thumbs){
			this.left.innerHTML = '&#8249;';
			this.right.innerHTML = '&#8250;';
			this.left.onmousedown = new Function('PG.scroll.init("'+this.slider.id+'",-1,'+this.scrollSpeed+')');
			this.left.onmouseup = this.right.onmouseup = this.left.onmouseout = this.right.onmouseout = new Function('PG.scroll.cl("'+this.slider.id+'")');
			this.right.onmousedown = new Function('PG.scroll.init("'+this.slider.id+'",1,'+this.scrollSpeed+')');
		}
		for(i; i<this.slidesCount; i++){
			this.slidesArray[i] = {};
			var thisSlide = slidesList[i], a = this.slidesArray[i];
			a.t = typeof $$('h3',thisSlide)[0] !='undefined'? $$('h3',thisSlide)[0].innerHTML:'';
			a.d = typeof $$('p',thisSlide)[0] !='undefined'? $$('p',thisSlide)[0].innerHTML:'';
			a.l = typeof $$('a',thisSlide)[0] !='undefined'? $$('a',thisSlide)[0].href:'';
			a.p = typeof $$('span',thisSlide)[0] !='undefined'? $$('span',thisSlide)[0].innerHTML:'';
			if(this.thumbs){
				if(typeof $$('img',thisSlide)[0] != 'undefined'){
					var thisImage = $$('img',thisSlide)[0];
					this.slider.appendChild(thisImage);
					a.slidePosition = sliderWidth;
					a.slideWidth = parseInt(thisImage.offsetWidth);
					sliderWidth += a.slideWidth;
					if(i != this.slidesCount-1){
						thisImage.style.marginRight = this.spacing+'px';
						sliderWidth += this.spacing;
					}
					this.slider.style.width = sliderWidth+'px';
					thisImage.style.opacity = this.thumbOpacity/100;
					thisImage.style.filter = 'alpha(opacity = '+this.thumbOpacity+')';
					thisImage.onclick = new Function(this.name+'.pr('+i+',1)');
				}
			}
		}
		if(this.captioncontrols==1){
			this.toggle.innerHTML = 'Hide Caption';
			this.toggle.onclick = new Function(this.name+'.toggleinfo()');
		}
		this.prev.innerHTML = '&laquo;';//&#8249;';//'&#0171';
		this.prev.title = 'Previous Photo'
		this.next.innerHTML = '&raquo;';//'&#8250;';//'&#0187';
		this.next.title = 'Next Photo';
		this.pause.innerHTML = this.auto==1? '<b>&#124;&#124;</b>':'&#9658;';
		this.pause.title = this.auto==1? 'Pause':'Play';
		this.prev.style.opacity = this.next.style.opacity = this.pause.style.opacity = this.navOpacity/100;
		this.prev.style.filter = this.next.style.filter = this.pause.style.filter = 'alpha(opacity = '+this.navOpacity+')';
		this.prev.onmouseover = this.next.onmouseover = this.pause.onmouseover = new Function('PG.alpha.set(this,'+this.navHover+',5)');
		this.prev.onmouseout = this.next.onmouseout = this.pause.onmouseout = new Function('PG.alpha.set(this,'+this.navOpacity+',5)');
		this.prev.onclick = new Function(this.name+'.move(-1,1)');
		this.next.onclick = new Function(this.name+'.move(1,1)');
		this.pause.onclick = new Function(this.name+'.playpg()');
		
		this.paused = this.auto==1? 0:1;
		this.auto==1? this.showimage(0,0): this.showimage(0,1);
	},
	
	toggleinfo:function(){
		if(this.infoShow==1){//hide it
			this.infoShow = 0;
			this.toggle.innerHTML = 'Show Caption';
			PG.height.set(this.caption, 1, this.infoSpeed/2); 
		} else {
			this.infoShow = 1;
			this.toggle.innerHTML = 'Hide Caption';
			this.caption.style.height = 'auto';
			var h = parseInt(this.caption.offsetHeight);
			this.caption.style.height = 0;
			PG.height.set(this.caption,h,this.infoSpeed); //,0
		}
	},
	
	playpg:function(){
		if(this.paused==1){//resume
			this.pause.innerHTML = '<b>&#124;&#124;</b>';
			this.paused = 0;
			this.pause.title = 'Pause';
			this.move(1,0);
		} else {
			this.move(0,1);
		}
	},
	
	//move to next/prev slide
	move:function(direction,pause){
		var target = this.currentSlide + direction;
		//this.currentSlide = 
		target = target<0? this.slidesCount-1: target>this.slidesCount-1? 0:target;
		this.pr(target,pause);
	},
	//prepare
	pr:function(target,pause){
		clearTimeout(this.lt);
		if(pause){
			clearTimeout(this.at);
			this.pause.innerHTML = '&#9658;';
			this.paused = 1;
			this.pause.title = 'Play';
		}
		this.currentSlide = target;
		this.showimage(target,pause);
	},
	//show image
	showimage:function(slide,pause){
		if(this.caption){//hide existing info
			PG.height.set(this.caption, 1, this.infoSpeed/2);
		}
		var i = new Image();
		i.style.opacity = 0;
		i.style.filter = 'alpha(opacity = 0)';
		this.pgimage = i;
		i.onload = new Function(this.name+'.loadimage('+slide+','+pause+')');
		i.src = this.slidesArray[slide].p;
		if(this.thumbs){
			var a = $$('img',this.slider), l = a.length, x = 0;
			for(x;x<l;x++){
				a[x].style.borderColor = (x != slide)?'':this.active;
				x==slide? PG.alpha.set(a[x],100,5): PG.alpha.set(a[x],this.thumbOpacity,5);
			}
			//scroll to keep active thumb visible
			var sliderWidth = parseInt(this.slider.style.width);
			if(sliderWidth > parseInt(this.width)-40){
				var scrollCenter = Math.ceil((parseInt(this.width)-40-this.slidesArray[slide].slideWidth)/2);
				var scrollto = scrollCenter - this.slidesArray[slide].slidePosition;
				if(parseInt(this.width)-40-scrollto > sliderWidth){
					this.scrollto(this, parseInt(this.width)-sliderWidth-40, this.scrollSpeed)
				} else if(scrollto < 0){
					this.scrollto(this, scrollto, this.scrollSpeed)
				} else { //rewind
					this.scrollto(this, 0, this.scrollSpeed*2);
				}
			}
		}
	},
	
	scrollto:function(element,endat,speed){
		element = typeof element == 'object'? element:$(element);
		var scrollPosition = parseInt(this.slider.style.left);
		if(scrollPosition == endat){
			return false;
		} else {
			var i = Math.abs(endat-scrollPosition);
			var dir = endat>scrollPosition? -1:1;
			i = i<speed? i:speed; 
			var n = scrollPosition-i*dir;
			this.slider.style.left = n+'px';
			st = setTimeout(function(){element.scrollto(element,endat,speed)},20);
		}
	},
	
	loadimage:function(slide,pause){
		this.image.appendChild(this.pgimage);
		var w = parseInt(PG.style.val(this.image,'width'))-parseInt(this.pgimage.offsetWidth);
		if(w>0){
			var lw = Math.floor(w/2);
			this.pgimage.style.borderLeft = lw+'px solid '+this.letterbox;
			this.pgimage.style.borderRight = (w-lw)+'px solid '+this.letterbox;
		}
		var h = parseInt(PG.style.val(this.image,'height'))-parseInt(this.pgimage.offsetHeight);
		if(h>0){
			var lh = Math.floor(h/2);
			this.pgimage.style.borderTop = lh+'px solid '+this.letterbox;
			this.pgimage.style.borderBottom = (h-lh)+'px solid '+this.letterbox;
		}
		PG.alpha.set(this.pgimage,100,this.imgSpeed);
		var n = new Function(this.name+'.info('+slide+')');
		this.lt = setTimeout(n,this.imgSpeed*100);
		if(!pause){
			this.at = setTimeout(new Function(this.name+'.move(1,0)'),this.speed*1000);
		}
		if(this.slidesArray[slide].l != ''){ //set up the link
			this.pgimage.onclick = new Function('window.location = "'+this.slidesArray[slide].l+'"');
			this.pgimage.style.cursor = 'pointer';
			this.pgimage.title = this.slidesArray[slide].l;
		}else{
			this.pgimage.onclick = this.pgimage.onmouseover = null;
			this.pgimage.style.cursor = 'default';
		}
		var m = $$('img',this.image);
		if(m.length>2){
			this.image.removeChild(m[0]);
		}
	},
	
	info:function(slide){
		if(this.caption){
			slide = this.slidesArray[slide];
			$$('h3',this.caption)[0].innerHTML = slide.t;
			$$('p',this.caption)[0].innerHTML = slide.d;
			if(this.infoShow==1){
				if(slide.t == ''){
					$$('h3',this.caption)[0].style.height = 0;
				} else {
					$$('h3',this.caption)[0].style.height = 'auto';
				}
				if(slide.d == ''){
					$$('p',this.caption)[0].style.height = 0;
				} else {
					$$('p',this.caption)[0].style.height = 'auto';
				}
				this.caption.style.height = 'auto';
				var h = parseInt(this.caption.offsetHeight);
				this.caption.style.height = 0;
				PG.height.set(this.caption,h,this.infoSpeed);
			}
		}
	}
};

PG.scroll = function(){
	return{
		init:function(element,direction,speed){//speed in pixels/iteration
			element = typeof element == 'object'? element:$(element); 
			var p = element.style.left||PG.style.val(element,'left'); 
			element.style.left = p;
			var limit = direction == 1? parseInt(element.offsetWidth) - parseInt(element.parentNode.offsetWidth): 0; 
			element.si = setInterval(function(){PG.scroll.move(element,limit,direction,speed)},20);
		},
		move:function(element,limit,direction,speed){
			var scrollPosition = parseInt(element.style.left); 
			if(scrollPosition == limit){
				PG.scroll.cl(element)
			} else {
				var i = Math.abs(limit+scrollPosition); 
				i = i<speed? i:speed; 
				var n = scrollPosition-i*direction;
				element.style.left = n+'px';
			}
		},
		cl:function(element){
			element = typeof element == 'object'?element:$(element); 
			clearInterval(element.si);
		}
	}
}();

PG.height = function(){
	return{
		set:function(element,setHeight,step){//,d
			element = typeof element == 'object'? element:$(element); 
			var objectHeight = element.offsetHeight; 
			var elementHeight = element.style.height||PG.style.val(element,'height');
			elementHeight = objectHeight - parseInt(elementHeight); 
			var direction = objectHeight-elementHeight>setHeight? -1:1; 
			clearInterval(element.si); 
			element.si = setInterval(function(){PG.height.tw(element, setHeight, elementHeight, direction, step)},20);
		},
		tw:function(element, setHeight, elementHeight, direction, step){
			var objectHeight = element.offsetHeight-elementHeight;
			if(objectHeight == setHeight){
				clearInterval(element.si);
			} else {
				element.style.height = objectHeight+Math.ceil(Math.abs(setHeight-objectHeight)/step)*direction+'px';
			}
		}
	}
}();

//fade an image in or out
PG.alpha = function(){
	return{
		set:function(image,setValue,step){
			image = typeof (image == 'object')? image:$(image); 
			var opacity = image.style.opacity||PG.style.val(image,'opacity');
			var direction = setValue>opacity*100? 1:-1; 
			image.style.opacity = opacity; 
			clearInterval(image.fade); 
			image.fade = setInterval(function(){PG.alpha.tw(image,setValue,direction,step)},20);
		},
		tw:function(image,setValue,direction,step){
			var opacity = Math.round(image.style.opacity*100);
			if(opacity == setValue){
				clearInterval(image.fade);
			} else {
				var n = opacity + Math.ceil(Math.abs(setValue-opacity)/step)*direction; 
				image.style.opacity = n/100; 
				image.style.filter = 'alpha(opacity = '+n+')';
			}
		}
	}
}();

//get a style property value
PG.style = function(){
	return{
		val:function(element,p){
			element = typeof element == 'object'?element:$(element); 
			return element.currentStyle? element.currentStyle[p]: document.defaultView.getComputedStyle(element,null).getPropertyValue(p);
		}
	}
}();

if (!window.addEventListener) {
    window.addEventListener = function (type, listener, useCapture) {
        attachEvent('on' + type, function() { listener(event) });
    }
}

window.addEventListener('load',PG.init,false);

