
var Photos = {
	body:null,
	matte:null,
	img:null,
	make:function(){

		var imgs = document.getElementById("page").getElementsByTagName("img");
		for(var i = 0; i < imgs.length; i++){
			var src = imgs[i].getAttribute("src");
			if(imgs[i].className.indexOf("photo") >= 0 && src.match(/_square|_small|_medium/)){
				imgs[i].style.cursor = "pointer";
				imgs[i].onclick = Photos.enlarge;
			}
		}
	},
	enlarge:function(e){
		var ei = new EventInfo(e, true);
		var src = ei.source.getAttribute("src");
		Photos.body = new Element(document.body);
		Window.construct();
		var matte = Photos.body.append("div", "<span>LOADING</span>", 'class="photomatte" style="width:100%;height:100%;" onclick="Photos.delarge();"');
			matte.style.height = Window.height + 'px';
			matte.style.top = Window.scrolledy + 'px';
			
		Photos.matte = new Animation(matte, {alpha:0.01});
		Photos.matte.element.show('block');
		Photos.matte.effect({alpha:90, duration:0.2, fps:1/50, trans:physics.easeOut});
				
		src = src.replace(/_small|_medium|_square/,'_large');
		var img = Photos.body.append("img", false, 'src="'+src+'" style="visiblity:hidden;position:absolute;top:0;left:0;z-index:6002;"');
			img.onload = Photos.onload;
							
		Photos.img = new Animation(img,{alpha:0.01});	
	},
	onload:function(e){
		Window.construct();
		var ei = new EventInfo(e, true);
		Photos.matte.element.setContent("");

		var left = (Window.width/2) - Photos.img.element.getRealWidth()/2;
		var top = (Window.height/2) + Window.scrolledy - (Photos.img.element.getRealHeight()/2);
		
		Photos.img.element.element.style.top = top + 'px';
		Photos.img.element.element.style.left = left + 'px';
		
		Photos.img.element.show();
		Photos.img.effect({alpha:99.9, duration:0.2, fps:1/50, trans:physics.easeOut});
	},
	delarge:function(){
		Photos.img.effect({alpha:0.01});
		Photos.matte.effect({alpha:0.01}, function(){ 
			Photos.body.element.removeChild(Photos.img.element.element);
			Photos.body.element.removeChild(Photos.matte.element.element);
		});
		
	},
	track:function(){	
		if(Photos.img){
				Window.construct();			
				var left = (Window.width/2) - Photos.img.element.getRealWidth()/2;
				var top = (Window.height/2) + Window.scrolledy - (Photos.img.element.getRealHeight()/2);
				Photos.matte.element.element.style.top = Window.scrolledy + 'px';
					
				Photos.img.element.element.style.top = top + 'px';
				Photos.img.element.element.style.left = left + 'px';			
		}
	}
	
}

addWindowEventListener("scroll",Photos.track);

