
var currentPage;
var lastPage;
var navs;
var pages;
var currentWorkPage = "3D";
var lastWorkPage;
var worknavs;
var workpages;
var mouseOnPopContent = false;

window.addEvent('domready',function() { 
	
	setTimeout(function () { resizePageContainers(); }, 1000);
	$(window).addEvent('resize', function(){ resizePageContainers(); });
	
	navs = $$('#nav a');
	pages = $$('#content div.page');
	worknavs = $$('#workNav a');
	workpages = $$('#page-Work .workpage');
	
	$("popOverlay").setStyle('opacity',0);
	$("popBoxContainer").setStyle('opacity',0);
	$("popContent").setStyle('opacity',0);
	$('popBoxContainer').addEvent('click', function(e){ togglePop(); });
	$('popBox').addEvent('mouseover', function(e){ mouseOnPopContent = true; });
	$('popBox').addEvent('mouseout', function(e){ mouseOnPopContent = false; });

	initNavAndPage();
	initWorknavAndPage();
	initWorkThumbs();
	initTestimonials();
	initContactForm();
	
	//PARSE THE HASH FOR THE FIRST PAGE
	var firstPage;
	if(window.location.hash) {
		if(window.location.hash.replace('#','').split("/")) {
			var hashParts = window.location.hash.replace('#','').split("/");
			document.title = 'Adaptive Design : '+hashParts[0]+' : '+hashParts[1];
			switchPage(hashParts[0]);
			switchWorkPage(hashParts[1]);
			if(hashParts[2]) togglePop(hashParts[2]);
		}
		else {
			firstPage = window.location.hash.replace('#','');
			document.title = 'Adaptive Design : '+firstPage;
			switchPage(firstPage);
			switchWorkPage('3D');
		}
	}
	else {
		firstPage = "Work";
		document.title = 'Adaptive Design : '+firstPage+' : 3D';
		switchPage(firstPage);
		switchWorkPage('3D');
	}
});


var thisTest = 0;
function initTestimonials() {
	$$('#testimonials .testimonial').setStyles({'display':'none',opacity:0});
	switchTestimonial();
}
function switchTestimonial() {
	var testimonials = $$('#testimonials .testimonial');
	testimonials.setStyles({'display':'none',opacity:0});
	thisTest = (thisTest+1 > testimonials.length) ? 1: thisTest+1;
	testimonials[thisTest-1].setStyle('display','block').fade('in');
	setTimeout(function () { switchTestimonial(); }, 10000);
}

function resizePageContainers() {
	var containerHeight = ($(window).getSize().y <= 805) ? 805: $(window).getSize().y;
	$('container').setStyle('height',containerHeight);
	$('roomBottom').setStyle('height',containerHeight);
}
function initNavAndPage() {
	pages.each(function(el) {
		el.setStyle('opacity',0);
	});
	navs.each(function(el) {
    	el.addEvent('click', function(e){
			if(el.id != currentPage) switchPage(el.id);
		});
	});
}
function moveSelector(nav) {
	var zero = $('nav').getLeft();
	var pos = nav.getLeft()-zero;
	var width = nav.getWidth();
	var myEffect = new Fx.Morph($('navSelector'), {duration: 400,transition: Fx.Transitions.Quad.easeInOut}).start({'left':pos,'width':width});
}

function switchPage(page,subpage) {
	
	lastPage = currentPage;
	currentPage = page;
	document.title = 'Adaptive Design : '+currentPage;
	
	//NAV
	$$('#nav a').removeClass('selected');
	$(currentPage).addClass('selected');
	moveSelector($(currentPage));
	
	//PAGE
	var direction = (!lastPage || $(currentPage).getProperty('class').toInt() < $(lastPage).getProperty('class').toInt()) ? 'right': 'left';
	if(lastPage) {
		var endPosLast = (direction == "right") ? $('page-'+lastPage).getLeft()+1000: $('page-'+lastPage).getLeft()-1000;
		var myEffect1 = new Fx.Morph($('page-'+lastPage), {duration: 400,transition: Fx.Transitions.Quad.easeInOut}).start({'left':endPosLast,opacity:0})
		.chain(function() { $('page-'+lastPage).setStyle('display','none'); });
	}
	var startPosCurrent = (direction == "right") ? $('content').getLeft()-1000: $('content').getLeft()+1000;
	$('page-'+currentPage).setStyles({'left':startPosCurrent,'display':'block'});
	var myEffect2 = new Fx.Morph($('page-'+currentPage), {duration: 400,transition: Fx.Transitions.Quad.easeInOut}).start({'left':0,opacity:1});
	
	if(currentPage == "Work") switchWorkPage("3D");
}



