/**
 * @author SoyDigital.com digital@soydigital.com
 *
 * @version 1.0
 *
 * @copyleft 2008
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
window.onload = function(){
    init();
    setOptions();
    if ($("autocompleteProvincia") != undefined) {
		$("autocompleteProvincia").className = "autocomplete";
		$("autocompleteCiudad").className = "autocomplete";
		new Ajax.Autocompleter("EMPRegion", "autocompleteProvincia", "src/auto_complete.php", {
			paramName: "provincias"
		});
		new Ajax.Autocompleter("EMPCiudad", "autocompleteCiudad", "src/auto_complete.php", {
			paramName: "localidades"
		});
		if ($("EMPPais") != undefined) {
			$Ajax("src/paises.php", {
				onfinish: cargarPaises,
				tipoRespuesta: $tipo.JSON
			});
			/*$("EMPPais").onchange = function(){
				if (this.value == "54") {
					$("EMPPhoneError").className = "campo";
					$("EMPCIF_NIFError").className = "campo";
					//$("residente").className = "nivel1";
				}
				else {
					$("EMPPhoneError").className = "hidden";
					$("EMPCIF_NIFError").className = "hidden";
					//$("residente").className = "hidden";
				}
			}*/
		}		
	}
}

function ChooseTab(obj){
    var links = $('menu').getElementsBySelector('a');
    links.each(function(s, index){
        var id = s.readAttribute('id');
        if (id == obj) {
            id != "crear-cuenta" ? $(obj).addClassName("seleccionado") : "micuenta seleccionado";
            saveSettings(obj);
        }
        else {
            if (id != "crear-cuenta") {
                $(obj).addClassName("");
            }
            if (obj == "none") {
                $(obj).addClassName("");
                saveSettings("none");
            }
        }
    });
}

function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

function saveSettings(currentOpt){
    createCookie("marketing_selected_section", currentOpt);
}

function setOptions(){
    var DefaultOp = "inicio";
    var cookie = readCookie("marketing_selected_section") ? readCookie("marketing_selected_section") : DefaultOp;
    var LinkChoosed = $(cookie);
    LinkChoosed.className = "seleccionado";
}

function initRSH(){
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(handleHistoryChange);
    var initialLocation = dhtmlHistory.getCurrentLocation();
    if (initialLocation === null) {
        initialLocation = "1";
    }
    updateUI(initialLocation, null);
}

function handleHistoryChange(newLocation, historyData){
    updateUI(newLocation, historyData);
}

function updateUI(newLocation, historyData){
    var whichPage;
    var output = $("output");
    switch (newLocation) {
        case ("1"):
            whichPage = "inicio";
            break;
        case ("2"):
            whichPage = "que-es";
            break;
        case ("3"):
            whichPage = "como-funciona";
            break;
        case ("4"):
            whichPage = "costes";
            break;
        case ("5"):
            whichPage = "ventajas";
            break;
        case ("6"):
            whichPage = "crear-cuenta";
            break;
        case ("null"):
            whichPage = "none";
            break;
        default:
            whichPage = "inicio";
            break;
    }
    ChooseTab(whichPage);
}

function AddTextNode(){
    var account = "\x63\x6F\x6E\x74\x61\x63\x74\x6F";
    var arroba = "\x40";
    var domain = "\x6D\x61\x72\x6B\x65\x74\x69\x6E\x67\x2D\x6D\x6F\x76\x69\x6C\x2E\x65\x73";
    var e_mail = $("footer").getElementsBySelector("a");
    e_mail[0].setAttribute("href", "mailto:" + account + arroba + domain);
}

function cargarPaises(paises){
    var opcion = new Option("[Seleccione un País]", 0);
    $("EMPPais").options[$("EMPPais").options.length] = opcion;
    for (var i = 0; i < paises.length; i++) {
        var pais = paises[i];
        var opcion = new Option(pais.descripcion, pais.idpaises);
        try {
            $("EMPPais").options[$("EMPPais").options.length] = opcion;
        } 
        catch (e) {
            alert("Error interno");
        }
    }
}
function init(){
    if (arguments.callee.done) {
        return;
    }
    initRSH();
    AddTextNode();
}
