spd = 400;
$(function(){
	$('a[href^=http://]').add('a[hrerf^=www]').attr('target','_blank');
	// ------ HEIGHT CHECK ----------//
	var leftheight = $('#leftcolumn').height();
	var centheight = $('#centralcolumn').height();
	var rightheight = $('#rightcolumn').height();
	if (leftheight>centheight&&leftheight>rightheight) {
		$('#leftcolumn .partners').hide();
	}
	// ------- NAVIGATION ----------//
	$('.anchorclass').mouseover(function(e){
		e.preventDefault();
		if ($('#'+$(this).attr('rel')).not(':visible').length) {
			$('.submenu:visible').hideNav();
			$('#'+$(this).attr('rel')).showNav();
		}
	}).mouseout(function(e){
		e.preventDefault();
		if(!(e.pageY>$(this).offset().top+$(this).height()-1))
			$('#'+$(this).attr('rel')).hideNav();
		else $(this).css({'background-position':'0px -50px'});
	}).each(function(){
		var top = $(this).parents('div:first').offset().top+$(this).parents('div:first').height();
		var right = parseInt($(this).next().css('right'))+$('#canvas').offset().left;
		right -= 5;
		$(this).next().css({display:'none',right:right+'px',top:top+'px'})
		.mouseleave(function(e){
			$(this).hideNav();
		});
		$('body').append($(this).next().detach());
	});
	$(window).bind('resize',function(){
		left = $('#canvas').offset().left;
		$('.submenu').css({right:left+'px'})	
	});
	
	$('ul.subleftnav').each(function(){
		if($(this).parent('li.selected').length==1) $(this).css({display:'block'});
	});
	
	$('a.level2').mouseover(function(event){
		//event.preventDefault();
		/*$('#leftnav ul li').removeClass("selected noClass");
		$(this).parent().addClass("selected");
		
		$('ul.subleftnav:visible').each(function(){
			//if($(this).parent().not('.current').length)
			$(this).hideLeftNav();
		});
		$('#'+$(this).attr('rel')).showLeftNav();*/
	});
	
	$('#leftnav').mouseleave(function(){
		/*$('#leftnav ul li').removeClass("selected noClass")
		.find('.current').addClass('selected');
		$('ul.subleftnav:visible').each(function(){
			$(this).hideLeftNav();
		});
		$('#'+$('#leftnav ul li.current').children('a:first').attr('rel')).showLeftNav();*/
	});
	
	/*var navheights = new Array();
	$('.submenu .topnavcolumn').each(function(){
		$(this).children('li').each(function(i){
			if(typeof navheights[$(this).parent().parent().attr('id')][i] == 'undefined' || $(this).height()>navheights[$(this).parent().parent().attr('id')][i])
				navheights[$(this).parent().parent().attr('id')][i] = $(this).height();
		});
	});
	$('.submenu .topnavcolumn').each(function(i){
		$(this).children('li').each(function(i){
			console.log('height '+i+': '+navheights[$(this).parent().parent().attr('id')][i]);
			$(this).css({height:navheights[$(this).parent().parent().attr('id')][i]+'px'});
		});			
	});*/
	// ------ HOME PAGE ----------//
	var tabWidth = 0; var i = 0;
	$('#tabblock').find('.tab').each(function(){
		tabWidth += $(this).width();
	});
	while($('#tabblock').width()>tabWidth){
		if ($($('#tabblock').find('.tab').get(i)).length==0) i=0;
		crtTab = $('#tabblock').find('.tab').get(i);
		$(crtTab).css('width',function(){ return $(this).width()+1; });
		tabWidth++;
		i++;
	}
	$('.tab').click(function(e){
		e.preventDefault();
		$(this).parents('[rel=holder]').find('.tab').removeClass('selected');
		$(this).parents('[rel=holder]').find('.tabContent').hide();
		$(this).addClass('selected');
		$(this).parents('[rel=holder]').find('.tabContent[rel='+$(this).attr('rel')+']').show();
	});
	$('#tabblock .tabContent').find('li').click(function(e){
		e.preventDefault;
		window.location.href = $(this).find('a').attr('href');
	})/*.mouseover(function(){
		$(this).css({background:'#ddd',color:'#111'});
	}).mouseout(function(){
		$(this).css({background:'transparent',color:'#fff'});
	})*/;
	$('#tabblock').find('select[name=season]').change(function(){
		var select = $(this).next('select');
		$.post('_inc/comp_select.php?season='+$(this).val(),function(data){
			select.html(data);
		});
	});
	$('#tabblock').find('select[name=competition]').change(function(){//.each(function(){$(this).change(function(){
		var select = $(this).next('select');
		$.post('_inc/team_select.php?id='+$(this).val(),function(data){
			select.html(data);
		});
	});//});
	
	function PromoAuto() {
		if (!$('div:animated').length) {
			$('#promo .panel:visible').each(function(i){
				if ($(this).next('.panel').length) nextPanel = $(this).next('.panel');
				else nextPanel = $(this).parents('#promo:first').find('.panel:first');
				$(this).css({'z-index':'50'}).animate({opacity:'hide'},'slow');
				nextPanel.css({position:'absolute',top:'0px','z-index':'0'}).show(); //animate({opacity:'show'},'slow',function(){$(this).css({position:'relative',top:'auto'});});
			});
		}
	}
	
	setPromoAuto = setTimeout(function(){PromoAuto; setPromoAuto = setInterval(PromoAuto,spd*30)},spd*7);
	$('#promo').mouseenter(function(){clearInterval(setPromoAuto);});
	$('#promo').mouseleave(function(){setPromoAuto = setInterval(PromoAuto,spd*15);});
	$('#promo').find('.panel:gt(0)').hide();
	
	$('.areahold:gt(0)').hide();
	$('.area .title').click(function(e){
		e.preventDefault();
		if (!$('div:animated').length) {
			$hold = $(this).next('.areahold');
			if ($hold.is(':visible')) {
				$hold.animate({height:'hide'},spd);
			} else {
				$('.areahold:visible').animate({height:'hide'},spd);
				$hold.animate({height:'show'},spd);
			}
		}
	});
	
	$('.fixturesinner').find('select[name=season]').change(function(){
		var select = $('.fixturesinner select[name=competition]');
		$.post('/_inc/comp_select.php?season='+$(this).val()+'&type=fixture&competition='+select.val(),function(data){
			select.html(data);
		});
		$('.fixturesinner').find('select[name=time]').val('all');
	});
	$('.fixturesinner').find('select[name=competition]').change(function(){
		var select = $('.fixturesinner select[name=team]');
		var season = $('.fixturesinner select[name=season]');
		$.post('/_inc/team_select.php?competition='+$(this).val()+'&season='+season.val()+'&type=fixture&team='+select.val(),function(data){
			select.html(data);
			select.attr('disabled',false);
		});
	});
	
	$('.resultsinner').find('select[name=season]').change(function(){
		var select = $('.resultsinner select[name=competition]');
		$.post('/_inc/comp_select.php?season='+$(this).val()+'&type=result&competition='+select.val(),function(data){
			select.html(data);
		});
		$('.resultsinner').find('select[name=time]').val('all');
	});
	$('.resultsinner').find('select[name=competition]').change(function(){
		var select = $('.resultsinner select[name=team]');
		var season = $('.resultsinner select[name=season]');
		$.post('/_inc/team_select.php?competition='+$(this).val()+'&season='+season.val()+'&type=result&team='+select.val(),function(data){
			select.html(data);
			select.attr('disabled',false);
		});
	});
	
	// ------ RESULTS PAGE ----------//
	$('#results #lefthold').find('select[name=season]').change(function(){//.each(function(){$(this).change(function(){
		var select = $('select#comp');
		$.post('/_inc/comp_select.php?season='+$(this).val()+'&type=result&competition='+select.val(),function(data){
			select.html(data);
		});
		$('select#timeframe').val('all');
	});
	// ------ FIXTURES PAGE ----------//
	$('#fixtures #lefthold').find('select[name=season]').change(function(){//.each(function(){$(this).change(function(){
		var select = $('select#comp');
		$.post('/_inc/comp_select.php?season='+$(this).val()+'&type=fixture&competition='+select.val(),function(data){
			select.html(data);
		});
	});
	// ------ LEAGUE TABLE PAGE ----------//
	$('#leaguetable #lefthold').find('select[name=season]').change(function(){//.each(function(){$(this).change(function(){
		var select = $('select#comp');
		$.post('/_inc/comp_select.php?season='+$(this).val()+'&type=table&competition='+select.val(),function(data){
			select.html(data);
		});
	});
	// ------ NEWS PAGE ----------//
	$('#newsfilter').find('select').each(function(){
		$(this).change(function(){
			$(this).parents('form:first').submit();
		});
	});
	// ------ SEARCH PAGE ----------//
	$(function(){
		$('#search .head').click(function(e){
			e.preventDefault();
			var table = $(this).siblings('table:first');
			if(table.is(':visible'))
				table.animate({height:'hide'},spd);
			else table.animate({height:'show'},spd);
		}).css({cursor:'pointer'});
	});
});
var cache = [];
$.fn.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}
$.fn.showNav = function(){
	$('a[rel='+$(this).attr('id')+']').css({'background-position':'0px -50px'});
	return $(this).show();//.animate({height:"show",opacity:"show"},spd);
}
$.fn.hideNav = function(){
	$('a[rel='+$(this).attr('id')+']').css({'background-position':'0px 0px'});
	return $(this).hide();//.animate({height:"hide",opacity:"hide"},spd);
}
$.fn.hideLeftNav = function(){
	if($(this).parent('li.selected').length==0){
		return $(this).animate({height:"hide",opacity:"hide"},spd);
	} else return $(this);
}
$.fn.showLeftNav = function(){
	return $(this).animate({height:"show",opacity:"show"},spd);
}
