window.onload = function () {
	$(window).resize(function() {
		FullScreenBackground("#bgimg"); //scale bg image
		CenterItem('.centered');
	});
	LargeImageLoad("#bgimg");
}

//loading bg image
$("#bgimg").load(function() {
	LargeImageLoad($(this));
});

function LargeImageLoad($this){
	$('#load').fadeOut('fast');
	FullScreenBackground($this); //scale new image
	$("#bgimg").fadeIn("fast"); //fadein background image
}

var default_content="";
var lasturl="";

$(document).ready(function(){
	
	
	CenterItem('.centered');
	
	$('ul#navigation li').append('<div class="hover"></div>');
    $('ul#navigation li').hover(   
        function() {  
            $(this).children('div').fadeIn('1000');
        },
        function() {
            $(this).children('div').fadeOut('1000');
    })
	
	$('ul#navigation li a').click(function (e){
		checkURL(this.hash);
		$("ul#navigation li a").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
	});
    
    $('div#navigation-wrap a').click(function (e){ //For Home page
		checkURL(this.hash);
		
		
	});
	
	//filling in the default content
	default_content = $('#content').html();
		
	/*var loc = window.location.hash;
	
	if(loc!='')
	{
		checkURL(loc);
		if(loc!='#home')
		document.getElementById(loc).className += " active";
	}*/
	
	setInterval("checkURL()",250);//Its for back button
	
	
});



function checkURL(hash)
{
	
	if(!hash)//empty when timer called
		hash = window.location.hash;
	
	if(hash=='#home')
			$("ul#navigation li a").removeClass("active");
	if(lasturl!=hash)
	{
		
		lasturl=hash;
		$("ul#navigation li a").removeClass("active");
		if(hash!='#home')
		document.getElementById(hash).className += " active";
		loadPage(hash);
	}	
		
}

function loadPage(url)
{
	url=url.replace('#','');
	
	$('#content').fadeOut(300);
	$('#load').show();	
	$.ajax({
		type: "POST",
		url: "load_page.php",
		data: 'page='+url,
		dataType: "html",
		success: function(msg){
			if(parseInt(msg)!=0)
			{
				
				$('#content').html(msg).fadeIn('300');
				$('#load').hide();
				// $('#works-section').codaSlider({dynamicArrows: true,dynamicTabs: false,crossLinking: false});

				$('#works-section').movingBoxes({
				    startPanel  : 3,     // start with this panel
				    width       : 960,   // overall width of movingBoxes
				    hashTags    : false,
				    buildNav    : false, 
				    imageRatio  : 16/9  // Image ratio set to 16:9
			   	});
				
				$("a.works-more").fancybox({
		        	'padding'	: 0,
		        	'transitionIn'	: "none",
		        	'transitionOut'	: "none",
		        	'overlayColor'  : "#222"
				});
				
				$(function() {

					// when the Submit button is clicked...
					$('#contactForm').submit(function() {
					$('.error').hide().remove();
						//Inputed Strings
						var c_name = $('#c_name').val(),
							c_email = $('#c_email').val(),
							c_subject = $('#c_subject').val(),
							c_query = $('#c_query').val();
						
					
						//Error Count
						var error_count = 0;
						
						//Regex Strings
						var username_regex = /^[a-z0-9_-]{3,16}$/,
							email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
						
							//Test Username
							if(c_name == '') {
								$('input#submit').after('<p class=error>Invalid username entered!</p>');
								error_count += 1;
							}
							
							//Test Email
							if(!email_regex.test(c_email)) {
								$('input#submit').after('<p class=error>Invalid email entered!</p>');
								error_count += 1;
							}
							
							//Blank Subject?
							if(c_subject == '') {
								$('input#submit').after('<p class=error>No Subject was entered</p>');
								error_count += 1;
							}
							
							//Blank Comment?
							if(c_query == '') {
								$('input#submit').after('<p class=error>No Query was entered!</p>');
								error_count += 1;
							}
							
							//No Errors?
							if(error_count === 0) {
								$.ajax({
									type: "post",
									url: "contact.php",
									data: "c_name=" + c_name + "&c_email=" + c_email + "&c_subject=" + c_subject + "&c_query=" + c_query,
									error: function() {
										$('.error').hide();
										$('#sendError').slideDown('slow');
									},
									success: function () {
										$('.error').hide();
										//$('.success').slideDown('slow');
										$('form#contactForm').fadeOut('slow');
										$('.success').show();
										
									}				
								});	
							}
							
							else {
				                $('.error').show();
				            }
							
						return false;
					});
					
				});//Contact
				
			}
		}
	});
	
}

function FullScreenBackground(theItem){
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	var imageWidth=$(theItem).width();
	var imageHeight=$(theItem).height();
	var picHeight = imageHeight / imageWidth;
	var picWidth = imageWidth / imageHeight;
	if ((winHeight / winWidth) < picHeight) {
		$(theItem).css("width",winWidth);
		$(theItem).css("height",picHeight*winWidth);
	} else {
		$(theItem).css("height",winHeight);
		$(theItem).css("width",picWidth*winHeight);
	};
	$(theItem).css("margin-left",winWidth / 2 - $(theItem).width() / 2);
	$(theItem).css("margin-top",winHeight / 2 - $(theItem).height() / 2);
}

function CenterItem(theItem){
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	var windowCenter=winWidth/2;
	var itemCenter=$(theItem).width()/2;
	var theCenter=windowCenter-itemCenter;
	var windowMiddle=winHeight/2;
	var itemMiddle=$(theItem).height()/2;
	var theMiddle=windowMiddle-itemMiddle;
	if(winWidth>$(theItem).width()){ //horizontal
		$(theItem).css('left',theCenter);
	} else {
		$(theItem).css('left','0');
	}
	if(winHeight>$(theItem).height()){ //vertical
		$(theItem).css('top',theMiddle);
	} else {
		$(theItem).css('top','0');
	}
}

function Slidebox(slideTo,autoPlay){
    var animSpeed=1000; //animation speed
    var easeType='easeInOutExpo'; //easing type
	var sliderWidth=$('#slidebox').width();
	var leftPosition=$('#slidebox .container').css("left").replace("px", "");
	$("#slidebox .content").each(function (i) {
		totalContent=i*sliderWidth;	
		$('#slidebox .container').css("width",totalContent+sliderWidth);
	});
	if( !$("#slidebox .container").is(":animated")){
		if(slideTo=='next'){ //next
			if(autoPlay=='stop'){
				clearInterval(autoPlayTimer);
			}
			if(leftPosition==-totalContent){
				$('#slidebox .container').animate({left: 0}, animSpeed, easeType); //reset
			} else {
				$('#slidebox .container').animate({left: '-='+sliderWidth}, animSpeed, easeType); //next
			}
		} else if(slideTo=='previous'){ //previous
			if(autoPlay=='stop'){
				clearInterval(autoPlayTimer);
			}
			if(leftPosition=='0'){
				$('#slidebox .container').animate({left: '-'+totalContent}, animSpeed, easeType); //reset
			} else {
				$('#slidebox .container').animate({left: '+='+sliderWidth}, animSpeed, easeType); //previous
			}
		} else {
			var slide2=(slideTo-1)*sliderWidth;
			if(leftPosition!=-slide2){
				clearInterval(autoPlayTimer);
				$('#slidebox .container').animate({left: -slide2}, animSpeed, easeType); //go to number
			}
		}
	}
}