function initWorknavAndPage() {
	workpages.each(function(el) {
		el.setStyle('opacity',0);
	});
	worknavs.each(function(el) {
    	el.addEvent('click', function(e){
			if(el.id != currentWorkPage) switchWorkPage(el.id);
		});
	});
}
function moveWorkSelector(nav) {
}
function switchWorkPage(page) {
	
	lastWorkPage = currentWorkPage;
	currentWorkPage = page;
	
	//NAV
	$$('#workNav a').removeClass('selected');
	$(currentWorkPage).addClass('selected');
	
	var nav = $(currentWorkPage);
	var zero = $('workNav').getLeft();
	var pos = nav.getLeft()-zero;
	var width = nav.getWidth();
	var myEffect = new Fx.Morph($('workNavSelector'), {duration: 400,transition: Fx.Transitions.Quad.easeInOut}).start({'left':pos,'width':width});
	
	//PAGE
	if(lastWorkPage) var myEffect1 = new Fx.Morph($('workpage-'+lastWorkPage), {duration: 0,transition: Fx.Transitions.Quad.easeInOut}).start({opacity:0});
	var myEffect2 = new Fx.Morph($('workpage-'+currentWorkPage), {duration: 400,transition: Fx.Transitions.Quad.easeInOut}).start({opacity:1});
}



function initWorkThumbs() {
	var thumbDesc = new Fx.Morph($('portThumbDesc'), {duration: 400,transition: Fx.Transitions.Quad.easeInOut});
	
	//THUMB DESC BOX BEHAVIORS
	$('portThumbDesc').setStyle('opacity',0);
	$('portThumbDesc').addEvent('mouseoover', function(e) {
		$('portThumbDesc').setStyles({'opacity':1});
	});
	$('portThumbDesc').addEvent('mouseout', function(e) {
		thumbDesc.start({'opacity':0});
	});
	$('portThumbDesc').addEvent('click', function(e) {
		window.location.hash = $('portThumbDesc').getProperty('class');
		var id = $('portThumbDesc').getProperty('class').split('/');
		togglePop(id[2]);
	});
	$('container').addEvent('mouseover', function(e) {
		thumbDesc.start({'opacity':0});
	});
	
	$$('#page-Work a').each(function(el) {
		el.addEvent('mouseover', function(e) {
			thumbDesc.stop();
			
			//TURN EVERYTHING OFF FIRST
			$('portThumbDesc').setStyles({'opacity':0});
			$('portThumbDescImage').getElement('img').setStyle('display','none').setProperty('src','');
			
			var theLeft = (el.getPosition().x - $('content').getLeft())-22;
			var theTop = el.getPosition().y - $('content').getTop()-20;
			
			var pic = el.getProperty('href');
			var textStuff = el.getProperty('rel').split('|');
			$('portThumbDescImage').getElement('img').setStyle('display','inline-block').setProperty('src',pic);
			$('portThumbDesc').getElement('h1').set('html',textStuff[0]);
			$('portThumbDesc').getElement('span').set('html',textStuff[1]);
			$('portThumbDesc').setProperty('class',textStuff[2]);
			
			$('portThumbDesc').setStyles({'left':theLeft,'top':theTop});
			thumbDesc.start({'opacity':1});
		});
	});
}



