﻿// Archivo JScript
var divSobre = true;
var divOtros = true;
var divIlustracion = true;
var divPersonal = true;
var divHipermercart = true;
var divCandela = true;
var divPira = true;

var divSobreHeight;
var divOtrosHeight;
var divIlustracionHeight;
var divPersonalHeight;
var divHipermercartHeight;
var divCandelaHeight;
var divPiraHeight;

var langCookie="bravo_lang";
var lang=null;

function setLangCookie(c_name, value,expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    
    var val = document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    
    document.cookie= val;
    
    var frm = document.getElementById('contentFrame');
    
    if(frm == null)
    {
        frm = parent.document.getElementById('contentFrame');
    }
    
    window.location = "default.aspx";
}

function getLangCookie(c_name)
{
    if(document.cookie.length >0)
    {
        c_start = document.cookie.indexOf(c_name + '=');
        
        if(c_start != -1)
        {
            c_start=c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(';', c_start);
            
            if(c_end==1)c_end=document.cookie.length;
            
            var result = unescape(document.cookie.substring(c_start));
            
            return result;
        }
    }
    
    return "";
}

function checkLangCookie()
{
    language = getLangCookie(langCookie);
    
    if(language != null && language != "")
    {
       
        lang = language;
    }   
    else
    {
        setLangCookie(langCookie,'es', 365);
        
        lang = 'es';
    }
    
    
}


function checkPass()
{

    var name = prompt("Password", "");
    
    if(name!='cadaques')
    {
        window.location = "default.aspx";
    }
}

function doNothing()
{
}

function loadPage(page)
{
    
    var frm = document.getElementById('contentFrame');
    
    if(frm == null)
    {
        frm = parent.document.getElementById('contentFrame');
    }
    
    switch(page.split('.')[0])
    {
        case "personal":
        {
            if(!divPersonal)
            {
                showDivPersonal();
                switch(page.split('=')[1].substr(0,2))
                {   
                    case "10":
                    {
                        showDivHipermercart();
                        break;
                    }
                    case "09":
                    {
                        showDivCandela();
                        break;
                    }
                    case "08":
                    {
                        showDivPira();
                        break;
                    }
                }
            }
            break;
        }
        case "ilustracion":
        {
            if(!divIlustracion)
            {
                showDivIlustracion();
            }
            break;
        }
        case "otros":
        {
            if(!divOtros)
            {
                showDivOtros();
            }
            break;
        }
    }
    
    
    for(i = 0; i<document.links.length; i++)
    {
        var link = String(document.links[i]);
        if(link.indexOf("\'")!= -1)
        {
            var link = link.split("\'")[1];
            if(link.indexOf("?id=")!= -1)
            {          
                if(page ==link)
                {
                    //alert("page: " + page + "\nlink: " + link);
                    document.links[i].style.background = "rgb(225,74,21)";
                    document.links[i].style.color="White";
                    
                    var text = document.links[i].innerHTML;
                    
                    var browser=navigator.appName;
                    var b_version=navigator.appVersion;
                    var version=parseFloat(b_version);
                    if (browser=="Microsoft Internet Explorer")
                    {                    
                        var newText = text.replace("<B class=NEW","<B class=RevNEW");
                    } 
                    else
                    {
                        var newText = text.replace("<b class=\"NEW\"","<b class=\"RevNEW\"");
                    }
                    
                    document.links[i].innerHTML = newText;   
                }
                else
                {
                    document.links[i].style.background = "White";
                    document.links[i].style.color="rgb(38,38,38)";
                    
                    var text = document.links[i].innerHTML;
                    
                    var browser=navigator.appName;
                    var b_version=navigator.appVersion;
                    var version=parseFloat(b_version);
                    if (browser=="Microsoft Internet Explorer")
                    {                    
                        var newText = text.replace("<B class=RevNEW","<B class=NEW");
                    } 
                    else
                    {
                        var newText = text.replace("<b class=\"RevNEW\"","<b class=\"NEW\"");
                    }
                    
                    document.links[i].innerHTML = newText; 
                }
            }
        }
    }
    
    
    if(startsWith(lang, "es")||startsWith(lang, "en"))
    {
        lang = lang.substring(0,2);
    }
    else
    {
        lang = "es";
    }
    var newUrl = page + "&lang=" + lang;

    frm.src = newUrl;
    
    
}

