
// --------------------------------------------------------------------------
// image list : managing the slide show
var imageList = {
	'images': null,
	'index': 0,
	'fadeTimeout': null,
	'nextTimeout': null,
	
	initialize: function(current, images) {
		this.images = images;
		var newimg = document.createElement('img');
		newimg.setAttribute('id','image_switcher');
		$('header').appendChild(newimg);
		
		//Switch to first image
		if ($('image_switcher') != null && this.images.length > 0) {
			$('image_switcher').style.zIndex = 10;
			var curfile = this.images[this.index].file;
			$('image_switcher').setStyle("opacity",0);
			$('image_switcher').src = curfile;
			setTimeout(function() {
				var cur = curfile;
				$('image_switcher').set("tween", {
					duration : 1700,
					onStart : function() {
						ajaxUpdate('','site/ajax.slideshow.php','file='+ cur,function(req) {});
					}
				});
				$('image_switcher').tween("opacity",0,1);
			},1000);
		
			//Create a periodical executer
			if(images.length > 1) {this.nextTimeout = setTimeout("imageList.next();", 5000);}
		}
		
		
	},
	
	next: function() {
		this.index++;
		if(this.index == this.images.length) this.index = 0;
		var curfile = this.images[this.index].file;
		$('header').style.backgroundImage = 'url('+ $('image_switcher').src +')';
		$('image_switcher').set("tween", {
			duration  : 1700,
			onComplete : function() {
				$('image_switcher').src = curfile;
			}
		});
		$('image_switcher').tween("opacity",1,0);
		
		setTimeout(function() {
			var cur = curfile;
			$('image_switcher').set("tween", {
				duration  : 1700,
				onStart : function() {
					ajaxUpdate('','site/ajax.slideshow.php','file='+ cur,function(req) {});
				}
			});
			$('image_switcher').tween("opacity",0,1);
		},2000);
		this.nextTimeout = setTimeout("imageList.next();", 7000);
		
	}
}

function ajaxUpdate(divout,handler,params,oncomplete) {
	new Request.HTML({
		url: 'ajax.php',
		method: 'post',
		update: divout,
		data : 'page='+handler+"&"+params,
		evalScripts: true,
		onComplete: oncomplete
	}).send();			
}


// ---------------------------------------------------------------------------------
var ajaxLightbox = {
	"pageList" : new Array,
	"nextPage" : 0,
	"prevPage" : 0,
	"shown" : false,
	
	initialize : function() {
		$('lightbox_close').addEvent('click', function(e) {
			ajaxLightbox.close();
			return false;
		});
		
		$('lightbox_prev').addEvent('click', function(e) {
			ajaxLightbox.prev();
			return false;
		});
		
		$('lightbox_next').addEvent('click', function(e) {
			ajaxLightbox.next();
			return false;
		});
		
		$$(".port_link").each(function(elm) {
			elm.addEvent('click', function(e) {
				var id = elm.get("id");
				id = id.substring("item_".length);				
				
				ajaxLightbox.pageList = [];
				var classname = ajaxLightbox.findClassId(elm.get("class"),"gallery_item_");
				$$(".port_link."+classname).each(function(ec) {
					var id = ec.get("id");
					id = id.substring("item_".length);
					ajaxLightbox.pageList.push(id); // add to page list
				});
				
				ajaxLightbox.show(id,false);
				return false;
			});
		});
	},
	
	findClassId : function(className,groupName) {
		var nameList = className.split(' ');
		for(i = 0; i < nameList.length;i++) {
			name = nameList[i];
			if (name.match(groupName)) return name;
		} 
		return '';
	}, 
	
	show : function(id,shown) {
		this.shown = shown;
		for(i =0;i<ajaxLightbox.pageList.length;i++) {
			if (ajaxLightbox.pageList[i] == id) {
				ajaxLightbox.nextPage = ajaxLightbox.pageList[i+1];
				ajaxLightbox.prevPage = ajaxLightbox.pageList[i-1];
				if (i == 0) {
					//console.log(ajaxLightbox.pageList);
					ajaxLightbox.prevPage = ajaxLightbox.pageList[ajaxLightbox.pageList.length-1];
					//console.log(ajaxLightbox.prevPage);
				}
				break;
			}
		}	
		
		if (!shown)	{
			var arrPageSize = getPageSize();
			var arrPageScroll = getPageScroll();
			
			
	
			// display overlay
			$("overlay").setStyle("opacity","0");
			$("overlay").setStyle("display","block");
			$("overlay").tween("opacity",0,0.5);
			
			$('lightbox').setStyle("top",arrPageScroll[1]+(arrayPageSize[3] / 20))+"px";
			$("lightbox").set("tween", {
				duration : 500,
				transition : Fx.Transitions.Quad.easeIn
			});
			$("lightbox").set("morph", {
				duration : 500,
				transition : Fx.Transitions.Quad.easeOut,
				link : "chain"
			});
			
			window.addEvent('scroll', function() {
				//var arrPageScroll = getPageScroll();
				//$("lightbox").tween("top",arrPageScroll[1]+(arrayPageSize[3] / 10));
			}); 
	
			// display lightbox
			$("lightbox").setStyle("opacity","0");
			$("lightbox").setStyle("display","block");
			$("lightbox").morph({
				"opacity" : [0.5,1],
				"width" : [0,650],
				"marginLeft" : [0,-325]
			});
			
			var initsize = document.getElement('body').getScrollSize();
			$('overlay').setStyle("height",initsize.y);
		}
		this.ajaxLoad("site/port.php", "id="+id); 
	},
	
	close : function() {
		$("lightbox").set("tween",{
			duration : 500,
			onComplete : function() {
				$('overlay').setStyle("height",0);
				$("lightbox").setStyle("display","none");
			}
		});
		$("overlay").fade("out");
		$("lightbox").fade("out");
		
	},
	
	ajaxLoad : function(handler,params) {
		if (!ajaxLightbox.shown) {
			$('lightbox_container').setStyle('opacity',0);
		}
		new Request.HTML({
			url: 'ajax.php',
			method: 'post',
			update: 'lightbox_container',
			data : 'page='+handler+"&"+params,
			evalScripts: true,
			onComplete: function(){
				if (!ajaxLightbox.shown) {
					$("lightbox_container").set("tween", {
						duration : 1000,
						transition : Fx.Transitions.Quad.easeIn
					});
					$("lightbox_container").tween("opacity",1);
				}
				setup_functions();
			}
		}).send();		
	},
	
	next : function() {
		if (!this.nextPage) return; 
		this.show(this.nextPage,true);
		return;
	},
	
	prev : function() {
		if (!this.prevPage) return;
		this.show(this.prevPage,true);
		return;
	}
	
}
//--------------------------------------------------------------------------


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

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function show_dialog() {
	
	$('dialoggray').style.visibility = 'visible';
   
   
	$('dialoggray').setStyle('height', 0);
	$('dialoggray').setStyle('display', 'block');
	
	$('dialoggray').set('tween', {duration: 'long',
	onComplete: function() {
		$('dialog').setStyles({ display: '', 'opacity': 0}).set('tween', { duration: 600 }).fade('in');
	}
	});
	$('dialoggray').tween('height', screen.height);

	$('html').style.overflow = 'hidden';
	
}

function hide_dialog() {
	
	$('dialog').setStyles({ display: '', 'opacity': 1}).set('tween', { duration: 600, onComplete: function() { 
		
		$('dialoggray').set('tween', { duration: 'long' });
		$('dialoggray').tween("height", 0);
		$('html').style.overflow = 'auto';
	
	} }).fade('out')
}