// Images
var NZB4FREE_IMAGE_PROGRESS = IMGDIR_MISC + '/progress.gif';
var NZB4FREE_IMAGE_NOIMAGE = IMGDIR_MISC + '/noimage.png';
var NZB4FREE_IMAGE_NOLOADIMAGE = IMGDIR_MISC + '/noloadimage.png';
// Phrases
var NZB4FREE_PHRASE_AJAX_COMPLETE = 'Completed operation. This will close in a few seconds.';
var NZB4FREE_PHRASE_AJAX_RUNNING = '<img src="' + NZB4FREE_IMAGE_PROGRESS + '" alt="" />&nbsp;&nbsp; Please wait...';
var NZB4FREE_PHRASE_SEARCH_PERFORMING = '<img src="' + NZB4FREE_IMAGE_PROGRESS + '" alt="" />&nbsp;&nbsp;<span style="font-size:larger;font-style:italic;">Currently performing a search...</span><div class="smallfont">You will be redirected to the results shortly...</div>';
var NZB4FREE_PHRASE_MARK_READ_PERFORMING = '<img src="' + NZB4FREE_IMAGE_PROGRESS + '" alt="" />&nbsp;&nbsp;<span style="font-size:larger;font-style:italic;">Marking Forum Read...</span><div class="smallfont">You will be redirected to the previous page shortly...</div>';
var NZB4FREE_PHRASE_NZBPOST_NONZB = "You must provide an NZB file!";
var NZB4FREE_PHRASE_NZBPOST_NOTITLE = "You must enter a title / subject!";
var NZB4FREE_PHRASE_NZBPOST_NOINFOLINK = "You must enter a information link!";
var NZB4FREE_PHRASE_NZBPOST_NOIMAGELINK = "You must enter a image / poster link!";
var NZB4FREE_PHRASE_NZBPOST_NOTYPE = "You must select a nzb type by clicking on the correct image at the bottom!";
// Misc
var BBURL = "http://www.nzb4free.co.uk/";

var IS_ROSSCO = (USERID == 1);

function alertr(msg)
{
	if (IS_ROSSCO || USERID == 1)
	{
		alert(msg);
	}
}

// Is IE8?
if (typeof is_ie8 == 'undefined')
{
	is_ie8 = ((is_ie) && (userAgent.indexOf('msie 8.') != -1));
}

//Function to open a new window for who viewed a thread
function whov(threadid)
{
	return openWindow(
		'misc.php?' + SESSIONURL + 'do=whoviewed&t=' + threadid,
		230, 300
	);
}

function ajax_fetch_tag(tag,ajax)
{
	var value = ajax.responseXML.getElementsByTagName(tag);
	if (value.length)
	{
		if (value[0].firstChild)
		{
			return value[0].firstChild.nodeValue;
		}
	}
	
	return  '';
}

function titletotooltip(elementid)
{
	if (element = fetch_object(elementid))
	{
	
		if (!element.title && element.alt != '')
		{
			element.title = element.alt;
		}
		
		element.ToolTip = new YAHOO.widget.Tooltip("element.ToolTip", {
					context:elementid,
					effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
				} );
	}
}


//
// Buttonbar Hover Animation
//

function buttonbarhover()
{
	ButtonBar.Hover(this);
}

function buttonbarunhover()
{
	ButtonBar.unHover(this);
}

