//-----------------------------------------------------------------------
//																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);
}




//-----------------------------------------------------------------------
//														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;

}

