function readCookie(nameOfCookie) {
    var nameEQ = nameOfCookie + "=";
    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;
}

function createCookie(nameOfCookie, value, days) {
    if (days) {
        var date = new Date;
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    
    document.cookie = nameOfCookie + "=" + value + expires + "; path=/";
}

//Dunno
function homerootlink() 
{ 
    var myurl=window.location.href; 
    if(myurl.match("/is")) 
        {window.open("/shs/is/","_self")} 
    else if(myurl.match("/en")) 
        {window.open("/shs/en/","_self")} 
    else if(myurl.match("/se")) 
        {window.open("/shs/se/","_self")} 
    else if(myurl.match("/th")) 
        {window.open("/shs/th/","_self")} 
    else if(myurl.match("/ru")) 
        {window.open("/shs/ru/","_self")} 
    else if(myurl.match("/li")) 
        {window.open("/shs/li/","_self")} 
    else if(myurl.match("/sp")) 
        {window.open("/shs/sp/","_self")} 
    else if(myurl.match("/po")) 
        {window.open("/shs/po/","_self")} 
    else
		{window.open("/shs/is/","_self")}
}

function loc()
{
    window.open('/shs/is/leit/print_page/');
}

var newWindow1 = null;

function sendFriend(path) {
    if (newWindow1 != null) {
        try {
            newWindow1.close();
        } catch (e) {
        }
    }
    newWindow1 = window.open(path, "", "width=400,height=300,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no ");
    newWindow1.focus();
}

//Function to change page style
function changeclass(obj, nameOfClass) {
     document.getElementById(obj).className = nameOfClass;
     createCookie('skincolor', nameOfClass);
}

//Load cookie selected skin
window.onload = function() {
    var cookie = readCookie('skincolor');

    if(cookie != null && cookie != '' || cookie != 'default') {
        document.getElementById("frame_area").className = cookie;
    }
    
    //Load font weight and size
    var title = "";
    
    cookie = readCookie("style");
    title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title,"fontsize");
    
    cookie = readCookie("wstyle");
    title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title,"weight");
}

function resizeElement(stgHeight)
{
    document.getElementById('flash_quote_container1').style.height = stgHeight+"px";
    document.getElementById('flash_quote_container1').style.width = "158px";
}

//IE6 PNG transparency
$(document).ready(function() {
    $('img[@src$=.png], .paper_clip').ifixpng('/shs/upload/images/templates/pixel.gif'); 
});


function trim(str)
{
    return str.replace(/^\s*|\s*$/g,'');
}

function validate_leita(searchid)
{
    var searchObj = document.getElementById(searchid);
    search_value=trim(searchObj.value);
    
    if(search_value.length > 3 && search_value!='Leita' && search_value!='Search')
        return true;
    else {
        searchObj.value=search_value; 
        alert("Leitarstrengur verður að vera minnst 4 stafir");           
        return false;
    }
}

/**** Style switcher *****/

function decreaseFontSize()
{
    var titleOfActiveStyle = getActiveStyleSheet("fontsize");

    if(titleOfActiveStyle == null) 
        titleOfActiveStyle = "Size:8pt";
    if(titleOfActiveStyle == "Size:14pt")
        setActiveStyleSheet("Size:12pt","fontsize");
    if(titleOfActiveStyle == "Size:12pt")
        setActiveStyleSheet("Size:10pt","fontsize");
    if(titleOfActiveStyle == "Size:10pt")
        setActiveStyleSheet("Size:9pt","fontsize");
    if(titleOfActiveStyle == "Size:9pt")
        setActiveStyleSheet("Size:8pt","fontsize");
}

function increaseFontSize()
{
    var titleOfActiveStyle = getActiveStyleSheet("fontsize");

    if(titleOfActiveStyle == null) 
        titleOfActiveStyle = "Size:8pt"; 
    if(titleOfActiveStyle == "Size:8pt")
        setActiveStyleSheet("Size:9pt","fontsize");
    if(titleOfActiveStyle == "Size:9pt")
        setActiveStyleSheet("Size:10pt","fontsize");
    if(titleOfActiveStyle == "Size:10pt")
        setActiveStyleSheet("Size:12pt","fontsize");
    if(titleOfActiveStyle == "Size:12pt")
        setActiveStyleSheet("Size:14pt","fontsize");
}

function setActiveStyleSheet(title,type) {
    var i, a, main;

    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && isStyleSheetType(a.getAttribute("title"),type)) {
            a.disabled = true;
            
            if(a.getAttribute("title") == title) 
                a.disabled = false;
        }
    }
}

function isStyleSheetType(str,type) {
    if (str == "") { return true; }
    else if (type == "fontsize" && str.match(/\Size/)) {return true;}
    else if (type == "weight" && str.match(/\Font/)) {return true;}
    else if (type == "contrast" && str.match(/^Back/)) {return true;}
    else if (type == "family" && str.match(/\FontFamily/)) {return true;}
    else if (type == "color" && str.match(/^Color/)) {return true;}

    return false;
}

function getActiveStyleSheet(type) {
    var i, a;

    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && isStyleSheetType(a.getAttribute("title"),type) && !a.disabled)
            return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() {
    var i, a;
    
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
            return a.getAttribute("title");
    }
    return null;
}

