var appContext = "/portal";
var isHome = false;

/*
 * Funcionalidad de autocomplete.
 */
$(document).ready(function(){
		$("#query").autocomplete(appContext + '/autocomplete.do',{
		width:'306px',
		delay:1000,
		minChars:3,
		matchSubset:3,
		matchContains:3,
		max: 200,
		formatItem: function(row) {return row[0];},
		onItemSelect: function(li){alert(li);},
		formatResult: function(row) {return row[0];}       	
	});	
	
	$("#query").click(function (){
		if ($(this).val()=="Buscar"){
			$(this).val("");
			return true;
		}
	});
	$("#query").blur(function (){
		if ($(this).val()==""){
			$(this).val("Buscar");
			return true;
		}
	});
});

/*
 * Funcionalidad para forzar la ubicacion.
 */
$(document).ready(function(){
	var cookieValue = $.cookie(COOKIE_NAME);

	if(cookieValue == null && isHome == false){
		$.fn.colorbox({
			href: appContext + '/ubicacion.do?ajax', 
			open:true,
			height: 270,
			onClosed:function(){ }
		});
	}
});

var seleccionaUbicacion = function() {
	var id = $("#estadoForzado").val();

	if (id != "0") {
		$.cookie(COOKIE_NAME, id, { expires: 30, path: '/' });
		$.fn.colorbox.close();
		window.location.reload();
	} else {
		alert("Favor de seleccionar un estado, es necesario para darte un mejor servicio.");
	}
}

/*
 * Funcionalidad para la cookie de ubicacion.
 */
var COOKIE_NAME = "estadoActual";
$(document).ready(function(){
	var idDestino  = $.cookie(COOKIE_NAME);
	if (idDestino!=null)
		$("#estado").val(idDestino);

	$('#estado').change(function(){
		var id = $(this).val();
		if (id != "0"){
			$.cookie(COOKIE_NAME, id, { expires: 30, path: '/' });
		     document.location.reload(); 
		}
	});
});

/*
 * Funcionalidad para tooltip del footer
 */
var idActual = "";
 function cerrar(){
 $("#"+idActual).btOff();
 }
$(document).ready(function(){
  var hideDelayTimer = null;
 $('.trigger').each(function (){

 $("#"+this.id).bt(
  { contentSelector: "$('#links_'+this.id)",
    trigger: 'none',
    fill: '#CAD9EC',
    cornerRadius: 10,
    strokeWidth: 0,
    shadow: true,
    shadowOffsetX: 3,
    shadowOffsetY: 3,
    shadowBlur: 8,
    shrinkToFit: true,
    shadowColor: 'rgba(0,0,0,.9)',
    shadowOverlap: false,
	clickAnywhereToClose: true,               
    closeWhenOthersOpen: true,              
    noShadowOpts: {strokeStyle: '#999', strokeWidth: 2},
    positions: ['top']
  });
 
 
 
  $("#"+this.id).mouseover(function(){ $('#'+this.id).btOn(); });
  $("#"+this.id).mouseout(function(){ 
   idActual = this.id;
   if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrar()',500);
    $(".bt-content").mouseover(function (){if (hideDelayTimer) clearTimeout(hideDelayTimer);});
 	$(".bt-content").mouseout(function (){
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrar()',500); });
    });
    
  });

  });



$(document).ready(function(){
	var text = 'Cambia el Estado para ver informaci&oacute;n local.';
	var cookieValue = $.cookie(COOKIE_NAME);
	if (cookieValue == null) {
		text = 'Si cambia el estado seleccionado cambiar&aacute; el contenido de la busqueda.'; 
	}


 $("select#estado").bt(text,
  { 
    trigger: 'hover',
    fill: '#CAD9EC',
    cornerRadius: 10,
    strokeWidth: 0,
    shadow: true,
    shadowOffsetX: 3,
    shadowOffsetY: 3,
    shadowBlur: 8,
    shadowColor: 'rgba(0,0,0,.9)',
    shadowOverlap: false,
	clickAnywhereToClose: true,               
    closeWhenOthersOpen: true,              
    noShadowOpts: {strokeStyle: '#999', strokeWidth: 2}
  });
 });
 
 function cerrarTrav(){
 $("#traveling").btOff();
 }
 
 function cerrarBt(selector){
 $(selector).btOff();
 }
 $(document).ready(function(){

 $("#traveling").bt(
  { contentSelector: "$('#links_traveling')",
    trigger: 'none',
    fill: '#0B4DA2',
    cornerRadius: 10,
    strokeWidth: 0,
    shadow: true,
    shadowOffsetX: 3,
    shadowOffsetY: 3,
    shadowBlur: 8,
    shrinkToFit: true,
    shadowColor: 'rgba(0,0,0,.9)',
    shadowOverlap: false,
	clickAnywhereToClose: true,               
    closeWhenOthersOpen: true,       
    positions: ['bottom'],       
    noShadowOpts: {strokeStyle: '#999', strokeWidth: 2}
  });
  
 
  
   var hideDelayTimer = null;
    $("#traveling").mouseover(function(){ $('#traveling').btOn(); });
  $("#traveling").mouseout(function(){ 
   if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrarTrav()',500);
    $(".bt-content").mouseover(function (){if (hideDelayTimer) clearTimeout(hideDelayTimer);});
 	$(".bt-content").mouseout(function (){
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrarTrav()',500); });
    });
 });
 /************************************************/
 
 $(document).ready(function(){

 $("#contact").bt(
  { contentSelector: "$('#links_contact')",
    trigger: 'none',
    fill: '#0B4DA2',
    cornerRadius: 10,
    strokeWidth: 0,
    shadow: true,
    shadowOffsetX: 3,
    shadowOffsetY: 3,
    shadowBlur: 8,
    shrinkToFit: true,
    shadowColor: 'rgba(0,0,0,.9)',
    shadowOverlap: false,
	clickAnywhereToClose: true,               
    closeWhenOthersOpen: true,       
    positions: ['bottom'],       
    noShadowOpts: {strokeStyle: '#999', strokeWidth: 2}
  });
  
 
 
   var hideDelayTimer = null;
    $("#contact").mouseover(function(){ $('#contact').btOn(); });
  $("#contact").mouseout(function(){ 
   if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrarBt("#contact")',500);
    $(".bt-content").mouseover(function (){if (hideDelayTimer) clearTimeout(hideDelayTimer);});
 	$(".bt-content").mouseout(function (){
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer =  setTimeout('cerrarBt("#contact")',500); });
    });
 });
 /***********************************************************/
 
 
 
  
 /*
  * Funcionalidad lo de ciudades fronterizas.
  */
 $(document).ready(function(){
	modalTF();
 });
 
function modalTF(){
 	 $(".tarifasFrontera").bind("click", function(){
			$.fn.colorbox({
				href: appContext + '/tarifas_frontera.html?ajax', 
				open:true,
				height: 360,
				width: 575,
				onClosed:function(){ }
			});
			return false;
	 });
}
 
 var open_celular = function(idUnico){
	 window.location = appContext + '/verEquipo/' + idUnico; 
 }