// JavaScript Document

window.addEvent("domready", function(){
	def.init();
});

// default init functions
def={
	init:function(){
		//if($('projectswrap')) { project.init(); }
		pageload.init();
		if($('navlist')){
			menu.init();
			//sounds.init();
		}
		if($('collapseall')){ collapse.init(); }
		if($$('.photolink').length > 0){ photos.init(); }
		if($('photoviewer')){ photos.photoSetup(); }
		if($$('.movielink').length > 0){ movies.init(); }
		if($('singleproject')){ collapse.reveal(); }
		if($$('.links').length > 0){ le.init(); }
	}
}

// menu
menu={
	init:function(){
		$('menu').getElements('a').each(function(element){
			element.addEvent('mouseenter',function(){
				if($('audiotag1')){
					$('audiotag1').play();
				}
			});
		});
		$('navlist').getElements('a').each(function(element){
			if(!element.hasClass('cursel')){
				var fx = new Fx.Morph(element,{
					'duration':'normal',
					'link':'cancel',
					'transition':'quint:out'
				});
				element.addEvent('mouseenter',function(){			  
					//menu.resetNav();
					fx.start({
						'padding-left':15,
						'background-position':'-8px 3px'
					});
				});
				element.addEvent('mouseleave',function(){
					fx.start({
						'padding-left':0,
						'background-position':'-22px 3px'
					});
				});
			}
		});
	}
}

// loading tweens
pageload={
	init:function(){
		pageload.contentHide();
		//Cookie.dispose('loadstages');
		//Cookie.write('loadstages', '0' ,{ path:'/'});
		// loadstage cookie stages
		// 0 : never loaded - load intro + headers + content
		// 1 : home page has loaded - load headers + content
		// 2 : intro + headers loaded - load content
		//alert(Cookie.read('loadstages'));
		var loadCookie = Cookie.read('loadstages');
		if(!loadCookie || loadCookie == '0'){
			Cookie.write('loadstages', '0' ,{ path:'/'});
			if($('homepg')){ // loading home page
				pageload.opener();
			}else{
				pageload.contentOpen();
			}
		}else if(loadCookie == '1' && !$('homepg')){
			pageload.contentHide();
			pageload.contentOpen();
		}else if(loadCookie == '2'){
			if(!$('homepg')){
				pageload.contentLoad();
			}else{
				Cookie.write('loadstages', '1' ,{ path:'/'});
			}
		}
	},
	opener:function(){
		//alert('opener');
		Cookie.write('loadstages', '1',{path:'/'});
		$('header').setStyle('width', '0px');
		$('header').getElement('h1').fade('hide');
		var headerTween = new Fx.Tween($('header'),{
			fps: 80,
			transition: 'sine:in:out',
			duration: 2000,
			onComplete: function(){
				$('header').getElement('h1').fade('in');
			}
		});
		headerTween.start('width','630px');
	},
	contentOpen:function(){
		if($('pagetitle')){
			Cookie.write('loadstages', '2',{path:'/'});
			//alert('header');
			if($('worknav')) {
				$('worknav').fade('hide');
				$('worknav').removeClass('hideMe');
			}
			if($('collapseall')) { $('collapseall').fade('hide'); }
			$('pagetitle').getElement('h2').fade('hide');
			$('navlist').setStyle('width','0px');
			$('pagetitle').setStyle('width','0px');
			var myFx = new Fx.Tween($('navlist'),{
				fps: 60,
				transition: 'sine:in:out',
				duration: 800
			});
			var myFx2 = new Fx.Tween($('pagetitle'),{
				fps: 60,
				transition: 'sine:in:out',
				duration: 1000,
				onComplete:function(){
					$('pagetitle').getElement('h2').fade('in');
					if($('worknav')) { $('worknav').fade('in'); }
					(function(){
						if($('collapseall')) { $('collapseall').fade('in'); }
					}).delay(600);
					pageload.contentLoad();
				}
			});
			// set off tweens
			(function(){
				myFx.start('width','180px');
			}).delay(100);
			(function(){
				myFx2.start('width','630px');
			}).delay(300);
			// content load put under pagetitle complete
			//pageload.contentLoad();
		}
	},
	contentHide:function(){
		if($('contentarea')){
			//alert('content hide');
			var projectposts = $$('.project');
			var newsposts = $$('.newsitem');
			if(projectposts.length > 0){
				// hide projects
				projectposts.each(function(element){
					element.setStyle('width','0px');
					element.getElement('.projecthead').fade('hide');
					if(element.getElement('.listingpic')){ element.getElement('.listingpic').fade('hide'); }
				});
			}else if(newsposts.length > 0){
				// hide news
				newsposts.each(function(element){
					element.setStyle('width','0px');
					element.getElement('.newshead').fade('hide');
					if(element.getElement('.listingpic')){ element.getElement('.listingpic').fade('hide'); }
					element.getElement('.additionalcontent').fade('hide');
				});
			}else if($('info')){
				$('info').fade('hide');
			}
			$('mainwrap').removeClass('hideMe');
		}
	},
	contentLoad:function(){
		if($('contentarea')){
			if($('worknav')){
				 if($('worknav').hasClass('hideMe')){
					$('worknav').removeClass('hideMe');
				}
			}
			//alert('content');
			var projectposts = $$('.project');
			var newsposts = $$('.newsitem');
			if(projectposts.length > 0){
				// load projects
				projectposts.each(function(element){
					var showProject = function(element){
						myFx3.start('width','630px');
					}
					var myFx3 = new Fx.Tween(element,{
						fps: 60,
						transition: 'sine:in:out',
						duration: 500,
						onComplete:function(){
							element.getElement('.projecthead').fade('in');
							if(element.getElement('.listingpic')){ element.getElement('.listingpic').fade('in'); }
						}
					});
					
					if(element.getElement('.listingpic')){ 
						var listingpic = element.getElement('.listingpic img').get('src');
						var loader = new Asset.image(listingpic, {
							onload: function(){
								showProject(element);
							}
						});
					}else{
						showProject(element);
					}
				});
			}else if(newsposts.length > 0){
				// load news posts
				newsposts.each(function(element){
					var showProject = function(element){
						myFx3.start('width','630px');
					}
					var myFx3 = new Fx.Tween(element,{
						fps: 60,
						transition: 'sine:in:out',
						duration: 500,
						onComplete:function(){
							element.getElement('.newshead').fade('in');
							if(element.getElement('.listingpic')){ element.getElement('.listingpic').fade('in'); }
							element.getElement('.additionalcontent').fade('in');
						}
					});
					
					if(element.getElement('.listingpic')){ 
						var listingpics = element.getElements('img').get('src');
						//alert(listingpics.length);
						var loader = new Asset.images(listingpics, {
							onComplete: function(){
								showProject(element);
							}
						});
					}else{
						showProject(element);
					}
				});
			}else if($('info')){
				(function() { $('info').fade('in'); }).delay(500);
			}
		}
	}
}

