var bigImgSize = 2012;
var bigImgHeight = 722;
var bigImgProportion = bigImgHeight/bigImgSize;
var currentImgHeight = 0;
var fadeSpeed = 100;
var shiftSpeed = 200;
var topHeader = 0;

var video;
var videoProportion = 0.5625;
var supportsVideo = !!document.createElement('video').canPlayType;

/* function supports_video() {
	return !!document.createElement('video').canPlayType;
} */

function ifBigImgExists() {
	if ($(".background-image").length > 0) {
		return true;
	} else {
		return false;
	}
}

function stratchImg() {
	var docWidth = $(document).width();
	if (docWidth > bigImgSize) {
		// then stratch img
		$(".background-image").css("width", docWidth + "px");
		currentImgHeight = docWidth*bigImgProportion;
	} else {
		$(".background-image").css("width", bigImgSize + "px");
		currentImgHeight = bigImgHeight;
	}
} 

function stratchVideo(videoElement) {
	var docWidth = $(document).width();
	videoElement.width = docWidth;
	videoElement.height = docWidth*videoProportion;
} 

function fitSectionHeight() {
	$(".big-image").css("height", (currentImgHeight-179)+"px");
}

// adds an effect called "fadeAjax" to tabs
$.tools.tabs.addEffect("fadeAjax", function(tabIndex, done) {
	//console.log("tabs event " + tabIndex);
	if (this.getTabs().eq(tabIndex).hasClass("external")) {
		done.call();
		window.open( this.getTabs().eq(tabIndex).attr("href"), "_self" );
		return false;
	} else {
		$.ajax({
			url: this.getTabs().eq(tabIndex).attr("id")+".html",
			beforeSend: function( xhr ) {
				//$("div.page").append("<img class='ajax-loader' src='images/ajax-loader.gif' />");
				$("div.page").append("<div class='ajax-loader'> </div>");
				$('.ajax-loader').activity({segments: 12, width: 4, space: 6, length: 13, color: '#252525', speed: 1.5});
			},
			complete: function(xhr, status) { 
				if (status == 'success') {
					$("div#hidden-next-page").html(xhr.responseText);
					
					$("div#hidden-next-page").waitForImages(function() {
						//alert('All images are loaded.');
						
						$("div.page").fadeOut(fadeSpeed, function() {
							if (tabIndex == 0) {
								window.location.href = window.location.pathname + '#murdoch-marketing-home';
							}
							//add content to page
							$("div.page").html($("div#hidden-next-page").html());
							
							//alert($("div#hidden-next-page").html());

							//remove hidden content:
							$("div#hidden-next-page").empty();
							
							video = document.getElementsByTagName('video')[0];
							if (video && supportsVideo) {
								stratchVideo(video);
								video.load();
							} else {
								video = false;
							}

							if (ifBigImgExists()) {
								stratchImg();
								fitSectionHeight();
							}	
							//fix for clicking through long/short pages
							$(document).scrollTo( 0, 0 );	
							
							$("div.page").fadeIn(fadeSpeed, function() {
								//play video
								if (video && supportsVideo) {
									//alert(video);
									video.play();
								}
								
							});
							
							//$("#navigation-tip p").delay(4000).fadeOut(fadeSpeed);
							done.call();
						});
						
											
					});

				} else {
					alert('We could not find the file. Please try again later.'); 
				}
			}
		});
	}
	done.call();
});

$(window).resize(function() {
	stratchImg();
	if (video && supportsVideo) {
		stratchVideo(video);
	}
	fitSectionHeight();
});

$(document).ready(function() {
	//hide header after 2 sec
	var header_timeout_id = window.setTimeout('$("header").fadeOut(fadeSpeed);', 4000);
	
	$(".page-logo").live("mouseover", function(event) {
		$("header").fadeIn(fadeSpeed);		
	});	
	$("header").hover(
		function () {
			//
		}, 
		function () {
			if ( $(window).scrollTop() == 0 ) {
				//hide only if we are at the top
				$("header").fadeOut(fadeSpeed);
			}
		}
	);
	$("#social-buttons a").hover(
		function () {
			var buttonClass = $(this).attr("class");
			$("#social-buttons a."+buttonClass+".over").fadeIn(fadeSpeed);
		}, 
		function () {
			//
		}
	);
	$("#social-buttons a.over").hover(
		function () {
			//
		}, 
		function () {
			$(this).fadeOut(fadeSpeed);
		}
	);
	
	//Main Nav Ajax
	$("nav ul").tabs("div.page", {effect: 'fadeAjax', history: true});

	$("a.murdoch-marketing-logo").click(function() {
		var api = $("nav ul").data("tabs");
		api.click(0);
		return false;
	});
	$(".select-clients .img-list li a").live("mouseenter",function(){
		$(this).find('span').fadeIn(fadeSpeed);
	});
	$(".select-clients .img-list li a").live("mouseleave",function(){
		$(this).find('span').fadeOut(fadeSpeed);
	});
	$(".case-studies .img-list li a").live("mouseenter",function(){
		$(this).find('span').fadeIn(fadeSpeed);
	});
	$(".case-studies .img-list li a").live("mouseleave",function(){
		$(this).find('span').fadeOut(fadeSpeed);
	});
	$(".shifted").live("click",function(){
		$('.shifted-section').show();
		$(document).scrollTo( '.shifted-section', shiftSpeed );
		return false;
	});
	
	
	/* Scrolling */
	function isScrolled(element) {
		var bodyTop = $(window).scrollTop();
		var orgOffset = '1'
		if ( bodyTop >= orgOffset) {
			return true;
		}
		if ( bodyTop <= orgOffset) {
			return false;
		}
	}
	$(window).scroll( function() {
		//clear hide header timeout 
		clearTimeout(header_timeout_id);
		if( isScrolled('header') ){
			if (!$('header').hasClass('visible_fixed')) {
				$("header").fadeIn(fadeSpeed);
				$('header').addClass('visible_fixed');
			}	
		} else {
			if ($('header').hasClass('visible_fixed')) {
				$("header").fadeOut(fadeSpeed);
				$('header').removeClass('visible_fixed');
			}	
		}
	});
	
});