var GalleryToggled = false;
function Enlarge(){
	if(!GalleryToggled){
		document.getElementById("left").style.display = "none";
		document.getElementById("enlarge-text").innerHTML = "SHRINK";
		document.getElementById("enlarge").className = "control unlarge";
		var a = new Animation("gallery",{width:496,height:552});
			a.effect({width:928,height:800,duration:0.2,trans:physics.easeOut}, function(){
									
								
									if( document.getElementById("inner").offsetHeight < a.element.getRealHeight() + 150){
											document.getElementById("inner").style.height = (a.element.getRealHeight() + 300) + 'px';	
									}
											
									document.getElementById("videoplayer").style.width = '640px';
									document.getElementById("videoplayer").style.height = '500px';
											
									var p = new Animation("photos",{width:486,height:320});
										p.effect({width:918,height:535,duration:0.2,trans:physics.easeOut}, function(){ 
	
											var v = new Animation("videos",{width:486,height:320});
												 v.effect({width:918,height:535,duration:0.2,trans:physics.easeOut}, function(){ 
											
											var cw = Gallery.container.getRealWidth();
											var pw = Gallery.fadein.element.element.offsetWidth ? Gallery.fadein.element.element.offsetWidth : Gallery.fadeout.element.element.offsetWidth;
											
											//center image if width is less
											if(pw < cw){
												Gallery.fadeout.element.setPosition((cw - pw)/2, 0);	
												Gallery.fadein.element.setPosition((cw - pw)/2, 0);	
											} else{
												Gallery.fadeout.element.setPosition(0, 0)
												Gallery.fadein.element.setPosition(0, 0);	
											}
									});
											
								});
							});
			
				
		Gallery.size = false;
		GalleryToggled = true;
		
		
		
	} else {
		
		document.getElementById("enlarge").className = "control enlarge";
		var p = new Animation("photos",{width:918,height:535});
									p.effect({width:486,height:320,duration:0.15}, function(){
							
							var v = new Animation("videos",{width:918,height:535});
												 v.effect({width:486,height:320,duration:0.2,trans:physics.easeOut}, function(){ 
																															  
																															  
		var a = new Animation("gallery",{width:928,height:800});
			a.effect({width:496,height:522,duration:0.2,trans:physics.easeOut}, function(){
							
								document.getElementById("left").style.display = "block";
								document.getElementById("enlarge-text").innerHTML = "ENLARGE";
		
		
							if( document.getElementById("left").offsetHeight < a.element.getRealHeight() + 150){
								document.getElementById("inner").style.height = '745px';
							}else{
								//set to container height	
							}
							
							document.getElementById("videoplayer").style.width = '320px';
									document.getElementById("videoplayer").style.height = '270px';
							
								var cw = Gallery.container.getRealWidth();
											var pw = Gallery.fadein.element.element.offsetWidth; // ? Gallery.fadein.element.element.offsetWidth : Gallery.fadeout.element.element.offsetWidth;
											
											//center image if width is less
											if(pw < cw){
												Gallery.fadeout.element.setPosition((cw - pw)/2, 0);	
												Gallery.fadein.element.setPosition((cw - pw)/2, 0);	
											} else{
												Gallery.fadeout.element.setPosition(0, 0)
												Gallery.fadein.element.setPosition(0, 0);	
											}
									});
									
						 });
			
		 });
			
		Gallery.size = false;
		GalleryToggled = false;
	}
}



