/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 3.8.0 								   				  # ||
|| # ---------------------------------------------------------------- # ||
|| # 				Post Rank Product -	by FullyTested		 	      # ||
|| #################################################################### ||
\*======================================================================*/

/**
* Flip element visibility
*
*/
function flipvis(posts)
{
	var spans = fetch_tags(fetch_object(posts), 'span');
	for (var i = 0; i < spans.length; i++)
	{
		if (spans[i].id)
		{
			if (spans[i].id.substr(0, 11) == 'span_score_')
			{
				spans[i].style.display = spans[i].style.display == 'none' ? 'inline' : 'none';
			}
			if (spans[i].id.substr(0, 17) == 'span_scoredetail_')
			{
				spans[i].style.display = spans[i].style.display == 'none' ? 'inline' : 'none';
			}
		}
	}
}


/**
* Adds onclick events to appropriate elements for post ranking
*
* @param	string	The ID of the post list element
*/
function vB_AJAX_Posts_Init(posts)
{
	if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
	{
		var spans = fetch_tags(fetch_object(posts), 'span');
		for (var i = 0; i < spans.length; i++)
		{
			if (spans[i].hasChildNodes() && spans[i].id && spans[i].id.substr(0, 5) == 'span_')
			{
				var anchors = fetch_tags(spans[i], 'a');
				for (var j = 0; j < anchors.length; j++)
				{
					if (anchors[j].name && anchors[j].name.indexOf('postrank::AJAX') != -1)
					{
						var details = spans[i].id.split('_');

						switch (details[1])
						{
							case 'rankup':
							case 'rankdown':
							{
								spans[i].style.cursor = pointer_cursor;
								spans[i].onclick = vB_AJAX_Posts_Events.prototype.rank_click;
								anchors[j].removeAttribute('href'); // remove the non-ajax link to postrank.php
								break;
							}
							case 'viewstats':
							{
								anchors[j].removeAttribute('href'); // remove the non-ajax link to the profile.php stats
								break;
							}							
						}

						break;
					}
				}
			}
		}
	}
}


/**
* Class to handle events in the post list
*/
function vB_AJAX_Posts_Events()
{
}


/**
* Class to handle post ranking with XML-HTTP
*
* @param	object	The clickable post rank image for the postbit
*/
function vB_AJAX_Rank(obj)
{
	this.obj = obj;
	this.postid = this.obj.id.substr(this.obj.id.lastIndexOf('_') + 1);
	this.updown = this.obj.id.substr(this.obj.id.indexOf('_') + 1, 6);
	this.imgobj1 = fetch_object('postrankup_icon_'+this.postid);
	this.imgobj2 = fetch_object('postrankdown_icon_'+this.postid);
	
	this.span_rank = fetch_object('span_rank_'+this.postid);
	this.span_score = fetch_object('span_score_'+this.postid);
	this.span_scoredetail = fetch_object('span_scoredetail_'+this.postid);
	
	if (this.updown === 'rankup')
	{
		this.rankbutton = fetch_object('span_rankdown_'+this.postid);
		this.updown = 'up';
	}
	else
	{
		this.rankbutton = fetch_object('span_rankup_'+this.postid);
		this.updown = 'down';
	}
            

	/**
	* Function to post rank
	*/
	this.toggle = function()
	{
		YAHOO.util.Connect.asyncRequest("POST","ajax.php?do=postrank&updown="+this.updown+"&p="+this.postid,{success:this.handle_ajax_response,failure:vBulletin_AJAX_Error_Handler,timeout:vB_Default_Timeout,scope:this},SESSIONURL+"securitytoken="+SECURITYTOKEN+"&do=postrank&updown="+this.updown+"&p="+this.postid);
	}


	/**
	* Post rank function response (ajax.php)
	*/
	this.handle_ajax_response = function(B)    {
		// get response
		if (B.responseXML)
		{
			var vote_phrase = B.responseXML.getElementsByTagName("voted_phrase")[0].firstChild.nodeValue;
			var rank_name = B.responseXML.getElementsByTagName("rank_name")[0].firstChild.nodeValue;
			var rank_color = B.responseXML.getElementsByTagName("rank_color")[0].firstChild.nodeValue;
			
			if (this.span_rank && rank_name && rank_color)
			{
				this.span_rank.style.color = rank_color;
				this.span_rank.innerHTML = rank_name+"&nbsp;";
			}
			
			if (this.span_scoredetail)
			{
		 	this.span_scoredetail.innerHTML = vote_phrase+"&nbsp;";
		 	this.span_scoredetail.onclick = this.span_scoredetail.onmouseover = this.span_scoredetail.style.cursor = '';
		 	//this.span_scoredetail.style.display = 'none';
			}
			if (this.span_score)
			{
			  	this.span_score.innerHTML = vote_phrase+"&nbsp;";
			 	this.span_score.onclick = this.span_score.onmouseover = this.span_score.style.cursor = '';
			 	//this.span_score.style.display = 'block';
			}
				
			this.imgobj1.title = this.imgobj2.title = vote_phrase;
		}
  		
  		// change image to the voted one
		this.imgobj1.src = IMGDIR_MISC+"/postrank_up_voted.gif";
		this.imgobj1.name = this.imgobj1.name+'_voted'; // prevent the mouseover image flip
		this.imgobj2.src = IMGDIR_MISC+"/postrank_down_voted.gif";
		this.imgobj2.name = this.imgobj2.name+'_voted'; // prevent the mouseover image flip
      	            
		this.rankbutton.onclick = '';
		this.rankbutton.style.cursor = '';
		this.obj.onclick = '';
		this.obj.style.cursor = '';
		          
		rank.obj = null;       			
    }
  
	// send the data
	this.toggle();
}


/**
* Handles clicking on post rank images
*/
vB_AJAX_Posts_Events.prototype.rank_click = function(e)
{
	rank = new vB_AJAX_Rank(this);
}

/*======================================================================*\
|| ####################################################################
|| # Post Rank Product
|| ####################################################################
\*======================================================================*/