var y_pos = 0,
timer;

$(document).ready(function(){
	
	// Main menu
	/*$('#primary-menu > a').hover(
		function(){ $('img', this).stop().animate({'opacity':.5}, 300, 'easeOutSine'); },
		function(){ $('img', this).stop().animate({'opacity':1}, 800, 'easeOutQuad'); }
	);*/
	
	// Intro
	if ($("#flashIntro").size() > 0) showIntro();
	else init();
	
	// Make sure shadowbox images are clickable to go to the next image
	$("#sb-body").live("click", function(){
		if (Shadowbox.gallery.length > 1 && Shadowbox.getCurrent().player != "flv" && Shadowbox.getCurrent().player != "qt"){
			if (Shadowbox.hasNext()) Shadowbox.next()
			else Shadowbox.change(0);	
		}
	});
});

function init(){
	// Handle rollovers with preloading
	$("img.hover").each(function(){
		var img = new Image();
		img.src = $(this).attr("src").replace("_off","_on");
				
		$(this).hover( function(){
			this.src = this.src.replace("_off","_on");
		}, function(){
		  	this.src = this.src.replace("_on","_off");
		});		
	});

	
	if (case_mode){
		
		$(window).bind('resize', updateCasePosAndSize);
		updateCasePosAndSize();
		
	} else {

		// Scrolling sidebar
		if ( $("#header").size() > 0 ){
			y_pos = parseInt($("#header").css("top").substring(0, $("#header").css("top").indexOf("px")));
			$(window).scroll(function () {
				var offset, win_height = (typeof(window.innerHeight) == 'number') ? window.innerHeight : document.documentElement.clientHeight;
				if (win_height > $("#header").height() + y_pos){
					offset = y_pos + $(document).scrollTop() +"px";
				} else {
					offset = win_height - $("#header").height() + $(document).scrollTop() +"px";
				}
				console.log(offset);
				if (parseInt(offset) < y_pos) offset = y_pos;
				$("#header").animate({top:offset},{duration:500,queue:false})
			});	
		}
	
	}
}
function updateCasePosAndSize(){
	
	if ($("#case-slider").size() > 0){
		
		var main_height = $(window).height() - $("#cc-header").height() - $("#cc-footer").height() - 130,
		slider_height = 305;
		if (main_height < slider_height) main_height = slider_height;
		
		$("#cc-main").css("height", parseInt(main_height) +"px");
		if (main_height > slider_height) $("#case-wrapper").css("margin-top", parseInt((main_height - slider_height) / 2) +"px");
		
	} else {
		// Adjust the width if neccessary
		var target_width = $(window).width() - 100,
		case_width = $(".case-box").width()
		has_thumbs = ($("#case-box-thumbs").size() > 0) ? true : false,
		has_image = ($("#case-box-image").size() > 0) ? true : false;
		if (target_width > 1150) target_width = 1150;

		$("#case-box-holder").css("width", parseInt(target_width) +"px");
		$(".case-box").css("width", parseInt(target_width - 125) +"px");



		if (has_thumbs){

			if ($(".case-box").width() < 575){
				$("#case-box-thumbs").hide();
				$("#case-box-image").hide();
				$("#case-box-text").css("width", "90%");
			} else if ($(".case-box").width() < 900){
				$("#case-box-thumbs").show();
				$("#case-box-image").hide();
				$("#case-box-text").css("width", parseInt($(".case-box").width() - 280) +"px");
			} else {
				$("#case-box-thumbs").show();
				$("#case-box-image").show();
				$("#case-box-text").css("width", parseInt($(".case-box").width() - 686) +"px");
			}

		} else if (has_image) {

			if ($(".case-box").width() < 700){
				$("#case-box-image").hide();
				$("#case-box-text").css("width", "95%");
			} else {
				$("#case-box-image").show();
				$("#case-box-text").css("width", parseInt($(".case-box").width() - 460) +"px");
			}

		}


		// Center the box
		var main_height = $(window).height() - $("#cc-header").height() - $("#cc-footer").height() - 130,
		case_height = $(".case-box").height();
		if (main_height < case_height) main_height = case_height;
		$("#cc-main").css("height", parseInt(main_height) +"px");
		if (main_height > case_height) $("#case-wrapper").css("margin-top", parseInt((main_height - case_height) / 2) +"px");
	}
	
}


function popupLink(t,w,h){
	window.open(t.href, 'JCP', 'scrollbars=1,menubar=0,width='+ w +',height='+ h +',resizable=1,toolbar=0,location=0,status=0,left='+ (screen.width-w)/2 +',top='+ (screen.height-h)/3);
	return false;
}

function showIntro(){
	$("#bg").hide().removeClass("hidden").fadeIn(600, function(){
		var params = { scale:'noScale', salign:'cm', menu:'false', allowscriptaccess:'always', allowfullscreen:'true', wmode:'transparent' };
		swfobject.embedSWF(theme_root +"/flash/jcp_intro.swf", "flashIntro", "100%", "90%", "9.0.0", "js/expressinstall.swf", {}, params, {});
		timer = setTimeout("closeIntro()", 8000);
	});
}
function flashIntroCompleted(){
	// Called from within flash
	clearInterval(timer);
	closeIntro();
}
function closeIntro(){
	$("#flashIntro").remove();
	if (case_mode) $("#cc-container").hide().removeClass("hidden").fadeIn("slow");
	else $("#container").hide().removeClass("hidden").fadeIn("slow");
	init();
}
