/******************************************************************
SiteCore defaults
/******************************************************************/

function newImage(ID, file) {
    document.getElementById(ID).src = '/images/' + file;
}

function open_window(URL, name, left, top, width, height, menubar) {
    rp = window.open(URL, name, "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width + ",menubar=" + menubar + ",scrollbars=1,resizable=1,status=1");
    rp.focus();
    return (false);
}

/******************************************************************
SST UI
/******************************************************************/

SST = {
    getQueryParams: function() {
        var r = {};
        var q = location.search.replace(/^\?/, '').replace(/\&$/, '');
        jQuery.each(q.split('&'), function() {
            var val = this.split('=')[1];
            if (val)
                r[this.split('=')[0]] = val;
        });
        return r;
    },

    redirectToParam: function(param) {
        location.href = decodeURIComponent(SST.getQueryParams()[param]).replace(/\+/g, "%20");
    },

    setFontSize: function(size) {
        if (typeof (size) === "undefined" || size == null) {
            // initialize with value from cookie, if any
            size = jQuery.cookie('sst-size');
        }
        size = (size == null ? 0 : parseInt(size, 10));

        switch (size) {
            case 0:
                jQuery("body").css("font-size", "0.69em");
                break;
            case 1:
                jQuery("body").css("font-size", "0.79em");
                break;
            case 2:
                jQuery("body").css("font-size", "0.89em");
                break;
        }

        jQuery.cookie('sst-size', size, { path: '/' });
    },

    searchResultUrl: "/Soegeresultat.aspx?",

    searchResultUrl_en: "/English/Search.aspx?",

    bindSearchField: function() {

        function search() {
            var search_url = jQuery("#searchSubmit").attr("name") == "en" ? SST.searchResultUrl_en : SST.searchResultUrl;

            location.href = search_url + "terms=" + encodeURIComponent(jQuery("#searchField").attr("value"));
        }

        jQuery("#searchField").keypress(function(e) {
            if (e.keyCode == 13) {
                e.stopPropagation();
                e.preventDefault();
                search();
            }
        });

        jQuery("#searchSubmit").click(function(e) {
            e.stopPropagation();
            e.preventDefault();
            search();
        });
    }
};

jQuery(document).ready(
    function() {
        SST.setFontSize(null);
        SST.bindSearchField();
        jQuery("img.tooltip").tipsy({ gravity: 's' });
    }
);

/******************************************************************
SST UI - Tabs
/******************************************************************/

SST.tabs = {
    current: null,

    initialize: function(defaultSelection) {
        jQuery(document).ready(function() {
            if (defaultSelection !== null) {
                jQuery(jQuery("#tabs li a").get(defaultSelection)).addClass("selected");
            } else {
                // an indication that the tabs are popups, so hide all when visitor clicks somewhere
                jQuery(document).click(function(evt) {
                    if (jQuery(evt.target).parents(".tabPanels").length != 0)
                        return; // inside tab panel, do not hide tabs

                    // hide tabs .. no deselect in ui.tabs, really!? .. so hack:
                    var ul = jQuery("#tabs > ul");
                    var count = ul.tabs("length");
                    ul.tabs("add", "#hack", "").tabs("select", count).tabs("remove", count)
                });
            }

            SST.tabs.current = jQuery("#tabs > ul")
                .tabs({ selected: defaultSelection, fx: { opacity: 'show' }, unselect: true })
                .bind('tabsselect',
                    function(event, ui) {
                        jQuery("#tabs a").removeClass("selected");
                        jQuery(ui.tab).addClass("selected");
                    }
                );
        });
    }
};

/******************************************************************
SST UI - Frontpage Category/Link Presentation boxes
/******************************************************************/

jQuery().ready(function() {
    jQuery('.subjectBoxes .foldout').click(function() {
        var $this = jQuery(this);
        var box = $this.parents("div").get(0);
        var jb = jQuery(box);
        if (jb.css("height") != "80px") {
            // clicked on already expanded box
            jQuery("img", $this).attr("src", "/images/sst/icon_plus.gif");
            jb.animate({ height: 80 }, 300, function() {
                // hide all 'hidden' keywords, when folded
                jQuery(this).find(".not").toggleClass("not", false).toggleClass("hid", true);
            });
            return;
        }
        // reset all, expand current
        jQuery(".subjectBoxes .not").toggleClass("not", false).toggleClass("hid", true); // hide all 'hidden' keywords
        jQuery(".subjectBoxes .subjectBox").css("height", "80px");
        jQuery(".subjectBoxes .foldout img").attr("src", "/images/sst/icon_plus.gif");

        jQuery("img", $this).attr("src", "/images/sst/icon_minus.gif");
        jQuery(".hid", jb).toggleClass("hid", false).toggleClass("not", true); // unhide all 'hidden' keywords                        
        var fullHeight = (jQuery(".catKeywords", jb).outerHeight() + jQuery(".header", jb).outerHeight());
        if (fullHeight < 80)
            fullHeight = 80;
        jb.animate({ height: fullHeight }, 300);
    });
});

function FoldSubjectBoxes(single) { 

}


/******************************************************************
SST UI - Automatic window-target for links
/******************************************************************/

SST.autoTarget = {
    localhost: "http://" + document.location.host,
    debug: false,

    initialize: function() {
        jQuery('#content a').each(function() {
            if (!this.target) {
                if (SST.autoTarget.isExternalOrFile(this.href)) {
                    this.target = "_blank";
                    SST.autoTarget.addDownloadStat(this);
                }
            }
        });
    },

    isLocalHost: function(url) {
        if (url.substring(0, 1) == "/")
            return true;

        return (url.substring(0, SST.autoTarget.localhost.length) == SST.autoTarget.localhost);
    },

    isDownloadable: function(url) {
        return url.indexOf(".ashx") > 0;
    },

    addDownloadStat: function(link) {
        if (SST.autoTarget.isDownloadable(link.href)) {
            jQuery(link).click(function(event) {
                var target = event.target;
                if (target.tagName != "A")
                    target = jQuery(target).parent("a");
                if (!target)
                    return;

                if (target.tagName == "A") {
                    var tiltIndex = target.href.indexOf("~");
                    var hostPrefix = "http://" + location.host;
                    if (tiltIndex != -1 && target.href.indexOf(hostPrefix) == 0) {
                        // dealing with a sitecore media link
                        var pagePath = target.href.substring(hostPrefix.length, tiltIndex);
                        var fileName = target.href.substring(target.href.lastIndexOf("/") + 1, target.href.length - 5);
                        if (typeof (urchinTracker) != "undefined") {
                            urchinTracker("/downloads" + decodeURIComponent(pagePath + fileName));
                        } else {
                            if (SST.autoTarget.debug)
                                alert("urchinTracker no available.");
                        }
                    }
                }
            });
        }
    },

    isExternalOrFile: function(url) {
        // check for javascript
        if (url.indexOf("javascript:") >= 0) {
            return false;
        }

        if (!SST.autoTarget.isLocalHost(url)) {
            return true;
        } else {
            // check for file
            return url.indexOf("/~/media/") >= 0 || url.indexOf("/publ/") >= 0 || url.indexOf("/RSS/") >= 0;
        }
    }
};

jQuery(document).ready(SST.autoTarget.initialize);




