/*
 * si alguien copia y pega una ruta con un # y la pega en el navegador,
 * este código lo redirige a la página correspondiente
 */

if (window.location.hash.substring(1) != "") {
    var url_hash = String(window.location.href).substring(String(window.location.href).indexOf('#') + 1, String(window.location.href).length);

    if (url_hash.indexOf("oauth")==-1) {
        window.location.href = WEB_PATH + url_hash;
    }
    
}

function navegar_ajax(objeto_vista, hash_link, e) {

    var str = '';
    for(i in e) str += i+":"+e[i]+"\n";

    if (e.type == 'click' && (e.button == 0 || e.which == 1) && e.ctrlKey == false) {

        var url_base = String(window.location.href);
        if (window.location.hash.substring(1)) {
            url_base = url_base.substring(0, url_base.indexOf('#'));
        }
        window.location.href = url_base + "#" + hash_link;
        $.history.load(hash_link);
        return false;
    } else {
        window.open(WEB_PATH + hash_link);
        window.focus();
        return false;
    }
	
}

//function recargar_por_ajax(objeto_vista, hash_link) {
function recargar_por_ajax(hash_link) {
    scroll_top();
 

    var width = $(document).width();
    var height = $(document).height();

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

    var url = WEB_PATH + 'app/frontend/ajax/get_seccion.php';

    $.ajax({
        type: 'POST',
        url: url,
        data: {
            hash_link : hash_link
        },
        success : function(response){
            $("#divMainZonas").html(response.contenido);
            $("#banner").html(response.banner_cabezal);
            $("#menuIzquierdo").html(response.menu_izquierdo);
            $("#banner_izquierdo").html(response.banner_izquierdo);

            $("#hd").attr('class', 'header seccion_'+ response.css_class);
            $("#doc").attr('class', "seccion_" + response.css_class);
            $("#imgLogo").attr('class', "eologo seccion_" + response.css_class);

            var keywords = $("meta[name='keywords']");
            keywords.attr('content', response.meta_keywords);
            var title = $("meta[name='title']");
            var description = $("meta[name='description']");
            title.attr('content', response.meta_title);
            description.attr('content', response.meta_description);
            document.title = response.meta_title;
            $('#loaderOverlay').hide();
            $('#SiteLoader').hide();

            inicializar();
            
          
            
            // Tracking Google Analytics
            _gaq.push(['_trackPageview', WEB_PATH + hash_link]);
			
            // Tag certifica
            var certifica_value = response.seccion;
            certifica_value += "/index";
            cert_registerHit(CERTIFICA_ID, certifica_value);
			
            // Controlador de refresh
            url_seccion = response.seccion;
            refresh_controller_restart();

        },
        error: function(jqXHR, textStatus, errorThrown) {
            $('#loaderOverlay').hide();
            $('#SiteLoader').hide();
            if (jqXHR.status == 403) {
                window.location = WEB_PATH + "login/";
            }
        },
        dataType: "json"
    });
}

function inicializar()
{    
    $("#btnAgregar").click(addHashtag);

    _initInput("txtHashTag", "Añadir un tag...");
    
    //INIT DE FACEBOOK
    $('<div id="fb-root"></div>').appendTo($('body'));
    FB.init({
        appId  : FACEBOOK_APP_ID,
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
    });
}



