var tm, tmi;
$(document).ready(function(){
	var body = $('#h').offset({ "scroll": "false" }); 
	var left1 = body.left + 660;
	var left2 = body.left + 410;
	$("#lang a.l"+l).insertAfter("#lang a.l8");
	$("#lang").css({ "top":"-156px", "left": left1+"px", "display":"block" });
	$("#lang").hover(
		function () {
			clearTimeout(tm);
			if($(this).css('top') != '0px'){
				$("#lang a.l"+l).insertBefore("#lang a.l"+(l+1));
				$(this).animate({ 
					top: 0,
					opacity: 0.8
				}, 500 );
			}
		},
		function () {
			tm = setTimeout("HideLang()", 400);
		}
	);
	$("#lang a").hover(
		function() { $(this).addClass('sel'); },
		function() { $(this).removeClass('sel'); }
	)
	$("#info").css({ "top":"-59px", "left": left2+"px", "display":"block" });
	$("#info").css({ left: left2+"px" });
	$("#info").hover(
		function () {
			clearTimeout(tmi);
			if($(this).css('top') != '0px'){
				$(this).animate({ 
					top: 0,
					opacity: 0.8
				}, 300 );
			}
		},
		function () {
			tmi = setTimeout("HideInfo()", 600);
		}
	);
	if($(".select_countries").length > 0){

		$.ajax({ 
			type: "GET", 
			url: "/xml/countries.php", 
			dataType: "xml",
			success: function(xml){
				
				var options = new Array();
				
				$(xml).find('country').each(function (i){
					options[$(this).attr('value')] = $(this).text();
				});
				
				$(".select_countries").addOption(options).css('visibility','visible');

				$(".select_countries").each(function(u){
					var selected = $(this).attr('title');
					if(selected == undefined) selected = 0;
					$(this).find('option[value='+selected+']').attr('selected','selected');
				});

		    }
		  });	
	}
	$(window).resize(function(){
		var body = $('#h').offset({ scroll: false }); 
		var left1 = body.left + 660;
		var left2 = body.left + 410;
		$("#lang").css({ left: left1+"px" });
		$("#info").css({ left: left2+"px" });
	});
	
	$('#f2 select').change(function(){
		document.location = $('#f2').attr('action') + '/' + $(this).val() + '.html';
	});
	
	$('#f3').submit(function(){
		document.location = $(this).attr('action') + '/' + url_encode($('#f3 input').val()) + '.html';
		return false;
	});
	
	$('#f4').submit(function(){
		document.location = $(this).attr('action') + '/' + url_encode($('#f4 div.f2 input').val()) + '_' + url_encode($('#f4 div.f4 input').val()) + '_' + url_encode($('#f4 div.f6 input').val()) + '.html';
		return false;
	});

	$('#f1').submit(function(){
		document.location = $(this).attr('action') + '/' + $('#f1 div.f4 select').val() + '-' + $('#f1 div.f2 select').val() + '.html';
		return false;
	});
	
	$("a.ext").click(function(){
		pageTracker._trackEvent('Link', $(this).attr('rel'), $(this).attr('href'));
	});
	
	$("a.tm").click(function(){
		pageTracker._trackPageview('/view-tournament/'+$(this).attr('rel')+'/')
	});
	
	$("a.ty").click(function(){
		pageTracker._trackPageview('view-year/'+$(this).attr('rel')+'/')
	});
});

function HideLang(){
	$("#lang").animate({ 
			top: -156,
			opacity: 1
		}, 500 );
	$("#lang a.l"+l).removeClass('sel').insertAfter("#lang a.l8");
}

function HideInfo(){
	$("#info").animate({ 
			top: -59,
			opacity: 1
		}, 500 );
}

function url_encode(url){
     return encodeURIComponent(encodeURIComponent(url));
}

/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 2.2.3
 * Demo: http://www.texotela.co.uk/code/jquery/select/
 *
 * $LastChangedDate: 2008-06-17 17:27:25 +0100 (Tue, 17 Jun 2008) $
 * $Rev: 5727 $
 *
 */
