/* @Tableros home de canchallena.com
* Autor: Claudio Romano Cherñac
* lanacion.com  03-2009
*/

jQuery(function(){
	jQuery("div.csw").prepend("<p class='loading'>Cargando...<br /><img src='/_ui/desktop/imgs/layout/spinner.gif' alt=''/ ></p>");
});
var j = 0;

jQuery.fn.tableros = function(settings) {
	 settings = jQuery.extend({
     easeFunc: "expoinout",
     easeTime: 750,
     toolTip: false
  }, settings);
	return this.each(function(){

		var fecha = new Date();
		fecha.setTime( fecha.getTime() + 4000 * 60 * 60); // Expira en 4 hs

		var container = jQuery(this);
		container.find("p.loading").remove();
		container.removeClass("csw").addClass("stripViewer");
		var panelWidth = container.find("div.panel").width();
		var panelCount = container.find("div.panel").size();
		jQuery("#total").html(panelCount);
		var stripViewerWidth = panelWidth*panelCount;
		container.find("div.panelContainer").css("width" , stripViewerWidth);
		var timer = null;
		var cookie = LeerCookie("tablerosHome");
		
		// Specify the current panel.
		if(cookie!=''){
			var cPanel = parseInt(cookie);
			var cnt = - (panelWidth*(cPanel - 1));
			jQuery(this).find("div.panelContainer").css({ left: cnt });
		} else { 
			var cPanel = 1;
		};
		jQuery("#pos").html(cPanel);
		

		container.each(function(i) {

			// Create the Left and Right arrows
			jQuery(this).before("<div class='stripNavL' id='stripNavL" + j + "'><a href='#'><img src='/_ui/desktop/imgs/layout/botones/envivoAtras.gif' width='18' height='18' alt='Anterior' /></a><\/div>");
			jQuery(this).after("<div class='stripNavR' id='stripNavR" + j + "'><a href='#'><img src='/_ui/desktop/imgs/layout/botones/envivoSiguiente.gif' width='18' height='18' alt='Siguiente' /></a><\/div>");
			jQuery(this).after("<div class='stripNavP' id='stripNavP" + j + "'><a href='#'><img src='/_ui/desktop/imgs/layout/botones/envivoPlay.gif' width='18' height='18' alt='Automático' /></a><\/div>");
			jQuery(this).after("<div class='stripNavP' id='stripNavS" + j + "'><a href='#'><img src='/_ui/desktop/imgs/layout/botones/envivoPausa.gif' width='18' height='18' alt='Pausa' /></a><\/div>");


			jQuery("div#stripNav" + j + " a").each(function(z) {

				jQuery(this).bind("click", function() {
					stop();
					jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
					var cnt = - (panelWidth*z);
					cPanel = z + 1;
					current = z + 1;
					jQuery(this).parent().parent().parent().next().find("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
					
				});
			});


			jQuery("select#torneo").change(function(){
				cPanel = parseInt(jQuery(this).find("option:selected").val());

				var cnt = - (panelWidth*(cPanel - 1));
				jQuery("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				jQuery("#pos").html(cPanel);
			});


			//Play
			jQuery("div#stripNavP" + j + " a").click(function(){
				start();
				return false;
			});
			
			//Play
			jQuery("div#stripNavS" + j + " a").click(function(){
				stop();
				return false;
			});
			
			// Left nav
			jQuery("div#stripNavL" + j + " a").click(function(){
				stop();
				if (cPanel == 1) {
					var cnt = - (panelWidth*(panelCount - 1));
					cPanel = panelCount;
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().parent().find("li:last a").addClass("current");
				} else {
					cPanel -= 1;
					var cnt = - (panelWidth*(cPanel - 1));
					jQuery(this).parent().parent().find("div.stripNav a.current").removeClass("current").parent().prev().find("a").addClass("current");
				};
				jQuery(this).parent().parent().find("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);

				jQuery("#pos").html(cPanel);

				//location.hash = cPanel;
				document.cookie = "tablerosHome="+ cPanel + ";expires="+fecha.toGMTString() + "; path=/";
				return false;
			});


			// Right nav
			jQuery("div#stripNavR" + j + " a").click(function(){
				stop();
				if (cPanel == panelCount) {
					var cnt = 0;
					cPanel = 1;
				} else {
					var cnt = - (panelWidth*cPanel);
					cPanel += 1;
				};
				jQuery(this).parent().parent().find("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);

				jQuery("#pos").html(cPanel);

				//location.hash = cPanel;
				document.cookie = "tablerosHome="+ cPanel + ";expires="+fecha.toGMTString() + "; path=/";
				return false;
			});


			/* Rulo */
			start = function (){
				timer = setTimeout("start()", 6000);
				if (cPanel == panelCount) {
					var cnt = 0;
					cPanel = 1;
				} else {
					var cnt = - (panelWidth*cPanel);
					cPanel += 1;
				};

				jQuery("div.panelContainer").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
				//location.hash = cPanel;
				jQuery("#pos").html(cPanel);
				jQuery("div#stripNavS0 a").removeClass("hide").addClass("show");
				jQuery("div#stripNavP0 a").removeClass("show").addClass("hide");
				document.cookie = "tablerosHome=;";
			}

			

			stop = function (){
				jQuery("div#stripNavS0 a").removeClass("show").addClass("hide");
				jQuery("div#stripNavP0 a").removeClass("hide").addClass("show");
				if(timer!=null){clearTimeout(timer);}
				if(cookie!=''){}else{
					//alert("no tiene cookie");
					document.cookie = "tablerosHome="+ cPanel + ";expires="+fecha.toGMTString() + "; path=/";
				}
			}

			if(cookie==''){
				timer = setTimeout("start()", 6000);
			}

		});
		
		j++;
  });
};




jQuery.timer = function (interval, callback){
	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
};

function deslizar(sentido)
{
    if ($("div#grupoTablerosCabezal1").css("display") != "none")
    {
        if (sentido == "left")
        {
            moverTablerosCabezalIzquierda(1,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal1"));               
        }
        else
        {
            moverTablerosCabezalDerecha(1,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal1"));
        }
    }
    else if ($("div#grupoTablerosCabezal2").css("display") != "none")
    {
        if (sentido == "left")
        {    
            moverTablerosCabezalIzquierda(2,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal2"));   
        }
        else
        {
            moverTablerosCabezalDerecha(2,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal2"));        
        }            
    }
    else if ($("div#grupoTablerosCabezal3").css("display") != "none")
    {
        if (sentido == "left")
        {    
            moverTablerosCabezalIzquierda(3,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal3"));
        }
        else
        {
            moverTablerosCabezalDerecha(3,$("div#grupoTablerosCabezal1"), $("div#grupoTablerosCabezal2"), $("div#grupoTablerosCabezal3"), $("div#grupoTablerosCabezal3"));
        }
    }
}
function moverTablerosCabezalDerecha(tablero, tablero1, tablero2, tablero3, tableroAOcultar)
{
    tableroAOcultar.hide();
    switch (tablero)
    {
        case 1:
            tablero2.show();
            break;
        case 2:
            if (tablero3.length)
            {
                tablero3.show();
            }
            else
            {
                tablero1.show();
            }
            break;
        case 3:
            tablero1.show();
            break;
    }
}

function moverTablerosCabezalIzquierda(tablero, tablero1, tablero2, tablero3, tableroAOcultar)
{
    tableroAOcultar.hide();
    switch (tablero)
    {
        case 1:
            if (tablero3.length)
            {
                tablero3.show();
            }
            else
            {
                tablero2.show();
            }
            break;
        case 2:
            tablero1.show();
            break;
        case 3:
            tablero2.show();
            break;
    }
}


function ordenarTablerosCabezal()
{
    var cantidadFinalizados = 0;
    var hayTableroEnJuego = false;

    for (var i = 1; i < 7; i++)
    {
        var objTablero = $("div#tableroCabezalPos" + i);
        if (objTablero.length)
        {
            var objTableroInterno = $("span.est", objTablero);
            if (objTableroInterno.length)
            {
                hayTableroEnJuego = (hayTableroEnJuego || (objTableroInterno.html() == "En juego" || objTableroInterno.html() == "Entretiempo"));
                if (objTableroInterno.html() == "Finalizado") 
                {
                    cantidadFinalizados++;
                }
            }
        }
    }
    
    if ((cantidadFinalizados == 2 && hayTableroEnJuego) || (cantidadFinalizados == 3) || (cantidadFinalizados == 4 && !hayTableroEnJuego))
    {
        deslizar('right');
    }
    else if ((cantidadFinalizados == 4 && hayTableroEnJuego) || (cantidadFinalizados > 4))
    {
        deslizar('left');
    }
}


$(window).load(function(){setTimeout("ordenarTablerosCabezal()", 5000);});