// collapseable projects
collapse={
	init:function(){
		$$('.additionalcontent').each(function(element){
			collapse.createSlider(element);
		});
	},
	createSlider:function(element){
		//alert(element.getStyle('display'));
		element.setStyle('display','block');	
		var mySlide = new Fx.Slide(element).hide();
			if(element.getElement('.closeadditional')){
				element.getElement('.closeadditional').addEvent('click',function(){
					mySlide.slideOut();
					return false;
				});
			}
			if($('collapseall').getElement('a')){
				$('collapseall').getElement('a').addEvent('click',function(){
					mySlide.slideOut();
					return false;
				});
			}
			if(element.getParent('.project').getElements('a')){
				element.getParent('.project').getElements('.projecttitle a').each(function(element){
					element.addEvent('click',function(){		  
						// load content
						var urlId = this.get('href').split("?id=")[1];
						var target = this.getParent('.project').getElement('.additionalcontent');
						//alert(target.getStyle('height'));
						/*if(!target.hasClass('loaded')){
							var req = new Request({
								url:"../processadditional.php?id="+urlId,
								onSuccess: function(html) {
									target.set('html',html);
								},
								onComplete: function() {
									//mySlide.slideIn();
								},
								onFailure: function() {
									//target.set('text', 'The request failed.');
								}
							});
							req.send();
							target.addClass('loaded');
						}else{
							mySlide.toggle();	
						}*/
						
						mySlide.toggle();
						//alert(this.getParent('.project').getElement('.additionalcontent').getStyle('height'));
						return false;
					});
				});
				
			}
			if(element.getParent('.project').getElement('.listingpic a')){
				element.getParent('.project').getElement('.listingpic a').addEvent('click',function(){
					mySlide.toggle();
					return false;
				});
			}
	},
	reveal:function(){
		$('singleproject').getElement('.additionalcontent').setStyle('display','block');
	}
}

// link events
le={
	init:function(){
		$$('.links a').each(function(element){
			element.setStyle('color','#FFF');
			element.addEvent('mouseenter',function(){
				element.setStyle('color','#D03F7A');
			});
			element.addEvent('mouseleave',function(){
				element.setStyle('color','#FFF');
			});
		});
	}
}