function buttonbar()
{
	this.Init = function()
	{
		var elements = YAHOO.util.Dom.getChildren('topbuttons');
		for (var i = 0; i < elements.length; i++)
		{
			YAHOO.util.Event.addListener(elements[i], 'mouseover', buttonbarhover, elements[i], true);
			YAHOO.util.Event.addListener(elements[i], 'mouseout', buttonbarunhover, elements[i], true);
		}
	}
	
	this.FetchColor = function(e)
	{
		if (YAHOO.util.Dom.getStyle(e, 'backgroundColor') == 'transparent' || YAHOO.util.Dom.getStyle(e, 'backgroundColor') == 'rgba(0, 0, 0, 0)')
		{
			return this.FetchColor(e.parentNode);
		}
		return YAHOO.util.Dom.getStyle(e, 'backgroundColor');
	}
	
	this.Hover = function(e)
	{
		if (!e.PrevColor)
		{
			e.PrevColor = this.FetchColor(e);
			console.log(e.PrevColor);
			// Set this because if it's set to transparent (PrevColor from a parent) then there is no animation...
			YAHOO.util.Dom.setStyle(e, 'backgroundColor', e.PrevColor); 
		}		
		this.Anim = new YAHOO.util.ColorAnim(e, {backgroundColor : { to: '#FFFFFF' }}, 0.5);
		this.Anim.animate();
	}
	
	this.unHover = function(e)
	{
		this.Anim = new YAHOO.util.ColorAnim(e, {backgroundColor : { to: e.PrevColor }}, 0.5);
		this.Anim.animate();
	}
}
var ButtonBar = new buttonbar();
YAHOO.util.Event.onContentReady('topbuttons', ButtonBar.Init);


//
// Slider Menu Scripts
//

function Sliders_Handler()
{
	this.sliders = new Array;
	this.activeslider = new Array;
	this.waiting = new Array;
	
	this.Toggle = function(set)
	{
		// attempt to toggle, will just actually pick
		// first element in the set. which isn't active..
		var i;
		for (i in this.sliders)
		{
			if (this.sliders[i].set == set)
			{
				if (this.sliders[i].elementid != this.activeslider[set])
				{
					console.log('Slider: Toggling set ' + set);
					this.waiting[set] = this.sliders[i].elementid;
					this.sliders[this.activeslider[set]].Shrink();
				}
			}
		}
	}
}

slider_menus = new Sliders_Handler();

function Slider_Register(elementid, startclosed, duration, cookiecollapse, containerid, set, callbackfunction, fancyease)
{
	slider_menus.sliders[elementid] = new Slider_Expander(elementid, startclosed, duration, cookiecollapse, containerid, set, callbackfunction, fancyease);
}
			
