/**
 * Common scripts used on all pages
 *
 * Christian Johansen (christian@ixd.no)
 * www.ixd.no
 */

/**
 * Configure functions to perform on click for different values of rel on links
 */
var newWindow = function(e) { window.open(this.href); return false; };
var appendRelToClass = function(link) { HTMLDOMUtil.addClass(link, link.rel); };
var relEvents = ['external', [newWindow, appendRelToClass], 'document', [newWindow, appendRelToClass],
                 'print', function(e) { if (window.print) { window.print(); return false; } else { return true; } }];

addDOMLoadEvent(function() {
    if (window.Popup) {
        relEvents = relEvents.concat(['popup', function() { return openPopup(tmp_prototype_getURL(this.href)); }]);
        openReferringPopup();
    }

		 HTMLUtil.linkRelEvents(relEvents);
    tableRowMouseOver();
    correctSubNavigation();
    clickEnableProducts();
    correctPopupContext();
    hackSearch();
});

function tableRowMouseOver() {
    var table, row;

    for (var i = 0; table = document.getElementsByTagName('tbody')[i]; i++)
        for (var j = 0; row = table.getElementsByTagName('tr')[j]; j++)
            row.onmouseover = row.onmouseout = function(e) { HTMLDOMUtil.toggleClass(this, 'active'); };
}

function correctSubNavigation() {
    var nav = document.getElementById('sub_navigation'), element = null, i = 1;

    if (nav == null) return;

    for (var i = 0; i < nav.childNodes.length; i++)
        if (nav.childNodes[i].tagName)
            element = nav.childNodes[i];

    if (element.className.indexOf('open') >= 0)
        HTMLDOMUtil.addClass(nav, 'last-open');
}

function clickEnableProducts() {
    var products = HTMLDOMUtil.getElementsByClassName('product', 'div');
//		 products = products.concat(HTMLDOMUtil.getElementsByClassName('action', 'div', document.getElementById('related_information')));

    for (var i = 0; i < products.length; i++) {
        if (products[i].getElementsByTagName('a').length == 0)
            continue;

        products[i].url = products[i].getElementsByTagName('a')[0].href;
        products[i].onclick = function(e) { window.location = this.url; };

		 		 if (products[i].className.indexOf('action') >= 0)
		 		 		 HTMLDOMUtil.addClass(products[i], 'clickable');
    }
}

function correctPopupContext() {
    var link = document.getElementById('parent_relation');

    if (link == null || link.href == '' || link.href == window.location.href ||
        window.parent.location.href != window.location.href)
        return;

    var url = link.href;
    url += url.indexOf('?') >= 0 ? '&' : '?';
    url += url.indexOf('OpenDocument') < 0 ? 'OpenDocument&' : '';
    window.location = url + 'open_popup=' + URL.encode(window.location.href);
}

function openReferringPopup() {
    var popup = URL.parameter('open_popup');

    if (popup.length <= 0)
        return;

    var url = URL.decode(popup);
    url += url.indexOf('?') >= 0 ? '&' : '?';
    openPopup(url + 'paged=true&layout=popup');
}

function openPopup(url, id) {
    id = id || 'resource_window';
    var popup = new IframePopup(url, id);

    popup.render();
    return false;
}

function hackSearch() {
    if (!document.getElementById('search') || !document.getElementById('k'))
        return;

    document.getElementById('search').onsubmit = function(e) {
        window.location.href = 'http://finnfrem.storebrand.no/Proxy/results.aspx?k=' + document.getElementById('k').value.replace(/Ã¦/g, '%C3%A6').replace(/Ã¸/g, '%C3%B8').replace(/Ã¥/g, '%C3%A5').replace(/Ã&#8224;/g, '%C3%86').replace(/Ã&#732;/g, '%C3%98').replace(/Ã&#8230;/g, '%C3%85');
        return false;
    }
}

function openNewWindow(URLToOpen, w, h){
    params = "width=" + w + ",height=" + h + ", scrollbars=1, resizable=1, menubar=0, toolbar=1, location=0";
    popupWin = window.open(URLToOpen, 'stbwindow', params);

    return false;
}

/**
 * TEMPORARY function that adds the parameter ?layout=popup to the
 * links url. This works in the prototype, but is not required for
 * the popups to work. It provides an elegant fallback
 */
function tmp_prototype_getURL(href) {
    var pieces = href.split('#');

    if (pieces.length == 1)
        return href + '?paged=true&layout=popup'
    else
        return pieces[0] + '?paged=true&layout=popup#section_' + pieces[1];
}

