var slideDownTime = 500;
var products = new Array;
var currentBgId = 0;

$(document).ready(function(){	
	$("#item_thumb_0").find("img").fadeTo("fast", 0.5); 
	$("#item_thumb_0").find("img").hover(
		function(){ $(this).fadeTo("fast", 1.0); }, 
		function(){ $(this).fadeTo("fast", 0.5); }
	);
	$("#item_thumb_1").find("img").fadeTo("fast", 0.5); 
	$("#item_thumb_1").find("img").hover(
		function(){ $(this).fadeTo("fast", 1.0); }, 
		function(){ $(this).fadeTo("fast", 0.5); }
	);

	clock();
	setTimeout("resizeBgImg(); reposFooter();", 500);
	$(window).resize( function () { resizeBgImg(); reposFooter(); } )
	setInterval("clock();", 1000);
	
	$(document).bind('keydown', 'left', function() { change_background('left');});
	$(document).bind('keydown', 'right', function() { change_background('right');});

	// WINDOW RESIZE
	/*
	var reposFooterTimer = null;
	$(window).bind('resize', function() {
		if (reposFooterTimer) { clearTimeout(reposFooterTimer); }
		reposFooterTimer = setTimeout( function () { resizeBgImg(); reposFooter(); }, 100);
	});
	*/
});


var toggle_more_info = function () {
	if ($("#more_info").html() == "More Information") {
		$("#more_info").html("Less Information");
	} else {	
		$("#more_info").html("More Information");
	}
	$('#projects_lower').slideToggle( 500 );
}


var change_background = function (/* site, */id)
{
	var bgs = backgrounds.data.length;	
	
	// 
	if (id == 'left') {
		id = currentBgId-1;
	} else if (id == 'right') {
		id = currentBgId+1;
	}	
	if (id >= bgs) {
		id = 0; 
	}	
	if (id < 0) {
		id = bgs-1; 
	}		
	
	if (bgs > 1)
	{		
		var id0 = (id == 0) ? bgs-1 : id-1;
		var id1 = (id == bgs-1) ? 0 : id+1;
		
		if (id0 == id1) {
			id1 = -1;
		} 	
		//alert(bgs);
		if (bgs == 2 && id == 0) {
			tmp = id0;
			id0 = id1;
			id1 = tmp;
		}
	
		
		currentBgId = id;
		var src = eval("image_"+id+".src");
		$("#backgroundimageimg").fadeOut(150, function () {
			$('#bg_image').html("<img src=\""+src+"\" id=\"backgroundimageimg\" style=\"display: none;\" />");
			resizeBgImg();
			$("#backgroundimageimg").fadeIn(150);
		} );
		
		$("#current_bg").html( id + 1);
		
		$("#item_thumb_0").html(thumb_switch_html(/*site, */id0));
		$("#item_thumb_1").html(thumb_switch_html(/*site, */id1));
		
		$("#item_thumb_0").find("img").fadeTo("fast", 0.5); 
		$("#item_thumb_1").find("img").fadeTo("fast", 0.5); 
		
		$("#item_thumb_0").find("img").hover(
			function(){ $(this).fadeTo("fast", 1.0); }, 
			function(){ $(this).fadeTo("fast", 0.5); }
		);	
		$("#item_thumb_1").find("img").hover(
			function(){ $(this).fadeTo("fast", 1.0); }, 
			function(){ $(this).fadeTo("fast", 0.5); }
		);		
	}
}

var resizeBgImg = function () {
	if ($("#bg_image").length > 0) {
		var winW = $(window).width();
		var winH = $(window).height(); 
		
		var imgW = eval("image_"+currentBgId+".width");
		var imgH = eval("image_"+currentBgId+".height");

		var wRatio = winW / imgW;
		var hRatio = winH / imgH;
		
		var newW = winW;
		var newH = imgH * wRatio;
		
		var top = 0;
		
		if (newH > winH) {
			top = (winH - newH) / 2;
		} else {
			newH = winH;
			newW = imgW * hRatio;
			top = 0;
		}
			
		$("#bg_image").css( { display : 'block', width: newW + "px", height : newH + "px", top: top + "px"});
	}
}


var thumb_switch_html = function (/*site, */offset)
{
	if (backgrounds.data[offset] != undefined)
	{
		//var html = "<a href=\"#\" onclick=\"change_background('"+site+"', "+offset+");\">";
		var html = "<a href=\"#\" onclick=\"change_background("+offset+");\">";
		html += "<img src=\"/media/"+backgrounds.data[offset].small_id+"/"+backgrounds.data[offset].small_name+"."+backgrounds.data[offset].small_ext+"\" />";
		html += "</a>";
	}
	else 
	{ 	
		var html = "<a href=\"#\">";
		html += "<img src=\"/media/images/";
		html += site;
		html += "_default_thumb.png\" />";
		html += "</a>";
	}
	return html;
}










var working = false;
var navProducts = function (action, offset)
{
	if (!working)
	{
		offset = (action == "next") ? offset+1 : offset-1;	
		if (products[offset] == undefined) {
			offset = (action == "next") ? 0 : products.length - 1;	
		}
		
		//hideProductDetails();
		working = true;
		showProductDetails(products[offset], offset);
	}
}

var showProductDetails = function (id, offset) 
{	
	currentProductOffset = offset;
	
	// $("#product_details").slideUp();
	// $("#product_details").css("opacity", "0.95");
	$.post ("/silkscreen/products/product/"+id+"/"+products.length+"/"+offset, {}, 
		function (data) {
			$("#product_details").html(data);
			$("#product_details").slideDown();
			working = false;
	});
}
var hideProductDetails = function () 
{	
	$("#product_details").slideUp();
}