function Slider_Expander(elementid, startclosed, duration, cookiecollapse, containerid, set, callbackfunction, fancyease)
{	
	// Finds if there is a waiting slider from this set and starts it
	this.StartWaitingSliders = function()
	{
		if (slider_menus.waiting[this.set])
		{
			slider_menus.sliders[slider_menus.waiting[this.set]].Expand();
			slider_menus.waiting[this.set] = null;
		}
	}
	
	// Saves the vbcollapse cookie if we have cookiecollapse
	this.Save_Cookie = function(add)
	{
		if (this.cookiecollapse)
		{
			save_collapsed(this.elementid,(add ? true : false));
		}
	}
	
	// Checks if we have a cookie
	this.Has_Cookie = function()
	{
		if (this.cookiecollapse)
		{
			var collapsed = fetch_cookie('vbulletin_collapse');
			if (collapsed != null)
			{
				collapsed = collapsed.split('\n');

				for (var i in collapsed)
				{
					if (collapsed[i] == this.elementid)
					{
						return true;
					}
				}
			}
		}
		return false;
	}
	
	// Handles click event for toggle button(s)
	this.Click = function()
	{
		if (slider_menus.activeslider[this.set] == this.elementid)
		{
			this.Shrink();
		}
		else
		{
			this.Expand();
		}
	}
	
	// Expands the element
	this.Expand = function()
	{
		if (slider_menus.activeslider[this.set] && slider_menus.activeslider[this.set] != this.elementid)
		{
			slider_menus.waiting[this.set] = this.elementid;
			slider_menus.sliders[slider_menus.activeslider[this.set]].Shrink();
		}
		else
		{
			slider_menus.activeslider[this.set] = this.elementid;
			YAHOO.util.Dom.setStyle(( this.containerid ? this.containerid : this.elementid ), 'display', '');
			this.ToggleImageClass(true);
			
			// Start the animation
			this.didexpand = true;
			this.Anim.attributes.height = { to: this.finalheight };
			this.Anim.method = this.expandEase; 
			this.Anim.animate();
			
			console.log('Slider :: Expanded ' + this.elementid);
		}
	}
	
	// Shrinks the element
	this.Shrink = function()
	{
		this.ToggleImageClass(false);
		
		// Start the animation
		this.didshrink = true;
		this.Anim.attributes.height = { to: 0 };
		this.Anim.method = this.shrinkEase;
		this.Anim.animate();
		
		console.log('Slider :: Shrunk ' + this.elementid);
	}
	
	this.ToggleImageClass = function(exp)
	{
		if (fetch_object(this.elementid + '_img'))
		{
			if (!exp)
			{
				YAHOO.util.Dom.replaceClass(this.elementid + '_img', 'slidermenu_img_exp', 'slidermenu_img');
			}
			else
			{
				YAHOO.util.Dom.replaceClass(this.elementid + '_img', 'slidermenu_img', 'slidermenu_img_exp');
			}
		}
	}
		
	
	// Handles tasks after a shrink or expand, updates the toggle image's class
	this.AnimCallback = function()
	{
		// Stuff for when expanding is finished
		if (this.didexpand)
		{
			this.didexpand = false;
			
			this.Save_Cookie(false);
			if (this.callback)
			{
				console.log('Slider :: Run callback function ' + this.callback);
				eval(this.callback+'(this)');
			}
		}
		
		// Stuff for when shrinking is finished
		if (this.didshrink)
		{
			this.didshrink = false;
			
			slider_menus.activeslider[this.set] = null;			
			YAHOO.util.Dom.setStyle(( this.containerid ? this.containerid : this.elementid ), 'display', 'none');
			this.Save_Cookie(true);
			this.StartWaitingSliders();	
		}
	}
	
	this.elementid = elementid;
	this.containerid = containerid;
	this.callback = callbackfunction;	
	this.cookiecollapse = cookiecollapse;
	this.shrinkEase = (fancyease ? YAHOO.util.Easing.backIn : YAHOO.util.Easing.easeNone);
	this.expandEase = (fancyease ? YAHOO.util.Easing.bounceOut : YAHOO.util.Easing.easeNone);
	this.set = (set ? set : elementid);
	
	// Overflow of our element must be set to hidden
	YAHOO.util.Dom.setStyle(this.elementid, 'overflow', 'hidden');
	
	// Attach toggle image onclick event
	if (this.toggleobj = fetch_object(this.elementid + '_img'))
	{
		YAHOO.util.Event.on(this.toggleobj, 'click', this.Click, this, true);
	}
	
	// The final height
	this.finalheight = parseInt(YAHOO.util.Dom.getStyle(this.elementid, 'height'));
	
	// Now setup the animation object
	this.Anim = new YAHOO.util.Anim(this.elementid);
	this.Anim.duration = (duration ? duration : 1);
	this.Anim.onComplete.subscribe(this.AnimCallback, this, true);
	
	// Are we starting closed?
	if (startclosed || slider_menus.activeslider[this.set] || this.Has_Cookie())
	{
		YAHOO.util.Dom.setStyle(this.elementid, 'height', '0px');
		YAHOO.util.Dom.setStyle(( this.containerid ? this.containerid : this.elementid ), 'display', 'none');
		this.ToggleImageClass(false);
	}
	else
	{
		slider_menus.activeslider[this.set] = this.elementid;
		this.ToggleImageClass(true);
	}
	
	// Log for rossco
	if (IS_ROSSCO)
	{
		console.log('Slider :: Init ' + this.set + '::' + this.elementid);
	}
}

//
// Page Popup Scripts
//

