// Hide select when showing positioned layer
// IE 6 the One and Only
$(document).ready(function() {
    $(".module .highslide a").click(function() {
        $(".module select.block").css({ visibility: "hidden" });
    });
    $(".highslide-close a").click(function() {
        $(".module select.block").css({ visibility: "visible" });
    });

    // Expandable list
    theLang = $("html").attr("lang");
    thePath = themePath;
    $(".expandedInfo").hide();

    $(".buttons .expandable").click(function() {
        $(this).parent().parent().children(".expandedInfo").slideToggle(100);
        if (!$(this).attr("src").match("hide")) {
            $(this).attr("src", "" + thePath + "images/hide.read.more.btn." + theLang + ".gif");
        } else {
            $(this).attr("src", "" + thePath + "images/read.more.btn." + theLang + ".gif");
        }
        return false;
    });

    // Expandable List
    theLang = $("html").attr("lang");
    thePath = themePath;
    $("ul.expandedList").hide();

    $(".headLine").each(function() {

        if ($.trim($(this).parent().find(".expandedList").text()) == "") {
            $(".expandableButton", this).hide();
        } else {
            $(this).click(function() {
                theParent = $(this).parent()
                $("> ul.expandedList", theParent).slideToggle(100, function() {
                    $(".text", theParent).hide()
                    $("h3>img", theParent).attr("src", "" + thePath + "images/arrow.gif");

                });
                if (!$("img", this).attr("src").match("hide")) {
                    $("img", this).attr("src", "" + thePath + "images/hide.btn." + theLang + ".gif");
                } else {
                    $("img", this).attr("src", "" + thePath + "images/show.btn." + theLang + ".gif");
                }
                return false;
            })
        }
    });
    
    // ExpandableList another level, used in FAQ
    $("ul.expandedList li .text").hide();

    $("ul.expandedList h3").click(function() {
        thisParent = $(this).parent()
        $(".text", thisParent).slideToggle(100);
        if (!$("img", this).attr("src").match("down")) {
            $("img", this).attr("src", "" + thePath + "images/arrow.down.gif");
        } else {
            $("img", this).attr("src", "" + thePath + "images/arrow.gif");
        }
        return false;
    });

    // addThis
    $('#addThis')
	    .mouseover(function() {
	        return addthis_open(this, '', '[URL]', '[TITLE]')
	    })
	    .mouseout(function() {
	        addthis_close()
	    })
	    .click(function() {
	        return addthis_sendto()
	    });
});
var addthis_options = 'favorites, facebook, twitter, more';
// Print layer
jQuery.jPrintArea = function(el) {
    var iframe = document.createElement('IFRAME');
    var doc = null;

    $(iframe).attr('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
    document.body.appendChild(iframe);
    doc = iframe.contentWindow.document;

    var links = window.document.getElementsByTagName('print');

    for (var i = 0; i < links.length; i++) {
        if (links[i].rel.toLowerCase() == 'stylesheet')
            iframe.contentWindow.document.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');
    }

    iframe.contentWindow.document.write('<link type="text/css" rel="stylesheet" href="/ui/public/styles/print.css" media="print" />');
    iframe.contentWindow.document.write('<div class="' + $('#' + el).attr("class") + '">' + $('#' + el).html() + '</div>');
    iframe.contentWindow.document.close();

    iframe.contentWindow.focus();
    iframe.contentWindow.print();
    setTimeout("$('iframe').remove()", 10000);
}
// extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups
function extWindows() {
    $("a[rel=external]").attr("target", "_blank");
    $("a[rel^=popup]").click(function() {
        theHref = $(this).attr("href");
        //		$(this).attr("href","javascript:void(0)");
        var popupWidth, popupHeight
        var relSplit = $(this).attr("rel").split("|");
        if (relSplit[1]) {
            popupWidth = relSplit[1];
            popupHeight = relSplit[2];
        } else {
            popupWidth = 435;
            popupHeight = 350;
        }
        popMeUp(theHref, popupWidth, popupHeight)
        return false;
    });

}
function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}
// Open a link in a new window - not in use? use above instead
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (
			anchor.getAttribute("href") && (
			anchor.getAttribute("rel") == "external" ||
			anchor.getAttribute("rel") == "external nofollow" ||
			anchor.getAttribute("rel") == "nofollow external")
			)
            anchor.target = "_blank";
    }
}
window.onload = function() {
    externalLinks();
}

