﻿GIH = {};

$(document).ready(function () {
    GIH.FixOddRowsOnTables();
    GIH.SetupTopMenuLinks();
    GIH.SetupFoldingLists();
    GIH.SetupSearchOnEnter();
    GIH.AddNewWinIcon();
    GIH.AddSpanToDocLink();
    GIH.AddSocialIcon();
    //GIH.BlockRightClickOnImage();

    if ($('#internet-explorer6')[0] != null) {

        if (!$.cookie('ie6-open'))
            $($('#internet-explorer6')[0]).removeClass('hidden');

        $('.CloseIEInformation').click(function () {
            if (!$($('#internet-explorer6')[0]).hasClass('hidden')) {
                $($('#internet-explorer6')[0]).addClass('hidden');
                $.cookie('ie6-open', true);
            }
        });
    }
});

GIH.SetupSearchOnEnter = function () {

    if ($('#ctl00_TopSearch_SearchText')[0] != null) {
        $('#ctl00_TopSearch_SearchText').keydown(function (e) {
            if (e.keyCode == 13) {
                $(this).blur();
                $('#ctl00_TopSearch_LinkButtonSearch').focus().click();
            }
        });
    }

    if ($('#ctl00_ctl00_TopSearch_SearchText')[0] != null) {
        $('#ctl00_ctl00_TopSearch_SearchText').keydown(function (e) {
            if (e.keyCode == 13) {
                $(this).blur();
                $('#ctl00_ctl00_TopSearch_LinkButtonSearch').focus().click();
            }
        });
    }

    if ($('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucProfileSearchForm_txtQuery')[0] != null) {
        $('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucProfileSearchForm_txtQuery').keydown(function (e) {
            if (e.keyCode == 13) {
                $(this).blur();
                $('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucProfileSearchForm_btnSearch').focus().click();
            }
        });
    }

    if ($('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucSearchForm_SearchText')[0] != null) {
        $('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucSearchForm_SearchText').keydown(function (e) {
            if (e.keyCode == 13) {
                $(this).blur();
                $('#ctl00_ctl00_contentPlaceHolderPage_contentPlaceHolderContent_ucSearchForm_LinkButtonSearch').focus().click();
            }
        });
    }
}

GIH.SetupFoldingLists = function () {

    if ($('.billboard-section')[0] != null) {
        $('.billboard-section ul.start').quickTree();

        if ($($('.billboard-section ul.start').find('li')[0]).hasClass('last')) {
            var span = $($($('.billboard-section ul.start').find('li')[0]).find('span')[0]);
            if ($(span).hasClass('expand')) {
                span.css('background', 'none');
                span.click();                
            }
        }
    }

    if ($('.site-map')[0] != null)
        $('.site-map ul ul').quickTree();
}

GIH.SetupTopMenuLinks = function () {

    $('.SiteMapLink').click(function () {

        GIH.ToggleSiteMap(this);

        var focusable = jQuery('ul a', $('#header-sitemap'));
        if (focusable.length > 0) focusable[0].focus();

        return false;
    });

    $('.SiteMapClose').click(function () {

        GIH.ToggleSiteMap($('.SiteMapLink'));
        return false;
    });

    $('.popup-parent').each(function () {

        if ($(this).hasClass('Translate')) {
            GIH.SetupBubble($('#popup-translate'), this);
        }

        if ($(this).hasClass('Login')) {
            GIH.SetupBubble($('#popup-login'), this);
        }
    });

}

GIH.ToggleSiteMap = function (link) {

    var menuButton = $(link).find('img')[0];

    if ($('#header-sitemap').css('display') == 'block') {
        $(menuButton).attr('src', '/Templates/GIH/Images/slide-down-button.png');
        $(menuButton).attr('alt', 'Fäll ned webbkartan');
    }
    else {
        $(menuButton).attr('src', '/Templates/GIH/Images/slide-up-button.png');
        $(menuButton).attr('alt', 'Fäll upp webbkartan');
    }

    $('#header-sitemap').slideToggle('slow');
}

GIH.FixOddRowsOnTables = function () {

    $('table').each(function () {

        if (!$(this).hasClass('NoOddRows')) {
            $($(this).find('tr:odd')).addClass('odd');
        }
    });

}

GIH.DisplayInformationBubble = function (bubble) {
    var bubbleElement = $('#' + bubble);
    bubbleElement.css('display', 'block');
}

GIH.HideInformationBubble = function (bubble) {
    var bubbleElement = $('#' + bubble);
    if (bubbleElement != null)
        bubbleElement.css('display', 'none');
}

GIH.SetupBubble = function (bubble, parent) {

    $(parent).mouseover(function () {
        GIH.DisplayInformationBubble($(bubble).attr('id'));
    });

    $(parent).mouseout(function () {
        GIH.HideInformationBubble($(bubble).attr('id'));
    });

    $(bubble).mouseover(function () {
        GIH.DisplayInformationBubble($(bubble).attr('id'));
    });

    //fix select for IE
    if (navigator.appName != 'Microsoft Internet Explorer') {
        $(bubble).mouseout(function () {
            GIH.HideInformationBubble($(bubble).attr('id'));
        });
    } else {
        $('body').click(function () {
            GIH.HideInformationBubble($(bubble).attr('id'));
        });
    }
}

GIH.FunctionX = function() {
}
    function FunctionY() {
}

GIH.IsXPage = function() {
    return $('#X')[0] != null;
}

GIH.AddNewWinIcon = function() {
    $('#page-container a[target*="_blank"]').addClass('open-blank').append("<span class='icon'>(icon)</span>");
}

GIH.AddSpanToDocLink = function () {
    $("a.doc").append("<span class='icon'>(icon)</span>");
    $("a.pdf").append("<span class='icon'>(icon)</span>");
    $("a.rss").append("<span class='icon'>(icon)</span>");
}

GIH.AddSocialIcon = function () {
    $('#footer a[title="facebook"]').addClass('facebook').prepend("<span class='icon'>(icon)</span>");
    $('#footer a[title="twitter"]').addClass('twitter').prepend("<span class='icon'>(icon)</span>");
    $('#footer a[title="youtube"]').addClass('youtube').prepend("<span class='icon'>(icon)</span>");
}

/*
$('#customerInformationForm').keydown(function(e) {
    if (e.keyCode == 13) {
        //Stop the event
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = false;

        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
        return false;
    }
});    
*/

