/**
 * Javascript library for rating system
 *
 * Version 2.2
 *
 **/

//Define the ajax processing array.
var ajaxCheckArray = new Array();

//Add highlight class on mouseover.
function highlight_on_mouseover(index, maxIndex, content, endofID) {
	
	var j = index;
	var i = maxIndex;
	
	//Define the whole and empty classes.
	var wholeClass = endofID + 'whole';
	var emptyClass = endofID + 'empty';
	
	for (i; i > 0; i--) {
		
		//The id to write the new class to.
		var totalID = i + '_' + endofID + content;
		
		//Make sure i is a number.
		i = new Number(i);
		
		//If the iteration value is equal to or greater than the highest whole point.
		if (j >= i) {
			
			//Give this ID the whole class. 
			document.getElementById(totalID).className = wholeClass;
			
		}
		else {
			
			//Give this ID the empty class.
			document.getElementById(totalID).className = emptyClass;
		}

	}
	
}

//Restore the overall average stars for this content.
function highlight_default(drstring, content, divID) {
	
	if (drstring == '') {
		return;
	}
	
	var default_rating_array = drstring.split(',');
	
	var countNum = default_rating_array.length;
	
	for (i=0; i < countNum; i++) {
		
		var newID = (i+1) + '_' + divID + content;
		var newClass = divID + default_rating_array[i];
		
		document.getElementById(newID).className = newClass;
		
	}
	
	return;
	
}

//Update the rating through ajax.
function sendrating(content, number, li, divID, changeDivID, form, content_type, remRate) {
	
	if (ajaxCheckArray[content]) {
		return;
	}
	else {
		ajaxCheckArray[content] = 1;
	}
	
	if (form == 'thumbs') {
		document.getElementById('rating_' + divID + content).blur();
	}
	else {
		document.getElementById(divID + content).blur();
	}
	
	var url = "/resources/ajax/_rating_ajax.one";
	
	var params = 'content_id=' + content + '&number=' + number + '&li=' + li;

	if (remRate) {
		params += '&remRate=1';
	}
	
	if (content_type != '') {
		params += '&content_type='+content_type;
	}
	
	//Add on date so the ajax isn't cached.
	params += '&ms='+ new Date().getTime();
	
	var callback = {
		argument : [form, content, divID, changeDivID],
		success  : function (responseData) {
			var text = responseData.responseText;
			var form = responseData.argument[0];
			var content = responseData.argument[1];
			var divID = responseData.argument[2];
			var changeDivID = responseData.argument[3];
			
			if (form == 'thumbs') {
				update_rating_response_thumbs(text, divID);
			}
			else if (form == 'props') {
				update_rating_response_props(text, content, changeDivID, divID);
			}
			else {
				update_rating_response(text, content, changeDivID);
			}
			
			ajaxCheckArray[content] = false;
		}
	};
	
	var request = YAHOO.util.Connect.asyncRequest(
		'GET',
		url + '?' + params,
		callback
	);
	
 }

 //Update the page html with the new rating info.
function update_rating_response(responseText, content, divID) {
	
	if(responseText == '') {
		return false;
	}
	
	//Strip out the default_rating_string javascript and stats from the result string. [[string]]:;stats;:
	var drstring = responseText.substring(responseText.search(/\[\[/)+2, responseText.search(/\]\]/));
	var number_stats = responseText.substring(responseText.search(/:;/)+2, responseText.search(/;:/));
	
	//Update the given stars count by evaluating the drstring as javascript code.
	eval(drstring);
	
	var anotherID = 'num_of_ratings'+content;
	
	document.getElementById(anotherID).innerHTML = number_stats;

	return false;
}

 //Update the page html with the new rating info for props.
function update_rating_response_props(responseText, content, divID, buttonID) {
	
	if(responseText == '') {
		return false;
	}
	
	//Strip out the new stats from the response string. :;stats;:
	var number_stats = responseText.substring(responseText.search(/:;/)+2, responseText.search(/;:/));
	
	var contID = divID+content;
	
	//Update the number of props given to this content.
	document.getElementById(contID).innerHTML = number_stats;
	
	var butID = buttonID+content;
	
	//Make sure the button is turned on.
	document.getElementById(butID).className = 'props_rating_button';
	document.getElementById(butID).onclick = '';
	document.getElementById(butID).onmouseover = '';
	document.getElementById(butID).onmouseout = '';
	
	return false;
}

 //Update the page html with the new rating info for thumbs.
