/*
Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;
var rfNum = '' ;

// Rollover for image Stars //
function rating(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
			}
		}else{
			rating(preSet);
		}
	}
}

// When you actually rate something //
function rateIt(me){
	if(!rated){
		preSet = me;
		//rated=1;
		document.getElementById("rateStatus").value = me.title;
		rating(me);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function reviewThisItem(pID) {
	var url = 'ratings.php?action=writeReview';		   
	var str = 'pID='+pID;
	_ratingsAjax(url,str);								
}

function postDirectReviewAction(direct) { 
	$('#reviewLink'+direct).html('<center>Thank You</center>');
	$('#reviewLink'+direct).css('color', 'green');
	$('#ratingContent').html('');
	$('html,body').animate({
		scrollTop: $("#ratingContentMessage").offset().top
	}, 1000);
}

function reviewThisItemDirect(pID,itemRow) {
	var url = 'ratings.php?action=writeReview';		   
	var str = 'pID='+pID+'&direct='+itemRow;
	_ratingsAjax(url,str);	
	$('html,body').animate({
		scrollTop: $("#ratingContentMessage").offset().top
	}, 1000);
}

function login(string) {
	var json = jQuery.parseJSON(string);
	$.log('json: '+json);
	var url = 'ratings.php?action='+json.action;		   
	var str = 'pID='+json.pID+'&url='+json.url;
	$.log('url: '+url);
	$.log('str: '+str);
	_ratingsAjax(url,str);
}

function setRatingContentMessage(string) {
	var json = jQuery.parseJSON(string);
	$.log(json);
	$('#ratingContentMessage').html(json.msg);
	$('#ratingContentMessage').css('color', json.color);
	
}

function viewReviews(string) {
	var json = jQuery.parseJSON(string);
	$.log('json: '+json);
	var url = 'ratings.php?action=viewReviews';
	var str = 'pID='+json.pID+'&score='+json.score+'&count='+json.count;
	$.log('str: '+str);
	_ratingsAjax(url,str);
	if (json.top) scrollToHeading();
}

function scrollToHeading(){
	$('html,body').animate({
		scrollTop: $("#tabs").offset().top
	}, 1000);
}


function _ratingsAjax(url,str) {  	
			$.log('starting ajax');
			$(function() {	
					$.log('str: '+str);
					$.log('calling ajax -> '+url);			
				$.ajax({
					 type:'POST',
					 url: url, 
					 data: str,
					 dataType:'json',
					 error:	function (XMLHttpRequest,textStatus,errorThrown){ 
								$.log(errorThrown); 
								$.log(textStatus); 
								$.log(XMLHttpRequest.status); 
							  },
					 success: function(json) { 
					 	$.log('json returned'); 
						
							if (json.callback) {
								$.log('json callback -> '+json.callback); 
								$.log('json callbackParams -> '+json.callbackParams); 
								var js = json.callback+"('"+json.callbackParams+"');";
								var callBack = new Function(js);
								callBack();
							}
						
							$('.reviewLink').html(json.reviewLink); 
							
							$('#ratingScore').html(json.ratingScore);
							
							$('#ratingContent').html(json.ratingContent);
							
							if (json.clickValue) {
								$('#reviewLinkHref').unbind('click');
								var js = json.clickValue+"('"+json.clickValueParams+"'); return false;";
								var newclick = new Function(js);
								$('#reviewLinkHref').click(newclick);
							}
							
							if (json.ratingContentMessage) {
								setRatingContentMessage(json.ratingContentMessage);								
							}
							
							if (json.url) {
								window.location = json.url;
							}
						 }					 
					});
				});
   }

function submitRatingReview(required, direct) {	
			var errMsg = '';
			$.each(required, function(aKey,field){				
				if ($('input:[name='+field+']').val() == '') errMsg += field.replace("_", " ").replace("rating", "").toUpperCase() +' is required<br />';
			 });
			
			if (errMsg != '') {
   				$('#ratingContentMessage').html(errMsg);			
				$("#ratingContentMessage").dialog({
						autoOpen: true,
						width: 250,
						position: ['center',100],
						modal: true,
						hide: 'scale',
						show: 'scale',
						resizable: false,
						title: 'Please correct the following...',
						close: function() {
							$(this).html('');
						},
						buttons: {
							OK: function() {
								$(this).dialog( "close" );
							}
						}
					});			
				$('.ui-widget-header').css('color', 'red');
			}else{
				var $inputs = $('#ratingForm :input');	
				
				var str = '';
    				$inputs.each(function() {
        				str+= this.name+'='+$(this).val()+'&';
   					 });
					
					 var pros = '';
					 $("#pros input:checked").each(function() {
					   pros += $(this).val()+',';
					 });
					 str+= 'pros='+pros+'&';
					 
					 var cons = '';
					 $("#cons input:checked").each(function() {
					   cons += $(this).val()+',';
					 });
					 str+= 'cons='+cons+'&';					
					if (direct > 0) str+='direct='+direct;
					var url = 'ratings.php?action=postReview';		   
					_ratingsAjax(url,str);	
			}	
}