// photo viewer
photos={
	init:function(){
		$$(".photolink").each(function(element){
			element.addEvent('click',function(){
				var url = this.get('href');
				pu.openwin(url,1224,890);
				return false;
			});
		});
	},
	photoSetup:function(){
		photos.loaderSetup();
		photos.scrollSetup();
		photos.viewerNavSetup();
		photos.loadImage();
	},
	loaderSetup:function(){
		var thumbitems = $$('.thumbitem');
		thumbitems.each(function(element){
			element.fade('hide');
		});
		var thumbimages = $$('.thumbitem img').get('src');
		//var loader = new Asset.images(thumbimages, {
//			onProgress: function(counter, index){
//				thumbitems[index].fade('in');
//			},
//			onComplete: function(){
//			}
//		});
		
		$$('.thumbitem').each(function(element){
			element.fade('hide');
			var thisImg = element.getElement('img').get('src');
			var loader = new Asset.image(thisImg, {
				onload: function(){
					//alert("loaded");
					element.fade('toggle');
				},
				onError: function (){
					//alert("failed");
				}
			});
		});
	},
	scrollSetup:function(){
		var thumbsArr = $$('.thumbitem');
		var thumbWidth = thumbsArr[0].getStyle('width').toInt();
		var thumbsWidth = thumbWidth*thumbsArr.length;
		$('thumbsstrip').setStyle('width',thumbsWidth+'px');
		var slideAmount = thumbsWidth - 1024;
		var mySlider = new Slider($('slideArea'), $('slideHandle'), {	
			steps: slideAmount,	
			mode: 'horizontal',
			onChange: function(step){
				$('thumbsstrip').setStyle("left",-step+"px");
			}
		}).set(0);
	},
	viewerNavSetup:function(){
		if($('imgID')){
			photos.curvis = parseInt($('imgID').get('text'));
		}else{
			photos.curvis = 0;
		}
		photos.photoSize = $$('.thumbitem').length;
		$$('.thumbitem a').each(function(element,index){
			element.addEvent('click',function(){
				photos.curvis = index;
				photos.updateViewer(photos.curvis);
				return false;
			});
		});	
		$('photonext').getElement('a').addEvent('click',function(){
			if(photos.curvis == photos.photoSize-1){
				photos.curvis = 0;
			}else{
				photos.curvis++;
			}
			photos.updateViewer(photos.curvis);
			return false;
		});
		$('photoprev').getElement('a').addEvent('click',function(){
			if(photos.curvis == 0){
				photos.curvis = photos.photoSize-1;
			}else{
				photos.curvis--;
			}
			photos.updateViewer(photos.curvis);
			return false;
		});
	},
	updateViewer:function(id){
		$('photo').fade('hide');
		$('photoloading').fade('show');
		photos.photoid = $$('.thumbitem a')[id].get('id');
		aj.request('photoload.php?id='+photos.photoid,$('photo'));
	},
	loadImage:function(){
		$('photo').fade('hide');
		var imagePath = $('photo').getElement('img').get('src');
		var loader = new Asset.image(imagePath, {
			onload: function(){
				$('photo').fade('show');
				$('photoloading').fade('hide');
			}
		});
	}
}

// movie viewer
movies={
	init:function(){
		$$(".movielink").each(function(element){
			element.addEvent('click',function(){
				var url = this.get('href');
				pu.openwin(url,827,550);
				return false;
			});
		});
	}
}

// pop up window
pu={
	init:function(){
		
	},
	openwin:function(url, width, height){
		var name = Math.round(Math.random()*10000);
		var config = 'width=' + width + ',height=' + height + ',toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0';
		var newWindow = window.open(url, name, config);
		var x = (screen.width - width) / 2;
		var y = (screen.height - height) / 2;
		newWindow.moveTo(x, y);
		newWindow.focus();
	}
}

// ajax
aj={
	init:function(){
		
	},
	request:function(url,target){
		aj.target = target;
		xhr = false;
		if(window.XMLHttpRequest){
			xhr = new XMLHttpRequest();
		}else{
			if(window.ActiveXObject){
				try{
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e){}
			}
		}
		if(xhr){
			xhr.onreadystatechange = aj.showContents;
			xhr.open("GET",url,true);
			xhr.send(null);
		}else{
			alert("couldn't create XMLHttpRequest");
		}
	},
	showContents:function(){
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				if(window.location.hash){
					//checkMe(window.location.hash.substring(1));
					//pageTracker._trackPageview(window.location.hash.substring(1));
				}
				var outMsg = xhr.responseText;
				aj.target.set('html',outMsg);
				// post load content init
				if(aj.target == $('photo')){
					photos.loadImage();
				}else{
					
				}
			}else{
				alert("There was a problem with the request: " + xhr.status);
			}
		}
	}
}
