//-----------------------------------------------------------------------
//																openPopUp
//
//-----------------------------------------------------------------------
function openPopUp(PageName){
	new_window = window.open(PageName,"new_window","width=420,height=520,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50"); 
	if (new_window.opener == null) { 
		new_window.opener = self; 
	} 
	new_window.focus();
}

//-----------------------------------------------------------------------
//														openSizedPopUp
//
//-----------------------------------------------------------------------
function openSizedPopUp(PageName, Width, Height){
	new_window = window.open(PageName,"new_window","width="+Width+",height="+Height+",location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50"); 
	if (new_window.opener == null) { 
		new_window.opener = self; 
	} 
	new_window.focus();
}


//-----------------------------------------------------------------------
//																decimalOf
//
// returns number to two decimal places (0.00)
//-----------------------------------------------------------------------
function decimalOf(n) {
	n = (n + 0.005) + "0";
	i = n.indexOf(".");
	n = n.substr(0, i+3);
	return n;
}


//-----------------------------------------------------------------------
//																dollarOf
//
//-----------------------------------------------------------------------
function dollarOf(n) {
	return '$' + decimalOf(n);
}


//-----------------------------------------------------------------------
//														Products Page
//
//-----------------------------------------------------------------------

function updateHero(image)
{
	$('#hero').attr({
			"src": "../images/products/large/" + image
		});
}



function productCheckQuantities(pie_assortment)
{
	var total_pies = 0;
	var found = false;
	$(".qty").each(function(i) {
		if (this.value > 0) {
			found = true;
			if (pie_assortment) {
				var name = $(this).attr('name');
				if (name == "qty_PIE-ASS-6")
					total_pies += 6 * parseInt(this.value);
				else if (name = "qty_PIE-ASS-8")
					total_pies += 8 * parseInt(this.value);
			}
		}
	});

	if (!found) {
		alert("Please enter the quantity you wish to order");
		return false;
	}
		
	if (!pie_assortment)
		return true;

	// make sure they ordered the right number of pies

	var flavors = ["apple", "applesqueeze", "cherry", "blueberry", "raspberry", "blackberry", "peach"]

	var count = 0;
	for (f in flavors) {
		var n = $("#" + flavors[f]).val();
		if (n != "")
			count += parseInt(n);
	}
	
	if (count != total_pies) {
		var msg = "You are ordering a total of " + (total_pies == 1 ? "1 pie" : total_pies + " pies") + ",  ";
		if (count < total_pies)
			msg += "but have only chosen " + count + ". Please choose " + (total_pies - count) + " more.";
		else
			msg += "but you have chosen " + count + "(" + (count - total_pies) + " too many).";
		alert(msg);
		return false;
	}
	
}


function showHideGiftMessage(i)
{
	if ($("#gift_message_" + i).attr("checked"))
		$('#gift_message_entry_' + i).slideDown();
	else
		$('#gift_message_entry_' + i).slideUp();
}


//-----------------------------------------------------------------------
//														Ratings Module
//
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
//													refreshAllRatings
// update all ratings stars
//-----------------------------------------------------------------------
function refreshAllRatings() {

	$(".current-rating").each( function(i) {
		var index = this.id.substr(15);
		var item = this.title;
		refreshRating(index);
	});
	
}

//-----------------------------------------------------------------------
//															refreshRating
//
// refresh the rating displayed for the specified item
//-----------------------------------------------------------------------
function refreshRating (index) {

	var item = ratingSKU(index);

	$.ajax({
		type: "GET",
		url: "/php/ratingsAjax.php",
		data: "cmd=getrate&item=" + item,
		cache: false,
		async: false,
		success: function(result) {
			// apply star rating to element
			$("#current-rating-" + index).css({ width:  "" + result + "%"});
		},
		error: function(result) {
// 				alert("some error occured for " + item + ", please try again later");
		}
	});

}



//-----------------------------------------------------------------------
//																rateItem
// The user is rating an item
//-----------------------------------------------------------------------
function rateItem(index, rating) {

	var item = ratingSKU(index);

	$.ajax({
		type: "GET",
		url: "/php/ratingsAjax.php",
		data: "cmd=setrate&item=" + item + "&rating=" + rating,
		cache: false,
		async: false,
		success: function(result) {
			// change class to reflect it's been rated
			$("#current-rating-" + index).addClass("rated-already");
			// remove #ratelinks element to prevent another rate
			$("#rating-stars-" + index + " .ratelinks").remove();
			// get rating after click
			refreshRating(index);
		},
		error: function(result) {
// 				alert("some error occured, please try again later");
		}
	});

}


//-----------------------------------------------------------------------
//																ratingSKU
// get the sku for a rating
//-----------------------------------------------------------------------
function ratingSKU(index) {

	// fetch the item sku from the title attribute
	var item = $("#current-rating-" + index).attr("title");

	return item;

}


//-----------------------------------------------------------------------
//																qtyChanged
// a shopping cart qty changed
//-----------------------------------------------------------------------
function qtyChanged(n)
{
	var qty = $("#qty_" + n).val();
	$("#cart").submit();
	// alert("line item " + n + ", qty=" + qty)
}