function loadNoticias(page)
{
    
    var frm = document.getElementById('contentFrame');
    
    if(frm == null)
    {
        frm = parent.document.getElementById('contentFrame');
    }
       
    
    frm.src =page;
        
}

function loadContacto(page)
{
    
    var frm = document.getElementById('contentFrame');
    
    if(frm == null)
    {
        frm = parent.document.getElementById('contentFrame');
    }
       
    
    frm.src =page;
        
}

function autoFitIFrame()
{
    var frm = document.getElementById('contentFrame');
    
    if(window.opera && !document.mimeType && document.all && document.getElementById)
    {
        frm.style.height=this.document.body.offsetHeight+"px";
    }
    else if(document.getElementById)
    {
        frm.style.width="900px";                
        frm.style.height = frm.contentWindow.document.body.scrollHeight + 60 + "px";
    }
}

//----------------------------------------------------------

function showDivSobre()
{
   
    var div=document.getElementById('divSobre');
    div.style.height = divSobreHeight + "px";
    div.style.visibility = "visible";
    divSobre = true;
    
    if(divPersonal)
    {
        hideDivPersonal();
    }
    if(divIlustracion)
    {
        hideDivIlustracion();
    }
    if(divOtros)
    {
        hideDivOtros();
    }
    
}

function hideDivSobre()
{
    if(divSobre)
    {
        var div=document.getElementById('divSobre');
        div.style.visibility = "hidden";
        div.style.height = 0;
        divSobre = false;
    }
}

function alterDivSobre()
{
    if(divSobre)
    {
        hideDivSobre();
    }
    else
    {
        showDivSobre();   
    }
}

function showDivOtros()
{
    
    
    var div=document.getElementById('divOtros');
    div.style.height = divOtrosHeight + "px";
    div.style.visibility = "visible";
    divOtros = true;
    
    if(divPersonal)
    {
        hideDivPersonal();
    }
    if(divIlustracion)
    {
        hideDivIlustracion();
    }
    
    if(divSobre)
    {
        hideDivSobre();
    }
        
}

function hideDivOtros()
{

    var div=document.getElementById('divOtros');
    div.style.visibility = "hidden";
    div.style.height = 0;
    divOtros = false;
}

function alterDivOtros()
{
    if(divOtros)
    {
        hideDivOtros();
    }
    else
    {
        showDivOtros();   
    }
}

function showDivIlustracion()
{
    var div=document.getElementById('divIlustracion');
    div.style.height = divIlustracionHeight + "px";
    div.style.visibility = "visible";
    divIlustracion = true;   
    
    if(divPersonal)
    {
        hideDivPersonal();
    }
    if(divOtros)
    {
        hideDivOtros();
    }
    if(divSobre)
    {
        hideDivSobre();
    }
}

function hideDivIlustracion()
{
    var div=document.getElementById('divIlustracion');
    div.style.visibility = "hidden";
    div.style.height = 0;
    divIlustracion = false;
}

function alterDivIlustracion()
{
    if(divIlustracion)
    {
        hideDivIlustracion();
    }
    else
    {
        showDivIlustracion();   
    }
}

function showDivPersonal()
{    

    var div=document.getElementById('divPersonal');
    div.style.height = divPersonalHeight + "px";
    div.style.visibility = "visible";
    divPersonal = true; 
    
    
    if(divIlustracion)
    {
        hideDivIlustracion();
    }
    if(divOtros)
    {
        hideDivOtros();
    }
    if(divSobre)
    {
        hideDivSobre();
    }
}

function hideDivPersonal()
{
    if(divHipermercart)
    {
        hideDivHipermercart();
    }
    if(divCandela)
    {
        hideDivCandela();
    }
    if(divPira)
    {
        hideDivPira();
    }
    var div=document.getElementById('divPersonal');
    div.style.visibility = "hidden";
    div.style.height = 0;
    divPersonal = false;
}

function alterDivPersonal()
{
    if(divPersonal)
    {
        hideDivPersonal();
    }
    else
    {
        showDivPersonal();   
    }
}

