/*
if(window.location.href.indexOf("shashi131")>0){
	AJAX_SITE_REQUEST_URL = "http://"+window.document.domain+"/bolega/ajax_actions/questions/";
	AJAX_SITE_REQUEST_QNA_URL= "http://"+window.document.domain+"/bolega/ajax_actions/questions/";
	IMAGE_PREFIX = "http://"+window.document.domain+"/bolega/";
}else if(window.location.href.indexOf("66.148.13.116")>0){		
	AJAX_SITE_REQUEST_URL = "http://"+window.document.domain+"/ajax_actions/questions/";
	AJAX_SITE_REQUEST_QNA_URL = "http://"+window.document.domain+"/ajax_actions/questions/";
	IMAGE_PREFIX = "http://"+window.document.domain+"/";
}else{
	AJAX_SITE_REQUEST_URL = "http://"+window.document.domain+"/ajax_actions/questions/";
	AJAX_SITE_REQUEST_QNA_URL= "http://"+window.document.domain+"/ajax_actions/questions/";
	IMAGE_PREFIX = "http://"+window.document.domain+"/";
}
*/
IMAGE_PREFIX = SITE_URL ;
function hideRateChart(id){
	elementName = "#rating_chart_container_"+id;
	$(elementName).hide();
}
function showRateChart(id){
	elementName = "#rating_chart_container_"+id;
	iconName = "#ratingPreviewIcon"+id;
	
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	var popupHeight = $(elementName).height();
	var popupWidth = $(elementName).width();
	
	var icon = $(iconName);
	var iconPosition = icon.position();
	

	leftPos = iconPosition.left;
	topPos =  iconPosition.top;
	//$(elementName).text( "left: " + iconPosition.left + ", top: " + iconPosition.top );
	$(elementName).show();
	$(elementName).css({
		"position": "absolute",
		"top": topPos+10,
		"left":leftPos-150 
	});
	/*if(document.getElementById("rating_chart_container_"+id)){
		document.getElementById("rating_chart_container_"+id).style.display="block";
	}*/
	
}
function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}
MM_preloadImages('image.gif');
//-->





if (document.images){
  pic1 = new Image(150,19); 
  pic1.src = "' + IMAGE_PREFIX  + 'images/rating_loading.gif"; 

  pic2 = new Image(25,75); 
  pic2.src = "' + IMAGE_PREFIX  + 'images/rating_star.gif"; 

  pic3 = new Image(25,75); 
  pic3.src = "' + IMAGE_PREFIX  + 'images/rating_star_2.gif"; 
  
  pic4 = new Image(16,13); 
  pic4.src = "' + IMAGE_PREFIX  + 'images/rating_tick.gif";
  
  pic5 = new Image(14,14); 
  pic5.src = "' + IMAGE_PREFIX  + 'images/rating_warning.gif";
}


function do_rate(frm){
	$.ajax({	method: "get",
				url: AJAX_SITE_REQUEST_USERS_URL+"check_login.php",
				data: '',
				beforeSend: function(){}, 
				complete: function(){ }, 
				success: function(html){ //so, if data is retrieved, store it in html
					is_login=html;
					if(is_login=='yes'){
							rating = frm.which_rate.value;
							id = frm.id.value 
							cid = frm.cid.value
							qpid = frm.qpid.value;
							uid = frm.uid.value; 
							sid= document.getElementById("globalCurrentUserID").innerHTML;	// Logged in user ID
							show5 = frm.show5.value;
							showPerc = frm.showPerc.value;
							showVotes = frm.showVotes.value;
							static = frm.static.value
							//alert("Rating  : " + rating + "\r\n id : "  + id + " \r\n cid :" + cid + "\r\n qpid : " + qpid + " \r\nuid : " + uid + "\r\n sid : " + sid + "\r\n show5: " + show5 + "\r\nshowPerc: "+showPerc + "\r\nshowVotes :" + showVotes + "\r\n static : " + static);
							rate(rating,id,cid,qpid,sid,show5,showPerc,showVotes,uid)
							
							
						
					}else{
						var t = null;
						var a = AJAX_SITE_REQUEST_USERS_URL + "short_login_register.php?jsfunc=do_rate&jsfunc_frmref="+frm.name+"&popup&keepThis=true&TB_iframe=true&height=288&width=790";
						var g = false;
						tb_show(t,a,g);
					}
					return false;
				}
	});
	return false;
}


