//Plugins

/*
 * jqModal - Minimalist Modaling with jQuery
 *   (http://dev.iceburg.net/jquery/jqModal/)
 *
 * Copyright (c) 2007,2008 Brice Burgess <bhb@iceburg.net>
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * $Version: 03/01/2009 +r14
 */

(function($) {
$.fn.jqm=function(o){
var p={
overlay: 50,
overlayClass: 'jqmOverlay',
closeClass: 'jqmClose',
trigger: '.jqModal',
ajax: F,
ajaxText: '',
target: F,
modal: F,
toTop: F,
onShow: F,
onHide: F,
onLoad: F
};
return this.each(function(){if(this._jqm)return H[this._jqm].c=$.extend({},H[this._jqm].c,o);s++;this._jqm=s;
H[s]={c:$.extend(p,$.jqm.params,o),a:F,w:$(this).addClass('jqmID'+s),s:s};
if(p.trigger)$(this).jqmAddTrigger(p.trigger);
});};

$.fn.jqmAddClose=function(e){return hs(this,e,'jqmHide');};
$.fn.jqmAddTrigger=function(e){return hs(this,e,'jqmShow');};
$.fn.jqmShow=function(t){return this.each(function(){t=t||window.event;$.jqm.open(this._jqm,t);});};
$.fn.jqmHide=function(t){return this.each(function(){t=t||window.event;$.jqm.close(this._jqm,t)});};

$.jqm = {
hash:{},
open:function(s,t){var h=H[s],c=h.c,cc='.'+c.closeClass,z=(parseInt(h.w.css('z-index'))),z=(z>0)?z:3000,o=$('<div></div>').css({height:'100%',width:'100%',position:'fixed',left:0,top:0,'z-index':z-1,opacity:c.overlay/100});if(h.a)return F;h.t=t;h.a=true;h.w.css('z-index',z);
 if(c.modal) {if(!A[0])L('bind');A.push(s);}
 else if(c.overlay > 0)h.w.jqmAddClose(o);
 else o=F;

 h.o=(o)?o.addClass(c.overlayClass).prependTo('body'):F;
 if(ie6){$('html,body').css({height:'100%',width:'100%'});if(o){o=o.css({position:'absolute'})[0];for(var y in {Top:1,Left:1})o.style.setExpression(y.toLowerCase(),"(_=(document.documentElement.scroll"+y+" || document.body.scroll"+y+"))+'px'");}}

 if(c.ajax) {var r=c.target||h.w,u=c.ajax,r=(typeof r == 'string')?$(r,h.w):$(r),u=(u.substr(0,1) == '@')?$(t).attr(u.substring(1)):u;
  r.html(c.ajaxText).load(u,function(){if(c.onLoad)c.onLoad.call(this,h);if(cc)h.w.jqmAddClose($(cc,h.w));e(h);});}
 else if(cc)h.w.jqmAddClose($(cc,h.w));

 if(c.toTop&&h.o)h.w.before('<span id="jqmP'+h.w[0]._jqm+'"></span>').insertAfter(h.o);	
 (c.onShow)?c.onShow(h):h.w.show();e(h);return F;
},
close:function(s){var h=H[s];if(!h.a)return F;h.a=F;
 if(A[0]){A.pop();if(!A[0])L('unbind');}
 if(h.c.toTop&&h.o)$('#jqmP'+h.w[0]._jqm).after(h.w).remove();
 if(h.c.onHide)h.c.onHide(h);else{h.w.hide();if(h.o)h.o.remove();} return F;
},
params:{}};
var s=0,H=$.jqm.hash,A=[],ie6=$.browser.msie&&($.browser.version == "6.0"),F=false,
i=$('<iframe src="javascript:false;document.write(\'\');" class="jqm"></iframe>').css({opacity:0}),
e=function(h){if(ie6)if(h.o)h.o.html('<p style="width:100%;height:100%"/>').prepend(i);else if(!$('iframe.jqm',h.w)[0])h.w.prepend(i); f(h);},
f=function(h){try{$(':input:visible',h.w)[0].focus();}catch(_){}},
L=function(t){$()[t]("keypress",m)[t]("keydown",m)[t]("mousedown",m);},
m=function(e){var h=H[A[A.length-1]],r=(!$(e.target).parents('.jqmID'+h.s)[0]);if(r)f(h);return !r;},
hs=function(w,t,c){return w.each(function(){var s=this._jqm;$(t).each(function() {
 if(!this[c]){this[c]=[];$(this).click(function(){for(var i in {jqmShow:1,jqmHide:1})for(var s in this[i])if(H[this[i][s]])H[this[i][s]].w[i](this);return F;});}this[c].push(s);});});};
})(jQuery);


 //Funciones al cargar la pagina
 $(document).ready(function(){

	// find all the input elements with title attributes
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();
	
	// Mascara de la Contrase�a en logon
	$("#logon_pw_pantalla").focus(function (){
	$("#logon_pw_pantalla").addClass("hidden_elem");
	$("#logon_pw").removeClass("hidden_elem");
	$("#logon_pw").focus();
	
	});
 /*autocomplete*/
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	$("#search").autocomplete(dp+'incs/sug'+leng_post+'.asp',{
		selectFirst:false,
		minChars:2,
		delay:10,
		width:150,
		max: 20,
		matchSubset:false,
		formatResult: function(data,pos) {
			return ' ';
		}
	});

	$("#search").result(function(event, data, formatted) {
		if (data)
			location.href = dp+data[1];
	});

	$("#search").focus();
	//Nos cuidamos del scroll en ventanas
	$('#guindou').bind('dialogopen', function(event, ui) {
		if ($('.slidechow').length>0){
			$('.slidechow').cycle('pause');
		}
	});
	$('#guindou').bind('dialogclose', function(event, ui) {
		if ($('.slidechow').length>0){
			$('.slidechow').cycle('resume');
		}
	});
	$('#dialog').bind('dialogopen', function(event, ui) {
		if ($('.slidechow').length>0){
			$('.slidechow').cycle('pause');
		}
	});
	$('#dialog').bind('dialogclose', function(event, ui) {
		if ($('.slidechow').length>0){
			$('.slidechow').cycle('resume');
		}
	});
 });
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function arr_busca (arreglo,que){//busqueda en arreglos
	var bnd = -1;
	for(var i = 0;i<arreglo.length;i++){
		if (arreglo[i] == que)
		{
			bnd = i;
		}
	}
	return bnd;
}
function arr_busca2 (arreglo,que){//busqueda en arreglo de arreglos
	var bnd = -1;
	for(var i = 0;i<arreglo.length;i++){
		if (arreglo[i] [0]== que)
		{
			bnd = i;
		}
	}
	return bnd;
}
function valida (que){
	var regreso = que.replace(/^\s*|\s*$/g,"");
	return regreso.length
}
//creamos instancia
var arr_cola = new Array()
//carga de contenido
function load_div(url,donde){
	$("#"+donde).html("<div align=\"center\" style=\"vertical-align:center;\"><img src=\"/dl/imgs/ajax-loader.gif\"></div>");
	$.get(url, function(data){
		$("#"+donde).html(data);
	});
}

