//
// NZB Slider Scripts
//

var thread = new Array();
var nzbpicture = new Array();
var nzbextras = new Array();
var nzbtimer;

function nzb_extras_init(elementid)
{
	if (img = fetch_object(elementid + '_img'))
	{
		img.onmouseover = nzbhovertimer;
		img.onmouseout = nzbcleartimer;
		titletotooltip(img.id);
	}
}

function nzbhovertimer()
{
	elementid = this.id.split('_img');
	
	if (slider_menus.activeslider['nzbextras'] != elementid[0])
	{
		nzbtimer = window.setTimeout( "slider_menus.sliders['"+elementid[0]+"'].Expand()", 1000 );
	}
}

function nzbcleartimer()
{
	if (nzbtimer)
	{
		window.clearTimeout(nzbtimer);
	}
}





function nzb_extras_callback(slider)
{
	this.ImageLoaded = function()
	{

		fetch_object('nzb_extra_' + thread['nzbid'] + '_picture')).title = fetch_object('thread_title_' + thread['nzbid']).innerHTML + ' Poster';
		fetch_object('nzb_extra_' + thread['nzbid'] + '_picture')).alt = fetch_object('thread_title_' + thread['nzbid'] + ').title;


	}
	this.ImageFailed = function()
	{
		fetch_object('nzb_extra_' + thread['nzbid'] + '_picture').src = NZB4FREE_IMAGE_NOLOADIMAGE;
	}		
	
	if (!slider_menus.sliders[slider.elementid + '_1'])
	{
		Slider_Register(slider.elementid + '_1', false, 0.3, false , null, slider.elementid);
		Slider_Register(slider.elementid + '_2', true, 0.3, false, null, slider.elementid, 'nzb_extras_loader');
		Slider_Register(slider.elementid + '_3', true, 0.3, false, null, slider.elementid, 'nzb_extras_loader');
	}
	
	// Image Loader

			// Pic exists, continue...
			fetch_object(nzb_extra_' + thread['nzbid'] + '_picture').src = NZB4FREE_IMAGE_PROGRESS;
			//nzbimage = new Image();
			//YAHOO.util.Event.on(nzbimage,"load",this.ImageLoaded);
			//YAHOO.util.Event.on(nzbimage,"error",this.ImageFailed);
			//nzbimage.src = 'image.gif';
		
}

