var SlideShow = {
	container:null,
	photos:null,
	cached:null,
	timer:null,
	animate:null,
	displayed:0,
	stopped:false,
	init:false,
	busy:false,
	make:function(layer, delay, speed){
		SlideShow.cached = new Array();
		SlideShow.speed = speed ? speed : 2.5;
		SlideShow.delay = delay ? delay : 5;
		SlideShow.container = new Element(layer);
		SlideShow.photos = new Array();
		var photos = SlideShow.container.getChildren("img");
		for(var p = 0; p < photos.length; p++){
			if(photos[p].className != "detail")
				SlideShow.photos.push(photos[p]);
				
		}
	},
	next:function(){
		//if(!SlideShow.init) return false;	
		window.clearTimeout(SlideShow.timer);
		
		//if(!SlideShow.busy) 
		var current = SlideShow.displayed;
		SlideShow.displayed++;
		
		if(SlideShow.displayed == SlideShow.photos.length){
			SlideShow.displayed = 0;
				//SlideShow.displayArticle(current);
				//return;
				//SlideShow.timer = window.setTimeout(SlideShow.next, 500);
				//return;	
		}
			
		/*if(!SlideShow.iscached(SlideShow.photos[SlideShow.displayed])){	
			SlideShow.displayed--;
			if(SlideShow.displayed == -1){
				SlideShow.displayed = SlideShow.photos.length - 1;	
			}							
			SlideShow.timer = window.setTimeout(SlideShow.next, 500);
			return;	
		}*/
		
		//if(!SlideShow.busy){
			SlideShow.crossfade(current, SlideShow.displayed);	
		//}
		
		if(!SlideShow.Stopped){
			
		}		
	},
	previous:function(){
		if(!SlideShow.init) return false;	
		window.clearTimeout(SlideShow.timer);
		/*if(SlideShow.articleDisplayed){
			var a = new Animation("article", { alpha:99.9 });
				a.effect({alpha:0.01, duration:SlideShow.speed*.4, fps:1/50}, function(){
					a.element.hide('none');			
					if(!SlideShow.busy) var current = SlideShow.displayed;
					SlideShow.displayed--;
					if(SlideShow.displayed == -1){
						SlideShow.displayed = SlideShow.photos.length - 1;				
					}
					if(!SlideShow.busy) SlideShow.crossfade(current, SlideShow.displayed);	
			    });	
		}else{*/
			if(!SlideShow.busy) var current = SlideShow.displayed;
			SlideShow.displayed--;
			if(SlideShow.displayed == -1){
				SlideShow.displayed = SlideShow.photos.length - 1;				
			}
			if(!SlideShow.busy) SlideShow.crossfade(current, SlideShow.displayed);	
		
		
	},
	crossfade:function(fout, fin){
		//fade out anything that's not to be faded in
		for(var p = 0; p < SlideShow.photos.length; p++){
			if(fin != SlideShow.photos[p] && SlideShow.photos[p].style.display == 'block'){
				var o = new Animation(SlideShow.photos[p], {alpha:99.9});
					o.effect({alpha:0.01, duration:SlideShow.speed*.1, fps:1/50}, function(){
						o.element.hide('none');
					});
			}
		}
		var a = new Animation(SlideShow.photos[fout], { alpha:99.9 });
			a.effect({alpha:0.01, duration:SlideShow.speed*.1, fps:1/50}, function(){
				a.element.hide('none');
				
			});
			var b = new Animation(SlideShow.photos[fin], { alpha:0.01 });
					b.element.show('block');
					b.effect({alpha:99.9, duration:SlideShow.speed*.2, fps:1/50}, function(){
						//SlideShow.busy = false;	
						SlideShow.timer = window.setTimeout(SlideShow.next, SlideShow.delay*1000);
					});		
			SlideShow.busy = true;
	},
	restart:function(){
		/*var a = new Animation("article", { alpha:99.9 });
				a.effect({alpha:0.01, duration:SlideShow.speed*.4, fps:1/50}, function(){
					a.element.hide('none');			
				*/
					SlideShow.displayed = 0; //SlideShow.indexof(loaded);
					SlideShow.animate = new Animation(SlideShow.photos[SlideShow.displayed], { alpha:0.01 });
					SlideShow.animate.element.show('block');
					SlideShow.animate.effect({alpha:99.9, duration:SlideShow.speed*.1, fps:1/50});				
					SlideShow.stopped = false;
					SlideShow.timer = window.setTimeout(SlideShow.next, SlideShow.delay*1000);
				//});	
		
	},
	start:function(){
		//SlideShow.timer = window.setTimeout(
		SlideShow.next(); //, 5000);
	},
	stop:function(){
		window.clearTimeout(SlideShow.timer);
		SlideShow.stopped = true;
	},
	loaded:function(show, loaded){
		//center photo in frame
		if(!SlideShow.init){	
			SlideShow.make(show);						
			if(SlideShow.indexof(loaded)==0){
				//var lo = new Animation("loader", {alpha:99.9});
					//lo.effect({alpha:0.01}, function(){
												//document.getElementById("loader").style.display = "none";
												
				SlideShow.displayed = 0; //SlideShow.indexof(loaded);
				SlideShow.animate = new Animation(SlideShow.photos[SlideShow.displayed], { alpha:0.01 });
				SlideShow.animate.element.show('block');
				SlideShow.animate.effect({alpha:99.9, duration:SlideShow.speed*.1, fps:1/50});
				SlideShow.cached.push(loaded);	
				SlideShow.init = true;
				SlideShow.timer = window.setTimeout(SlideShow.next, SlideShow.delay*1000);
				
										//});
					
				
			}
		}else{
			//SlideShow.cached.push(loaded);
		}
		
	},
	iscached:function(img){
		for(var p = 0; p < SlideShow.cached.length; p++){
			if(img == SlideShow.cached[p])
				return p;
		}
		return -1;
	},
	indexof:function(first){
		for(var p = 0; p < SlideShow.photos.length; p++){
			if(first == SlideShow.photos[p])
				return p;
		}
		return -1;
	}
}




