/**
 * @author r
 */
//inicialitzem variable global d'idoma
var langAct;

$(document).ready(function(){
    var formulari = document.getElementById("search");
    if (formulari.addEventListener) {
        formulari.addEventListener("keydown", busca, false);
    }
    else 
        if (formulari.attachEvent) {
            formulari.attachEvent("onkeydown", busca);
        }
})

//per a que funcioni el enter al buscador
function busca(evt){
    var elementActiu = document.activeElement.id
    var tecla = evt.which || evt.keyCode;
    if (tecla == 13) {
        paraula = document.getElementById("search").value;
		paraula = paraula.replace(" ", "_");
        $('#main_esq').load("../phps/buscador.php?paraula=" + paraula);
        document.getElementById("search").value = "Buscar";
    }
}

//funcio de navegacio principal
function seccions(div, pag){
    divdesti = "#" + div;
    $(divdesti).load(pag);
}

function validEmail(email){
    var invalidChars = " /:,;";
    for (var k = 0; k < invalidChars.length; k++) {
        var badChar = invalidChars.charAt(k);
        if (email.indexOf(badChar) > -1) {
            alert("Si us plau introdueixi una adreça de correu vàlida");
            return false;
        }
    }
    var atPos = email.indexOf("@", 1);
    if (atPos == -1) {
        alert("Si us plau introdueixi una adreça de correu vàlida");
        return false;
    }
    if (email.indexOf("@", atPos + 1) != -1) {
        alert("Si us plau introdueixi una adreça de correu vàlida");
        return false;
    }
    var periodPos = email.indexOf(".", atPos);
    if (periodPos == -1) {
        alert("Si us plau introdueixi una adreça de correu vàlida");
        return false;
    }
    if (periodPos + 3 > email.length) {
        alert("Si us plau introdueixi una adreça de correu vàlida");
        return false;
    }
}

function validForm(form){
    if (form.elements.nom.value == "") {
        alert("Si us plau, introdueixi un nom");
        return false;
    }
    if (form.elements.pais.value == "") {
        alert("Si us plau, introdueixi un país");
        return false;
    }
    if (form.elements.email.value == "") {
        alert("Si us plau, introdueixi una adreça de correu");
        return false;
    }
    if (form.elements.email.value != "" && !validEmail(form.elements.email.value)) {
        return false;
    }
    if (form.elements.assumpte.value == "") {
        alert("Si us plau, introdueixi un assumpte");
        return false;
    }
    if (form.elements.missatge.value == "") {
        alert("Si us plau, introdueixi un missatge");
        return false;
    }
    if (form.elements.tmptxt.value == "") {
        alert("Si us plau, introdueixi el codi");
        return false;
    }
    if (form.elements.tmptxt.value !== codi) {
        alert("Si us plau, introdueixi el codi correctament");
        return false;
    }
    return true;
}

function checkMenuSec(seccio){
    $('#menu_ppal a#btn_' + seccio + '').removeClass("normal").addClass("activat");
    /*$('#menu_ppal a#btn_'+seccio+'.activat').click(function(){
     return false;
     });*/
    $('#menu_ppal a:not(#btn_' + seccio + ')').removeClass("activat").addClass("normal");
    $('#submenu').load("../pags/submenu_" + seccio + "_cat.html");
}

/*
 Unfocus History (plugin per a historial navegador)
 */
function PromptMe(){
    // some application vars
    var stateVar = "nothin'", displayDiv = document.getElementById("main_esq");
    
    // the sole public method to manipulate this application
    this.promtForNew = function(){
        // do the stuff that changes the state
        var newVal = window.prompt("Please enter some value to store in the history and url");
        // Set the new history hash. This value must be a string, so serializtion is up to you.
        // JSON works nicely, if you need something quick: http://www.json.org
        // When a new entry is made, unFocus.History will notify the historyListener 
        // method of the change, even on the first call. For this demo app we can 
        // rely on the historyListener method to update the state, and view.
        unFocus.History.addHistory(newVal);
    };
    
    this.historyListener = function(historyHash){
        // update the stateVar
        stateVar = historyHash;
        // update display content
        if (historyHash) {
            if (historyHash == "contacte") {
                seccions('main_esq', "pags/contacte.php");
            } else if(historyHash.substr(0,5) == 'media'){
             idBolo =historyHash.substring(12);
             seccions('main_esq', "pags/fitxa.php?id=" + idBolo);
             } else  {
                seccions('main_esq', "pags/" + historyHash + ".html");
            } 
        }
        else {
            $('#main_esq').load("pags/que_es.html");
        }
        /* if (historyHash) {
         
         if (historyHash == "process_form") {
         seccions('stage', "php/" + historyHash + ".php");
         }
         else {
         seccions('stage', "php/" + historyHash + ".html");
         }
         }
         else {
         // seccions('stage', "php/home.html");
         initWeb('cat');
         }*/
    };
    // subscribe to unFocus.History
    unFocus.History.addEventListener('historyChange', this.historyListener);
    // Check for an initial value (deep link).
    // In this demo app, the historyListener can handle the task.
    this.historyListener(unFocus.History.getCurrent());
};

var MediaNav;

