$(function() {
	//alert(viewport.height());
	adjust_hero_height();
	load_default_billboard();
	set_mask_height();
	set_nav_width();
	$(".th_mask").each(function(){
        $("#"+this.id).mouseover(function(){
        	//remove_active_classes();
        	$(".th_mask").removeClass("th_active");
        	$("#"+this.id).addClass("th_hover");
        	clearTimeout(t);
        }).mouseout(function(){
        	$("#"+this.id).removeClass("th_hover");
        	$("#"+this.id).addClass("th_active");
        }).click(function(){
			//location.href = $("#"+this.id+" a").attr("href");
			var raw_id = this.id.split("_");
        	$("body#home").removeClass().addClass("hero"+raw_id[1]);
        	show_billboard(raw_id[1]);
		});
	 });
	 //link blue sections
	$("#home_middle .hm").each(function(){
		$("#"+this.id).click(function(){
			location.href = $("#"+this.id+" a").attr("href");
		});
	});
	//link bottom charts
	$("#home_bottom .c2_1").click(function(){
		location.href = $("#home_bottom .c2_1 p a:first").attr("href");
	});
	$("#home_bottom .c2_2").click(function(){
		location.href = $("#home_bottom .c2_1 p a:first").attr("href");
	});
	t=setTimeout('startSlideShow(1)',8000);
});


function startSlideShow(n){
	var thn = $(".th_mask");
	var x = n;
	$("body#home").removeClass().addClass("hero"+x);
	$(".th_mask").removeClass("th_active");
	$("#th_"+x).addClass("th_active");
    show_billboard(x);
    x++;
    if(x==thn.length){x=0}
    t=setTimeout('startSlideShow('+x+')',8000);
}


function load_default_billboard(){
	$("body#home").addClass("hero0");
	$("#home_hero_txt_0").show();
	$("#th_0").addClass("th_active");
}

function show_billboard(n){
	//hide all billboards first
	$(".home_hero_txt").hide();
	$("#home_hero_txt_"+n).show();
}

function adjust_hero_height(){
	if (viewport.height() > 960){
		$("#home_top").height(500);
	} else if (viewport.height() < 960 && viewport.height() > 768){
		$("#home_top").height(450);
	} else if (viewport.height() < 768){
		$("#home_top").height(400);
	}
	
}

function set_mask_height(){
	if (viewport.height() < $("#container").height()){
		$("#mask").height($("#container").height());
	}else{
		$("#mask").height(viewport.height());
	}
	
}

function set_nav_width(){
	var prim = $("#primary_nav").width();
	$("#secondary_nav").width(prim+10);
}

jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

window.viewport = {
    height: function() {
        return $(window).height();
    },
    width: function() {
        return $(window).width();
    },
    scrollTop: function() {
        return $(window).scrollTop();
    },
    scrollLeft: function() {
        return $(window).scrollLeft();
    }
};

$(window).resize(function() {
  adjust_hero_height();
  set_mask_height();
});

