/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 1.0.0
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2007 Jelsoft Enterprises Ltd. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

/**
* Class to handle thread rating
*
* @param	object	The form object containing the vote options
*/
function vB_AJAX_BlogLatest(varname)
{
	// AJAX handler
	this.xml_sender = null;

	this.varname = varname;
	this.active = null;
	this.noresults = 0;
	
	this.containers = new Array('entries');

	// Closure
	var me = this;

	this.init = function()
	{
		if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
		{
			for (var i = 0; i < this.containers.length; i++)
			{
				var images = fetch_object(this.containers[i]).getElementsByTagName('img');
				if (images.length)
				{
					for (var j = 0; j < images.length; j++)
					{
						img_alt_2_title(images[j]);
					}
				}
			}
			
			var blogrecent_link = fetch_object('vb_bloglatest_recent_link');
			var blogbest_link = fetch_object('vb_bloglatest_best_link');
			var blogviewed_link = fetch_object('vb_bloglatest_viewed_link');
			
			
			
			//var blogrecent_more = fetch_object('vb_bloglatest_recent_findmore');
			//var blogbest_more = fetch_object('vb_bloglatest_best_findmore');
			

			if (this.active == null)
			{
				// default value
				this.active = 'recent';
				
				if (blogbest_link && blogbest_link.style.display == 'none')
				{
					this.active = 'recent';
				}
				if (blogrecent_link && blogrecent_link.style.display == 'none')
				{
					this.active = 'recent';
				}
			}

			/*if (blogblograting_link)
			{
				blogblograting_link.varname = this.varname;
				blogblograting_link.type = 'blog';
				blogblograting_link.which = 'blograting';
				blogblograting_link.onclick = load_data;
				blogblograting_link.style.cursor = pointer_cursor;

				blogblograting_link.style.display = (this.active == 'blograting') ? 'none' : '';
				fetch_object('vb_bloglatest_blograting_findmore').style.display = (this.active == 'blograting' && this.noresults == 0) ? '' : 'none';
			}

			fetch_object('vb_bloglatest_blograting').style.display = (this.active != 'blograting') ? 'none' : '';*/

			if (blogbest_link)
			{
				blogbest_link.varname = this.varname;
				blogbest_link.type = 'entries';
				blogbest_link.which = 'best';
				blogbest_link.onclick = load_data;
				blogbest_link.style.cursor = pointer_cursor;
				if(document.getElementById('duration').value != '')
					blogbest_link.duration = document.getElementById('duration').value;
				else
					blogbest_link.duration = 'week';
				
				blogbest_link.style.display = (this.active == 'best') ? 'none' : '';
				//fetch_object('vb_bloglatest_best_findmore').style.display = (this.active == 'best' && this.noresults == 0) ? '' : 'none';
				//fetch_object('vb_bloglatest_best_duration').style.display = (this.active == 'best' ) ? '' : 'none';
			}

			fetch_object('vb_bloglatest_best').style.display = (this.active != 'best') ? 'none' : '';

			if (blogrecent_link)
			{
				blogrecent_link.varname = this.varname;
				blogrecent_link.type = 'entries';
				blogrecent_link.which = 'recent';
				blogrecent_link.onclick = load_data;
				blogrecent_link.style.cursor = pointer_cursor;
				blogrecent_link.duration = 'all';
				blogrecent_link.style.display = (this.active == 'recent') ? 'none' : '';
				//fetch_object('vb_bloglatest_recent_findmore').style.display = (this.active == 'recent') ? '' : 'none';
				//fetch_object('vb_bloglatest_recent_duration').style.display = (this.active == 'recent' ) ? '' : 'none';
			}

			fetch_object('vb_bloglatest_recent').style.display = (this.active != 'recent') ? 'none' : '';
			
			// kcorp ////////////////////////////////////////////////////
			if (blogviewed_link)
			{
				blogviewed_link.varname = this.varname;
				blogviewed_link.type = 'entries';
				blogviewed_link.which = 'viewed';
				blogviewed_link.onclick = load_data;
				blogviewed_link.style.cursor = pointer_cursor;
				blogviewed_link.duration = 'all';
				if(document.getElementById('duration').value != '')
					blogviewed_link.duration = document.getElementById('duration').value;
				else
					blogviewed_link.duration = 'week';
				blogviewed_link.style.display = (this.active == 'viewed') ? 'none' : '';
				//fetch_object('vb_bloglatest_viewed_findmore').style.display = (this.active == 'viewed' && this.noresults == 0) ? '' : 'none';
				//fetch_object('vb_bloglatest_viewed_duration').style.display = (this.active == 'viewed' ) ? '' : 'none';
			}
			fetch_object('vb_bloglatest_viewed').style.display = (this.active != 'viewed') ? 'none' : '';
			

			/// kcorp /////////////////////////////////////////////////////
		}
	}

	/**
	* OnReadyStateChange callback. Uses a closure to keep state.
	* Remember to use me instead of this inside this function!
	*/
	this.handle_ajax_response = function()
	{
		if (me.xml_sender.handler.readyState == 4 && me.xml_sender.handler.status == 200)
		{
			fetch_object('progress_latest').style.display = 'none';
			if (me.xml_sender.handler.responseXML)
			{
				var obj = fetch_object(me.objid);
				// check for error first
				var error = me.xml_sender.fetch_data(fetch_tags(me.xml_sender.handler.responseXML, 'error')[0]);
				if (error)
				{
					alert(error);
				}
				else
				{
					var updateinfo =  fetch_tags(me.xml_sender.handler.responseXML, 'updated')[0];
					var type = updateinfo.getAttribute('type');
					var which = updateinfo.getAttribute('which');
					var data = updateinfo.getAttribute('data');
					me.noresults = updateinfo.getAttribute('noresults');

					me.active = which;
					if (data != '')
					{
						if (type == 'entries')
						{
							fetch_object('entries').innerHTML = data;
						}
						else
						{
							fetch_object('blogs').innerHTML = data;
						}
						me.init();

					}
				}
			}

			if (is_ie)
			{
				me.xml_sender.handler.abort();
			}
		}
	}

	this.init();
};