function URLEncode(texto)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = texto;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(texto)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = texto;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}


/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
 if (!blurClass) blurClass = 'blur';

 return this.each(function () {
 var $input = $(this),
 title = $input.attr('title'),
 $form = $(this.form),
 nombre = $input.attr('id'),
 $win = $(window);

 function remove() {
 if (this.value === title && $input.hasClass(blurClass)) {
 $input.val('').removeClass(blurClass);
 }
 }

 // only apply logic if the element has the attribute
 if (title && (Left(nombre,6)=='logon_') || (Left(nombre,6)=='cuerpo')) {
 // on blur, set value to title attr if text is blank
 $input.blur(function () {
 if (this.value === '') {
 $input.val(title).addClass(blurClass);
 }
 }).focus(remove).blur(); // now change all inputs to title

 // clear the pre-defined text when form is submitted
 $form.submit(remove);
 $win.unload(remove); // handles Firefox's autocomplete
 }
 });
};

})(jQuery);

/*Funcion para limitar las entradas en campos largos*/
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}



/*
 * Tooltip script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 


this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



// starting the script on page load
$(document).ready(function(){
	tooltip();
});



//Feedback
function feedback(familia,item,default_a,default_b){
 $.get(dp+"cmts/feedback.asp", { familia: familia, item: item, english:english, default_a:default_a, default_b:default_b } , function(data){
	pageTracker._trackEvent("Feedback", familia, item);
	$('#feedback_guindou').html(data);
	var titulo = "";
	var alto = 330;
	var ancho = 400;
	switch (familia){
	case "mapa":
		titulo=(english==1)?'Report Error in the Map':'Reportar Error en el Mapa';
		alto = 390;
		ancho = 550;		
		break;
	case "item":
		titulo=(english==1)?'Report Error in Listing':'Reportar Error de Negocio';
		alto = 390;
		ancho = 450;
		break;
	}
	$('#feedback_guindou').dialog({title:titulo,modal:true,width:ancho,height:alto,resizable:false,buttons:{"Ok": function(){ 
		send_feedback();}
		}});
	$('#feedback_guindou').dialog('open');
 });
}
function send_feedback(){
	var texto = $('#input').val();
	$('[id^="feedback_"]:checked').each(function(i){
		texto = this.value + ', ' + texto;
   }); 
 $.post(dp+'cmts/feedback_post.asp', { 
		familia:$('#familia').val(), 
		item: $('#item').val(), 
		calificacion: $('#calificacion').val(),
		input: texto,
		english:english } ,
			function(data){
			$('#feedback_guindou').dialog('close');
		 });
}
function logon_win(){
	$.post(dp+'incs/sign_in.asp',{english:english},function (data){
		$('#logon_guindou').html(data);
		$('#logon_guindou').dialog({title:(english==1)?'Sign In':'Inicio de Sesi&oacute;n',modal:true,width:300,height:170,resizable:false});
		$('#logon_guindou').dialog('open');
	});
}
var buffer = "";
function logon_act(){
	buffer = $('#boton_logon').html();
	$('#boton_logon').html('<img src='+dp+'\"imgs/ajax-loader.gif\" align=\"absmiddle\">&nbsp;'+((english==1)?'Sending..':'Enviando..'));
	$.post(dp+'cmts/logon.asp',{english:english,logon_email:$('#logon_email').val(),logon_pw:$('#logon_pw').val()},function (data){
		if (data!='ok'){
			$('#logon_guindou').html(data);
			$('#logon_guindou').dialog({title:'Error',modal:true,width:300,height:270,resizable:false, buttons: { "Ok": function() { $(this).dialog("close"); }}});
			$('#logon_guindou').dialog('open');
			$('#boton_logon').html(buffer);
		}else{
			$('#logon_guindou').dialog('close');
			window.location.href=window.location.href;
		}
	});
}
function logon_forgot_win(){
	$.post(dp+'incs/password_fgot.asp',{english:english},function (data){
		$('#guindou3').html(data);
		$('#guindou3').dialog({title:(english==1)?'Forgot Your Password':'Olvidaste tu Contrase&ntilde;a',modal:true,width:300,height:170,resizable:false});
		$('#guindou3').dialog('open');
	});
}
function logon_forgot_act(){
	$('#pwr_botonsend').html('<img src='+dp+'\"imgs/ajax-loader.gif\" align=\"absmiddle\">&nbsp;'+((english==1)?'Sending..':'Enviando..'));
	$.post(dp+'cmts/pwrecovery.asp',{english:english,email:$('#pwr_email').val(),ano_nacimiento:$('#pwr_ano_nacimiento').val()},function (data){
		if (data=='x'){
			$('#pwr_botonsend').html('Error !')
		}else{
			$('#guindou3').html(data);
		}
	});
}
//cambio de ciudad
function switch_ciudad_window(){
	if($('#city_switch_window').is(':hidden')){
		$.post(dp+'incs/switch_city.asp',{english:english},function (data){
			$('#city_switch_window').html(data);
			/*$('#city_switch_window').show('fast');*/
			$('#city_switch_window').dialog({title:(english==1)?'Change City':'Cambiar de Ciudad',position:['right','top'],modal:true,width:200,height:180,resizable:false, buttons: { "Ok":switch_ciudad}});
			$('#city_switch_window').dialog('open');
		});
	}
}
function switch_ciudad_window_close(){
	$('#city_switch_window').hide('fast');
}
function switch_ciudad(){
	if ($('#swciudad').val()!=null){
		var adonde = '/'+$('#swciudad').val()+'/'+ur.replace(/\.\.\//g,'');
		window.location = adonde;
	}
}