/////////////////////////////////
// POPUP                /////////
/////////////////////////////////
var popOpen = false;
var overOp, popBoxOp, popWidth, popContentOp;
function togglePop(id,forceKill) {
	if(mouseOnPopContent == false || forceKill == true) {
		
		//STRIP ID FROM HASH ON CLOSE
		if(popOpen) {
			var hashParts = window.location.hash.replace('#','').split('/');
			window.location.hash = hashParts[0]+"/"+hashParts[1];
		}
		
		var theWidth = (popOpen) ? 100: 860;
		var theOp = (popOpen) ? 0: 1;
		(popOpen) ? $('testimonials').setStyle('display','block'): $('testimonials').setStyle('display','none');

		$('popContent').empty();
		overOp = new Fx.Morph($('popOverlay'), {duration:200, transition: Fx.Transitions.Quad.easeOut, wait:true}).start({'opacity':theOp});
		popBoxOp = new Fx.Morph($('popBoxContainer'), {duration:200, transition: Fx.Transitions.Quad.easeOut, wait:true}).start({'opacity':theOp})
		.chain(function() {
			popWidth = new Fx.Morph($('popBox'), {duration:400, transition: Fx.Transitions.Cubic.easeInOut, wait:true}).start({'width':theWidth})
			.chain(function() {
				
				//OPEN
				if(!popOpen) popContentOp = new Fx.Morph($('popContent'), {duration:400, transition: Fx.Transitions.Cubic.easeInOut, wait:true}).start({'opacity':theOp});
				
				//CLOSE
				else {
					$('popBox').addClass('loading');
					var req1 = new Request.HTML({ url:"/ad/popup.php?id="+id, method:'get', urlEncoded:true, evalScripts:true, evalResponse:true, 
						onComplete: function(txt) {
							
							$('popContent').adopt(txt);
							var textStuff = $('thumb'+id).getProperty('rel').split('|');
							$('galleryTitle').getElement('h1').set('html',textStuff[0]);
							$('galleryTitle').getElement('span').set('html',textStuff[1]);
							$('popBox').removeClass('loading');
							
							popContentOp = new Fx.Morph($('popContent'), {duration:400, transition: Fx.Transitions.Cubic.easeInOut, wait:true}).start({'opacity':theOp});
						}
					}).send();
				}
				
				
			});
		});
		popOpen = (popOpen) ? false: true;
	}
}





function choosePortItem(num,type,file) {
	if(!$('popThumb'+num).hasClass('selected')) {
		$('viewer').empty().addClass('loading');
		$$('#thumbs a').each(function(el) { el.removeClass('selected');	});
		$('popThumb'+num).addClass('selected');
		var theFile;
		if(type == "video") {
			theFile = new Element('embed', {
				'id': 'videoPlayer',
				'src': "moviePlayer.swf?theVid=/ad/images/portfolio/viewer/"+file, 
				'quality': "high", 
				'bgcolor': "#000000", 
				'width': "720", 
				'height': "445", 
				'name': "moviePlayer", 
				'allowScriptAccess': "sameDomain", 
				'allowFullScreen': "true", 
				'type': "application/x-shockwave-flash", 
				'pluginspage': "http://www.macromedia.com/go/getflashplayer"
			});
		}
		else {
			theFile = new Element('img', { 
				'src': "/ad/images/portfolio/viewer/"+file, 'width': "720", 'height': "405"
			});
		}
		$('viewer').adopt(theFile).removeClass('loading');
	}
}



function initContactForm() {
	var contactForm = $('contactForm');
	contactForm.addEvent('submit', function(e) {			
		e.stop();
		errors = false;
		if(contactForm.yourmessage.value == "" || contactForm.youremail.value == "" || contactForm.yourname.value == "") {
			errors = true;
			alert("Please fill in all required fields.");
		}
		if(!isValidEmail(contactForm.youremail.value)) {
			errors = true;
			alert("Please use a valid email address.");
		}
		if(errors == false) {
			contactForm.setStyles({opacity:0});
			$('contactFormBox').addClass('loading');
			contactForm.set('send', {
				onComplete: function(txt) { 
					contactFormBox.empty();
					contactFormBox.set('html',txt);
					$('contactFormBox').removeClass('loading');
				},
				onFailure: function() {
					alert('Sorry! The network was busy. Please try again later.');
					contactForm.setStyles({opacity:1});
					$('contactFormBox').removeClass('loading');
				}
			}).send();
		}
	});
}



function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}