function load_data(e)
{
	
	var varpage='';
	if(this.page)
		varpage = '&page='+this.page;
	if(this.categoryid)
		varpage += '&categoryid='+this.categoryid;
	var latestObj = eval(this.varname);
	//if(document.getElementById('duration').value != '')
		//this.which = document.getElementById('duration').value;
	//alert('blog_ajax.php?do=loadupdated&type=' + PHP.urlencode(this.type) + '&which=' + PHP.urlencode(this.which)+'&duration=' + PHP.urlencode(this.duration)+varpage);
	fetch_object('progress_latest').style.display = '';
	latestObj.xml_sender = new vB_AJAX_Handler(true);
	latestObj.xml_sender.onreadystatechange(latestObj.handle_ajax_response);
	latestObj.xml_sender.send(
		'blog_ajax.php?do=loadupdated&type=' + PHP.urlencode(this.type) + '&which=' + PHP.urlencode(this.which)+'&duration=' + PHP.urlencode(this.duration)+varpage, 'do=loadupdated&type=' + PHP.urlencode(this.type) + '&which=' + PHP.urlencode(this.which) +'&duration=' + PHP.urlencode(this.duration) + varpage + '&ajax=1'
	);
	document.getElementById('btn_findmore').href= 'blog.php?do=entrieslist&blogtype=' + PHP.urlencode(this.which) + '&duration=' + PHP.urlencode(this.duration)+varpage;
	return false;
};

function callajax(itemid, varname, type, which, duration, page, categoryid){
	
	if(varname=='')
		varname = 'blogLatest';
	if(type=='')
		type = 'entries';
	if(which=='')
		which = 'viewed';
	if(duration=='')
		duration = 'all';
	
	var id = fetch_object(itemid);
	id.varname = varname;
	id.type = type;
	id.which = which;
	id.onclick = load_data;
	id.duration = duration;
	id.page = page;
	id.categoryid = categoryid;
	//document.getElementById('durationPage').value = duration;
}
function changecategory(obj,itemid,varname, type, which, duration ){
	
	var categoryid;
	categoryid = obj.options[obj.selectedIndex].value
	callajax(itemid, varname, type, which, duration, '', categoryid)
}
