/* Mootools 1.2 compat */

// CORE
var Window = window;
var Document = document;
var $native = Native;
window.extend(Client.Engine);

// SELECTORS
function $E(selector, filter){
	return ($(filter) || document).getElement(selector);
};

function $ES(selector, filter){
	return ($(filter) || document).getElementsBySelector(selector);
};

// IE Friendly Debug
function debug(thing)
{
	var prefix_date = new String(new Date().getTime());
	if (window.gecko)
	{
		if (typeof(thing) != "object")
			console.debug("["+prefix_date+"] "+thing);
		else
			console.debug(thing);
	}
	else
		alert(thing);
}

function clearSelection()
{
	var sel ;
	if (document.selection && document.selection.empty)
	{
		document.selection.empty() ;
	}
	else if (window.getSelection)
	{
		sel=window.getSelection();
		if (sel && sel.removeAllRanges) sel.removeAllRanges();
	}
}

/*
 * Generic Tour JS object.
 *
 * Use this to implement any tour-only javascript methods that would otherwise be
 * global functions.
 */
var Tour = new Class({

	initialize: function(options) {
	
		this.menupath = options.menu;
		this.episodepath = options.episode;
		this.bottompath = options.bottom;
		this.joinpagepath = options.join_page;
		this.natscode = options.nats;
		this.ratingboxes = [];
	
		$(window.document.body).getElements('div.content_menu div.elements img').each(function(menu_image){
			menu_image.addEvent('mouseenter',this.hoverImage.pass([this.menupath,menu_image],this));
            menu_image.addEvent('mouseleave',this.unHoverImage.pass([this.menupath,menu_image],this));
        },this);
		
		$(window.document.body).getElements('div.episode div.entice img').each(function(entice_image){
			entice_image.addEvent('mouseenter',this.hoverImage.pass([this.episodepath,entice_image],this));
			entice_image.addEvent('mouseleave',this.unHoverImage.pass([this.episodepath,entice_image],this));
		},this);
		
		$(window.document.body).getElements('div.bottom-button img').each(function(next_image){
			next_image.addEvent('mouseenter',this.hoverImage.pass([this.bottompath,next_image],this));
			next_image.addEvent('mouseleave',this.unHoverImage.pass([this.bottompath,next_image],this));
		},this);
		
		/*$(window.document.body).getElements('div.join img').each(function(join_image){
			join_image.addEvent('mouseenter',this.hoverImage.pass([this.bottompath,join_image],this));
			join_image.addEvent('mouseleave',this.unHoverImage.pass([this.bottompath,join_image],this));
		},this);*/
		
		$(window.document.body).getElements('div.site_heading div.join-page img').each(function(join_page_image){
			join_page_image.addEvent('mouseenter',this.hoverImage.pass([this.joinpagepath,join_page_image],this));
			join_page_image.addEvent('mouseleave',this.unHoverImage.pass([this.joinpagepath,join_page_image],this));
		},this);
		
		$('searchbox').addEvent('keypress',this.sendSearch.bind(this));
		
	},
	
	sendSearch: function(event) {
		if (event.key == "enter" && $('searchbox').value)
			window.location.assign("?t=search&search="+$('searchbox').value+"&nats="+this.natscode);
	},
	
	getHoverLocation: function(rel) {
		return rel.split(':').pop();
	},
	
	hoverImage: function(path,image) {
		var gethover = this.getHoverLocation(image.getProperty('rel'));
		image.src = path+'over/'+gethover+'.gif';
	},
	
	unHoverImage: function(path,image) {
		var gethover = this.getHoverLocation(image.getProperty('rel'));
		image.src = path+'regular/'+gethover+'.gif';
	},
	
	addBookmark: function(title,url) {
		if (Client.Engine.ie6 || Client.Engine.ie7)
		{ // IE Favorite
			window.external.AddFavorite(url, title);
		}
		else if(Client.Engine.opera)
		{ // Opera Hotlist
			return true;
		}
		else
		{
			alert("Press CTRL + D to bookmark this page");
		}
	}

});

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}