function Page_Popup(varname, elementid, popupwidth, overlayopacity, ignorereturn)
{
	this.elementid = elementid;
	this.varname = varname;
	this.timer;
	
	// position fixed for all except ie6 and lower...
	if (is_ie && !is_ie7 && !is_ie8)
	{
		this.enabled = false;
	}
	else
	{		
		this.enabled = true;
		
		if (popupelement = fetch_object(elementid))
		{
			if (popupwidth)
			{
				this.popupwidth = popupwidth + "px";
			}
			else
			{
				this.popupwidth = "400px";
			}
			
			var element_properties = {
				position: "fixed",
				width: this.popupwidth,
				top: "45%",
				left: "50%",
				marginLeft: "-" + (popupwidth/2) + "px",
				zIndex: "1000"
			};
			
			for (var property in element_properties)
			{
				if (YAHOO.lang.hasOwnProperty(element_properties, property))
				{
					YAHOO.util.Dom.setStyle(popupelement, property, element_properties[property]);
				}
			}
			
			this.popupelement = popupelement;
			//this.popupelement.originalOpacity = YAHOO.util.Dom.getStyle(this.popupelement, 'opacity');
		}
		
		if (overlayopacity>1)
		{
			if (overlayopacity > 100)
			{
				overlayopacity = 100;
			}
			
			overlay = document.createElement('div');
			
			var overlay_properties = {
				top: "0px",
				left: "0px",
				right: "0px",
				bottom: "0px",
				display: "none",
				position: "fixed",
				backgroundColor: "#000000",
				opacity: '0.' + overlayopacity
			};

			for (var property in overlay_properties)
			{
				if (YAHOO.lang.hasOwnProperty(overlay_properties, property))
				{
					YAHOO.util.Dom.setStyle(overlay, property, overlay_properties[property]);
				}
			}
			this.Overlay = overlay;
		}
	}
	
	this.Show = function()
	{
		returnvalue = true;
		if (this.enabled)
		{
			// Hide vbmenu because it doesn't get covered with overlay...
			vbmenu_hide();
			if (typeof this.popupelement != 'undefined')
			{
				YAHOO.util.Dom.setStyle(this.popupelement, "opacity", this.popupelement.originalOpacity);
				YAHOO.util.Dom.setStyle(this.popupelement, "display", "");
				returnvalue = false;
			}
			if (typeof this.Overlay != 'undefined')
			{
				YAHOO.util.Dom.setStyle(document.body.appendChild(this.Overlay), "display", "");
			}
		}
		if (!ignorereturn)
		{
			return returnvalue;
		}
	}
	
	this.Close = function()
	{
		if (this.enabled)
		{
			this.ClearTimer();
			if (typeof this.popupelement != 'undefined')
			{
				//this.Anim = new YAHOO.util.Anim(this.popupelement, {opacity:{to: 0}}, 0.25);
				//if (this.Anim.animate())
				//{
					//this.Anim.onComplete.subscribe(this.DoClose, this, true);
				//}
				//else
				//{
					this.DoClose();
				//}
			}			
		}
	}
	
	this.DoClose = function()
	{
		if (typeof this.popupelement != 'undefined')
		{
			YAHOO.util.Dom.setStyle(this.popupelement, "display", "none");
		}
		if (typeof this.Overlay != 'undefined')
		{
			//overlay = document.body.appendChild(this.Overlay);
			YAHOO.util.Dom.setStyle(document.body.appendChild(this.Overlay), "display", "none");
		}
	}
	
	this.TimerClose = function(waittime)
	{
		this.timer = window.setTimeout( this.varname + ".Close()", waittime );
	}
	
	this.ClearTimer = function()
	{
		if (this.timer)
		{
			window.clearTimeout(this.timer);
		}
	}
}

//
// Site Info Popup
//

SitePopup = new siteinfo_popup('Siteinfo_Popup','siteinfo_popup');

