/*
* JS Scripts imported in the Omip site
*
* Copyright 2010, Safira
* http://safira.pt
*/

function showHideContainer(tabs, selected, showContainer, hideContainer, saveCookie) {
    $(showContainer).removeClass('hide');
    $(hideContainer).addClass('hide');

    $(tabs).removeClass('selected');
    $(selected).addClass('selected');

    if (saveCookie) {
        saveTabIndex(tabs, selected, showContainer, hideContainer);
    }
}

function expandeLinha(linha, botao, linhas, botoes) {
    var is_ativo = ($(botao)).hasClass('activo');
    $(linhas).addClass('hide');
    $(botoes).removeClass('activo');

    if (!is_ativo) {
        $(linha).removeClass('hide');
        $(botao).addClass('activo');
    }
    else {
        $(linha).addClass('hide');
        $(botao).removeClass('activo');
    }
}

function saveTabIndex(tabs, selected, showContainer, hideContainer) {
    //Call JS function to save cookie name, tab text, 
    //and days before cookie should expire
    setCookie("nsOmipCookietabs", tabs, 1);
    setCookie("nsOmipCookieselected", selected, 1);
    setCookie("nsOmipCookieshowContainer", showContainer, 1);
    setCookie("nsOmipCookiehideContainer", hideContainer, 1);
}

//Cookie operation helper function 
//Save the value to a cookie and set expiration date 
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function isValidCookie(cookieValue) {
    return (cookieValue != "" || cookieValue != null);
}

function loadTabIndex() {
    //Get cookie value
    var tabs = getCookie("nsOmipCookietabs");
    var selected = getCookie("nsOmipCookieselected");
    var showContainer = getCookie("nsOmipCookieshowContainer");
    var hideContainer = getCookie("nsOmipCookiehideContainer");

    if (isValidCookie(tabs) && isValidCookie(selected) && isValidCookie(showContainer) && isValidCookie(hideContainer)) {
        showHideContainer(tabs, selected, showContainer, hideContainer, false);
    }
}

//Cookie helper function 
//Gets a cookie based on supplied name and returns value 
//as a string 
function getCookie(c_name) {
    try {
        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;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
    } catch (err) { }

    //If there is an error or no cookie 
    //return an empty string 
    return "";
}


var stack = new Array();

function Push(elem) {
    elem.style.left = "1250px";
    var index = stack.length;
    stack[index] = elem;
}

function Pop() {
    if (stack.length > 0) {
        var elem = stack[0];
        var temp = new Array();
        for (var i = 1; i < stack.length; i++)
            temp[i - 1] = stack[i];
        stack = temp;

        return elem;
    }
    return null;
}

function InitVariables() {
    var csv = document.getElementById('marqueeDivs').value;
    var divs = csv.split(',');
    stack.length = divs.length;
    for (var i = 0; i < divs.length; i++) {
        stack[i] = document.getElementById(divs[i]);
    }
}

function DoAnimation(scriptId) {
    var validScriptId = document.getElementById('validScriptId').value;
    var speed = 600000 / document.getElementById('tickerSpeed').value;
    var interval = (speed * 3000) / 17500;
    if (scriptId == validScriptId) {

        var elem = Pop();
        if (elem != null) {
            $(elem).animate({ "left": "-=1400px" }, speed, "linear", function() {
                Push(elem);
            });
        }

        var callFn = "DoAnimation('" + scriptId + "')";
        setTimeout(callFn, interval);
    }
}

function setLoginVisibility(divId, loginId) {
    try {
        var inputs = document.getElementsByTagName("input");
        var div = document.getElementById(divId);
        var login = null;

        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "hidden" && inputs[i].id.indexOf(loginId) >= 0) {
                login = inputs[i];
            }
        }

        if (login != null && login.value == "true") {
            div.style.display = "block";
        }
    }
    catch (err) { }
}

function toggle(showHideDiv, username) {
    var ele = document.getElementById(showHideDiv);
    if (ele) {
        if (ele.style.display == "block") {
            ele.style.display = "none";
        }
        else {
            ele.style.display = "block";
            //            var field = getUsernameField(username);
            //            if (field != null)
            //                field.focus();
        }
    }
}

function getUsernameField(username) {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "text" && inputs[i].id.indexOf(username) >= 0) {
            return inputs[i];
        }
    }
    return null;
}

function setLeftColWitdth() {
    var col = document.getElementById("leftCol");
    var pane = document.getElementById("dnn_LeftPane");
    if (pane != null && pane != null) {
        if (pane.innerHTML == null || pane.innerHTML == "") {
            col.style.width = "0px";
        }
    }
}

function toggleSiteMap(hideAnchor, showAnchor, map) {
    $(showAnchor).removeClass("hide");
    $(hideAnchor).addClass("hide");
    var isHidden = ($(map)).hasClass("hide");
    if (!isHidden) {
        $(map).addClass("hide");
    }
    else {
        $(map).removeClass("hide");
    }
}