function setImage(imageId, imageUrl) {
    $("#" + imageId).attr({ src: imageUrl });
}
// ONLY Riksgransen Hemavan - use instead Expandable list
addEvent(document, 'click', function(e) {
    theObject = (window.event) ? window.event.srcElement : e.target;

    if (theObject.parentNode.parentNode) {
        theBox = theObject.parentNode.parentNode.parentNode;
        if (theObject.className == "trigger") {
            if (theBox.className == "expanded") {
                theBox.className = "normal";
            } else {
                theBox.className = "expanded";
            }
        }
    }
});
function addEvent(obj, type, fn) {
    if (obj.addEventListener)
        obj.addEventListener(type, fn, false);
    else if (obj.attachEvent) {
        obj["e" + type + fn] = fn;
        obj[type + fn] = function() { obj["e" + type + fn](window.event); }
        obj.attachEvent("on" + type, obj[type + fn]);
    }
}
function removeEvent(obj, type, fn) {
    if (obj.removeEventListener)
        obj.removeEventListener(type, fn, false);
    else if (obj.detachEvent) {
        obj.detachEvent("on" + type, obj[type + fn]);
        obj[type + fn] = null;
        obj["e" + type + fn] = null;
    }
}
// Show and hide images with checkbox, e g FO Resor
var listImg = {
    init: function() {
        $(".expandableList #idShow").click(listImg.showHide);

        listImg.showHide();

        /*var scr = document.createElement("script"), allscr = document.getElementsByTagName("head")[0].getElementsByTagName("script"), i, ls;
        scr.src = "/ui/public/scripts/jquery-ui-personalized-1.6rc5.min.js";
        
        for (i = 0; i < allscr.length; i += 1) {
        if(ls) {
        allscr[i].src.toString().match(/jquery/i);
                
        document.getElementsByTagName("head")[0].insertBefore(scr, ls.nextSibling);
        break;
        }
        ls = allscr[i];
        }*/
    },
    showHide: function() {
        var im = $(".expandableList .row .image img:not([class])"), i, that = this;

        if (that !== document.getElementById("idShow")) {
            that = document.getElementById("idShow");
        }

        if (that) {
            if (that.checked) {
                for (i = 0; i < im.length; i += 1) {
                    im[i].style.display = "";
                }
            } else {
                for (i = 0; i < im.length; i += 1) {
                    im[i].style.display = "none";
                }
            }
        }
    }
};

