
// create a global namespace
var ES = {};

// run scripts on load
$(function(){
	
	// initialise the common code
	$('html').addClass('JS');							// add .JS class
	$('a[rel=external]').attr('target', '_blank');		// set external links
	
	// start up the custom JS
	ES.utilityLinks.init();								// utility link drop downs
	ES.languageSwitch.init();							// switches language display
	
//	$('#static-nav li:has(li.selected)').toggleClass('open');
	
//	alert($('body').attr('id'));
	
	// set selected static nav
	var pageId = $('body').attr('id');
	var navId = pageId.replace('pg-', 'stn-');
	$('#' + navId).addClass('selected');
	$('#static-nav li:has(li.selected)').toggleClass('open')
	$('#static-nav li:has(ul) a:first').click(function(){
		$(this).parent().toggleClass('open');
		return false;
	});
	
});

// error message
ES.error = {
	display: function(message){
		alert(message);
	}
};

// manages the links in the top right of the page
ES.utilityLinks = {
	
	items: [],
	
	// initialise menu
	init: function(){
		// set details
		this.items[0] = {
			elem: '#quick-search',
			link: '#quick-search a:first',
			box: '#quick-search form',
			visible: false,
			hide: '#quick-login, #account-info'
		};
		this.items[1] = {
			elem: '#country-select',
			link: '#country-select a:first',
			box: '#country-select form',
			visible: false,
			hide: '#quick-login, #account-info'
		};
		this.items[2] = {
			elem: '#odds-display',
			link: '#odds-display ul',
			box: '#odds-display ul ul',
			visible: false,
			hide: null
		};
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
	},
	
	bindEventHandlers: function(){
		// bind events
		for(var i=0; i<this.items.length; i++){
			$(this.items[i].link).bind('click', {index:i}, function(event){
				ES.utilityLinks.toggleBox(event.data.index);
				return false;
			});
		}
		$('body').bind('click', function(event){ ES.utilityLinks.reset(event); });
	},
	
	toggleBox: function(index){
		// check to see if item is visible
		if(this.items[index].visible){
			// hide the box
			this.hideBox(this.items[index], true);
		}
		else{
			// hide visible boxes
			for(var i=0; i<this.items.length; i++){
				if(this.items[i].visible) this.hideBox(this.items[i], false);
			}
			// show the box
			this.showBox(this.items[index], true);
		}
	},
	
	showBox: function(item, animate){
		// hide the underlying account area
		if(item.hide) $(item.hide).hide();
		if(animate){
			// run show animation
			$(item.elem).addClass('open');
			$(item.link).animate({
				backgroundColor: '#333333'
			}, 'fast', function(){
				$(item.box).fadeIn('fast');
			});
		}
		else{
			$(item.elem).addClass('open');
			$(item.link).css({ backgroundColor: '#333333' });
			$(item.box).show();
		}
		item.visible = true;
	},
	
	hideBox: function(item, animate){
		if(animate){
			// run hide animation
			$(item.box).fadeOut('fast', function(){
				$(item.link).animate({
					backgroundColor: '#1a1a1a'
				}, 'fast', function(){
					$(item.elem).removeClass('open');
					// hide the underlying account area
					if(item.hide) $(item.hide).fadeIn('fast');
				});
			});
		}
		else{
			$(item.box).hide();
			$(item.link).css({ backgroundColor: '#1a1a1a' });
			$(item.elem).removeClass('open');
			if(item.hide) $(item.hide).fadeIn('fast');
		}
		item.visible = false;
	},
	
	hideAll: function(){
		// hide visible boxes
		for(var i=0; i<this.items.length; i++){
			if(this.items[i].visible) this.hideBox(this.items[i], false);
		}
	},
	
	reset: function(event){
		for(var i=0; i<this.items.length; i++){
			if($(event.target).parents(this.items[i].elem).length > 0){
				return;
			}
			else{
				this.hideAll();
			}
		}
	}
};


// language display form
ES.languageSwitch = {
	
	// initialise menu
	init: function(){
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
	},
	
	bindEventHandlers: function(){
		$('#country-select form a').bind('click', function(){
			var languageCode = $(this).attr('href').replace('#', '');
			var myUrl = window.location.href;
			var begin = myUrl.lastIndexOf('/');
			var end = myUrl.lastIndexOf('.');
			var page = myUrl.substring(begin+1, end);
			  
			// window.location.href = "../es/Rules.shtml";
			window.location.href = "../" + languageCode + "/" + page + ".shtml";
			return false;
		});
	}
};