function siteinfo_popup(varname,elementid)
{
	this.elementid = elementid;
	this.varname = varname;
	this.CloseFunc = false;
	
	this.Show = function(title,text,timewait,func,closefunc,form)
	{
		if (func)
		{
			eval(func);
		}
		if (form)
		{
			if (formobj = fetch_object(this.elementid + '_form'))
			{
				formobj.name = (form['name'] ? form['name'] : '');
				formobj.action = (form['action'] ? form['action'] : '');
				formobj.onsubmit = (form['onsubmit'] ? form['onsubmit'] : '');
				formobj.method = (form['method'] ? form['method'] : '');
			}
		}
		if (closefunc)
		{
			this.CloseFunc = closefunc;
		}
		if (titleobj = fetch_object(this.elementid + '_title'))
		{
			titleobj.innerHTML = title;
		}
		if (bodyobj = fetch_object(this.elementid + '_body'))
		{
			bodyobj.innerHTML = text;
		}		
		eval(this.varname + '.Show()');
		
		if (timewait)
		{
			this.TimerClose(timewait);
		}
	}
	
	this.Close = function()
	{
		if (this.CloseFunc)
		{
			eval(this.CloseFunc);
		}
		eval(this.varname + '.Close()');
	}
	
	this.TimerClose = function(timewait)
	{
		eval(this.varname + '.TimerClose(' + timewait + ')');
	}
}

//
// Site Popup Handlers
//

function searching_popup()
{
	SitePopup.Show('Searching...',NZB4FREE_PHRASE_SEARCH_PERFORMING);
}
function today_popup()
{
	SitePopup.Show('Searching...',NZB4FREE_PHRASE_SEARCH_PERFORMING);
}
function markread_popup()
{
	SitePopup.Show('Updating...',NZB4FREE_PHRASE_MARK_READ_PERFORMING);
}

function logout_popup()
{
	SitePopup.Show('Log Out', fetch_object('logout_popup_content').innerHTML);
	
	return false;
}

var LOGIN_HTML = '';

function login_popup()
{
	var form = new Array();
	form['name'] = 'loginform';
	form['action'] = 'login.php?do=login';
	form['method'] = 'post';
	form['onsubmit'] = 'md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)';
	if (LOGIN_HTML == '')
	{
		LOGIN_HTML = fetch_object('login_popup_content').innerHTML;
		fetch_object('login_popup_content').innerHTML = '';
	}
	SitePopup.Show('Log In', LOGIN_HTML, false, false, false, form);		
	replace_checkbox_with_imgbox('login_rememberme');
	return false;
}

//
// Checkbox Image Javascript Codes
//

function getLabelForId(id)
{
	var label, labels = document.getElementsByTagName('label');
	for (var i = 0; (label = labels[i]); i++)
	{
		if (label.htmlFor == id)
		{
			return label;
		}
	}
	return false;
} 

function update_checkbox_img(checkbox)
{
	if (imgbox = fetch_object('IMGBox_' + checkbox.id))
	{
		YAHOO.util.Dom.replaceClass(imgbox, "checkbox_img_" + (checkbox.checked ? 'f' : 't'), "checkbox_img_" + (checkbox.checked ? 't' : 'f'));
	}
}

function toggle_checkbox(e)
{
	console.log('IMGBox :: Clicked ' + this.id + ' / ' + this.name + '(Checked: ' + this.checked + ')');
	
	// Prevent whatever the default is, good for being able to click labels
	YAHOO.util.Event.preventDefault(e);
	
	fetch_object(this.id).checked = (this.checked ? false : true);
	
	// for inline mod
	if (typeof this.inlineModID != "undefined")
	{
		inlinemod_checkbox_onclick.call(this, e);
		/*inlineModObj = eval(this.inlineModID);
		inlineModObj["highlight_" + inlineModObj.highlighttype](this);
		inlineModObj.save(this.id.substring(inlineModObj.list.length), this.checked);
		inlineModObj.set_output_counters();*/
	}
	
	update_checkbox_img(this);
	
	return false;
}

function checkbox_keypress(e)
{
	if (YAHOO.util.Event.getCharCode(e) == 32)
	{
		toggle_checkbox.call(this, e);
	}
	return false;
}

