$(function(){
	// CSS overrides to position page elements controlled by Javascript
	$(".main").add("#page").hide();
	$(".subnav").css("display","none");
	var x = $(window).width()/2;
	$(".bar").hide().css({
		position:"absolute",
		left:x+"px",
		marginLeft: "-225px",
		marginRight:"0px",
		padding:"0px",
		backgroundPosition:"0px 0px"
	}).filter(".bottom").css({top: $(window).height()-80+"px"});
	var current = $("body").attr("class");
	// AJAX Loading & Fading Effects
	
	$("body").ajaxStart(function(){
		$(this).prepend('<div id="loading" style="display:none"><img src="images/loading.gif" alt="Loading" /></div>');
		$("#loading").stop().fadeIn("fast");
	});
	
	$("body").ajaxStop(function(){
		$("#loading").stop().fadeOut(500, function(){$(this).remove();});
		find_current();
		$("#page").fadeIn(1000, function(){ajaxify();})
		page_size();
	});
	
	$("#page").ajaxError(function(event, request, settings){
	   $(this).empty().append("<h1>Error requesting page " + settings.url + "</h1><h3>There's a lot of content to fill in. Wanna help?</h3>");
	});

	// Load the CSS Sprites for Navigation
	
	$(".logo").children("li").each(function() {
		$(this).children("a").css({backgroundImage:"none"});
	});
	$(".nav").children("li").each(function() {
		$(this).children("a").css({backgroundImage:"none"});
	});
	attachNavEvents(".logo", "zc");
	attachNavEvents(".nav-1", "ethical");
	attachNavEvents(".nav-2", "media");
	attachNavEvents(".nav-3", "professionals");
	
	// Load the History Manager for all browsers except ie6 and adjust the positioning of the main content area
	if(!($.browser.msie && parseInt($.browser.version) < 7)){
		$("#page").toggleClass("prepend-8").toggleClass("prepend-7");
		$(".subnav").css({marginLeft:"0px"}).parent().css({marginLeft:"18px"});
		var hash = $.historyInit(pageload);
		if(hash=="home") hash="";
		if ($("body").attr("class") != "intro"){
			location.href=location.protocol+"//"+location.host+"/#"+$("body").attr("class");
		}
		else if(hash && hash != "index"){
			$("#page").load("zc_cms.php", {"p":hash});
			$("body").removeClass().addClass(hash);
			open_nav();
		}
		else{
			ajaxify();
			$("body").removeClass("intro").addClass("home");
			close_nav();
		}
	}
	// For ie6 we use static URLs and no ajax
	else{
		//if(location.hash){location.href=location.protocol+"//"+location.host+"/"+$("body").attr("class");
		// Check if the site loaded to the first page or a specific page
		if(current!="intro"){
			page_size();
			$(".subnav").css({marginLeft:"0px"}).parent().css({marginLeft:"164px"});
			$(".subnav li a").each(function(){
				var title=$(this).attr("title").toLowerCase();
				title = title.replace(" ","_");
				if(title == current){
					$(this).addClass("current").parent().parent().parent().parent().addClass("current");
				}
			});
			$(".subnav").fadeIn("fast");
			open_nav();
		}
		else{
			close_nav();
		}
	}
    if (current == "intro") {
        $("#page").fadeIn("slow");
    }
	$(".main").fadeIn("slow");
	$(".bar").fadeIn();
});

function resetNav(){
	$(this).unbind("click",resetNav);
	$(".subnav").fadeOut("fast");
	var li = $(".current > li");
	var last = $(this).parent().attr("class");
	if (li.length > 1){
		li.each(function(){
			if($(this).attr("class") && last != $(this).attr("class")){
				$(this).parent().animate({
					backgroundPosition:"0px 0px"
				}, 200);
				$(this).children(":not(.subnav)").fadeIn();
			}
		});
	}
	else{
		li.parent().animate({
			backgroundPosition:"0px 0px"
		}, 200);
		li.children(":not(.subnav)").fadeIn();
	}
	var ul = $("ul.current");
	if (ul.length > 1){
		ul.each(function(){
			var hash = $(this).children("li").attr("class");
			if(last != hash){
				var classes = $(this).attr("class");
				var navClass = "."+classes.substr(classes.indexOf("nav-"),5);
				attachNavEvents(navClass, hash);
			}
		});
	}
	else if (ul.length==1){
		var hash = ul.children("li").attr("class");
		var classes = ul.attr("class");
		var navClass = "."+classes.substr(classes.indexOf("nav-"),5);
		attachNavEvents(navClass, hash);
	}
	if (ul.length<1){
		attachNavEvents(".nav-1", "ethical");
		attachNavEvents(".nav-2", "media");
		attachNavEvents(".nav-3", "professionals");
	}
}

function attachNavEvents(parent, myClass) {
    $(parent + "> li." + myClass).mouseover(function() {
        $(this).before('<div class="nav-' + myClass + '"></div>');
        $("div.nav-" + myClass).css({display:"none"}).fadeIn(400);
    }).mouseout(function() {
        $("div.nav-" + myClass).fadeOut(400, function() {
            $(this).remove();
        });
    });
}

function pageload(hash) {
	if(hash=="home") hash="";
	pageTracker._trackPageview("/"+hash); 
	$("#scrollbar").fadeOut("fast",function(){$(this).remove()});
	if(hash){
		$("#page").fadeOut("normal", function(){
			$(this).load("zc_cms.php", {"p":hash});
			if($("body").attr("class") == "intro" || $("body").attr("class") == "home") open_nav();
			$("body").removeClass().addClass(hash);
		});
	} else {
		$("#page").fadeOut("normal", function(){
			$(this).load("index.php", {"p":hash}).fadeIn("slow", function(){ajaxify();});
			$("body").removeClass().addClass("home");
			$(".nav").animate({
				backgroundPosition:"0px 0px"
			}, 200);
			$(".subnav").fadeOut("fast");
			$(".main > li > a").unbind("click",resetNav);
			close_nav();
		});
	}
}

