/////////////////////////////////////////
//PRELOAD DETAILED IMAGES OVERLAY STUFF//
/////////////////////////////////////////
if(navigator.appVersion.indexOf("MSIE 6") == -1)
{
	if(document.images)
	{
		var overlay1 = new Image(726, 626);
		overlay1.src = "/images/detailed/detailed-images-bg.png";
		
		var overlay2 = new Image(50, 54);
		overlay2.src = "/images/detailed/close-button.png";
		
		var overlay3 = new Image(34, 34);
		overlay3.src = "/images/detailed/forward-button-off.png";
		
		var overlay4 = new Image(34, 34);
		overlay4.src = "/images/detailed/forward-button-on.png";
		
		var overlay5 = new Image(34, 34);
		overlay5.src = "/images/detailed/backward-button-off.png";
		
		var overlay6 = new Image(34, 34);
		overlay6.src = "/images/detailed/backward-button-on.png";
	}
}

var starOn = new Image(30, 29);
starOn.src = "/images/star-on.jpg";

//////////////////////////////////////////////////////////
//DISPLAY APPROPRIATE PRODUCT INFO BASED ON TAB SELECTED//
//////////////////////////////////////////////////////////
function displayProdInfo(tab)
{
	var index = $("#productInfoTabs li a").index(tab);
	
	$("#productInfoContent > div:not(:eq("+index+"))").hide();
	$("#productInfoContent > div:eq("+index+")").fadeIn(1000);
	
	$("#productInfoTabs li").attr("class", "");
	tab.parent().attr("class", "activeTab");
}

////////////////////////////////////////////
//FUNCTION TO MOVE PRODUCT WEIGHT TO SPECS//
////////////////////////////////////////////
function moveProdWeight()
{
	var weight = $("#productWeight").html();
	
	$("#productWeight").remove();
	$("#productSpecs > ul").prepend("<li>"+weight+"</li>");
}

////////////////////////////////
//FUNCTION TO ADD ITEM TO CART//
////////////////////////////////
function addToCart()
{
	$("#addToCartBtn").css({"font-size" : "14px", "font-weight" : "bold", "text-align" : "center", "background" : "none"}).html("Adding to cart...");
	
	var vals = $("form[name=orderform]").serialize();
	$.post("cart.php?mode=add", vals, function(data)
	{
		$("#addToCartBtn").attr("style", "").html('<a href="javascript:void(0)" onclick="addToCart()"></a>');
		window.location.href = "#";
		miniCart(true);
	});
}

///////////////////////////
//FUNCTION FOR STAR RATER//
///////////////////////////
var ratings = new Array();
ratings[0] = "Piece of poop";
ratings[1] = "Better than a hummus sack";
ratings[2] = "Neat";
ratings[3] = "SERIOUS RESPONSE: I like it";
ratings[4] = "I take it to bed with me";

function rateStars(caller, state)
{
	switch(state)
	{
		case "on":
			var idx = $("#starRating a").index($(caller));
			
			if($("#finalRating").length > 0)
			{
				clearTimeout(t);	
			}
			
			$("#starRating a").each(function()
			{
				$(this).children("img").attr("src", "/images/star-off.jpg");
			});
			
			$("#starRating a").each(function()
			{
				if($("#starRating a").index($(this)) <= idx)
				{
					$(this).children("img").attr("src", "/images/star-on.jpg");	
				}
			});
			
			$("#ratingResponse").html(ratings[idx]);
		break;
		
		case "off":
			if($("#finalRating").length > 0)
			{
				t = setTimeout(function()
				{
					$("#starRating").html($("#finalRating").html());
					$("#starRating a").mouseover(function(){rateStars(this, "on")}).mouseout(function(){rateStars(this, "off")}).click(function(){finalizeRating(this)});
				});
			}
			else
			{
				$("#starRating a").each(function()
				{
					$(this).children("img").attr("src", "/images/star-off.jpg");
				});
				$("#ratingResponse").html("&nbsp;");
			}
		break;
	}
}

////////////////////////
//FINALIZE STAR RATING//
////////////////////////
function finalizeRating(caller)
{
	if($("#finalRating").length > 0)
	{
		$("#finalRating").html($("#starRating").html());
	}
	else
	{
		$('<div style="display:none" id="finalRating">'+$("#starRating").html()+'</div>').appendTo($(document.body));
	}
	
	$("#hiddenVote").val($("#starRating > a").index($(caller))+1);
}

////////////////////////////////////////////////
//FUNCTION TO COUNT CHARACTERS USED FOR REVIEW//
////////////////////////////////////////////////
function countChars()
{
	var chars = 700 - parseInt($("#review_message").val().length);
	var theColor;
	
	if(chars >= 0 && chars < 11)
	{
		theColor = "#777777";
	}
	else if(chars < 0)
	{
		theColor = "#CC3333";
	}
	else
	{
		theColor = "#333333";
	}
	
	if(chars < 0)
	{
		$("#reviewFormWrap input[type=button]").attr("disabled", "disabled");
	}
	else
	{
		$("#reviewFormWrap input[type=button]").removeAttr("disabled");
	}
	
	$("#charNumb span").css("color", theColor).html(chars.toString());
}