var Gallery = {
	container:null,
	photo_one:null,
	photo_two:null,
	photos:null,
	timer:null,
	animate:null,
	current:0,
	stopped:false,
	active:null,
	busy:false,
	galleries:false,
	directory:"photos",
	make:function(galleries, layer, status, loader, index, size, browser, delay, speed, directory){
		Gallery.cached = new Array();
		Gallery.size = size ? size : false;
		Gallery.speed = speed ? speed : 1;
		Gallery.delay = delay ? delay : 6;
		Gallery.container = new Element(layer);		
		//Gallery.gallery = index ? galleries[index] : galleries[0];
		Gallery.photos = galleries.photos;	
		Gallery.caption = document.getElementById("caption") ? document.getElementById("caption") : false;
		Gallery.galleries = galleries;
		if(directory) Gallery.directory = directory;
		if(loader){
			Gallery.loader = new Animation(loader, {alpha:0.01});	
		}else{
			Gallery.loader = false;
		}
		if(status)
			Gallery.status = new Element(status);
		else
			Gallery.status = false;
		if(browser){
			Gallery.browser = new Element(browser);			
		}else{
			Gallery.browser = false;
		}
		var imgs = Gallery.container.getChildren("img");
		//imgs[0].onload = Gallery.onload;
		//imgs[1].onload = Gallery.onload;
		Gallery.photo_one = new Animation(imgs[0], {alpha:0.01});		
		Gallery.photo_two = new Animation(imgs[1], {alpha:0.01});
		Gallery.fadein = Gallery.photo_one;
		Gallery.fadeout = Gallery.photo_two;
		
		if(Gallery.browser){
				Gallery.loadthumbs();
		}
		Gallery.current = -1;
		Gallery.stopped = true;
		Gallery.timer = window.setTimeout(Gallery.next, 200);
		Gallery.updatestatus();
		
	},
	next:function(manual){
		
		//if(manual) Gallery.stopped = true;
		window.clearTimeout(Gallery.timer);		
		
		if(!Gallery.wait){			
				Gallery.current++;
				if(Gallery.current == Gallery.photos.length)
					Gallery.current = 0

				Gallery.size = (Gallery.photos[Gallery.current].width > 640 || Gallery.photos[Gallery.current].height > 640) ? "large" : "medium";
				Gallery.size = (Gallery.photos[Gallery.current].width > 320 || Gallery.photos[Gallery.current].height > 320) ? Gallery.size : "small";
				
				var src = WEB_HOME + Gallery.directory + "/" + Gallery.photos[Gallery.current].file_name;
				var name = src.substr(0, src.lastIndexOf("."));
				var ext = src.substr(src.lastIndexOf("."), src.length);

				if(Gallery.size){
					src = name + "_" + Gallery.size + ext;	
				}
				
				
				if(Gallery.loader){ Gallery.loader.element.show('block'); Gallery.loader.effect({alpha:99.9, duration:0.2}); }
				Gallery.wait = true;
				Gallery.fadein.element.element.setAttribute("src", src);
				if(Gallery.caption) Gallery.caption.innerHTML = Gallery.photos[Gallery.current].caption;
				Gallery.updatestatus();
				
		} else {
			//Gallery.current--;
			Gallery.timer = window.setTimeout(Gallery.next, 200);	
			return;
		}
		
	},
	previous:function(manual){
		//if(manual) Gallery.stopped = true;
		window.clearTimeout(Gallery.timer);						
		if(!Gallery.wait){
			
				Gallery.current--;				
				if(Gallery.current == -1)
					Gallery.current = Gallery.photos.length - 1;			
				
				var src = WEB_HOME + Gallery.directory + "/" + Gallery.photos[Gallery.current].file_name;
				var name = src.substr(0, src.lastIndexOf("."));
				var ext = src.substr(src.lastIndexOf("."), src.length);
				
				if(Gallery.size){
					src = name + "_" + Gallery.size + ext;	
				}
				
				Gallery.wait = true;
				if(Gallery.loader){ Gallery.loader.element.show('block'); Gallery.loader.effect({alpha:99.9, duration:0.2}); }
				if(Gallery.caption) Gallery.caption.innerHTML = Gallery.photos[Gallery.current].caption;
				Gallery.fadein.element.element.setAttribute("src", src);
				Gallery.updatestatus();
				
		} else {
			//Gallery.current++;
			Gallery.timer = window.setTimeout(Gallery.previous, 200);	
			return;
		}
		
		
							
	},
	loadphoto:function(index){
		Gallery.current = index - 1;				
		//Gallery.next();
		Gallery.timer = window.setTimeout(Gallery.next, 100);
	},	
	loadvideo:function(index){		
	
		var id = Gallery.photos[index].video_id;
		//write and center flash..		
		document.getElementById("videoplayer").innerHTML= '<object width="100%" height="100%"><param name="movie" value="http://studio.legitify.com/player/trans.swf?id='+id+'"></param><param name="wmode" value="transparent"></param><embed src="http://studio.legitify.com/player/trans.swf?id='+id+'" type="application/x-shockwave-flash" wmode="transparent" width="100%" height="100%"></embed></object>';
		
	},
	start:function(){
		Gallery.stopped = false;
		Gallery.updatestatus();
		Gallery.timer = window.setTimeout(Gallery.next, 100);
	},
	stop:function(){
		window.clearTimeout(Gallery.timer);
		Gallery.stopped = true;
		Gallery.updatestatus();
	},
	onload:function(e){
		//alert('got load at least..');
		var ei = new EventInfo(e, true);
		
		var cw = document.getElementById("photos").offsetWidth;
		
		//center image if widh is less
		if(Gallery.fadein.element.getRealWidth() < cw){
			Gallery.fadein.element.setPosition((cw - Gallery.fadein.element.getRealWidth())/2, 0);	
		} else{
			Gallery.fadein.element.setPosition(0, 0);	
		}
		
		Gallery.fadein.effect({alpha:99.9, duration:Gallery.speed/5, trans:physics.easeOut});
		Gallery.fadeout.effect({alpha:0.01, duration:Gallery.speed/5, trans:physics.easeOut}, 
							   function(){
									  if(Gallery.loader){  Gallery.loader.effect({alpha:0.01, duration:0.2}, function(){ Gallery.loader.element.hide('none'); } ); }
								   			Gallery.wait = false;
											if(!Gallery.stopped) Gallery.timer = window.setTimeout(Gallery.next, Gallery.delay * 1000);
											
											var tmp = Gallery.fadein;
											Gallery.fadein = Gallery.fadeout;
											Gallery.fadeout = tmp;
											
								});						
		Gallery.cached.push(ei.source.getAttribute("src"));	
		
	},
	slidetimer:null,
	slidespeed:5,
	slidedir:-1,
	scrollthumbs:function(dir){
		Gallery.slidedir = dir ? dir : Gallery.slidedir;
		Gallery.doslide();
	},
	doslide:function(){
		
		var left = Gallery.slide.getRealLeft();
		var sw = Gallery.slide.getRealWidth();
		var right = left + sw;
		var gw = document.getElementById("gallery").offsetWidth;
		
		if(Gallery.slidedir == 1 && (left > 0) ) return;
		if( Gallery.slidedir == -1 && ( (sw < gw) ||  (right < (gw - 50) ))) return;		
		
		Gallery.slide.setPosition(left + Gallery.slidespeed*Gallery.slidedir );		
		Gallery.slidetimer = window.setTimeout(Gallery.doslide, 100);	
	},
	stopscroll:function(){
		window.clearTimeout(Gallery.slidetimer);	
	},
	loadthumbs:function(type){
		var type = type ? type : "photos";
		Gallery.slide = new Element(Gallery.browser.getChildren("div")[1]);
		Gallery.slide.setContent("");
		for(var p = 0; p < Gallery.photos.length; p++){
		
			var file = type == "photos" ? Gallery.photos[p].file_name : Gallery.photos[p].file_name + ".jpg";
			var src = WEB_HOME +Gallery.directory+"/" + file;
			var name = src.substr(0, src.lastIndexOf("."));
			var ext = src.substr(src.lastIndexOf("."), src.length);
				src = name + "_square" + ext;
			if(type == "photos")
				Gallery.slide.element.innerHTML += '<a href="javascript:void(false)" onclick="Gallery.loadphoto('+p+');" id="photo'+p+'"><img class="thumb" src="'+src+'" /></a>';	
			else	
				Gallery.slide.element.innerHTML += '<a href="javascript:void(false)" onclick="Gallery.loadvideo('+p+');" id="photo'+p+'"><img class="thumb" src="'+src+'" /></a>';	
		}
		
	
		Gallery.slide.element.style.width = (Gallery.photos.length*85) + 'px';
			
	},
	browse_photo:function(id){
		Gallery.slide.setPosition(0,0);
		document.getElementById("videos").style.display = "none";
		document.getElementById("photos").style.display = "block";
		Gallery.gallery = Gallery.galleries[id];
		Gallery.photos = Gallery.gallery.photos;
		Gallery.loadthumbs('photos');
		Gallery.loadphoto(0);
	},
	browse_video:function(id){
		Gallery.slide.setPosition(0,0);	
		document.getElementById("photos").style.display = "none";
		document.getElementById("videos").style.display = "block";
		window.clearTimeout(Gallery.timer);
		Gallery.stopped = true;
		
		Gallery.gallery = Gallery.galleries[id];
		Gallery.photos = Gallery.gallery.videos;
	
		Gallery.loadthumbs('videos');
		Gallery.loadvideo(0);
	},
	updatestatus:function(){
		if(Gallery.active){
			Gallery.active.className = "";			
		} 
		if(document.getElementById("photo"+Gallery.current)){
			Gallery.active = document.getElementById("photo"+Gallery.current);
			Gallery.active.className = "active";
		}
			
		
		if(Gallery.status){
			var status = Gallery.stopped ? " (Paused)" : " (Playing)";
			Gallery.status.setContent("Photo " + (Gallery.current +1) + " of " + Gallery.photos.length + status);
		}
	},
	iscached:function(img){
		for(var p = 0; p < Gallery.cached.length; p++){
			if(img == Gallery.cached[p])
				return p;
		}
		return -1;
	},
	indexof:function(first){
		for(var p = 0; p < Gallery.photos.length; p++){
			if(first == Gallery.photos[p])
				return p;
		}
		return -1;
	}
}