function showDivHipermercart()
{    

    if(divCandela)
    {
        hideDivCandela();
    }
    
    if(divPira)
    {
        hideDivPira();
    }
    
    
    var div=document.getElementById('divHipermercart');
    
    
    var newPersonalHeight = Number(document.getElementById('divPersonal').offsetHeight) + divHipermercartHeight;
    document.getElementById('divPersonal').style.height = newPersonalHeight + "px";
    
    div.style.height = divHipermercartHeight + "px";
    div.style.visibility = "visible";
    divHipermercart = true;      
}

function hideDivHipermercart()
{
    var div=document.getElementById('divHipermercart');
    
    if(divPersonalHeight)
    {
        document.getElementById('divPersonal').style.height = divPersonalHeight + "px";
    }
    
    div.style.visibility = "hidden";
    
    div.style.height = 0;
    divHipermercart = false;    
}

function alterDivHipermercart()
{
    if(divHipermercart)
    {
        hideDivHipermercart();
    }
    else
    {
        showDivHipermercart();   
    }
}

function showDivCandela()
{
    
    if(divHipermercart)
    {
        hideDivHipermercart();
    }
    if(divPira)
    {
        hideDivPira();
    }
    
    var div=document.getElementById('divCandela');
    
    var newPersonalHeight = Number(document.getElementById('divPersonal').offsetHeight) + divCandelaHeight;
    document.getElementById('divPersonal').style.height = newPersonalHeight + "px";
    
    div.style.height = divCandelaHeight + "px";
    div.style.visibility = "visible";
    divCandela = true;
}

function hideDivCandela()
{
    var div=document.getElementById('divCandela');
    
    if(divPersonalHeight)
    {
        document.getElementById('divPersonal').style.height = divPersonalHeight + "px";
    }
    
    div.style.visibility = "hidden";
    
    div.style.height = 0;
    divCandela = false;
}

function alterDivCandela()
{
    if(divCandela)
    {
        hideDivCandela();
    }
    else
    {
        showDivCandela();   
    }
}

function showDivPira()
{

    if(divCandela)
    {
        hideDivCandela();
    }
    if(divHipermercart)
    {
        hideDivHipermercart();
    }
    
    var div=document.getElementById('divPira');
    
    var newPersonalHeight = Number(document.getElementById('divPersonal').offsetHeight) + divPiraHeight;
    document.getElementById('divPersonal').style.height = newPersonalHeight + "px";
    
    div.style.height = divPiraHeight + "px";
    div.style.visibility = "visible";
    divPira = true;
}

function hideDivPira()
{
    var div=document.getElementById('divPira');
    
    if(divPersonalHeight)
    {
        document.getElementById('divPersonal').style.height = divPersonalHeight + "px";
    }
    
    div.style.visibility = "hidden";
    
    div.style.height = 0;
    divPira = false;
}

function alterDivPira()
{
    if(divPira)
    {
        hideDivPira();
    }
    else
    {
        showDivPira();   
    }
}

function showAll()
{
    showDivSobre();
    ShowDivOtros();
    showDivIlustracion();
    showDivPersonal();
    showDivHipermercart();
    showDivCandela();
    showDivPira();
}

function hideAll()
{
    divSobreHeight = document.getElementById('divSobre').offsetHeight;
    hideDivSobre();
    
    divOtrosHeight = document.getElementById('divOtros').offsetHeight;
    hideDivOtros();
    
    divIlustracionHeight = document.getElementById('divIlustracion').offsetHeight;
    hideDivIlustracion();
    
    divHipermercartHeight = document.getElementById('divHipermercart').offsetHeight;
    hideDivHipermercart();
    
    divCandelaHeight = document.getElementById('divCandela').offsetHeight;
    hideDivCandela();
    
    divPiraHeight = document.getElementById('divPira').offsetHeight;
    hideDivPira();
    
   
    divPersonalHeight = document.getElementById('divPersonal').offsetHeight;
    hideDivPersonal();
   
}

function hideAllNews()
{
    hideDivSobre();
    
    hideDivOtros();
    
    hideDivIlustracion();
    
    hideDivHipermercart();
    
    hideDivCandela();
    
    hideDivPira();
    
    hideDivPersonal();
   
}

function hideAllContacto()
{
    hideDivSobre();
    
    hideDivOtros();
    
    hideDivIlustracion();
    
    hideDivHipermercart();
    
    hideDivCandela();
    
    hideDivPira();
    
    hideDivPersonal();
   
}



