/*
Page:           rating.js
Created:        Aug 2006
Last Mod:       Mar 11 2007
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See readme.txt for full credit details.
--------------------------------------------------------- */

function sndReq(vote,id_num,ip_num,units) {
	var theUL = $('unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	theUL.update('<div class="loading"></div>');
	
    new Ajax.Request('/rating/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units,
    	{method: 'get',
    	onSuccess: function(transport) { 
    		$('rating_container').update(transport.responseText)
    	}});
}

function handleResponse(transport) {
	var response = transport.responseText;
	var update = new Array();
	if(response.indexOf('|') != -1) {
		update = response.split('|');
		changeText(update[0], update[1]);
	}
}

function changeText( div2show, text ) {
   $(div2show).update(text);
}

/* =============================================================== */
	var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(){

			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
			var parameterList = new Array();

			for (j = 0; j < parameterTokens.length; j++) {
				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				parameterList[parameterName] = parameterValue;
			}
			var theratingID = parameterList['q'];
			var theVote = parameterList['j'];
			var theuserIP = parameterList['t'];
			var theunits = parameterList['c'];
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq(theVote,theratingID,theuserIP,theunits); return false;		
			}
		}
		
	};
	
	Behaviour.register(ratingAction);
