/*
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.
--------------------------------------------------------- */

var visitorRated = false;

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	
	var languages = new Array ( 'en', 'ur', 'ru', 'fa' );
	var siteRoot = '';
	var site = document.URL;
	site = site.split('/');
	for ( i = (site.length - 1); i > 0; i-- ) {
		var cont = true;
		for ( x = 0; x < languages.length; x++ ) {
			if ( languages[x] == site[i] ) {
				cont = false;
				var currlang = site[i];
			}
		}
		
		if ( cont == false ) {
			i = 0;
		}
		else {
			siteRoot = siteRoot + '../';
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(vote,id_num,lang,units,info) {

	if(!visitorRated)
	{
		var params ='site=caii&id='+id_num+'&lang='+lang+'&rate='+vote;
		params += info ? '&info=true' : '';
	
		new Ajax.Request( '/php/rating/starrate.php' , {evalJSON:true, method:'post', postBody:params , asynchronous:true, onSuccess:function(req){
			var response = req.responseJSON;		
			var p = parseInt(response.ratings) > 1  ? 's' : '';		
			$("star-rating").innerHTML = '<strong> '+parseFloat(response.average).toFixed(1) + '</strong> /5 ('+response.ratings+' vote'+p+')';		
			$$('li.current-rating')[0].style.width = ((parseFloat(response.average) * 150) / 5) +'px';
		}});	
		
		if(!info) visitorRated = true;
	}

}

function handleResponse(xmlhttp) {

  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseJSON;
alert(response.toSource());
//        var update = new Array();

//        if(response.indexOf('|') != -1) {
//            update = response.split('|');
//            changeText(update[0], update[1]);
//        }
		//response += "<div class=\"checkmarkShow\">&nbsp;</div>";
		document.getElementById("unit_ul").innerHTML = 'Article Rating: <strong> '+response.average + '</strong> /5 ('+response.ratings+' vote)';
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById("rateStars");
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

/* =============================================================== */
var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(e){

			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 theLang = parameterList['t'];
			var theunits = parameterList['c'];
			
			
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq(theVote,theratingID,theLang,theunits); 
			try{
				Event.stop(e);
			}catch(e){}
			return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);