////////////////////////////////////
//FUNCTION TO HANDLE REVIEW SUBMIT//
////////////////////////////////////
function submitReview()
{
	if($("#hiddenVote").val() == "")
	{
		alert("Please rate the product with the star rating system!");
	}
	else if($("#hiddenAuthor").val() == "")
	{
		alert("Please enter your name!");
	}
	else if($("#review_message").val() == "")
	{
		alert("Please enter your review!");
	}
	else if($("#captcha").val() == "")
	{
		alert("Please enter the characters at the bottom of your review!");
	}
	else
	{
		var pid = $("input[name=productid]").val();
		var author = $("#hiddenAuthor").val();
		var review = $("#review_message").val();
		
		$.post("/product.php?productid="+pid, {productid : pid, mode : "review", review_author : author, review_message : review, antibot_input_str : $("#captcha").val()}, function (data)
		{
			if(data.toString().indexOf("wrongCaptcha") > -1)
			{
				alert("The captcha code you entered was incorrect.  Refreshing page.  Please try again.");
				window.location.reload();
			}
			else if(data.toString().indexOf("ErrorMessage") > -1)
			{
				alert("You have already reviewed this product!");
			}
			else
			{
				$('<div id="submittingRev">Submitting your review...<br />Please wait.</div>').css({display : "none", "font-size" : "24px", color : "#333333"}).prependTo("#rateProd").slideDown(500);
				$("#voteForm").submit();
				$("#reviewFormIframe").load(function()
				{
					$("#submittingRev").remove();
					
					var newRev = $(this).contents().find("#revs").find(".custRev:eq(0)");
					newRev.css("display", "none");
					
					if($("#noRevs").length > 0)
					{
						$("#noRevs").html("").prepend(newRev).css("border-bottom", "0 none");
					}
					else
					{
						$("#revs").prepend(newRev);
					}
					
					$(newRev).slideDown(500);
					$("#rateProd").slideUp(500);
				});
			}
		});
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function()
{
	////////////////////////////////////////////////
	//SET TABS TO DISPLAY APPROPRIATE PRODUCT INFO//
	////////////////////////////////////////////////
	$("#productInfoTabs li a").click(function(){displayProdInfo($(this))});
	
	////////////////////////////////
	//MOVE PRODUCT WEIGHT TO SPECS//
	////////////////////////////////
	if($("#productWeight").length > 0)
	{
		moveProdWeight();
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//SET DETAILED THUMBS TO TRIGGER DETAILED IMAGES OVERLAY - DOESN'T WORK IN IE6 AND HAS SOME BUGS IN OPERA, SO GIVE THEM WINDOW POPUPS//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(navigator.appName != "Opera" && navigator.appVersion.indexOf("MSIE 6") == -1)
	{
		var dExpose = $("#detailedImagesWrap").expose({api:true});
		var dImages = $("#detailedImages").scrollable({size:1, prev:"#detailedImagesPrevLink", next:"#detailedImagesNextLink", api:true});
		
		$("#detailedThumbs a").each(function()
		{
			var i = $("#detailedThumbs a").index($(this));
			$(this).overlay(
			{
				target:"#detailedImagesWrap",
				finish:{top:5},
				onLoad:function(){dImages.seekTo(i)},
				close:"#detailedImagesClose"
			});
		});
		
		$("#detailedThumbs a").click(function(){dExpose.load()});
		
		$("#detailedImagesClose").click(function(){dExpose.close()});
		
		$("#detailedImages img").tooltip(
		{
			effect:"toggle",
			position:["bottom", "center"],
			offset:[-60, 0],
			opacity:0.9,
			effect:"fade"
		});
	}
	else
	{
		$("#detailedThumbs a").click(function()
		{
			var theUrl = $(this).children("img:eq(0)").attr("src");
			window.open(theUrl);
		});
	}
	
	//////////////////////
	//SET UP STAR RATING//
	//////////////////////
	$("#starRating a").mouseover(function(){rateStars(this, "on")}).mouseout(function(){rateStars(this, "off")}).click(function(){finalizeRating(this)});
	
	///////////////////////////////
	//COUNT CHARS USED FOR REVIEW//
	///////////////////////////////
	$("#review_message").keyup(countChars);
	
	/////////////////////////////////////////////////////////
	//UPDATE HIDDEN AUTHOR INPUT VALUE FROM VISIBLE TEXTBOX//
	/////////////////////////////////////////////////////////
	if($("#revAuthor").length > 0)
	{
		$("#revAuthor").keyup(function()
		{
			$("#hiddenAuthor").val($("#revAuthor").val());
		});
	}
	
	/////////////////
	//SUBMIT REVIEW//
	/////////////////
	$("#reviewFormWrap input[type=button]").click(submitReview);
});