// JavaScript Document
//version 109

// Hero Banner
var contextRoot = '/JS/banner_top';
var tieneSiteCatalystBanner = null;

function sitecatalystBotonDerecha(varSC) {
	s=s_gi(s_account);
	s.tl(this, 'o', varSC+'-boton derecha');
}

function sitecatalystBotonIzquierda(varSC) {
	s=s_gi(s_account);
	s.tl(this, 'o', varSC+'-boton izquierda');
}

function sitecatalystAbrirBanner(varSC) {
	s=s_gi(s_account);
	s.tl(this, 'o', varSC+'-boton abrir');
}

function sitecatalystCerrarBanner(varSC) {
	s=s_gi(s_account);
	s.tl(this, 'o', varSC+'-boton cerrar');
}

function sitecatalystRadioButton(varSC, i) {
	s=s_gi(s_account);
	s.tl(this, 'o', varSC+'-boton radio'+i);
}

$(function(){

  $('.hideIfJs').hide();

    $(".megabannerItemList").each(function() {
        $(this).children().wrapAll('<div class="animated"></div>');
    });
    
    $(".megabanner").each(function() {
        var mb = $(this);
        var id = mb.attr("id");
	var to = $(".megabannerTimeout", mb);
        var timeout = 0;
	if (to.size() > 0) {
	    var classes = to.attr("class");
	    var pos = classes.indexOf("timeout");
	    if (pos != -1) {
		timeout = parseInt(classes.substr(pos + 7));
	    }
	}
        mb.css("overflow", "hidden");
         		  
        if (tieneSiteCatalystBanner && tieneSiteCatalystBanner != '')
        {
	    	    var mbo = $('<div class="megabannerOpener"><a href="#" onclick="sitecatalystAbrirBanner(tieneSiteCatalystBanner)" class="floatRight"><img src="'+contextRoot+'/img/arrows_btn_open_0.png" alt="Reabrir banner" /></a></div>');
  		      mb.before(mbo);
      			mb.prepend('<div class="nav"><div class="closeBanner"><a href="#" onclick="sitecatalystCerrarBanner(tieneSiteCatalystBanner)" class="close"><img alt="Ocultar banner" src="'+contextRoot+'/img/tab_close.png" alt="Cerrar banner"/></a></div>' + ($(".megabannerContent", mb).size() > 1 ? '<div class="controler"><a href="#" class="next"><img alt="Siguiente" onclick="sitecatalystBotonDerecha(tieneSiteCatalystBanner)" src="'+contextRoot+'/img/tab_next.png" class="next2"/></a>' + (timeout > 0 ? '<a href="#" class="playpause"><img alt="Play/Pause" src="'+contextRoot+'/img/tab_pause.png"/></a>' : '') + '<a href="#" onclick="sitecatalystBotonIzquierda(tieneSiteCatalystBanner)" class="previous"><img alt="Anterior" src="'+contextRoot+'/img/tab_previous.png" class="previous2"/></a><div class="bullets"></div></div>' : '') + '</div>');
      	}
      	else
        {
	    	    var mbo = $('<div class="megabannerOpener"><a href="#" class="floatRight"><img src="'+contextRoot+'/img/arrows_btn_open_0.png" alt="Reabrir banner" /></a></div>');
  		      mb.before(mbo);
      			mb.prepend('<div class="nav"><div class="closeBanner"><a href="#" class="close"><img alt="Ocultar banner" src="'+contextRoot+'/img/tab_close.png" alt="Cerrar banner"/></a></div>' + ($(".megabannerContent", mb).size() > 1 ? '<div class="controler"><a href="#" class="next"><img alt="Siguiente" src="'+contextRoot+'/img/tab_next.png" class="next2"/></a>' + (timeout > 0 ? '<a href="#" class="playpause"><img alt="Play/Pause" src="'+contextRoot+'/img/tab_pause.png"/></a>' : '') + '<a href="#" class="previous"><img alt="Anterior" src="'+contextRoot+'/img/tab_previous.png" class="previous2"/></a><div class="bullets"></div></div>' : '') + '</div>');
        }
        var close = $(".close", mb);
        var next = $(".next", mb);
        var prev = $(".previous", mb);
        var sls = $(".animated", mb);
        sls.cycle({
            fx: 'scrollHorz',
            speed: 600,
            timeout: timeout,
            pager: $(".bullets", mb),
            next: next,
            prev: prev
        });
        var pause = function(elm) {
            $("img", elm).attr("src", contextRoot + "/img/tab_play.png");
            sls.cycle('pause');
        }
        var play = function(elm) {
            $("img", elm).attr("src", contextRoot + "/img/tab_pause.png");
            sls.cycle('resume');
        }
        var toggle = function(elm) {
            var playImg = contextRoot + "/img/tab_play.png";
            var pauseImg = contextRoot + "/img/tab_pause.png";
            if (!$("img", elm)) return;
            if ($("img", elm).attr("src") == playImg) {
                $("img", elm).attr("src", pauseImg);
                sls.cycle('resume');
            } else {
                $("img", elm).attr("src", playImg);
                sls.cycle('pause');
            }
        }
        var pp = $(".playpause", mb);
        pp.click(function() {
            toggle(this);
            return false;
        });
        next.click(function() {
            pause(pp);
        });
        prev.click(function() {
            pause(pp);
        });
        $(".bullets a", mb).click(function() {
            pause(pp);
			if (tieneSiteCatalystBanner) {
				varSC = tieneSiteCatalystBanner;
				s=s_gi(s_account);
				s.tl(this, 'o', varSC+'-boton radio'+i);
			}
        });
        close.click(function() {
            pause(pp);
        });
       /* if (id != null && id.length > 0) {
            next.click(function() {
                sls.cycle('pause');
                s=s_gi('vodafoneesglobal');
                s.tl($(this), 'o', id + '-boton derecha');
            });
            prev.click(function() {
                sls.cycle('pause');
                s=s_gi('vodafoneesglobal');
                s.tl($(this), 'o', id + '-boton izquierda');
            });
            $(".bullets a", mb).each(function(index) {
                $(this).click(function() {
                    sls.cycle('pause');
                    s=s_gi('vodafoneesglobal');
                    s.tl($(this), 'o', id + '-boton radio ' + (index + 1));
                });
            });
            close.click(function() {
                sls.cycle('pause');
                s=s_gi('vodafoneesglobal');
                s.tl($(this), 'o', id + '-boton cerrar');
            });
            mbo.click(function() {
                s=s_gi('vodafoneesglobal');
                s.tl($(this), 'o', id + '-boton abrir');
            });
        }*/
        close.click(function () {
            mbo.slideDown("fast");
            mb.slideUp("fast");
            return false;
        });
        mbo.click(function () {
            $(this).slideUp("fast");
            mb.slideDown("fast");
            return false;
        });
        $(".megabannerSpinner", mb).remove();
    });


$(".carrusel").each(function() {
    var mb = $(this);
    mb.css("overflow", "hidden");
	var lele = $('<div class="carrusel-nav"></div>');
    var nav = $('<div class="carrusel-nav"><a href="#" class="next" title="Siguiente"><img alt="Siguiente" src="'+contextRoot+'/img/tab_next.png" class="next2"/></a><a href="#" class="previous" title="Anterior"><img alt="Anterior" src="'+contextRoot+'/img/tab_previous.png" class="previous2"/></a><div class="bullets"></div></div>');
    if (mb.children().length > 1) {
        nav.insertBefore(mb);
    } else { lele.insertBefore(mb); }
    $(mb).cycle({
      fx: 'scrollHorz',
      speed: 150,
      timeout: 0,
      pager: $(".bullets", nav),
      next: $(".next2", nav),
      prev: $(".previous2", nav),
	  onPagerEvent: onBullets,
	  onPrevNextEvent: onButtons
	  //after: onAfter
    }).css("overflow", "hidden");
  });

function onBullets(idx, slide) {
	
	slide = $(slide);
	
	var mod = slide.parents(".modBody").parent();

	var scDiv = mod.find(".siteCatalyst").html();
	if(scDiv) scDiv = $.trim(scDiv);

	if(scDiv && scDiv.length > 0) {
		
		var scVar = mod.find(".siteCatalystPage").html();
		if (!scVar) scVar = '';
		
		if(scVar.length > 0){
	
			var scNumber = mod.find(".siteCatalystParam").html();
			if (!scNumber) scNumber = '';
			
			var tabs =  mod.find(".tabs");
				
			var tabTitle;
			var productName;
			
			if (tabs && tabs.length > 0) {
				tabTitle = tabs.find(".active span").html();
				productName = slide.find("h4 a").html();
			} else
				productName = slide.find("p strong").html();
			
			s=s_gi(s_account);
			
			if(tabs && tabs.length > 0) {
				s.linkTrackVars="prop" + scNumber + ",eVar" +  scNumber;
				s["prop"+scNumber] = tabTitle+":"+productName;
				s["eVar"+scNumber] = tabTitle+':'+productName;
			}
			
			var bulletNumber = idx + 1;
			
			s.tl(this, 'o', scVar+'-boton radio ' + bulletNumber);
		}
	}
}

function onButtons(isNxt, idx, slide) {
	
	slide = $(slide);
	
	var mod = $(slide).parents(".modBody").parent();
	
	var scDiv = mod.find(".siteCatalyst").html();
	if(scDiv) scDiv = $.trim(scDiv);
	
	if(scDiv && scDiv.length > 0) {
		
		var scVar = mod.find(".siteCatalystPage").html();
		if (!scVar) scVar = '';

		if(scVar.length > 0){

			var scNumber = mod.find(".siteCatalystParam").html();
			if (!scNumber) scNumber = '';

			var tabs = mod.find(".tabs");
		
			var tabTitle;
			var productName;
			
			if (tabs && tabs.length > 0) {
				tabTitle = tabs.find(".active span").html();
				productName = slide.find("h4 a").html();
			} else
				productName = slide.find("p strong").html();
			
			s=s_gi(s_account);
			
			if(tabs && tabs.length > 0) {
				s.linkTrackVars="prop" + scNumber + ",eVar" +  scNumber;
				s["prop"+scNumber] = tabTitle+":"+productName;
				s["eVar"+scNumber] = tabTitle+':'+productName;
			}
			var txtBoton = '-boton izquierda';
			if (isNxt) txtBoton = '-boton derecha';
			
			s.tl(this, 'o', scVar+txtBoton);
		}
	}
}


 // TABS
function sc_CarouselClickTab(scVar, scNumber, carousel, tabs)
{
		var tabTitle = tabs.find(".active span").html();
	
		var productName =carousel.find("div:visible h4 a").html();
	
		s=s_gi(s_account);

		s.linkTrackVars="prop" + scNumber + ",eVar" +  scNumber + ",events";
		s.linkTrackEvents = "event38,event39";
		s["prop"+scNumber] = tabTitle+":"+productName;
		s["eVar"+scNumber] = tabTitle+':'+productName;
		s.events="event38,event39";
		s.tl(this, "o", scVar + "-Click en pesta�a " + tabTitle);
	
}


 $('.tab-container').css("overflow", "hidden").each(function() {
    var tc = $(this);
    var ul = $('<ul class="tabs"></ul>');
    ul.insertBefore(tc);
    $("h3", this).each(function(index, elm) {
      var id = $(this).parent().attr("id");
      ul.append('<li' + (index == 0 ? ' class="first-tab"' : '') + '><a href="#' + id + '" rel="' + id + '"><span>' + $(this).html() + '</span></a></li>');
    });
  });

  $("ul.tabs").each(function () {
    var ul = $(this);
    var par = ul.parent();
    var selected = null;
    
    if (location.hash) {
        selected = $("li a[rel='" + location.hash.substr(1) + "']", ul).parent();
    }
    if (selected == null || selected.length == 0) {
        selected = $("li:first", ul);
    }
    selected.addClass("active");
    $(".tab-content", par).hide();
    $("#" + selected.find("a").attr("rel")).show();
    $("li", ul).click(function() {
		$("li", ul).removeClass("active");
		$(this).addClass("active");
		$(".tab-content", par).hide();
		var activeTab = $(this).find("a").attr("rel");
		$('#'+activeTab).show();
		return false;
    });
  });


  // Eventos Site Catalyst a la botonera y las pesta�as de los carruseles
$(".siteCatalyst").each(function() {
	
	var siteCatalystPage = $(this).find(".siteCatalystPage").html();
	
	var siteCatalystParam = $(this).find(".siteCatalystParam").html();
	if (!siteCatalystParam) siteCatalystParam = '';

	
	var module = $(this).parent();
		
	if (siteCatalystPage && siteCatalystPage != '' && module) {
		
		var carouselNav = module.find(".carrusel-nav");
		
		var nextImage = carouselNav.find(".next2");
		var prevImage = carouselNav.find(".previous2");
		
		var carousel = module.find(".carrusel");
		
		var tabs = module.find(".tabs");
		
		if(tabs && tabs.length > 0) {
			module.find("ul.tabs li").bind('click', function() {
				sc_CarouselClickTab(siteCatalystPage, siteCatalystParam, carousel, tabs);
				//return false;
			});
		}
	}
});  
 
  // Bigphone
  $(".phone-details_container").css("overflow", "hidden");
  $(".bigPhone .modCopy").each(function () {
    var mc = $(this);
    mc.mouseover(function() {
      $(".phone-details-data", mc).css("top", "25em");
      $(".phone-details-price", mc).css("top", "0em");
    });
    mc.mouseout(function() {
      $(".phone-details-data", mc).css("top", "0em");
      $(".phone-details-price", mc).css("top", "25em");
    });
  });

	// Campo de busqueda de la cabecera
	$('#searchField').each(function(){
		// tomamos el valor actual del input
	   var currentValue = $(this).val();
	   // en el focus() comparamos si es el mismo por defecto, y si es asi lo vaciamos
	   $(this).focus(function(){
		  if( $(this).val() == currentValue ) {
			 $(this).val('');
		  };
	   });
	   
	   $(this).click(function(){
		  if( $(this).val() == currentValue ) {
			 $(this).val('');
		  };
	   });
	   
	   // en el blur, si el usuario dejo el value vacio, lo volvemos a restablecer
	   $(this).blur(function(){
		  if( $(this).val() == '' ) {
			 $(this).val(currentValue);
		  };
	   });
	});
	
	// Zebra
	$("table").each(function() {
		$("tr:even", this).addClass("even");
		$("tr:odd", this).addClass("odd");
	});
	
	// Ribbon Mi Vodafone: cambio de estilos y funcionalidad js
	$('.loginBox').addClass('loginBoxJs');
	$('.loginBoxOk').addClass('loginBoxOkJs');

/*
	$(".myVodafone > a").mouseover(function(){
		$('.loginBox').show();
		$('.loginBoxOk').show();
		$('#doctype').focus();
		$('.advantage').focus();
	});
*/
	  
	$(".myVodafone").mouseover(function(){
		cancelCloseLoginBox();
		$('.loginBox').show();
		$('.loginBoxOk').show();
		//$('#lineas').focus();
		$("#searchField").blur();
	});

	$(".myVodafone").mouseout(function(event){
                if (($("#doctype")[0] && $("#doctype")[0].focussed) || ($("#lineas")[0] && $("#lineas")[0].focussed)) return;
                programCloseLoginBox();
	});

	$('#doctype, #lineas').blur(function() {
		this.focussed = false;
	});

	$('#doctype, #lineas').focusout(function() {
		this.focussed = false;
                programCloseLoginBox();
        });

	$('#doctype, #lineas').focus(function() {
		this.focussed = true;
		cancelCloseLoginBox();
	});

	$("#doctype > option, #lineas > option").click(function() {
		$(this).parent().blur();
	});

	$("#doctype, #lineas").change(function() {
		this.blur();
	});

        var closeTimeout = null;
				
        function programCloseLoginBox() {
		if (closeTimeout != null) cancelCloseLoginBox();
                if (($("#doctype")[0] && $("#doctype")[0].focussed) || ($("#lineas")[0] && $("#lineas")[0].focussed)) return;
		closeTimeout = setTimeout(function() {
			$('.loginBox').hide();
			$('.loginBoxOk').hide();
		}, 100);
        }

	function cancelCloseLoginBox() {
		if (closeTimeout == null) return;
		clearTimeout(closeTimeout);
	}
/*
		if (($.browser.version == '6.0') || ($.browser.version == '7.0') || ($.browser.version == '8.0')) {
			if ((event.pageX > 0)&&(event.pageY > 0)) {
				$('.loginBox').hide();
				$('.loginBoxOk').hide();
				if ($.browser.version == '6.0') {
					$('.singleColumn select').css('visibility','visible');
				}
			}
		} else {
			$('.loginBox').hide();
			$('.loginBoxOk').hide();
			if ($.browser.version == '6.0') {
				 $('.singleColumn select').css('visibility','visible');
			}
		}
*/
});

// Caja login: cambio de campos
function showHideFields(elm) {
  document.loginForm.uuid.value = "";
  var tipodoc = elm[elm.selectedIndex].value;
  $("#numDocumento,#pwd").attr("value", "");
  $("#rememberme")[0].checked = false;
  $(".remind,#rememberme").css("visibility", tipodoc == 'tel' ? "visible" : "hidden");
  $(".user,#uuid,.password,#password").css("visibility", tipodoc == 'cert' ? "hidden" : "visible");
}

// Forzar foco inicial
$("#orientation").focus();

