// JavaScript Document
var Utf8 = {
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
}
}
function CargarCiudad( nombre,temperatura,tempalta,tempbaja,icono) {
    this.nombre=nombre;
    this.temperatura=temperatura;
    this.tempalta=tempalta;
    this.tempbaja=tempbaja;
    this.icono=icono;
}
function preloadImages(){
    if(document.images){
        if(!document.imageArray) document.imageArray = new Array();
        var i,j = document.imageArray.length, args = arguments;
        for(i=0; i<args.length; i++){
            if (args[i].indexOf("#")!=0){
                document.imageArray[j] = new Image;
                document.imageArray[j++].src = args[i];
            }
        }
        }
}
function LoadCarrouselFotosDia()
{
    $(".carrousel_fotos_dia").carouFredSel({
        items: 2, 
        circular:true, 
        auto: false
    });
    $(".prev_fotos_dia").click(function() {
        $(".carrousel_fotos_dia").trigger("prev", 1);
    });
    $(".next_fotos_dia").click(function() {
        $(".carrousel_fotos_dia").trigger("next", 1);
    });
}
function seleccionarTab(nrotab)
{
    //deselecciono todos los tabs y oculto todos los divs
    for (i=1;i<4;i++){
        if (i!=nrotab){
            $("#li_"+i).removeClass("seleccionado");
            $("#div_"+i).hide();
        }
    }
//selecciono el tab y muestto el div
$("#li_"+nrotab).addClass("seleccionado");
$("#div_"+nrotab).show();
}
function ObtenerClima()
{
    $.ajax({
        type: 'GET', 
        url: '/xml/clima.xml', 
        cache: false,
        dataType: ($.browser.msie) ? 'text' : 'xml', 
        success: function(data){
            var xml;
            if(typeof data == 'string'){
                xml = new	ActiveXObject('Microsoft.XMLDOM');
                xml.async = false;
                xml.loadXML(data);
            } else {
                xml = data;
            }
            var i =0;
            var ciudadaleatoria = Math.floor(Math.random()*$(xml).find('Ciudad').length);
            $(xml).find('Ciudad').each(function(){
                if (i==ciudadaleatoria)
                {
                    var id = $(this).attr('Id');
                    var image = $(this).find('Imagen').text();
                    var TemperaturaAlta = $(this).find('TemperaturaAlta').text();
                    var TemperaturaBaja = $(this).find('TemperaturaBaja').text();
                    var Temperatura = $(this).find('Temperatura').text();
                    var Humedad = $(this).find('Humedad').text();
                    var Nombre = $(this).find('Nombre').text();
                    var iconoclima = '<img src="'+image+'" id="ImgClima" alt="" />&nbsp;';
                    $('#Ciudad').html(Nombre+"&nbsp;");	
                    $('#IconoClima').html(iconoclima);
                    $('#Temperatura').html("T&nbsp;"+Temperatura+"&nbsp;-&nbsp;H.&nbsp;"+Humedad);
                    $('#Temperatura_alta').html(TemperaturaAlta);
                    $('#Temperatura_baja').html(TemperaturaBaja);	
                    return false;
                }
                i++;
            });			
        }
    });	
    return true;	
}

/**
 * Metodo para realizar uan busqueda
 */
function search( strSearch ){

    var resultStringSearch = "/busqueda/" + encodeURI( strSearch );

    location.href = resultStringSearch;

}

/**
 * abre una ventana de dialogo con twiter
 * @titulo <string>
 * @newsLink <string>
 */
function twitterDialog( titulo, newsLink ){

    var windowWidth = 450;
    var leftPostion = Math.round($(window).width() / 2) - Math.round(windowWidth / 2);

    window.open("http://twitter.com/home?status=" + titulo + " + " + newsLink, "","status=0, width="+windowWidth+"px, height=250px, left="+leftPostion+"px, top=200px" );
}

/**
 * abre una ventana de dialogo con facebook
 * @link <string>
 */
function facebookDialog( link, title )
{
    var windowWidth = 600;
    var leftPostion = Math.round($(window).width() / 2) - Math.round(windowWidth / 2);
    
    var facebookUrl = "http://www.facebook.com/sharer.php?u=" + encodeURI(link);
    
    if (typeof(title) != 'undefined' && title != '')
    {
        facebookUrl += '&t=' + encodeURI(title);
    }

    window.open(facebookUrl, "","status=0, width="+windowWidth+"px, height=250px, left="+leftPostion+"px, top=200px" );
}

function openWindow( strUrl ){
    window.open(strUrl, "myWindow", "");
}

// Ejecuta funcionalidad al cargar el documento
jQuery(document).ready(function(){

    // Simula el posicionamiento vertical de css
    jQuery('.vertical-center').each(function(index, element) {
        jQuery(element).css('position', 'relative');
        jQuery(element).css('margin-top', ((jQuery(element).parent().height() - jQuery(element).height()) / 2) + 'px');
    });

});

/*
 * Convierto la fecha desde el formato yyyy-mm-dd a dd-mm-yyyy
 * 
 */
function convertDateEngToLat( stringDate ){
    
    var newDate = "";
    var arrayDate = new Array();
    
    if( stringDate != ''){
        
        arrayDate = stringDate.split("-");

        newDate = arrayDate[2] + "-" + arrayDate[1] + "-" + arrayDate[0];
    }
    
    return newDate;
}