//---------------

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() 
{
    return document.documentElement.clientHeight;
}

function pageSize()
{
    var width = pageWidth() ;
    var height = pageHeight();
    alert(pageHeight());
    alert(document.documentElement.clientHeight);
}

function setFrameSize()
{
    var frm = document.getElementById('contentFrame');
    var nav = document.getElementById('navigation');
    
    frm.style.visibility="hidden";
    
    var w = (Number(pageWidth()) - 300) + "px";
    var h = (Number(pageHeight())-40) + "px";
    
    
    frm.style.width =  w;
    frm.style.height = h;
    
    frm.style.visibility = "visible"; 
}

function getRedirection()
{
    var qs = new Querystring();
    if((qs.contains('url'))&&(qs.contains('id')))
    {
        var newPage= qs.get('url') + ".aspx?id=" + qs.get('id');
        loadPage(newPage);
    }
}


/*
    Copyright (c) 2006 Lawrence Oluyede <l.oluyede@gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal 
    in the Software without restriction, including without limitation the rights 
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
    copies of the Software, and to permit persons to whom the Software is 
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all 
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
    SOFTWARE.
*/

/*
 startsWith(str, prefix[, start[, end]]) -> bool

 Return true if str ends with the specified prefix, false otherwise.
 With optional start, test str beginning at that position.
 With optional end, stop comparing str at that position.
 prefix can also be an array of strings to try.
*/
function startsWith(str, prefix, start, end) {
    if (arguments.length < 2) {
        throw new TypeError('startsWith() requires at least 2 arguments');
    }
        
    // check if start and end are null/undefined or a 'number'
    if ((start == null) || (isNaN(new Number(start)))) {
        start = 0;
    }
    if ((end == null) || (isNaN(new Number(end)))) {
        end = Number.MAX_VALUE;
    }
    
    // if it's an array
    if (typeof prefix == "object") {
        for (var i = 0, j = prefix.length; i < j; i++) {
            var res = _stringTailMatch(str, prefix[i], start, end, true);
            if (res) {
                return true;
            }
        }
        return false;
    }
    
    return _stringTailMatch(str, prefix, start, end, true);
}

/*
 endsWith(str, suffix[, start[, end]]) -> bool

 Return true if str ends with the specified suffix, false otherwise.
 With optional start, test str beginning at that position.
 With optional end, stop comparing str at that position.
 suffix can also be an array of strings to try.
*/
function endsWith(str, suffix, start, end) {
    if (arguments.length < 2) {
        throw new TypeError('endsWith() requires at least 2 arguments');
    }
    
    // check if start and end are null/undefined or a 'number'
    if ((start == null) || (isNaN(new Number(start)))) {
        start = 0;
    }
    if ((end == null) || (isNaN(new Number(end)))) {
        end = Number.MAX_VALUE;
    }
    
    // if it's an array
    if (typeof suffix == "object") {
        for (var i = 0, j = suffix.length; i < j; i++) {
            var res = _stringTailMatch(str, suffix[i], start, end, false);
            if (res) {
                return true;
            }
        }
        return false;
    }
    
    return _stringTailMatch(str, suffix, start, end, false);
}

/*
 Matches the end (direction == false) or start (direction == true) of str
 against substr, using the start and end arguments. Returns false
 if not found and true if found.
*/
function _stringTailMatch(str, substr, start, end, fromStart) {
    var len = str.length;
    var slen = substr.length;
    
    var indices = _adjustIndices(start, end, len);
    start = indices[0]; end = indices[1]; len = indices[2];
    
    if (fromStart) {
        if (start + slen > len) {
            return false;
        }
    } else {
        if (end - start < slen || start > len) {
            return false;
        }
        if (end - slen > start) {
            start = end - slen;
        }
    }
    
    if (end - start >= slen) {
        return str.substr(start, slen) == substr;
    }
    return false;
}

function _adjustIndices(start, end, len)
{
	if (end > len) {
	    end = len;
	} else if (end < 0) {
	    end += len;
	}
    
    if (end < 0) {
        end = 0;
    }
	if (start < 0) {
	    start += len;   
	}
	if (start < 0) {
		start = 0;
	}
	
	return [start, end, len];
}
