var blankStar;
var selectedStar;
var activeClear;
var inactiveClear;
var starIDList = new Array();
var starLeftList = new Array();
var starRightList = new Array();
var starTopList = new Array();
var starBottomList = new Array();

var dimsLoaded = 0;

function bodyMouseMove(e)
{

	if (!dimsLoaded)
		return;
		
    var y;
    var x;

    if (e) {
        y = e.pageY;
        x = e.pageX;
    } else {
        y = event.clientY + document.body.scrollTop;
        x = event.clientX + document.body.scrollLeft;
    }

    for (i = 0; i < starIDList.length; i++) {

		id = starIDList[i];
	    	
        starsDirty = document.getElementById(id + "_starsDirty");
		if (starsDirty.value == 0)
			continue;
        
        if ((y > starBottomList[i] || y < starTopList[i]) || (x < starLeftList[i] || x > starRightList[i])) {
			initialStar = document.getElementById(id + "_initialStar").value;
			SS(id, initialStar);
			starsDirty.value = 0;
		}
    }

    return;
    
}

function CO(url)
{
    blankStar = new Image();
    blankStar.src = url + "/blankstar.gif";

    selectedStar = new Image();
    selectedStar.src = url + "/selectedstar.gif";
    
	activeClear = new Image();
    activeClear.src = url + "/clearrating.gif";
    
    inactiveClear = new Image();
    inactiveClear.src = url + "/greyclearrating.gif";

    return;
}

function getTop(obj)
{
	return obj.offsetTop +
        (obj.offsetParent ? getTop(obj.offsetParent) : 0);
}

function getLeft(obj)
{
    return obj.offsetLeft +
        (obj.offsetParent ? getLeft(obj.offsetParent) : 0);
}

function ARI(id)
{
	starIDList[starIDList.length] = id;
	return;
}

function loadDims()
{
	for (i = 0; i < starIDList.length; i++) {
		id = starIDList[i];
		mydiv = document.getElementById(id + "_stardiv");
		
		t = getTop(mydiv);
		starTopList[starTopList.length] = t;
		starBottomList[starBottomList.length] = t + mydiv.offsetHeight;

		l = getLeft(mydiv);
		starLeftList[starLeftList.length] = l;
		starRightList[starRightList.length] = l + mydiv.offsetWidth;
	}

	dimsLoaded = 1;
	return;
}

function SS(id, no)
{
    
    for (i = 0; i < 5; i++) {
        img = document.getElementById(id + "_star" + i);
        
        if (i > no)
            img.src = blankStar.src;
        else 
            img.src = selectedStar.src;
        
        img.alt = "Click To Rate";
		img.title = "Click To Rate";
		img.style.cursor = "pointer";
    }
    
    clear = document.getElementById(id + "_clear");
    if(clear)
    {
		clear.alt = "Clear Rating";
		clear.title = "Clear Rating";
	}

    tickBox = document.getElementById(id + "_tick");
    if (tickBox) {
		initialTickBoxState = document.getElementById(id + "_initialTickBoxState");

		if (no >= 0) 
			tickBox.checked = 1;
		else 
			tickBox.checked = (initialTickBoxState.value == 0 ? 0 : 1);
		
	}

	dirty = document.getElementById(id + "_starsDirty");
    dirty.value = 1;
    
	return;
}


function RI(id, productId, rating)
{
	level = (5 - rating);
	backgroundRequest("/SearchAndBrowse/AddRating.aspx?productID=" + productId + "&level=" + level + "&showUI=false");
	initialTickBoxState = document.getElementById(id + "_initialTickBoxState");
	initialStar = document.getElementById(id + "_initialStar");
	starsDirty = document.getElementById(id + "_starsDirty");
	
	initialTickBoxState.value = 1;
	initialStar.value = rating;
	SS(id, rating);
	
	img = document.getElementById(id + "_clear");    
	if(img)
		img.src = activeClear.src;
	
    starsDirty.value = 0;
    
	return;
}

function RR(id, productId)
{
	initialStar = document.getElementById(id + "_initialStar");
	if (initialStar.value <= -1)
		return;
	
	initialTickBoxState = document.getElementById(id + "_initialTickBoxState");
	initialStar = document.getElementById(id + "_initialStar");
	
	initialStar.value = -1;
	SS(id, -1);
	
	img = document.getElementById(id + "_clear");    
	img.src = inactiveClear.src;
		
	starsDirty = document.getElementById(id + "_starsDirty");
	starsDirty.value = 0;
	
	backgroundRequest("/SearchAndBrowse/RemoveRating.aspx?productID=" + productId + "&showUI=false");

	return;
}


function SI(id, productId, addUrl, removeUrl)
{
	tickBox = document.getElementById(id + "_tick");
    if (!tickBox) 
		return;
		
	initialTickBoxState = document.getElementById(id + "_initialTickBoxState");
	initialStar = document.getElementById(id + "_initialStar");
	
	if (tickBox.checked == 0) {
		backgroundRequest("/SearchAndBrowse/removefromseenit.aspx?productID=" + productId + "&showUI=false");
		
		initialTickBoxState.value = 0;
		initialStar.value = -1;
		SS(id, -1);
		
		img = document.getElementById(id + "_clear");    
		img.src = inactiveClear.src;
		
		starsDirty = document.getElementById(id + "_starsDirty");
		starsDirty.value = 0;
		
		tickBox.checked = 0;
	} else {
		backgroundRequest("/SearchAndBrowse/addtoseenit.aspx?productID=" + productId + "&showUI=false");
		initialTickBoxState.value = 1;
	}

	return;
}


document.body.onmousemove = bodyMouseMove;

////////////////////
// background requesting system
// Stop multiple adds from happening
var processing = false;
var bgImg;

// Array to buffer adds that are attempted during a current add request
var requestQueue = new Array();

// Set up the 'Before Unload' handler; note, not all browsers support this
try
{   
    window.onbeforeunload = beforeUnloadHandler;
}
catch(e)
{
}

function beforeUnloadHandler()
{
    if (requestQueue.length > 0 || processing)
        return 'Your ratings are still being updated; please wait a few moments and try again';
}

function backgroundRequest(url)
{
    // Add this value to the queue
    requestQueue.push(url);    
    
    // Process the array and send the values to the server
    processQueue();
    
    return;

}

/*
    Process the array and send the current values
*/
function processQueue()
{
    if (processing)
        return;

    if (requestQueue.length == 0)
        return;
    
    processing = true;
        
    // Get the item from the queue    
    url = requestQueue.shift();
    url += '&';
     
    // Add some uniqueness to the url to avoid browser caching issues
	var currentDate = new Date();
	var uniqueValue = Date.UTC(currentDate.getYear(), currentDate.getMonth(), currentDate.getDay(), currentDate.getHours(), currentDate.getMinutes(), currentDate.getSeconds(), currentDate.getMilliseconds());
	url += 'uv=' + uniqueValue;


	bgImg = document.getElementById("myimg");
	bgImg.onload = function () {callback(url)};
	bgImg.src = url;
	
    return;
}


/*
	Callback when the remote call image finishes loading.
*/
function callback(url)
{
	bgImg = document.getElementById("myimg");
	if (bgImg.width != 1) 
		displayError();
	
	processing = false;
    processQueue();
	    
    return;
}

function displayError()
{
	alert("There was a problem rating, this could be due to your session timing out. Please log in and try again.");
}


