/**
 * Read's a cookie
 */
function readVKBCookie(pCookieName) {
    var cookieName = pCookieName + '='
    var cookies = document.cookie;
    var pos = cookies.indexOf(cookieName);
    if (pos != -1) {
        var start = pos + cookieName.length;
        var end = cookies.indexOf(';', start);
        if (end == -1) {
            end = cookies.length;
        }
        return unescape(cookies.substring(start, end));
    }
}

/**
 * Set a cookie
 */
function setVKBCookie(pName, pValue) {
    var expireDate = new Date();
    expireDate.setTime( expireDate.getTime() + 1000 * 60 * 60 * 24 * 365)

    document.cookie = pName + '=' + escape(pValue)+ ";expires=" + expireDate + ";path=/";
}

/**
 * Launches a pop-up window
 */
function launchPopup(url, name, width, height, resizable, scrollbars, toolbar, location) {
    popup_window = window.open(url, name, 'width=' + width + ',height=' + height + ',resizable=' + resizable + ',scrollbars=' + scrollbars + ',toolbar=' + toolbar + ',location=' + location + '');
    popup_window.focus();
}

/**
 * Holds a timestamp for use with cookie generation...
 */
var timestamp = '<%= System.currentTimeMillis() %>';

/**
 * Shows a element
 */

function show(pElementName, pDisplay) {
    //alert('show(' + pElementName + ', '+ pDisplay + ')');
    var voted = document.getElementById(pElementName);
//  voted.style.display = pDisplay;
}

/**
 * Hides an element
 */

function hide(pElementName) {
    //alert('hide(' + pElementName + ')');
    var voted = document.getElementById(pElementName);
//  voted.style.display = "none";
}

/**
 * Changes two element at once, switching states between them.
 */

function change(pHideElementName, pShowElementName) {
    //alert('change(' + pHideElementName + ', '+ pShowElementName + ')');
    hide(pHideElementName);
    show(pShowElementName, 'block');
}


/**
 * Checkes state
 */
function mentometerState(pollId) {
    //alert('mentometerState(' + pollId + ')');
    var cookies = document.cookie;
    var pos = cookies.indexOf('mentometer=');
    if (pos != -1) {
        var start = pos + 11;
        var end = cookies.indexOf(';', start);
        if (end == -1) {
            end = cookies.length;
        }
        var value = cookies.substring(start, end);
        value = unescape(value);
        var mentometerIds = value.split('M');
        for (var i = 0; i < mentometerIds.length; i++) {
            if (mentometerIds[i] == pollId) {
                change('vote-' + pollId, 'voted-' + pollId);
                return;
            }
        }
        change('voted-' + pollId, 'vote-' + pollId);
    }
    else {
        change('voted-' + pollId, 'vote-' + pollId);
    }
}

/**
 * Calculates the size of one individual width
 */
/*
function size(pTotalVotes, pVotes, pSize) {
    return Math.round((pSize * percent(pTotalVotes, pVotes)) / 100);
}
*/

/**
 * Calculates the percentage value of a set
 * of votes represents...
 */
function percent(pTotalVotes, pVotes) {
    var result = 0;
    if (pTotalVotes > 0) {
        result = (pVotes * 100) / pTotalVotes;
    }
    return Math.round(result);
}

/**
 * Read's a cookie
 */
function readCookie(pCookieName) {
    var cookieName = pCookieName + '='
    var cookies = document.cookie;
    var pos = cookies.indexOf(cookieName);
    if (pos != -1) {
        var start = pos + cookieName.length;
        var end = cookies.indexOf(';', start);
        if (end == -1) {
            end = cookies.length;
        }
        return unescape(cookies.substring(start, end));
    }
}

/**
 * Set a cookie
 */
function setCookie(pName, pValue) {
    document.cookie = pName + '=' + escape(pValue);
}

/**
 * Reads one individual poll cookie
 */
function readPollCookie(pollId) {
    var pollCookie = readCookie('pollCache');
    if (pollCookie == null) {
        return null;
    }
    var pollCacheCookies = pollCookie.split(';');
    for (var i = 0; i < pollCacheCookies.length; i++) {
        var pollCookie = pollCacheCookies[i];
        if (pollCookie.indexOf(pollId) != -1) {
            return pollCookie;
        }
    }
}

/**
 * An array of poll cookie id's
 */
function readPollCookieIds() {
    var pollCookie = readCookie('pollCache');
    if (pollCookie == null) {
        return null;
    }
    var pollCacheCookies = pollCookie.split(';');
    var result = new Array(pollCacheCookies.length);
    for (var i = 0; i < pollCacheCookies.length; i++) {
        var hepp = pollCacheCookies[i].split(':')
        result[i] = hepp[0];
    }
    return result;
}

/**
 * Set's a poll cookie
 */
function setPollCookie(pollId, vote) {
    var values = '';
    var pollCookieIds = readPollCookieIds();
    if (pollCookieIds != null) {
        for (var i = 0; i < pollCookieIds.length; i++) {
            values += readPollCookie(pollCookieIds[i]) + ';';
        }
    }
    values += 'pollId-' + pollId + ':' + timestamp + ':' + vote;
    //alert(values);
    setCookie('pollCache', values);
}