function nzb_extras_loader(slider)
{
	this.handle_ajax_error = function(ajax)
	{
		//TODO: Something bad happened, try again
		vBulletin_AJAX_Error_Handler(ajax);
	}
	
	this.ProcessNZBExtras = function(ajax)
	{		
		if (ajax.responseXML)
		{
			var nzbid = ajax_fetch_tag('nzbid', ajax);
			var nzbextra = ajax_fetch_tag('nzbextra', ajax);
			fetch_object('nzb_extra_'+nzbid+'_' + this.mode).innerHTML = nzbextra; // original
		}
	}
	
	if (!nzbextras[slider.elementid])
	{
		nzbextras[slider.elementid] = true;
		fetch_object(slider.elementid).innerHTML = '<img src="' + NZB4FREE_IMAGE_PROGRESS + '" alt="" /> <span style="font-size:150%;">Loading...</span>';
		this.mode = slider.elementid.split('_');
		this.mode = this.mode[this.mode.length-1];
		nzbid = slider.elementid.split('nzb_extra_');
		nzbid = nzbid[1].split('_' + this.mode);
		nzbid = nzbid[0];

		YAHOO.util.Connect.asyncRequest("POST", "nzbextra_ajax.php", {
			success: this.ProcessNZBExtras,
			failure: this.handle_ajax_error,
			timeout: vB_Default_Timeout,
			scope: this
		}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&nzbid=' + nzbid + '&mode=' + this.mode);
	}
}

function nzb_extra_expand(elementid)
{
	slider_menus.sliders[elementid].Expand();
	
	var tabs = YAHOO.util.Dom.getChildren(this.parentNode);
	for (i = 0; i < tabs.length; i++)
	{
		YAHOO.util.Dom.replaceClass(tabs[i], 'tcat', 'thead');
		YAHOO.util.Dom.setStyle(tabs[i], 'top', '4px');
	}
	
	YAHOO.util.Dom.setStyle(this, 'top', '0px');
	YAHOO.util.Dom.replaceClass(this, 'thead', 'tcat');
}




//
// Site Popup
//
function confirm_deletefilter_popup()
{
	SitePopup.Show('Confirm Deletion of Saved Filter...', fetch_object('confirm_deletefilter_content').innerHTML);
}

//
// Filter Checkbox Checking JS
//

function setfilterchecks(idlist)
{		
	if (idlist=='all')
	{
		for (i = 0; i < allfiltericonids.length; i++)
		{
			fetch_object('filtericon'+allfiltericonids[i]).checked=true;
			update_checkbox_img(fetch_object('filtericon'+allfiltericonids[i]));
		}
	}
	else
	{
		for (i=0;i<allfiltericonids.length;i++)
		{
			fetch_object('filtericon'+allfiltericonids[i]).checked=false;
			update_checkbox_img(fetch_object('filtericon'+allfiltericonids[i]));
		}
		
		if (idlist!='none')
		{
			idlist = idlist.split(",");
			for(i=0;i<idlist.length;i++)
			{
				fetch_object('filtericon'+idlist[i]).checked=true;
				update_checkbox_img(fetch_object('filtericon'+idlist[i]));
			}
		}
	}
}

//
// Move Top Posters with Ajax
//

function MoveTopPosters(where)
{
	this.handle_ajax_error = function(ajax)
	{
		//TODO: Something bad happened, try again
		vBulletin_AJAX_Error_Handler(ajax);
	}
	
	this.ProcessMove = function(ajax)
	{
		var error = ajax_fetch_tag('error', ajax);
		if (error)
		{
			SitePopup.Show('Error...', error);
		}
		
		var where = ajax_fetch_tag('where', ajax);
		var toppostershtml = ajax_fetch_tag('toppostershtml', ajax);
		
		// Remove the old toppposters and stickies...
		var topposters = fetch_object('topposters');
		topposters.parentNode.removeChild(topposters);
		var stickies = fetch_object(STICKIESCONTAINER_ID);
		stickies.parentNode.removeChild(stickies);
		
		// First lets insert the top posters
		var nzbslist = fetch_object('nzbslist');
		
		if (where == 'top')
		{
			nzbslist.innerHTML = toppostershtml + nzbslist.innerHTML;
		}
		else
		{
			nzbslist.innerHTML = nzbslist.innerHTML + toppostershtml;
		}
		
		// Now insert the stickies above the top posters
		var newstickies = document.createElement('tr');
		var newstickies_td = document.createElement('td');
		newstickies_td.innerHTML = this.stickieshtml;
		newstickies_td.setAttribute('colspan', 15);
		YAHOO.util.Dom.addClass(newstickies_td, 'alt1');
		newstickies.appendChild(newstickies_td);
		newstickies.id = STICKIESCONTAINER_ID;
		
		var newtopposters = fetch_object('topposters');
		YAHOO.util.Dom.insertBefore(newstickies, newtopposters);	
		
		console.log('Moved TopPosters to ' + where);
	}
	
	where = (where != 'down' ? 'up' : 'down');
	
	this.stickieshtml = YAHOO.util.Dom.getFirstChild(STICKIESCONTAINER_ID).innerHTML;
	
	YAHOO.util.Connect.asyncRequest("POST", "ajax.php", {
		success: this.ProcessMove,
		failure: this.handle_ajax_error,
		timeout: vB_Default_Timeout,
		scope: this
	}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=movetopposters' + where);
	
	return false;
}