/* star ratings */
function ratedSuccess(responseText, localArgs)
{
   /*        var elem = document.getElementById(localArgs+"_status");
   
    
   
    elem.innerHTML = 'Thanks!';
   
    
   
    elem.style.display = 'block'; */
   
}

var star_main_src = "http://static.pyzam.com/images/greystar.gif";
var star_mo_src = "http://static.pyzam.com/images/star1mo.gif";
var star_half_src = "http://static.pyzam.com/images/halfstar.gif";
var stat_button_src = "http://static.pyzam.com/images/ibutton2.gif";
var mouse_over_star = false;
var timer_started = false;
var g_a = new Array('x', 'y', 'z'); // dummy values shouldn't affect anything
var affil_done = false;
document.grabbed_items = g_a;

Array.find = function(ary, element)
{
   for (var i = 0; i < ary.length; i++) {
   
      if (ary[i] == element) {
      
         return i;
      }
   }
   
   return -1;
}

function ajaxUpdateRating(table, rating, content_id, the_container_id)
{
	var args = "id=" + content_id + "&" + "p1=" + rating;
	// disable the spring-back thingy.s
	var elem = document.getElementById(the_container_id);
	elem.onmouseout = function()
	{
		return false;
	};
	//      ajaxCall( 'layouts', 'rate', args, debugSuccess, debugFailFunc, args );
	/* disable any further mouseovers and clicks */
	for (var i = 1; i <= 5; i++) {
		var the_id = the_container_id + '_star_' + i;
		document.getElementById(the_id).onmouseover = function()
		{
			return false;
		};

		document.getElementById(the_id).onmouseout = function()
		{
			return false;
		};

		document.getElementById(the_id).onclick = function()
		{
			return false;
		};

		if (i <= rating) 
			document.getElementById(the_id).src = star_mo_src;
		else 
			document.getElementById(the_id).src = star_main_src;
	}
	//alert("I will set a rating of "+rating+" on layout id " + content_id + ' by using ajax.');

	var sr_callback = null;
	if (typeof(on_star_rated) == 'function') {
		sr_callback = on_star_rated;
	}

	if (typeof(before_star_rated) == 'function') {
		if (before_star_rated({'table' : table, 
			'rating' : rating, 
			'content_id' : content_id, 
			'the_container_id' : the_container_id}
		)) {
			ajaxCall(table, 'rate', args, sr_callback, null, the_container_id);
		}
	} else {
		ajaxCall(table, 'rate', args, sr_callback, null, the_container_id);
	}

}

function springBack(the_container)
{
   var the_container_elem = document.getElementById(the_container);
   
   //alert(mouse_over_star?'true':'false');
   if (!mouse_over_star) {
      setStar(the_container, the_container_elem.actual_rating);
      //console.info('set it back');
   }
   timer_started = false;
}

function setStar(the_container_id, rating)
{
   for (var i = 1; i <= 5; i++) {
      var the_id = the_container_id + '_star_' + i;
      
      if (!document.getElementById(the_id))          
         return false;
      
      if (rating >= i) {
         document.getElementById(the_id).src = star_mo_src;
      }
      else {
      
         if (rating >= (i - 0.750)) {
            document.getElementById(the_id).src = star_half_src;
         }
         else {
            document.getElementById(the_id).src = star_main_src;
         }
      }
   }
}

function setupStars(table, the_container_id, the_item_id, stat_fn, actual_rating)
{
   var use_stat_button = true;
   
   // this is so individual pages can override the default behavior.
   if (typeof(custom_star_main_src) == 'string')
		star_main_src = custom_star_main_src;
   if (typeof(custom_star_mo_src) == 'string')
		star_mo_src = custom_star_mo_src;
   if (typeof(custom_star_half_src) == 'string')
		star_half_src = custom_star_half_src;

   if (document.use_stat_button && document.use_stat_button == 'no') use_stat_button = false;
   var cont = document.getElementById(the_container_id);
   
   if (!cont)       
      return false;
   cont.style.display = "block";
   cont.actual_rating = actual_rating;
   for (var i = 0; i < 5; i++) {
      //                var img = document.createElement('img');
      var rating = i + 1;
      var img = document.getElementById(the_container_id + "_star_" + rating);
      //                img.id = the_container_id+"_star_"+rating;
      //              img.src = star_main_src;
      //                img.width = 20;
      //              img.height = 20;
      img.rating = rating;
      img.onmouseover = function(e)
      {
         mouse_over_star = true;
         
         if (!e) var e = window.event;
         e.cancelBubble = true;
         
         if (e.stopPropagation) e.stopPropagation();
         setStar(the_container_id, this.rating);
      };
      img.onmouseout = function(e)
      {
         mouse_over_star = false;
         
         if (!timer_started) setTimeout("springBack('" + the_container_id + "')", 300);
         timer_started = true;
         
         if (!e) var e = window.event;
         e.cancelBubble = true;
         
         if (e.stopPropagation) e.stopPropagation();
         setStar(the_container_id, this.rating);
      };
      img.onclick = function(e)
      {
         ajaxUpdateRating(table, this.rating, the_item_id, the_container_id);
      };
      //              cont.appendChild( img );
   }
   s_elem_id = "statbtn_for_" + the_item_id;
   
   if (use_stat_button && document.getElementById(s_elem_id)) {
   
      //       var statbtn = document.createElement('img');
      //     statbtn.src = stat_button_src;
      if (document) statbtn = document.getElementById(s_elem_id);
      
      if (statbtn) {
         statbtn.onclick = stat_fn;
      }
      //   statbtn.style.paddingLeft = '5px';
      //  statbtn.style.paddingBottom = '2px';
      // cont.appendChild(statbtn);
   }
   /*        var status = document.createElement( 'span' );
    status.style.fontSize = "11px";
    status.style.display = 'none';
    status.id = the_container_id + '_status';
    status.appendChild( document.createTextNode('Working...'));
    cont.appendChild( status ); */
}
