var ms_cookie = null;
var ms_title = null;

$(function(){
        ms_init();
});

function ms_init()
{
        $('head').append(ms_getStillingar());
        $('head').append(ms_getLocalStillingar());

        ms_cookie = ms_readCookie("style");
        ms_title = ms_cookie ? ms_cookie : '';
        ms_setActive(ms_title);
        if (ms_title == 'stillingar') {
                $('.stillingaris').css({display:"block"});
        }
}

function ms_toggle()
{
        if (ms_getActive() != "stillingar")
        {
                ms_setActive("stillingar");
                ms_createCookie("style", "stillingar", 0);
                $('.stillingaris').show();
         }
         else
         {
                ms_createCookie("style", "", 0);
                ms_setActive("");
                $('.stillingaris').hide();
                document.location = document.location.href;
         }
}

function ms_getActive() {
        var ret = null
        $('head').find('link').each(function(){
                var t = $(this);
                if (t.attr('rel') == 'stylesheet' && t.attr('disabled') == false) {
                        ret = t.attr('title');
                        return;
                }
        });

        return ret;
}
function ms_setActive(title) {
        $('head').find('link').each(function(){
                var t = $(this);
                if (t.attr('rel') == 'stylesheet') {
                        t.attr('disabled', true);
                        if (t.attr('title') == title || t.attr('title') == title + '-local') {
                                t.attr('disabled', false);
                        }
                }
        });
}

function ms_getStillingar()
{
        var stylesheet = document.createElement("link");
        stylesheet.rel = "stylesheet";
        stylesheet.title = "stillingar";
        stylesheet.href = "http://minar.stillingar.is/lesa/stilar/css/";
        stylesheet.type = "text/css";
        stylesheet.media = "screen";
        return stylesheet;
}

function ms_getLocalStillingar()
{
        var stylesheet = document.createElement("link");
        stylesheet.rel = "stylesheet";
        stylesheet.title = "stillingar";
        stylesheet.href = "http://sagacapital.is/static/themes/sc2/stillingar.css";
        stylesheet.type = "text/css";
        stylesheet.media = "screen";
        return stylesheet;
}

function ms_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 = "";
        }
        if (days!=0) {
                document.cookie = name+"="+value+expires+"; path=/";
        }
        else {
                document.cookie = name+"="+value+"; path=/";
        }
}
function ms_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;
}

