/* Resize */
function sizeThings(){
	$("#slides").css("marginLeft", 0 );
	var slidesL = Math.round( $(".slide:eq(1)").offset().left );
	var logoL = Math.round( $("#header h1").offset().left );
	var diff = logoL - slidesL;
	$("#slides").css("marginLeft", diff+"px" );
	$("#controls").css("marginLeft", logoL+"px" );
	// $("#underlay").css("marginLeft", (logoL-10)+"px" );
	$("#underlay").hide();
}

$(function(){
	sizeThings();
	$(window).resize( function(){ sizeThings(); });
});

 
/* Product Spinner */
var pT = 0;
var pL = 0;
var pani = false;

function changeProducts( x ){
	if ( pani == true ) return false;
	pL = $(".product").length;
	
	if ( x > (pL-3) ) return false; // x = pL-1;
	if ( x < 0 ) return false; //x = 0;
	pT = x;
	
	y = -(x*330);
	pani = true;
	$("#proda").animate({
		"marginLeft": y+"px"
		}, 1000, function(){
			pani = false;
		}
	);
}

$(function(){

	$(".product").live("mouseenter", function(){
		$(this).find(".button").addClass("on");
	}).live("mouseleave", function(){
		$(this).find(".button").removeClass("on");
	}).live("click", function(){
		window.location = $(this).find("a").attr("href");
	});
	
	$(".nav .arrow.left").live("click", function(){
		changeProducts( pT-1 );
		return false;
	});
	$(".nav .arrow.right").live("click", function(){
		changeProducts( pT+1 );
		return false;
	});
});

/* Homepage Feature Area */
var hovering = false;
var animating = false;
var timer = 0;

function slide(){
	if ( hovering == true ) return false;
	var left_indent = parseInt($("#slides").css("marginLeft")) - 4;
	resetTimer();
	animating = true; $("#underlay").hide();
	$("#slides:not(:animated)").animate({"marginLeft" : left_indent},40,function(){  
	
		if ( $("#slides .slide:first").offset().left < -970 ) {
			
			$("#slides .slide:last").after($("#slides .slide:first"));
			$('#slides').css({'marginLeft' : '+=970px'});
		}
		animating = false;
		// $("#underlay").show();
	});
}

function resetTimer(){
	if ( timer > 0 ) clearInterval( timer );
	timer = setInterval("slide()", 60 );
}

function slideRight(){
	if (animating == true) return false;

	var left_indent = parseInt($("#slides").css("marginLeft")) - 970;
	resetTimer();
	animating = true; $("#underlay").hide();
	$("#slides:not(:animated)").animate({"marginLeft" : left_indent},1000,function(){  
		$("#slides .slide:last").after($("#slides .slide:first"));
		$('#slides').css({'marginLeft' : '+=970px'});
		animating = false;
		$("#underlay").show();
	});
}

function slideLeft(){
	if (animating == true) return false;
	
	var left_indent = parseInt($("#slides").css("marginLeft")) + 970;
	resetTimer();
	
	animating = true; $("#underlay").hide();
	$("#slides .slide:first").before($("#slides .slide:last"));
	$('#slides').css({'marginLeft' : '-=970px'});

	var left_indent = parseInt($("#slides").css("marginLeft")) + 970;
	$("#slides:not(:animated)").animate({"marginLeft" : left_indent},1000,function(){  
		animating = false;
		$("#underlay").show();
	});
}

$(function(){
	$("#feature").live("mouseenter", function(){
		hovering = true;
		// $("#controls").fadeTo( 500, 1.0 );
	}).live("mouseleave", function(){
		// $("#controls").fadeTo( 200, 0 );
		hovering = false;
	});
	
	$("#slides .slide:first").before($("#slides .slide:last"));
	$("#slides .slide:first").before($("#slides .slide:last"));
	
	$("#controls a.right").live("click", function(){
		slideRight();
		return false;
	});
	$("#controls a.left").live("click", function(){
		slideLeft();
		return false;
	});

	resetTimer();
});


$(function(){


});

