var tblcache = [];
var buffcache = [];


function loadStandingsLeague (el)
{
	if (!($(el).attr('name').length > 0))
	{
		return false;
	}

	
	var table = '/static_tables/' + $(el).attr('name');
	//var table = '/tables/' + $(el).attr('name');
	
	var w = $("#right-column-standings").width();

	$("#right-column-standings").empty().append(
		$("<img>").attr('src', '/sites/sport/img/ajax_loader.gif').css('margin-top', '20px').css('margin-left', w / 2 - 16 + 'px')	
	);	

	
	$('ul.standings-leagues li').removeClass ('active');
	$(el).parent().addClass ('active');
	
	for (i = 0; i < tblcache.length; i++)
	{
		if (tblcache[i] == table)
		{
			var retrieved = i;
		}	
	}

	if (typeof retrieved != 'undefined')
	{
		document.getElementById('right-column-standings').innerHTML = buffcache[retrieved];
	}
	
	else
	{	
		url = table  + '?' + Math.floor(Math.random()*9999);
			
		$.get (url, function (buffer) {
			tblcache.push (table);
			buffcache.push (buffer);
			document.getElementById('right-column-standings').innerHTML = buffer;
		});
	}
		
	return false;
}

function loadStandingsSport (el)
{
	var argv = loadStandingsSport.arguments;
		
	tblcache = [];
	buffcache = [];
	sport = $(el).attr('name');
	$('ul#standings-sports li').removeClass ('active');
	$(el).parent().addClass ('active');
	$('ul.standings-leagues').hide();
	$('ul#' + sport).slideDown();
	
	if (typeof(argv[1]) !== 'undefined' 
		&& argv[1].length > 0 
		&& $('ul#' + sport + ' li a[name='+argv[1]+']').length > 0
		)
	{
		loadStandingsLeague ($('ul#' + sport + ' li a[name='+argv[1]+']'));
	}
	else
	{
		loadStandingsLeague ($('ul#' + sport + ' li:first-child a'));
	}
	
}



function setHomePage (url) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if (window.sidebar) {
	if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e) {
			var strTemp = '';
			strTemp += "Permission denied";
			alert(strTemp);
		}
	}
	var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
}

function loadDefaultLeague ()
{
	var loc = location.href;
	path = window.location.pathname.split( '/' );

	if (typeof (path[2]) !== 'undefined' && path[2].length > 0)
	{
		path[2] = path[2].replace('-', '_', 'g');
	}

	if (path[1] === 'basket')
	{
		loadStandingsSport ('#menu-basket', path[2]);
	}
	else if (path[1] === 'calcio')
	{
		loadStandingsSport ('#menu-calcio', path[2]);
	}
	else if (path[2] === 'motomondiale')
	{
		loadStandingsSport ('#menu-moto');
	}
	else if (path[1] === 'motori')
	{
		loadStandingsSport ('#menu-f1');
	}
	else 
	{
		loadStandingsSport ('#menu-calcio');
	}
}


