// By Burhan Khan
// www.burhankhan.com
// 23 Aug 2008
// Idea: http://www.nofunc.com/AJAX_Star_Rating/

var rate_obj, cur_rate_obj, user_obj, loading_obj, id_obj;
var select_rate_obj, select_cur_rate_obj, select_user_obj;
var halfImageSize	=	8.5;
var UserMsg	=	"", SelectUserMsg	=	"";
var	CurRating	=	0, SelectCurRating	=	0;;
var Review_form	=	document.formreview;
var pics_obj_arr	=	new Array();
var div_large_pic, img_large_pic;

function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,'Y':event.clientY+document.body.scrollTop+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }
function moveIt(o,t,l) { o.style.position = "absolute"; o.style.top = t+'px'; o.style.left = l+'px'; }


PicEnlarge = { 
	OnOverHandler: function (e) { 
		
		if(div_large_pic.style.display == 'none'){
			div_large_pic.style.display = 'inline';	
			var x	=	XY(e);
			moveIt(div_large_pic, x.Y+10, x.X-200);
			img_large_pic.src	=	this.src;
		}else if(div_large_pic.style.display == 'inline'){
			var x	=	XY(e);
			moveIt(div_large_pic, x.Y+10, x.X-200);
		}
	}, 
	
	OnOutHandler: function (e) { 
		div_large_pic.style.display = 'none';
	}, 
	
	Init: function () { 
		div_large_pic = document.getElementById("div_large_pic");
		img_large_pic = document.getElementById("img_large_pic");
		
		if(!div_large_pic || !img_large_pic)	return;
		div_large_pic.style.display = 'none';

		var c	=	0;
		var pic_obj;
		while(pic_obj = document.getElementById("rpic"+c)){
			pics_obj_arr[c]	=	pic_obj;
			pics_obj_arr[c].onmousemove = PicEnlarge.OnOverHandler; 
			pics_obj_arr[c].onmouseout = PicEnlarge.OnOutHandler; 
			c++;
		}
		if(pics_obj_arr.length < 1) return;
	} 
}; 



Rater = { 
	OnOverHandler: function (e) { 
		document.onmousemove=function(e) {
			var p	=	abPos(rate_obj), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
			
			if(oX<1 || oX>84 || oY<0 || oY>19){
					Rater.Revert();
			}else {
				cur_rate_obj.style.width=oX+'px';
				user_obj.style.color='#000000';
				user_obj.innerHTML=Math.round(oX/halfImageSize);
			}
		}
	}, 
	
	
	OnDownHandler: function (e) { 
	
		var p	=	abPos(rate_obj), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
		if(oX<1 || oX>84 || oY<0 || oY>19) return;
			
		var n = Math.round(parseInt(cur_rate_obj.style.width.replace("px", ""))/halfImageSize);
		
		rate_obj.style.display = 'none';
		loading_obj.style.display = 'inline';
		
		var url = "/ajax.php?mode=rate&rid=" + parseInt(id_obj.innerHTML) + "&rate=" + n + "";
		
		var onSuccessFunc = function (req) { 
			loading_obj.style.display = 'none';
			user_obj.innerHTML = req.responseText;
			rate_obj.style.display = 'inline';
		}
		var onErrorFunc = function (status) { 
			loading_obj.style.display = 'none';
			user_obj.innerHTML = "<b><font color=red>Couldn't Rate At This Time</font></b>";
			rate_obj.style.display = 'inline';
		}
		var myAjax = new _bsn.Ajax;
		myAjax.makeRequest( url, "GET", onSuccessFunc, onErrorFunc );

		rate_obj.onmousemove = ''; 
		rate_obj.onmousedown = ''; 
		document.onmousemove='';
	}, 


	Revert: function () { 
		cur_rate_obj.style.width=(CurRating)+'%';
		user_obj.innerHTML=UserMsg;
		user_obj.style.color='#999999';
		document.onmousemove='';
	}, 
	
	Init: function () { 
		rate_obj = document.getElementById("star"); 
		cur_rate_obj = document.getElementById("starcur"); 
		user_obj = document.getElementById("staruser"); 
		loading_obj = document.getElementById("loading"); 
		id_obj = document.getElementById("rid"); 
		
		if(!rate_obj || !cur_rate_obj || !user_obj || !loading_obj || !id_obj) return; 

		CurRating = parseInt(cur_rate_obj.style.width.replace("px", ""));
		UserMsg = user_obj.innerHTML;
		
		rate_obj.onmousemove = Rater.OnOverHandler; 
		rate_obj.onmousedown = Rater.OnDownHandler; 
	} 
	
}; 


