var windowWidth;
var windowHeight;
var iFrameActive = false;
var clientFrame = false;

$(document).ready(function(){	
	figurePage();
	positionMain();

	//User resizes window
	$(window).bind("resize", function(){
		figurePage();
	    positionMain();  
	});
	
	firstLoad();
	handlePrimaryNav();
	handleConceptNav();
	showCreatives();
	handleMessage();
	targetBlank();
});

//Get current width and height of the window
function figurePage(){
	windowWidth = $(window).width();
	windowHeight = $(window).height();
}

//reposition main
function positionMain(){
	var verticlePadding = (windowHeight/2) - 304;
	if( verticlePadding > 0 ){
		$('#main').animate({
			marginTop : verticlePadding + "px"
		});
	}
	else{
		$('#main').animate({
			marginTop : "0px"
		});
	}
};

function firstLoad(){
	$('#intro span').hide();
	$('#clouds').css({ backgroundPosition : "550px 0px" });
	$('#clouds').animate({ backgroundPosition : "425px 0px" }, 2000);
	
	$('#intro').append('<img src="image/smallGreyClouds.png" alt"" />');
	$('#intro img').hide();
	
	/*$('#love').fadeIn(1000);
	$('#intro img').animate({ left : "205px" }, 2000, function(){
		$('#intro img').css({top : '75px', left : '265px'});
		$('#push').fadeIn(1000);
		$('#intro img').animate({ left : "240px" }, 2000, function(){
			$('#intro img').css({top : '130px', left : '200px'});
			$('#sky').fadeIn(1000);
			$('#intro img').animate({ left : "175px" }, 2000)
		});
	});*/
	$('#love').fadeIn(1500, function(){
		$('#push').fadeIn(1500, function(){
			$('#sky').fadeIn(1500, function(){
				$('#intro img').css({position : 'absolute', top : '210px', left : '65px'}).fadeIn(1500);
			});
		});
	});
}

function handlePrimaryNav(){
	$('.stuff, .stuff2').hide();
	
	$('#primaryNav h2').click( function(){
		$('#primaryNav h2').removeClass('selected');
		$('#intro, .stuff, .stuff2').hide();
		$(this).addClass('selected').next().fadeIn(1000);
	});
	
	$('#mainOne').click( function(){
		$('#intro, .stuff, .stuff2').hide();
		$('#intro').fadeIn(1000);
		$('#primaryNav h2').removeClass('selected');
	});
}

function handleConceptNav(){
	$('#conceptNav li:eq(0)').mouseenter( function(){
		$('#conceptNav').animate({ backgroundPosition : "55px 0px" }, 750);
	});
	$('#conceptNav li:eq(1)').mouseenter( function(){
		$('#conceptNav').animate({ backgroundPosition : "213px 0px" }, 750);
	});
	$('#conceptNav li:eq(2)').mouseenter( function(){
		$('#conceptNav').animate({ backgroundPosition : "340px 0px" }, 750);
	});
}

function showCreatives(){
	$('#creativeWeb').click( function(){
		$('#main').append('<div id="overlay"></div><div id="theIFrame"><iframe id="theIFrameID" frameborder="0" border="0" height="339px" width="442px" src="creativeWeb.html" /></div>');
		iFrameActive = true;
		$('#overlay, #theIFrame').click(closeItAll);
	});
	$('#creativePrint').click( function(){
		$('#main').append('<div id="overlay"></div><div id="theIFrame"><iframe id="theIFrameID" frameborder="0" border="0" height="339px" width="442px" src="creativePrint.html" /></div>');
		iFrameActive = true;
		$('#overlay, #theIFrame').click(closeItAll);
	});
	$('#creativeArt').click( function(){
		$('#main').append('<div id="overlay"></div><div id="theIFrame"><iframe id="theIFrameID" frameborder="0" border="0" height="339px" width="442px" src="creativeArt.html" /></div>');
		iFrameActive = true;
		$('#overlay, #theIFrame').click(closeItAll);
	});
	$('#clientList').click( function(){
		$('#main').append('<div id="overlay"></div><div id="theClientFrame"><iframe id="theIFrameID" frameborder="0" border="0" height="333px" width="282px" src="' + $(this).attr('href') + '" /></div>');
		clientFrame = true;
		$('#overlay, #theClientFrame').click(closeItAll);
		return false;
	});
	
	document.onkeydown = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){
			closeItAll();
		}
	};

}

function closeItAll(){
	$('#overlay').unbind("click");
	if(iFrameActive){
		$('#theIFrame').unbind("click");
		$('#overlay, #theIFrame').remove();
		iFrameActive = false;
	}
	if(clientFrame){
		$('#theClientFrame').unbind("click");
		$('#overlay, #theClientFrame').remove();
		clientFrame = false;
	}
}

function handleMessage(){
	var theURL = document.location.href;

	if( theURL.indexOf("fail") != -1 ){
		$('#main').prepend('<p id="mailMessage">Your message failed to send, please try again</p>');
		$('#mailMessage').fadeOut(8000);
	}
	else if( theURL.indexOf("success") != -1 ){
		$('#main').prepend('<p id="mailMessage">Your message has been sent</p>');
		$('#mailMessage').fadeOut(8000);
	}
}

function targetBlank(){
	$('#conceptNav a:first').attr('target', '_blank');
	$('#conceptNav a:last').attr('target', '_blank');
}