function find_current(){
	$(".current").removeClass("current");
	var current = $("body").attr("class");
	if(current!="intro"){
		if($(".nav > li").each(function(){
			if($(this).attr("class") == current){
				$(this).parent().addClass("current");
				$("li."+current).unbind("mouseover").unbind("mouseout").children().hide();
				$("div.nav-" + current).remove();
				$(".main > li > a").click(resetNav);
				return true;
			}
		}) != true){
			$(".subnav li a").each(function(){
				var title=$(this).attr("title").toLowerCase();
				title = title.replace(" ","_");
				if(title == current){
					$(this).addClass("current").parent().parent().parent().parent().addClass("current");
					$(this).parent().parent().parent().parent().children("li:first").unbind("mouseover").unbind("mouseout").children().hide();
					$("div.nav-" + current).remove();
					$(".main > li > a").click(resetNav);
				}
			});
		}
		$("ul.current").animate({
			backgroundPosition:"-132px 0px"
		}, 200, null, function(){
			$(".current .subnav").fadeIn("fast");
		});
	}
}

function ajaxify(){
	$("a.ajax").removeClass("ajax").click(function(){
		this.href="#";
		var hash = this.title;
		hash = hash.replace(" ","_");
		hash = hash.toLowerCase();
		$.historyLoad(hash);
		return false;
	});
}

function page_size(){
$("#page").css({marginTop: "100px"}).height(parseInt($(window).height())-200+"px");
if($("#page").attr("scrollHeight") - $("#page").height() > 0) scroll();
}

function scroll(){
	$("#page").after("<div id='scrollbar' class='span-2 last'><div class='ui-slider-handle'></div></div>");
	var maxScroll = $("#page").attr("scrollHeight") - $("#page").height();
	var v = (100/maxScroll)*(maxScroll - $("#page").attr("scrollTop"));
	$("#scrollbar").slider({
		min:0,
		max:100,
		value:v,
		slide:update_scroll,
		change:update_scroll,
		orientation:"vertical"
	}).height($("#page").height());
	$('#page').mousewheel(handle_wheel);
	$("#scrollbar").mousewheel(handle_wheel);
}

function update_scroll(e, ui){
	var maxScroll = $("#page").attr("scrollHeight") - $("#page").height();
	$("#page").attr({"scrollTop": maxScroll - (ui.value * (maxScroll / 100) )});
}

function handle_wheel(event, delta) {
	var current = $("#scrollbar").slider("value");
	var maxScroll = $("#page").attr("scrollHeight") - $("#page").height();
	$("#scrollbar").slider("value", current + (2*delta*$("#page").height()/maxScroll));
}

function logo_size(){
	var correction = 0;
	if ($(window).height() < 740){
		$(".logo").fadeOut("slow", function(){
			$(".logo").before('<ul class="main logo-small"><li class="zc-small"><a class="ajax" title="Home"  href="/">Zero Coordinate</a></li></ul>').remove();
			$(".logo-small").children("li").each(function() {
				$(this).children("a").css({backgroundImage:"none"});
			});
			attachNavEvents(".logo-small", "zc-small");
		});
		$(".logo-small").fadeIn();
		correction = -160;
	}
	else if($(".logo-small").height()){
		$(".logo-small").fadeOut("slow", function(){
			$(".logo-small").before('<ul class="logo"><li class="zc"><a class="ajax" title="Home"  href="/">Zero Coordinate</a></li></ul>').remove();
			$(".logo").children("li").each(function() {
				$(this).children("a").css({backgroundImage:"none"});
			});
			attachNavEvents(".logo", "zc");
		});
		$(".logo").fadeIn();
	}
	return correction;
}

function close_nav(){
	var x = $(window).width()/2;
	var y = $(window).height()/2;
	$(".bar.top").animate({
		top:y+"px",
		left:x+"px",
		marginTop:"-167px",
		marginLeft:"-225px"
	}, 600);
	$(".bar.bottom").animate({
		top:y+"px",
		left:x+"px",
		marginTop:"98px",
		marginLeft:"-225px"
	}, 600);
	$(".nav-1").animate({
		top:y+"px",
		left:x+"px",
		marginTop:"-82px",
		marginLeft:"-225px"
	}, 600);
	$(".nav-2").animate({
		top:y+"px",
		left:x+"px",
		marginTop:"-82px",
		marginLeft:"-80px"
	}, 600);
	$(".nav-3").animate({
		top:y+"px",
		left:x+"px",
		marginTop:"-82px",
		marginLeft:"65px"
	}, 600);
}
function open_nav(){
	var x = $(window).width()/2;
	var y = $(window).height()/2;
	// Correct for 12" XGA monitors to fit Nav
	var correction = logo_size();
	$(".bar.top").animate({
		top:"1em",
		left:x+"px",
		marginTop:"0px",
		marginLeft:"-155px"
	}, 600);
	$(".bar.bottom").animate({
		top: $(window).height()-85+"px",
		left:x+"px",
		marginTop:"0px",
		marginLeft:"-155px"
	}, 600);
	$(".nav-1").animate({
		top:220+correction+"px",
		left:"10px",
		marginTop:"0px",
		marginLeft:"0px"
	}, 600);
	$(".nav-2").animate({
		top:384+correction+"px",
		left:"10px",
		marginTop:"0px",
		marginLeft:"0px"
	}, 600);
	$(".nav-3").animate({
		top:548+correction+"px",
		left:"10px",
		marginTop:"0px",
		marginLeft:"0px"
	}, 600);
}