var Photos = {
	body:null,
	matte:null,
	pic: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){
		//document.body.style.overflow = 'hidden';
		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><img src='http://static.legitify.com/img/circle_loader.gif' /></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.1, fps:1/80, trans:physics.easeOut});
		
		
		src = src.replace(/_small|_medium|_square/,'');
		var pic = Photos.body.append("img", false, 'src="'+src+'" style="visiblity:hidden;position:absolute;top:0;left:0;z-index:6002;"');
			 //pic.style.visibility = 'none';
			 pic.onload = Photos.onload;
			 pic.onclick = Photos.delarge;				
		Photos.pic = new Animation(pic,{alpha:0.01});	
	},
	onload:function(e){
		
		Window.construct();
		var ei = new EventInfo(e, true);
		Photos.matte.element.setContent("");

		var photo_width = Photos.pic.element.getRealWidth();
		var photo_height = Photos.pic.element.getRealHeight();
	
		if(Window.height < (photo_height + 300) || Window.width < (photo_width + 300)){
				
				
				if(photo_width > photo_height){
					
					var ratio = photo_height/photo_width;
					var new_width = Window.width - 300;
					var new_height = Math.floor(new_width * ratio);
					
				} else {
					var ratio = photo_width/photo_height;
					var new_height = Window.height - 300;
					var new_width = Math.floor(new_height * ratio);
					
				}
				
				Photos.pic.element.setDimensions(new_width, new_height);
				
		}
		
		
		var left = (Window.width/2) - Photos.pic.element.getRealWidth()/2;
		var top = (Window.height/2) + Window.scrolledy - (Photos.pic.element.getRealHeight()/2);
		
		Photos.pic.element.element.style.top = top + 'px';
		Photos.pic.element.element.style.left = left + 'px';
		
		Photos.pic.element.show('block');
		
		Photos.pic.effect({alpha:99.9, duration:0.1, fps:1/80, trans:physics.easeOut});
	},
	delarge:function(){
		Photos.pic.effect({alpha:0.01});
		Photos.matte.effect({alpha:0.01}, function(){ 
			Photos.body.element.removeChild(Photos.pic.element.element);
			Photos.body.element.removeChild(Photos.matte.element.element);
			//document.body.style.overflow = 'auto';
		});
		
	},
	track:function(){	
		if(Photos.pic){
				Window.construct();			
				var left = (Window.width/2) - Photos.pic.element.getRealWidth()/2;
				var top = (Window.height/2) + Window.scrolledy - (Photos.pic.element.getRealHeight()/2);
				Photos.matte.element.element.style.top = Window.scrolledy + 'px';
					
				Photos.pic.element.element.style.top = top + 'px';
				Photos.pic.element.element.style.left = left + 'px';			
		}
	}
	
}

addWindowEventListener("scroll",Photos.track);
addPageLoadListener(Photos.make);



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;
	}
}