// Calculate the rating
function rate(rating,id,cid,qpid,sid,show5,showPerc,showVotes,uid, aReference){
		/*alert(rating); alert(id); alert(cid);alert(qpid);alert(sid);alert(show5);alert(showPerc);alert(showVotes);alert(uid);alert(aReference);*/
	var page_url = AJAX_SITE_REQUEST_QNA_URL  + "rating_process.php";
	//alert(page_url); return false;
	var params = "id="+id+"&rating="+rating+"&cid="+cid+"&qpid="+qpid+"&sid="+sid+"&show5="+show5+"&showPerc="+showPerc+"&showVotes="+showVotes+"&uid="+uid;
	var extra_params = "&action=question&task=rate";
	params +=extra_params;
//	var params += "cid="+cid+"&cid="+cid;
//	var params += "sid="+sid+"&sid="+sid;
	//alert(page_url + "?" + params);
	//Calling Ajax 
	if(sid!=""){
	$.ajax({	method: "post",
		   		url: page_url,
				data: params ,
				beforeSend: function(){ $("#loading_"+id).html('<img src="' + IMAGE_PREFIX  + 'images/rating_loading.gif" alt="loading" />');},	//("fast");}, //show loading just when link is clicked
				complete: function(){ /*$("#loading").hide("fast");*/}, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
						var loader = document.getElementById('loading_'+id);
						var uldiv = document.getElementById('ul_'+id);
						var res = html;
							//alert(res);return false;
							//alert("its Res");
							//alert(res);
							//alert(sid);
							//alert(res);
							result = res.split("^");
							
						if(result[0] == 'already_voted'){
							loader.style.display = 'block';
							$("#loading_"+id).html('<div class="voted_twice">'+ result[1] +'</div>');
							//$("#loading_"+id).html('<div class="voted_twice">You\'ve already voted.</div>');
						} else if(result[0]=="error_own_post"){
							$("#loading_"+id).html('<div class="voted_twice">'+ result[1] +'</div>');
						}else {
							loader.style.display = '';
							$("#loading_"+id).html('<div class="voted">'+result[1]+'</div>');
							if(show5 == true){
								var out = document.getElementById('outOfFive_'+id);
								var calculate = parseInt(result[0])/20; ///res/20;
								out.innerHTML = Math.round(calculate*100)/100; // 3.47;		//out.innerHTML = Math.round((calculate*2),0)/2; // 3.5;
							} 
							if(showPerc == true){
								var perc = document.getElementById('percentage_'+id);		//var newPerc = Math.round(Math.ceil(res/5))*5;
								var newPerc = result[0];//res;
								perc.innerHTML = newPerc+'%';
								
							} else if(showPerc == false){
								
								var newPerc = result[0];//	res;
							}
							if(showVotes == true){
								var votediv = document.getElementById('showvotes_'+id).firstChild.nodeValue;
								var splitted = votediv.split(' ');
								var newval = parseInt(splitted[0]) + 1;
								if(newval == 1){
									//alert(newval+' Vote');return false;
									document.getElementById('showvotes_'+id).innerHTML = newval+' Vote';
								} else {
								
									document.getElementById('showvotes_'+id).innerHTML = newval+' Votes';
								}
							}
							var ulRater = document.getElementById('rater_'+id);
							ulRater.className = 'star-rating2';
							var all_li = ulRater.getElementsByTagName('li');
							// start at 1 because the first li isn't a star
							for(var i=1;i<all_li.length;i++){
								all_li[i].getElementsByTagName('a')[0].onclick = 'return false;';
								all_li[i].getElementsByTagName('a')[0].setAttribute('href','#');
							}
							if(navigator.appName == 'Microsoft Internet Explorer'){
								uldiv.style.setAttribute('width',newPerc+'%'); // IE
							 } else {
								uldiv.setAttribute('style','width:'+newPerc+'%'); // Everyone else
							 }
						}
				/*					
				$("#popupSelectQuestionCategoryArea").show("slow"); //animation
				$("#popupSelectQuestionCategoryArea").html(html); //show the html inside .content div
				*/
			 }
	});
}else{
	//window.location.href="index.php?action=user&task=short_loginregister&popup&keepThis=true&TB_iframe=true&height=270&width=780";
}

} 
