(function(){

var read = function(option, element){
	return (option) ? ($type(option) == 'function' ? option(element) : element.get(option)) : '';
};

this.TGTips = new Class({

	Implements: [Events, Options],

	options: {
		/*
		onAttach: $empty(element),
		onDetach: $empty(element),
		*/
		onShow: function(){
			this.tip.setStyle('display', 'block');
			this.tip.fade('in');
		},
		onHide: function(){
			this.tip.fade('out');
			//this.tip.setStyle('display', 'none');
		},
		text: function(element){
			return element.get('rel') || element.get('href');
		},
		showDelay: 50,
		hideDelay: 50,
		offset: {x: 3, y: 3},
		fixed: false
	},

	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		this.setOptions(params.options);
		document.id(this);
		
		if (params.elements) this.attach(params.elements);
	},

	toElement: function(){
		if (this.tip) return this.tip;
		
		this.container = $('tooltip.content');
		return this.tip = $('tooltip');
	},

	attach: function(elements){
		$$(elements).each(function(element){
			var text = read(this.options.text, element);
			
			element.retrieve('tip:text', text);
			this.fireEvent('attach', [element]);
			
			var events = ['enter', 'leave'];
			if (!this.options.fixed) events.push('move');
			
			events.each(function(value){
				var event = element.retrieve('tip:' + value);
				if (!event) event = this['element' + value.capitalize()].bindWithEvent(this, element);
				
				element.store('tip:' + value, event).addEvent('mouse' + value, event);
			}, this);
		}, this);
		
		return this;
	},

	detach: function(elements){
		$$(elements).each(function(element){
			['enter', 'leave', 'move'].each(function(value){
				element.removeEvent('mouse' + value, element.retrieve('tip:' + value)).eliminate('tip:' + value);
			});
			
			this.fireEvent('detach', [element]);
		}, this);
		
		return this;
	},

	elementEnter: function(event, element){
		this.container.empty();
		
		var content = element.retrieve('tip:text');
		if (content) this.fill($('tooltip.content'), content);
		
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this, element);
		this.position((this.options.fixed) ? {page: element.getPosition()} : event);
	},

	elementLeave: function(event, element){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this, element);
	},

	elementMove: function(event, element){
		this.position(event);
	},

	position: function(event){
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
		for (var z in props){
			obj[props[z]] = event.page[z] + this.options.offset[z];
			if ((obj[props[z]] + tip[z] - scroll[z]) > size[z]) obj[props[z]] = event.page[z] - this.options.offset[z] - tip[z];
		}
		
		this.tip.setStyles(obj);
	},

	fill: function(element, contents){
		element.set('html', contents);
	},

	show: function(element){
		this.fireEvent('show', [this.tip, element]);
	},

	hide: function(element){
		this.fireEvent('hide', [this.tip, element]);
	}

});

})();

// Detect chrome

function detectChrome()
{
	if( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 )
	{
		return 'load';
	}
	else
	{
		return 'domready';
	}
}

// AJAX page loading

var currentLocationHash;

function loadPage(page)
{
	$('breadcrumb').set('html', "");
	$$('li.active').removeClass('active');
	document.title = "Tenacious Gaming - Loading Page";
	$('content').set('html', "<div style='text-align: center; font-weight: bold; color: #800000; font-size: 125%;'><img src='http://www.tenaciousgaming.com/images/loading.gif' /><br/>&nbsp;<br/>Loading Page</div>");
	if( page.length < 1 )
	{
		window.location.hash = '';
		currentLocationHash = '';
		$('content').load('/?ajax=true');
	}
	else
	{
		window.location.hash = '#'+page;
		currentLocationHash = '#'+page;
		$('content').load('/'+page+'/?ajax=true');
	}
	
	return false;
}

window.addEvent(detectChrome(), function(){
	
	if(window.location.hash.length > 1)
	{
		loadPage(window.location.hash.substr(1));
	}
	
	currentLocationHash = window.location.hash;
	
	setTimeout("checkLocationHash()", 150);
});

var i = 0;

function checkLocationHash()
{
	i++;
	//document.title = i;
	if(window.location.hash != currentLocationHash)
	{
		loadPage(window.location.hash.substr(1));
	}
	setTimeout("checkLocationHash()", 150);
}