$(document).ready(function(){
	$(function() {  
	  $("#newslettersubmitajax").submit(function() {  
		 $.ajax({
		   type: "POST",
		   url: "/gonewsletter.php",
			data: $('#newslettersubmitajax').serialize(),
			success: function(msg){
			var result = msg.charAt(0);
			if (result == '0') {
				msg = msg.substring(1);
				//var message = $("#successmessage").html();
				$('#hiddennewslettermessage').html(msg);
				return false;
			}
			else {
				var randomnumber=Math.floor(Math.random()*10000)
				$(".captchaimage").attr("src", "/newsletter_captcha.php?rand=".concat(randomnumber)); 
				$('#error_message').html(msg);
				return false;
			}
		   }
		 });
		 return false;		  
		});  
	});  			
	
	
	$('.fixzindex').bgiframe();
	
	$('#bookmarkthis').click(function () {
		setHomePage ('http://www.film.it');
		return false;
	});

	var browserCheck = (document.all) ? 1 : 0; 
	if (!browserCheck) {
		$('#btli').hide();
		$('#btli_smap').hide();
	}

	$('#user-share').click(function () {
		$('#share').toggle();
		return false;
	});
	
	/*$('#user-vota').click(function(){
		$('#rate').toggle();
		return false;
	});*/	
	
	$('ul.standings-leagues li a').bind ('click', function(){loadStandingsLeague (this); return false;});
	$('ul#standings-sports li a').bind ('click', function(){loadStandingsSport (this); return false;});
	$('ul.standings-leagues').hide();
	$('ul#standings-football').show();
	
	loadDefaultLeague();
	
	$('.standings-right').live ('click', function(){	
		nextel = switchActiveTable (true, '_tables');
		
		$('.standings-left').show();		
		if ($(nextel).next('.right-standings_tables').length == 0)
			$('.standings-right').hide();

		return false;
	});
	
	$('.standings-left').live ('click', function(){	
		nextel = switchActiveTable (false, '_tables');
		
		$('.standings-right').show();		
		if ($(nextel).prev('.right-standings_tables').length == 0)
			$('.standings-left').hide();
		
		return false;
	});
	
	$('.fixtures-right').live ('click', function(){	
		nextel = switchActiveTable (true, '_fixtures');
		
		$('.fixtures-left').show();		
		if ($(nextel).next('.right-standings_fixtures').length == 0)
			$('.fixtures-right').hide();

		return false;
	});
	
	$('.fixtures-left').live ('click', function(){	
		nextel = switchActiveTable (false, '_fixtures');
		
		$('.fixtures-right').show();		
		if ($(nextel).prev('.right-standings_fixtures').length == 0)
			$('.fixtures-left').hide();
		
		return false;
	});


	$('.show-fixtures').live ('click', function(){
		$('.right-standings').fadeOut('fast', function(){
			$('.right-fixtures').fadeIn();
		});
		
		return false;
	});
	
	$('.show-standings').live ('click', function(){
		$('.right-fixtures').fadeOut('fast', function(){
			$('.right-standings').fadeIn();
		});
		
		return false;
	});


	$('.switch-constructors').live('click', function(){
		$('.right-moto:visible').fadeOut ('fast', function(){
			$('.right-moto-constructor').fadeIn();
		});
		
		$('.switch').show();
		$(this).hide();
		return false;
	});
	
	$('.switch-pilots').live('click', function(){
		$('.right-moto:visible').fadeOut ('fast', function(){
			$('.right-moto-personal').fadeIn();
		});
		
		$('.switch').show();
		$(this).hide();
		return false;
	});

	$('.switch-race').live('click', function(){
		$('.right-moto:visible').fadeOut ('fast', function(){
			$('.right-moto-race').fadeIn();
		});
		
		$('.switch').show();
		$(this).hide();
		return false;
	});

	$('.switch-from-name').live('click', function(){
		var s = $(this).attr('name');
		
		$('.switchable:visible').fadeOut ('fast', function(){
			$('.'+s).fadeIn();
		});
		
		$('.switch').show();
		$(this).hide();
		return false;
	});
	
	$('.switch-big-table-from-name').live('click', function()
	{
		var s = $(this).attr('name');
		
		$('.switchable-big-table:visible').fadeOut ('fast', function(){
			$('.'+s).fadeIn();
		});
		
		$('.switch-big-table').removeClass('active');
		$(this).addClass('active');
		return false;
	});

	
	
});

function switchActiveTable (next, suffix)
{
	var el = $('.right-standings'+suffix+':visible');
	
	if (next)
	{
		var nextel = $('.right-standings'+suffix+':visible').next('.right-standings'+suffix);
	}
	else
	{
		var nextel = $('.right-standings'+suffix+':visible').prev('.right-standings'+suffix);
	}

	if ($(nextel).length > 0)
	{
		$(el).fadeOut('fast', function(){
			$(nextel).fadeIn();
		});
	}
	
	return nextel;
}

function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}

function voteConcorso(user, user_id, type, extra1, extra2, site_id, redirect_url)
{   
    var query = '?user_id=' + user_id + '&type=' + type + '&extra1=' + extra1 + '&extra2=' + extra2 + '&action=check&site_id=' + site_id;
    
    $.ajax({
		type: "GET",
		url: "/check_user_voted.php" + query,
		contentType: "application/json; charset=utf-8",
  	  	dataType: "json",
		success: function(msg){

				if (msg.result == false)
                {
                    if (!confirm("Hai selezionato " + user + ": confermi il tuo voto?")) {
                         return false;
                    }
                    
                    var query = '?user_id=' + user_id + '&type=' + type + '&extra1=' + extra1 + '&extra2=' + extra2 + '&action=vote&site_id=' + site_id;
                    
                    $.ajax({
	                   	type: "GET",
	                   	url: "/check_user_voted.php" + query,
		                contentType: "application/json; charset=utf-8",
  	                 	dataType: "json",
		                  success: function(msg){
				                    if (msg.result) { location.href = redirect_url;  }
		                  }
	                   });
                }else
                {
                    alert('Hai già espresso la tua preferenza, puoi votare una volta sola, grazie!');
                }
		}
	});
}