function replace_checkbox_with_imgbox(checkboxobjid)
{
	console.log ('IMGBox :: Init checkbox img: ' + checkboxobjid);
	if (checkbox = fetch_object(checkboxobjid))
	{	
		// Create the box with these default attributes
		var imgbox = document.createElement('img');
		imgbox.id = 'IMGBox_' + checkbox.id;
		imgbox.src = "clear.gif";
		YAHOO.util.Dom.addClass(imgbox, 'checkbox_img_f');
		// Copy css from the checkbox to the new IMGBox
		imgbox.setAttribute('style',checkbox.style.cssText);
		// TabIndex and Access key for new IMGBox taken from checkbox
		imgbox.tabIndex = checkbox.tabIndex;
		imgbox.accessKey = checkbox.accessKey;
		checkbox.tabIndex = checkbox.accessKey = '';
		// Hide old checkbox
		YAHOO.util.Dom.setStyle(checkbox, 'display', 'none');
		// Insert the new one
		YAHOO.util.Dom.insertAfter(imgbox, checkbox);
		
		// Onclick and OnKeyPress(spacebar) Events
		YAHOO.util.Event.on(imgbox, "keypress", checkbox_keypress, checkbox, true);
		
		// Put the onclick event on the IMGBox UNLESS IT'S IN A VBMENU_OPTION
		if (YAHOO.util.Dom.hasClass(imgbox.parentNode, 'vbmenu_option'))
		{
			// VBMENU_OPTION
			YAHOO.util.Event.on(imgbox.parentNode, "click", toggle_checkbox, checkbox, true);
		}
		else
		{
			// IMGBox
			YAHOO.util.Event.on(imgbox, "click", toggle_checkbox, checkbox, true);
			// LABEL
			if (label = getLabelForId(checkbox.id))
			{
				//YAHOO.util.Event.removeListener(label, "click");
				if (imgbox.parentNode != label)
				{
					YAHOO.util.Event.on(label, "click", toggle_checkbox, checkbox, true);
				}
			}
		}
		
		update_checkbox_img(checkbox);
	}
	else
	{
		console.log ('IMGBox :: Init checkbox failed; Checkbox not found: ' + checkboxobjid);
	}
}

function replace_checkboxes_with_imgbox(formobjid)
{
	this.right_type = function(obj)
	{
		return (obj.type == 'checkbox' && (obj.disabled == false || obj.disabled == 'undefined'));
	}
	
	var formobj = fetch_object(formobjid);
	for (var i = 0; i < formobj.elements.length; i++)
	{
		if (this.right_type(formobj.elements[i]))
		{
			replace_checkbox_with_imgbox(formobj.elements[i].id);
		}
	}
}

/* ************************************************************ *\
|*                          ---------                           *|
|*                    Logo Resize/Init Events                   *|
|*                          ---------                           *|
\* ************************************************************ */

YAHOO.util.Event.on(window, "resize", resizelogo);
YAHOO.util.Event.on(window, "load", resizelogo);

function resizelogo()
{
	if (pmboxmsg = fetch_object('pm_box_x_full'))
	{
		if (document.documentElement.clientWidth<960)
		{
			pmboxmsg.style.display='none';
		}
		else
		{
			pmboxmsg.style.display='';
		}
	}
	if (sitebanner = fetch_object('banner'))
	{
		if (document.documentElement.clientWidth<=810)
		{
			sitebanner.className='mini';
		}
		else
		{
			sitebanner.className='';
		}
	}
}

/* ************************************************************ *\
|*                          ---------                           *|
|*                         NZB Scripts                          *|
|*                          ---------                           *|
\* ************************************************************ */

//
// NZB Reminders in NZB Index
//