var hideProductRed = function (id)
{
	$("#product_"+id+"_image").show();
	$("#product_"+id+"_red").hide();
}

var showProductRed = function (id)
{
	$("#product_"+id+"_image").hide();
	$("#product_"+id+"_red").show();
}



var reposFooter = function ()
{	
	var pos = $("#footer").position();
	var contHeight = $("#wrapper").height();
	
	if (pos != undefined || contHeight != undefined) 
	{
		if (pos.top < contHeight - 40 || pos.top < $(window).height()) {
			//$(window).height() - 40
			$("#footer").css({"bottom" : 0, "position" : "absolute"});
		} 
		
		if ( contHeight + 40 > $(window).height() ) {
			$("#footer").css({"clear" : "both", "position" : "static"});
		}
	}
}


/* 
HOMEPAGE
*/
var change_homepage_img = function (img)
{
	//$(".homepage_img").hide()
	//$("#homepage_img_"+img).show();
	
	$('body').css({ backgroundImage : "url('"+img+"')" });
}

/* 
PROJECTS
*/
var projectsToggle = function ()
{		
	if ($('#hide').html() == "Hide") {
		/*
		if ($("#projects_lower").is(":visible") ) {
			$("#projects_lower").slideUp(500);
		} else {
		*/
			$('#project').slideUp(500);		
			$('#hide').html("Show");
			/*
			if ($("#more_info").html() == "Less Information") {
				$("#more_info").html("More Information");
			}*/
		//}		
			
		//$('#hide').parent().css("padding-top", "0px");
		//$('#hide').parent().css("padding-bottom", "0px");
	} else {
		//$('#hide').parent().css("padding-top", "11px");
		//$('#hide').parent().css("padding-bottom", "11px");
		$('#project').slideDown(500);
		$('#hide').html("Hide");		
	}
}

/*
EVENTS
*/
var change_sponsor = function (obj, id, name, ext)
{
	$(obj).attr("src", "/media/"+id+"/"+name+"."+ext);
}

/*
BLOGS
*/
var blog_image_timer = null;
var blog_img;

var blogImgRotation = function (group, current)
{
	var current = (current == $(".blogImgs_"+group).length) ? 0 : current;
	var next 	= (current == $(".blogImgs_"+group).length -1) ? 0 : current+1;

	setTimeout( 
		function () { 
			$(".blogImgs_"+group).eq(current).fadeOut(1, function () {	
				$(".blogImgs_"+group).eq(next).fadeIn(1, function () { 
					blogImgRotation(group, next); 
				});
			})
		}
	, 1000);			
}

var initMainContainer = function (id, call) {
	$("#" + id).append("<div id=\"hide\" onclick=\"toggleMainContainer('"+id+"');\">Show</div>");
	
	show = getCookie('show');
	if (show == 'true' || show == '') {
		toggleMainContainer(id);
	}
	call();
}

var toggleMainContainer = function (id)
{
	if ( $("#hide").html() == "Hide" ) {
		$("#container").slideUp( 1000 );
		$("#hide").html("Show");
		setCookie('show', 'false', 1);
	} else if ( $("#hide").html() == "Show" ) {
		$("#container").slideDown( 1000 );
		$("#hide").html("Hide");
		setCookie('show', 'true', 1);
	}
}

var clock = function( )
{
	var currentTime = new Date ( );

	// DATE
	var currentDate 	= currentTime.getDate ();
	var currentDay 		= currentTime.getDay ();
	var currentMonth 	= currentTime.getMonth (); 
	var currentYear  	= currentTime.getYear ();
	
	if (currentYear < 1000)
		currentYear+=1900;
	
	//var dayarray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	if ( currentDate < 10)
		currentDate = "0" + currentDate;
	
	
	// TIME
	var currentHours = currentTime.getHours ( );
	var currentMinutes = currentTime.getMinutes ( );
	var currentSeconds = currentTime.getSeconds ( );

	// Pad the hours, minutes and seconds with leading zeros, if required
	currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
	currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
	currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

	// Choose either "AM" or "PM" as appropriate
	//var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

	// Convert the hours component to 12-hour format if needed
	//currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

	// Convert an hours component of "0" to "12"
	currentHours = ( currentHours == 0 ) ? 12 : currentHours;

	// Compose the string for display
	var currentTimeString = "Time - " + currentHours + ":" + currentMinutes + ":" + currentSeconds + "<br />";
	currentTimeString += "Date - " + currentDate + " " + montharray[currentMonth] + " " + currentYear ;

	// Update the time display
	$("#timedate").html(currentTimeString);
}


var menuClicked = function (group, area) {
	var isCurrent = (area == "current") ? "current" : "archive";
	setCookie(group+"left", isCurrent, 1);	
	return true;
}

var archiveSwtichTo = function (group, to, first) {	
	if (to == "") { 
		if ($("#present_"+group+"_archive").find(".current").length == 1) { 
			to = "archive";		
		} else {
			to = "current";				
		}
	}
	var from = (to == "current") ? "archive" : "current";
	
	var speed = 250;
	if (first == undefined) {
		$('#present_'+group+'_'+from).slideUp(speed);
		setTimeout(function() { $('#present_'+group+'_'+to).slideDown(speed); }, speed+(speed/4));
	} else {
		$('#present_'+group+'_'+to).slideDown(speed);
	}
}




// COOKIES
var setCookie = function(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
var getCookie = function (c_name) {
	value = "";
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) { c_end=document.cookie.length; }
			value = unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return value;
}