;(function($){$.fn.addOption=function(){var e=function(a,v,t,b){var c=document.createElement("option");c.value=v,c.text=t;var o=a.options;var d=o.length;if(!a.cache){a.cache={};for(var i=0;i<d;i++){a.cache[o[i].value]=i}}if(typeof a.cache[v]=="undefined")a.cache[v]=d;a.options[a.cache[v]]=c;if(b){c.selected=true}};var a=arguments;if(a.length==0)return this;var f=true;var m=false;var g,v,t;if(typeof(a[0])=="object"){m=true;g=a[0]}if(a.length>=2){if(typeof(a[1])=="boolean")f=a[1];else if(typeof(a[2])=="boolean")f=a[2];if(!m){v=a[0];t=a[1]}}this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(m){for(var a in g){e(this,a,g[a],f)}}else{e(this,v,t,f)}});return this};$.fn.ajaxAddOption=function(b,c,d,e,f){if(typeof(b)!="string")return this;if(typeof(c)!="object")c={};if(typeof(d)!="boolean")d=true;this.each(function(){var a=this;$.getJSON(b,c,function(r){$(a).addOption(r,d);if(typeof e=="function"){if(typeof f=="object"){e.apply(a,f)}else{e.call(a)}}})});return this};$.fn.removeOption=function(){var a=arguments;if(a.length==0)return this;var d=typeof(a[0]);var v,index;if(d=="string"||d=="object"||d=="function"){v=a[0];if(v.constructor==Array){var l=v.length;for(var i=0;i<l;i++){this.removeOption(v[i],a[1])}return this}}else if(d=="number")index=a[0];else return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(this.cache)this.cache=null;var b=false;var o=this.options;if(!!v){var c=o.length;for(var i=c-1;i>=0;i--){if(v.constructor==RegExp){if(o[i].value.match(v)){b=true}}else if(o[i].value==v){b=true}if(b&&a[1]===true)b=o[i].selected;if(b){o[i]=null}b=false}}else{if(a[1]===true){b=o[index].selected}else{b=true}if(b){this.remove(index)}}});return this};$.fn.sortOptions=function(f){var a=typeof(f)=="undefined"?true:!!f;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;var o=this.options;var d=o.length;var e=[];for(var i=0;i<d;i++){e[i]={v:o[i].value,t:o[i].text}}e.sort(function(b,c){o1t=b.t.toLowerCase(),o2t=c.t.toLowerCase();if(o1t==o2t)return 0;if(a){return o1t<o2t?-1:1}else{return o1t>o2t?-1:1}});for(var i=0;i<d;i++){o[i].text=e[i].t;o[i].value=e[i].v}});return this};$.fn.selectOptions=function(b,d){var v=b;var e=typeof(b);var c=d||false;if(e!="string"&&e!="function"&&e!="object")return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;var o=this.options;var a=o.length;for(var i=0;i<a;i++){if(v.constructor==RegExp){if(o[i].value.match(v)){o[i].selected=true}else if(c){o[i].selected=false}}else{if(o[i].value==v){o[i].selected=true}else if(c){o[i].selected=false}}}});return this};$.fn.copyOptions=function(b,c){var w=c||"selected";if($(b).size()==0)return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;var o=this.options;var a=o.length;for(var i=0;i<a;i++){if(w=="all"||(w=="selected"&&o[i].selected)){$(b).addOption(o[i].value,o[i].text)}}});return this};$.fn.containsOption=function(b,c){var d=false;var v=b;var e=typeof(v);var f=typeof(c);if(e!="string"&&e!="function"&&e!="object")return f=="function"?this:d;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;if(d&&f!="function")return false;var o=this.options;var a=o.length;for(var i=0;i<a;i++){if(v.constructor==RegExp){if(o[i].value.match(v)){d=true;if(f=="function")c.call(o[i],i)}}else{if(o[i].value==v){d=true;if(f=="function")c.call(o[i],i)}}}});return f=="function"?this:d};$.fn.selectedValues=function(){var v=[];this.find("option:selected").each(function(){v[v.length]=this.value});return v};$.fn.selectedOptions=function(){return this.find("option:selected")}})(jQuery);