// Highslide
var lhFix = {
    init: function() {
        if (navigator.userAgent.match(/firefox/i)) {
            $("a[title='Close (esc)']").click(lhFix.remOFLH);
        }
        lhFix.popWidthInit();

    },
    remOFLH: function() {
        var hhc = $(".highslide-html-content");
        if (hhc.length && hhc[0]) {
            hhc[0].style.overflow = "visible";
        }
    },
    pwc: 0,
    popWidthInit: function() {
        var obj = $("[id^='hs-flash']"), hlw = $("[id^='highslide-wrapper']");

        if ((obj.length && obj[0].tagName) && hlw.length) {
            if (hlw[0].offsetWidth < 650) {
                setTimeout(lhFix.popWidthInit, 10);
            } else {
                lhFix.popWidth(obj[0], hlw[0]);
            }
        }
    },
    pWidth: 950,
    popWidth: function(o, h) {
        var kids = h.getElementsByTagName("*"), i, re = new RegExp("^highslide\\-html"), ltd, first = true, second = true, third = true;

        h.style.width = lhFix.pWidth + "px";
        h.previousSibling.style.width = parseInt(lhFix.pWidth + 30, 10) + "px";

        lhFix.removeInlineMouseEvents(h);
        lhFix.removeInlineMouseEvents(h.previousSibling);

        for (i = 0; i < kids.length; i += 1) {
            if (kids[i].tagName.match(/^div$/i)) {
                if (!kids[i].className || (kids[i].className && kids[i].className.match(re))) {
                    kids[i].style.width = lhFix.pWidth + "px";
                }
            }

            lhFix.removeInlineMouseEvents(kids[i]);
        }

        ltd = h.previousSibling.getElementsByTagName("td");
        for (i = 0; i < ltd.length; i += 1) {
            if (!ltd[i].nextSibling) {
                if (first) {
                    ltd[i].style.backgroundPosition = "right top";
                    first = false;
                } else if (second) {
                    ltd[i].style.backgroundPosition = "right center";
                    second = false;
                } else if (third) {
                    ltd[i].style.backgroundPosition = "right -20px";
                    third = false;
                }
            }
        }

        lhFix.fixPos(h.previousSibling, h);
    },
    removeInlineMouseEvents: function(obj) {
        var me = ["over", "out", "move", "up", "down"], i, temp;

        for (i = 0; i < me.length; i += 1) {
            temp = "onmouse" + me[i];

            if (obj[temp]) {
                obj[temp] = function() { };
            }
        }
    },
    fixPos: function(l, h) {
        var ww = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth, wh = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

        h.style.left = (parseInt((ww / 2) - (h.offsetWidth / 2), 10)) + "px";
        //h.style.top = (parseInt((wh/2) - (h.offsetHeight/2), 10) - 8) + "px";

        l.style.left = (parseInt((ww / 2) - (l.offsetWidth / 2), 10)) + "px";
        //l.style.top = (parseInt((wh/2) - (l.offsetHeight/2), 10) - 8) + "px";

        lhFix.h = h;
        lhFix.l = l;

        lhFix.addMovePop();
    },
    addMovePop: function() {
        var h = lhFix.h, l = lhFix.l, newDiv, contDiv;
        var moveLink = $("[class^='highslide-move'] [title='Move']");

        if (!document.getElementById("bigMoveDiv")) {
            newDiv = document.createElement("div");
            contDiv = document.createElement("div");

            newDiv.style.position = "absolute";
            newDiv.id = "bigMoveDiv";

            contDiv.id = "bigContDiv";
        } else {
            newDiv = document.getElementById("bigMoveDiv");
            contDiv = document.getElementById("bigContDiv");
        }

        contDiv.style.position = "relative";

        newDiv.appendChild(contDiv);

        newDiv.style.position = "absolute";
        newDiv.style.top = l.offsetTop + "px";
        newDiv.style.left = l.offsetLeft + "px";

        //h.style.position = "absolute";
        h.style.top = (h.offsetTop - l.offsetTop) + "px";
        h.style.left = (h.offsetLeft - l.offsetLeft) + "px";

        //l.style.position = "relative";
        l.style.top = 0;
        l.style.left = 0;

        contDiv.appendChild(l);
        contDiv.appendChild(h);

        document.body.appendChild(newDiv);

        lhFix.orgTop = parseInt(newDiv.style.top, 10);
        lhFix.orgLeft = parseInt(newDiv.style.left, 10);

        addEvent(moveLink, "mousedown", lhFix.initMovePop);
    },
    initMovePop: function(e) {
        var p = lhFix.getMousePosition(e), h = lhFix.h, l = lhFix.l;

        //h.style.cursor = "move";
        //l.style.cursor = "move";

        //alert(p.x + "\n" + p.y);
    },
    trackMouse: function(e) {

    },
    removeTrackMouse: function(e) {

    },
    getMousePosition: function(e) {
        if (!e) {
            e = window.event;
        }
        var mp = {
            x: e.clientX ? (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft) : e.pageX,
            y: e.clientY ? (e.clientY + document.body.scrollTop + document.documentElement.scrollTop) : e.pageY
        };
        return mp;
    }
}

function lmf() {
    listImg.init();
}
addEvent(window, "load", lmf);

//Raiting mouse over/Out
function manageMouseOverOut(imageIds, imageUrl) {

    var mytool_array = imageIds.split(" ");

    // the split will result in an array one to long as there is an extra " " at the end => "- 1"

    for (i = 0; i < mytool_array.length - 1; i++) {
        $("#" + mytool_array[i]).attr({ src: imageUrl });
    }
}

//Raiting mouse over/Out
function manageMouseOut(imageIds, images) {

    var mytool_array = imageIds.split(" ");
    var myImages_array = images.split(" ");

    // the split will result in an array one to long as there is an extra " " at the end => "- 1"

    for (i = 0; i < mytool_array.length - 1; i++) {
        $("#" + mytool_array[i]).attr({ src: myImages_array[i] });
    }
}