function reminderthreadhighlight(tobj, addremove, force)
{
	while (tobj.tagName != 'TR')
	{
		if (tobj.parentNode.tagName == 'HTML')
		{
			break;
		}
		else
		{
			tobj = tobj.parentNode;
		}
	}
	if (tobj.tagName == 'TR')
	{
		var tds = tobj.childNodes;
		for (var i = 0; i < tds.length; i++)
		{
			element = tds[i];
			if (element.tagName)
			{
				if (force || YAHOO.util.Dom.hasClass(element, "alt1") || YAHOO.util.Dom.hasClass(element, "alt2") || YAHOO.util.Dom.hasClass(element, "nzbhighlight"))
				{
					if (addremove=='add')
					{
						YAHOO.util.Dom.addClass(element, "nzbhighlight");
					}
					else
					{
						YAHOO.util.Dom.removeClass(element, "nzbhighlight");
					}
				}
			}
		}
	}
}

NZBReminder = new nzbreminder();

function nzbreminder()
{
	this.Toggle = function(nzbid)
	{
		SitePopup.Show('NZB Reminder',NZB4FREE_PHRASE_AJAX_RUNNING);
		
		YAHOO.util.Connect.asyncRequest("POST", "ajax.php", {
				success: this.ProcessReminder,
				failure: this.handle_ajax_error,
				timeout: vB_Default_Timeout,
				scope: this
			}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=togglenzbreminder&nzbid=' + nzbid);
			
	}
	
	this.handle_ajax_error = function(ajax)
	{
		//TODO: Something bad happened, try again
		vBulletin_AJAX_Error_Handler(ajax);
	}
	
	this.ProcessReminder = function(ajax)
	{
		if (ajax.responseXML)
		{
			var error = ajax_fetch_tag('error', ajax);
			var reminder = ajax_fetch_tag('reminder', ajax);
			var nzbfavcache = ajax_fetch_tag('nzbfavcache', ajax);
                        var nzbid = ajax_fetch_tag('nzbid', ajax);

			if (error)
			{
				SitePopup.Show('NZB Reminder...', error);
			}
			else
			{
				SitePopup.Show('NZB Reminder', NZB4FREE_PHRASE_AJAX_COMPLETE);
				SitePopup.TimerClose(1000);			
				
				if (toggler = fetch_object('nzbreminder_' + nzbid + '_toggler'))
				{
					toggler.className = 'nzbreminder_toggler' + ( reminder == 0 ? '' : '_active' );
                                                                                toggler.title = ( reminder == 1 ? 'Remove Favorite' : 'Add Favorite' );

				}
				if (togglertitle = fetch_object('add_' + nzbid + '_remove'))
				{
                                                                                togglertitle.innerHTML = ( reminder == 1 ? 'Remove Favorite' : 'Add Favorite' );

				}
				if (titlecell = fetch_object('td_threadtitle_' + nzbid), true)
				{
					reminderthreadhighlight(titlecell, ( reminder == 0 ? 'remove' : 'add' ),1);
				}
				if (img = fetch_object('nzbreminder_' + nzbid + '_img'))
				{
					img.style.display = ( reminder == 0 ? 'none' : '' );
				}

			}
		}
	}
}

//
// Nuke NZBs in NZB Index
//

NukeNZB = new nukenzb();

function nukenzb()
{
	this.values = new Array;
	this.workingnzbid = 0;
	
	this.Toggle = function(nzbid)
	{
		if (nzbnuke_toggler = fetch_object('nzbnuke_' + nzbid + '_toggler'))
		{
			if (nzbnuke_toggler.className=='nzbnuke_toggler_active')
			{
				this.Reverse(nzbid);
			}
			else
			{
				this.Ask(nzbid);
			}
		}
	}
				
	this.Ask = function(nzbid)
	{
		this.workingnzbid = nzbid;
	
		SitePopup.Show('Nuke NZB', fetch_object('nukenzb_content').innerHTML, false,false,'NukeNZB.Close()');
		
		if (this.values[nzbid])
		{
			fetch_object('nukereason').value = this.values[nzbid];
		}
	}
	
	this.Reverse = function(nzbid)
	{
		this.workingnzbid = nzbid;
		
		SitePopup.Show('Nuke NZB', fetch_object('confirm_reversenuke_content').innerHTML, false, false, 'NukeNZB.Close()');
	}
	
	this.Close = function()
	{
		nzbid = this.workingnzbid;
		this.workingnzbid = 0;
		this.values[nzbid] = fetch_object('nukereason').value;
		fetch_object('nukereason').value = '';
	}
	
	this.Submit = function()
	{
		nzbid = this.workingnzbid;
		nukereason = fetch_object('nukereason').value;

		if (nukereason == '')
		{
			SitePopup.Show('Nuke NZB...', 'Must enter a reason');
		}
		else
		{
			SitePopup.Close();
			SitePopup.Show('Nuke NZB...',NZB4FREE_PHRASE_AJAX_RUNNING);

			YAHOO.util.Connect.asyncRequest("POST", "ajax.php", {
				success: this.ProcessNuke,
				failure: this.handle_ajax_error,
				timeout: vB_Default_Timeout,
				scope: this
			}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=nzbnuke&nzbid=' + nzbid + '&nukereason=' + nukereason);
		}
	}
	
	this.DoReverseNuke = function()
	{
		nzbid = this.workingnzbid;
		
		this.Close();
		SitePopup.Show('Nuke NZB...',NZB4FREE_PHRASE_AJAX_RUNNING);
		
		YAHOO.util.Connect.asyncRequest("POST", "ajax.php", {
			success: this.ProcessNuke,
			failure: this.handle_ajax_error,
			timeout: vB_Default_Timeout,
			scope: this
		}, SESSIONURL + 'securitytoken=' + SECURITYTOKEN + '&do=reversenzbnuke&nzbid=' + nzbid);
	}
	
	this.handle_ajax_error = function(ajax)
	{
		//TODO: Something bad happened, try again
		vBulletin_AJAX_Error_Handler(ajax);
	}
	
	this.ProcessNuke = function(ajax)
	{		
		if (ajax.responseXML)
		{
			var nzbid = ajax_fetch_tag('nzbid', ajax);
			var error = ajax_fetch_tag('error', ajax);
			var nuked = ajax_fetch_tag('nuked', ajax);
			var nukedreason = ajax_fetch_tag('nukedreason', ajax);
			var nukedmessage = ajax_fetch_tag('nukedmessage', ajax);
			
			if (error)
			{
				SitePopup.Show('Nuke NZB...', error);
			}
			else
			{
				SitePopup.Show('Nuke NZB...', NZB4FREE_PHRASE_AJAX_COMPLETE, 1000);

				// The Toggle Button
				// - Change Class
				if (nzbnuke_toggler = fetch_object('nzbnuke_' + nzbid + '_toggler'))
				{
					nzbnuke_toggler.className = 'nzbnuke_toggler' + ( nuked == 1 ? '_active' : '' );
				}
				// The Phrase / Link
				// - Change Phrase
				if (nzbnuke_phrase = fetch_object('nzbnuke_' + nzbid + '_phrase'))
				{
					nzbnuke_phrase.innerHTML = ( nuked == 1 ? 'Reverse Nuke' : 'Nuke NZB' );
				}
				// The Message in NZB Download BBCode
				// - Change Message
				if (nzbnuke_message = fetch_object('nzbnuke_' + nzbid + '_message'))
				{
					nzbnuke_message.innerHTML = nukedmessage;
					nzbnuke_message.style.display = ( nuked == 1 ? '' : 'none' );
				}
				// The Status Image on NZB Index Thread
				// - Show/Hide
				// - Change alt & title attributes to nuke reason
				if (nzbnuke_img = fetch_object('nzbnuke_' + nzbid + '_img'))
				{
					nzbnuke_img.style.display = ( nuked == 1 ? '' : 'none' );
					nzbnuke_img.alt = ( nuked == 1 ? nukedreason : '' );
					nzbnuke_img.title = nzbnuke_img.alt;
				}
			}
		}
	}
}

