/* 
 * Funciones javascript
 */

var f5 = true;

$(document).ready(function(){
    twitter_list_updater_start();
   
    // Abrir la div del clima

    $('#tabs').tabs();
    $('#tabs-multimedia').tabs();


    $("#login-btn img").click(function(){
        $("#login-btn").find(".login-box").show();
        $("#login-btn .login-box-password").hide();
        $('#txtEmail').focus();
        
    });
    $("#login-btn span.close").click(function(){
        $("#login-btn .login-box").hide();
        $("#login-btn .login-box-password").hide();
        $("#errorOlvido").hide();
        $("#okOlvido").hide();
        $("#txtEmailOlvido").val('');
    });
    $("#login-btn .recuperar_pass").click(function(){
        $("#login-btn .login-box").hide();
        $("#login-btn .login-box-password").show();
    });

    $("#btnAgregar").click(addHashtag);
    $(".btnEliminar").click(deleteHashtag);

    $("#link_top").click(scroll_top);

    _initInput("txtHashTag", "Añadir un tag...");
    
    $('#txtHashTag').live('keypress', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) {
            addHashtag();
        }

    });


}); //close doc ready

function scroll_top(){
    $('html, body').animate({
        scrollTop: 0
    }, 'slow' );
}

function callback(hash)
{
    var str_hash = String(hash);
    if (str_hash.length > 0) {
        f5 = false;
        recargar_por_ajax(str_hash);
    } else {
        if (!f5) {
            window.location.href = window.location.href;
        }
    }
}


// CONTROLADOR DEL TIEMPO PARA HACER EL REFRESH
var objRefreshController = null;
function refresh_controller_init()
{
    var int_timeout = 0;
    int_timeout += 10 * (60); // minutos
    int_timeout += 15; // segundos
    int_timeout *= 1000; // convertir a milisegundos
    objRefreshController = new Object();
    objRefreshController.id = setTimeout(function() {
        window.location.href = WEB_PATH + url_seccion;
    } , int_timeout);
}
function refresh_controller_restart()
{
    if (objRefreshController != null) {
        refresh_controller_kill();
    }
    refresh_controller_init();
}
function refresh_controller_kill()
{
    clearInterval(objRefreshController.id);
    objRefreshController.id = 0;
}


function mostrarCargando(){
    var width = $(document).width();
    var height = $(document).height();

    $('#loaderOverlay').css({
        'height' : height,
        'width' : width
    });
    $('#loaderOverlay').fadeIn();
    $('#SiteLoader').fadeIn();
}

function ocultarCargando(){
    $('#loaderOverlay').hide();
    $('#SiteLoader').hide();
}

// Funciones para Layer
function mostrarLayer() {
    document.getElementById('layerGAM').style.visibility = 'visible';
}

function ocultarLayer() {
    document.getElementById('layerGAM').style.visibility = 'hidden';
}

function atrasarLayer() {

    document.getElementById('layerGAM').style.zIndex = -1;

    document.getElementById('layerGAM').style.padding = 0;

}

/**
 * FUNCTIONS
 */
$(function(){
    if(typeof String.prototype.trim !== 'function') {
        String.prototype.trim = function() {
            return this.replace(/^\s+|\s+$/g, '');
        }
    }
});


function is_email(str)
{
    var filtro = /^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_.-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
    if (filtro.test(str)) return true;
    else return false;
}

function serialize(arr)
{
    var str = 'a:'+arr.length+':{';
    for(i=0; i<arr.length; i++)
    {
        str += 'i:'+i+';s:'+arr[i].length+':"'+arr[i]+'";';
    }
    str += '}';
    return str;
}

function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\0/g,'\0');
    str=str.replace(/\\\\/g,'\\');
    return str;
}

function delete_cookie( name, path, domain ){
    document.cookie = name + "=" +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

// Inicializar un input con un texto predeterminado
function _initInput(input_id, text_ini)
{
    input = $("#"+input_id);
    input.val(text_ini);
    input.attr("text_ini", text_ini);
    input.focus(function() {
        if ($(this).val() == $(this).attr("text_ini")) $(this).val("")
    });
    input.blur(function() {
        if ($(this).val() == "") $(this).val($(this).attr("text_ini"));
    });
    input.getTexto = function(){
        return $(this).val();
    };
}

