function setCookie(cName,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=cName+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(cName) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(cName + "=");
        if (c_start!=-1) {
            c_start=c_start + cName.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));
        }
    }
    return "";
}

function openPicture(url) {
    winName = 'win_' + Math.floor(Math.random() * 100000);
    window.open(url, winName, 'left=100,top=50,toolbar=1,location=0,' +
        'status=0,menubar=0,resizable=1,width=400,height=400,' +
        'scrollbars=1');
    return false;
}

function openPost(url) {
    winName = 'win_' + Math.floor(Math.random() * 100000);
    window.open(url, winName, 'left=0,top=0,toolbar=1,location=0,' +
        'status=0,menubar=0,resizable=1,width=800,height=700,' +
        'scrollbars=1');
    return false;
}

function openListener(url) {
    window.open(url, 'listener', 'left=0,top=0,toolbar=0,location=0,' +
        'status=0,menubar=0,resizable=0,width=700,height=400,' +
        'scrollbars=0');
    return false;
}
//
// Callback function to bring a hidden box back
// Used by runEffect()
//
function callback(){
    setTimeout(function(){
        $("#effect:hidden").removeAttr('style').hide().fadeIn();
    }, 1000);
}

//
// Run the currently selected effect using jquery
//
function runEffect(effect, effectId, action) {

    var options = {};
    // Certain effects need options set
    if (effect == 'scale') {  
        options = { percent: 0 }; 
    }
    else if (effect == 'transfer') { 
        options = { 
            to: "#button", 
            className: 'ui-effects-transfer' 
        }; 
    }
    else if (effect == 'size') { 
        options = { 
            to: {
                width: 200,
                height: 60
            } 
        }; 
    }
    
    // Run the effect
    if (action == 'show')
        $("#"+effectId).show(effect,options,500,callback);
    else
        $("#"+effectId).hide(effect,options,500,callback);
}

//
// Hide layer with jquery effect
//
function hideLayer(id) {
    if ($("#" + id).is(":visible"))
        $("#" + id).hide();
}

//
// Show layer with jquery effect
//
function showLayer(id) {
    if ($("#" + id).is(":hidden"))
        $("#" + id).show();
}

//
// Hide an array of layers
//
function hideLayers(layers) {
    for (i = 0; i < layers.length; i++)
        hideLayer(layers[i]);
}

//
// Go to an anchor
//
function gotoAnchor(anchor) {
    document.location.href = "#" + anchor;
}

function toggleContest(contestObj) {
    if (contestObj.innerHTML != 'Show&nbsp;Contest') {
        contestObj.innerHTML = 'Show&nbsp;Contest';
        runEffect('blind', 'contest', 'hide');
        setCookie('contestHidden', 'true', 1);
    } else {
        contestObj.innerHTML = 'Hide&nbsp;Contest';
        runEffect('blind', 'contest', 'show');
        setCookie('contestHidden', 'false', 1);
    }
} 

function toggleContent(id) {
    excerptObj = document.getElementById('excerpt_' + id);
    contentObj = document.getElementById('content_' + id);
    aObj = document.getElementById('a_' + id);
    if (aObj.innerHTML == 'Read more') {
        aObj.innerHTML = 'Read less';
        $('#excerpt_' + id).hide();
        runEffect('blind', 'content_'+id, 'show');
    } else if (aObj.innerHTML == 'Read less') {
        aObj.innerHTML = 'Read more';
        runEffect('blind', 'content_'+id, 'hide');
        $('#excerpt_' + id).show();
    } else if (aObj.innerHTML == 'View comments') {
        aObj.innerHTML = 'Hide comments';
        runEffect('blind', 'content_'+id, 'show');
    } else if (aObj.innerHTML == 'Hide comments') {
        aObj.innerHTML = 'View comments';
        runEffect('blind', 'content_'+id, 'hide');
    } 
    document.getElementById(id).scrollIntoView(true);
} 

contestHidden = getCookie('contestHidden');
if (contestHidden)
    contestDisplay = 'none';
else
    contestDisplay = 'block';