function update_rating_response_thumbs(tagdata, divID) {
		
	if(tagdata == '') {
		return false;
	}
	
	//pass the return string to a local variable
	tag_response = tagdata;
	
	//strip out the content id and the blog id from the return string - [$number]($content_id)<$uid>
	var number = tag_response.substring(tag_response.search(/\[/)+1,tag_response.search(/\]/));
	var content_id = tag_response.substring(tag_response.search(/\(/)+1,tag_response.search(/\)/));
	
	//now that we have the content id and blog id remove them from the response string 
	tag_response = tag_response.replace(/\[\w+\]\(\w+\)/,'');

	//strip out the up votes and the down votes for the rating...
	var upvotes = tag_response.substring(tag_response.search(/\</)+1,tag_response.search(/\>/));
	var downvotes = tag_response.substring(tag_response.search(/\:/)+1,tag_response.search(/\;/));		
	
	if(tagdata.responseText == '') {			
		document.getElementById('rating_' + divID + content_id).innerHTML = '';
		return false;
	}
	
	document.getElementById('rating_up' + content_id).innerHTML = upvotes;
	document.getElementById('rating_down' + content_id).innerHTML = downvotes;

	return false;
}

/*******************************************************************************
* Displays the loading screen and image while waiting for an ajax request.
*/
function showAjaxLoading(parentElmId) {

var loading_image_url = '/resources/images/admin/ajax-loader1.gif';

var parentElm = document.getElementById(parentElmId);
var coordinates = getElementCoordinates(parentElm); 
  
var loadingdiv = document.createElement("div");
loadingdiv.style.background = 'white';
loadingdiv.style.filter = 'alpha(opacity=50)';
loadingdiv.style.opacity = '.8';
loadingdiv.style.width = parentElm.offsetWidth + 'px';
loadingdiv.style.height = parentElm.offsetHeight + 'px';
loadingdiv.style.position = 'absolute';
loadingdiv.style.top = coordinates.y + 'px';
loadingdiv.style.left = coordinates.x + 'px';
loadingdiv.style.verticalAlign = 'middle';
loadingdiv.style.textAlign = 'center';
loadingdiv.innerHTML = '<img src="' + loading_image_url + '" ' + 'style="position:relative; top:20px;" />'; 
  
//add to container
parentElm.appendChild(loadingdiv);
} 


//////////////////////////////////////
//javascript for web svc ajax ratings
/////////////////////////////////////
function sendrating_tosvc(content, number, uid, updown, content_type)
{	
		
	document.getElementById('rating_' + updown + content).blur();	
	
	url = "/resources/ajax/_rating_ajax.one";
	
	upd = updown;
	
	prama = 'content_id=' + content + '&content_type=' + content_type + '&number=' + number + '&uid=' + uid + '&ms='+ new Date().getTime();
	
	var request = YAHOO.util.Connect.asyncRequest(
		'GET',
		url + '?' + prama,
		{
			success : update_rating_response_tosvc
		}
	);
	
	//vote has been clicked...disable the buttons...
	document.getElementById('rating_down'+content).disabled = true;
	document.getElementById('rating_up'+content).disabled = true;
	 
 }


function update_rating_response_tosvc(tagdata) {
		
	//pass the return string to a local variable
	tag_response = tagdata.responseText;		
	
	//strip out the content id and the blog id from the return string - [$number]($content_id)<$uid>
	var content_id = tag_response.substring(tag_response.search(/\(/)+1,tag_response.search(/\)/));	
		
	//now that we have the content id and voted message remove them from the response string
	
	
	tag_response = tag_response.replace(/\(\w+\)/,'');	
	
	if(tagdata.responseText == '') {			
			document.getElementById(rating_number + content_id).innerHTML = '0';
			return false;
	}
		
	document.getElementById('rating_number' + content_id).innerHTML = tag_response;	
	document.getElementById('rating_up'+ content_id).disabled=true;
	document.getElementById('rating_down'+ content_id).disabled=true;	
	
	return false;
}
