$(document).ready(function(){
	//What to do whan the page loads
	//$('#content').fadeIn(3000);
	$('#nav a').click(function(){return false;});
	$('#menu_services').blend();
	$('#menu_team').blend();
	$('#menu_portfolio').blend();
	$('#menu_contact').blend();
	$("#body-background").ezBgResize();
	$("#body-background").css('display','none');
	$("#body-background").fadeIn();
	$('#bottombar').css('height','0px');
	//$('#content').css({left: ($('#contentContainer').width() / 2) - ($('#content').width() / 2), display:'none'});
	$("#contentContainer").css({width:$(window).width(), height:$(window).height() - $('#header').height() - 50});
	//$('#content').css('left','-1000px');
	//$('#content').css({top: ($("#contentContainer").height() / 2) - ($("#content").height() / 2)+'px'});
	$('#content').css({top: ($("#contentContainer").height() / 2) - ($("#content").height() / 2)+'px'});
	$('#content').css({left: ($("#contentContainer").width() / 2) - ($("#content").width() / 2)+'px'});
	//What to do when browser is resized
	$(window).resize(function() {
		//$('#bg').css({width: $(window).width()});
		$("#contentContainer").css({width:$(window).width(), height:$(window).height() - $('#header').height() - 50});
		$('#content').css({top: ($("#contentContainer").height() / 2) - ($("#content").height() / 2)+'px'});
		$('#content').css({left: ($("#contentContainer").width() / 2) - ($("#content").width() / 2)+'px'});
    });
	
	
	
	//What to do on menu item click
	$('#logo').click(function(){
	    changeContent('content/home.php', true);
	});
	$('#menu_services, #homescroller').click(function(){
	    changeContent('content/services.php', true);
	});
	$('#menu_team').click(function(){
		changeContent('content/team.php', true, 'content/submenu.php?menu=team');
	});
	$('#menu_portfolio').click(function(){
		changeContent('content/portfolio.php', true, 'content/submenu.php?menu=portfolio');
	});
	$('#menu_contact').click(function(){
		changeContent('content/contact.php', true);
	});
	
    var thumbcount = 0;
	function subMenu(menu, callback){
		$('#arrow').fadeOut(1000, function(){
		$('#bottombar').animate({height: '0px'}, function(){
			$('#bottombar').load(menu, function(){
				$('#bottombar').animate({height: '40px'}, function(){
					$('#arrow').fadeIn(1000);
					setTimeout("$('#arrow').fadeOut(1000);", 2000);
					if(typeof callback == 'function'){
                        callback.call(this);
                    }
			    });
			});
		});
		});
	}
	
	$('.portfoliothumb').live('click', function(){
		var id = $(this).attr('item');
		changeContent('content/portfolioitems.php?id='+id);
	});
	$('.teamthumb').live('click', function(){
		var id = $(this).attr('item');
		changeContent('content/teammembers.php?id='+id);
	});
	
	
	var contentVisable = true;
	var contentRunning = false;
	var currentPage = 'content/home.php';
	
	function changeContent(page, mainpage, menu, callback){
		if(page != currentPage && contentRunning == false){
			currentPage = page;
		if(contentRunning == false){
		    contentRunning = true;	
		  if(contentVisable == true){
			  if(mainpage == true){
			    $('#arrow').fadeOut(500, function(){});
			    $('#bottombar').animate({height: '0px'});
			  }
			  var left = $('#contentContainer').width() + 10;
			  $('#content').animate({left: left+'px'}, 500, 'easeInOutExpo', function(){
				  $('#content').css({left: '-'+$('#content').width()+'px'});
				  
				  var left = ($('#contentContainer').width() / 2) - ($('#content').width() / 2);
			      $('#contentinside').load(page, function(){
					$('#content').animate({left: left+'px'}, 500, 'easeInOutExpo', function(){
				        contentRunning = false;
						if(mainpage == true){
						  $('#bottombar').load(menu, function(){
				              $('#bottombar').animate({height: '40px'}, function(){
					          $('#arrow').fadeIn(500);
					          setTimeout("$('#arrow').fadeOut(500);", 2000);
						  })});
						}
						if(typeof callback == 'function'){
                               callback.call(this);
                        }
			        });
			      });
			  });
		  }else{
			  var left = ($('#contentContainer').width() / 2) - ($('#content').width() / 2);
			  $('#contentinside').load(page, function(){
					$('#content').animate({left: left+'px'}, 500, 'easeInOutExpo', function(){
				        contentRunning = false;
						if(typeof callback == 'function'){
                               callback.call(this);
                        }
			        });
			  });
			  contentVisable = true;
		  }
		}
		}
	}
	
	$('input[name="send"]').live('click', function(){
		
		$.ajax({
           url: "classes/mailer.php",
           type: "POST",
           data: ({ name: $('input[name="name"]').val(), 
				    phone: $('input[name="phone"]').val(), 
					email: $('input[name="email"]').val(), 
					message: $('textarea[name="message"]').val() 
				 }),
           dataType: "json",
           success: function(data){
			   alert(data.message);
           }
        });
	});
	
});