SelectRater = { 
	OnOverHandler: function (e) { 
		document.onmousemove=function(e) {
			var p	=	abPos(select_rate_obj), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
			
			if(oX<1 || oX>84 || oY<0 || oY>19){
					SelectRater.Revert();
			}else {
				select_cur_rate_obj.style.width=oX+'px';
				select_user_obj.style.color='#000000';
				select_user_obj.innerHTML=Math.round(oX/halfImageSize);
			}
		}
	}, 
	
	
	OnDownHandler: function (e) { 
	
		var p	=	abPos(select_rate_obj), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
		if(oX<1 || oX>84 || oY<0 || oY>19) return;
			
		var n = Math.round(parseInt(select_cur_rate_obj.style.width.replace("px", ""))/halfImageSize);
		
		Review_form.rating.value	=	n;
		select_user_obj.style.color='#FF0000';
		select_user_obj.style.fontSize='15px';
		select_user_obj.style.fontWeight='bold';

		select_rate_obj.onmousemove = ''; 
		select_rate_obj.onmousedown = ''; 
		document.onmousemove='';
	}, 


	Revert: function () { 
		select_cur_rate_obj.style.width=(SelectCurRating)+'px';
		select_user_obj.innerHTML=SelectUserMsg;
		select_user_obj.style.color='#999999';
		document.onmousemove='';
	}, 
	
	Init: function () { 
		select_rate_obj = document.getElementById("selectstar"); 
		select_cur_rate_obj = document.getElementById("selectstarcur"); 
		select_user_obj = document.getElementById("selectstaruser"); 
		
		if(!select_rate_obj || !select_cur_rate_obj || !select_user_obj) return; 

		SelectCurRating = parseInt(select_cur_rate_obj.style.width.replace("px", ""));
		SelectUserMsg = select_user_obj.innerHTML;
		
		select_rate_obj.onmousemove = SelectRater.OnOverHandler; 
		select_rate_obj.onmousedown = SelectRater.OnDownHandler; 
	} 
	
}; 

window.onload=function(){
	Rater.Init();
	PicEnlarge.Init();
	SelectRater.Init();
};


function postReview(){

	$('loading_review').style.display = 'inline';
	Review_form.review_input.disabled = true;
	
	var url = "/ajax.php";
	var postData = "mode=review&rid=" + Review_form.rid.value + "&rating=" +  Review_form.rating.value  + "&review=" +  escape(Review_form.review.value);

	if(Review_form.username){
		postData	=	postData + "&user=" + 	escape(Review_form.username.value);
	}

	var onSuccessFunc = function (req) { 
		$('loading_review').style.display = 'none';
		$('review_msg').innerHTML = req.responseText;
		Review_form.review_input.disabled = false;
	}
	var onErrorFunc = function (status) { 
		$('loading_review').style.display = 'none';
		$('review_msg').innerHTML = "<b><font color=red>Couldn't Review At This Time</font></b>";
		Review_form.review_input.disabled = false;
	}
	var myAjax = new _bsn.Ajax;
	myAjax.makeRequest( url, "POST", onSuccessFunc, onErrorFunc, postData);
		
	return false;
}