﻿/// <reference path="/system/js/jquery-1.4.2.js" />
/// <reference path="/system/js/jquery-1.4.1-vsdoc.js" />

$(document).ready(function () {
    var ie6 = false;
    if ($.browser.msie && $.browser.version < 7) {
        var ie6 = true;
    }

    if ($.browser.mozilla && $.browser.version < 1.9) {
        var ff2 = true;
    }

    if (ie6 == true || ff2 == true) {
        var userLang;
        var browsermsg = "<div id='browsermsg'><p>Visste du att du har en gammal version av webbläsaren? Klicka på någon av ikonerna för att ladda ner den senaste versionen och få den bästa upplevelsen</p> <ul><li><a target='_blank' href='http://www.google.com/chrome?hl=sv'><img title='Chrome 2.0+' src='/system/images/chrome-icon24.gif' alt='Chrome 2.0+' /></a></li><li><a target='_blank'  href='http://www.mozilla.com/sv-SE/firefox/'><img src='/system/images/ff-icon24.gif' alt='Firefox 3.6+' title='Firefox 3.6+' /></a></li><li><a target='_blank' href='http://www.microsoft.com/sverige/windows/internet-explorer/default.aspx'><img src='/system/images/ie-icon24.gif' alt='Internet Explorer 8+' title='Internet Explorer 8+' /></a></li><li><a target='_blank' href='http://www.opera.com/'><img src='/system/images/opera-icon24.gif' alt='Opera 9.5+' title='Opera 9.5+' /></a></li><li><a target='_blank' href='http://www.apple.com/se/safari/'><img src='/system/images/safari-icon24.gif' alt='Safari 3.0+' title='Safari 3.0+' /></a></li></ul></div>";
        $.ajax({
            url: "http://ajaxhttpheaders.appspot.com",
            dataType: 'jsonp',
            success: function (headers) {
                userLang = headers['Accept-Language'].toLowerCase().substring(0, 2);
                if (userLang != 'sv' && userLang != 'se') {
                    browsermsg = "<div id='browsermsg'><p>Do you know that your browser is out of date? To get the best expirience click one of the browser icons to download a newer version.<ul><li><a target='_blank' href='http://www.google.com/chrome?hl=sv'><img title='Chrome 2.0+' src='/system/images/chrome-icon24.gif' alt='Chrome 2.0+' /></a></li><li><a target='_blank'  href='http://www.mozilla.com/sv-SE/firefox/'><img src='/system/images/ff-icon24.gif' alt='Firefox 3.6+' title='Firefox 3.6+' /></a></li><li><a target='_blank' href='http://www.microsoft.com/sverige/windows/internet-explorer/default.aspx'><img src='/system/images/ie-icon24.gif' alt='Internet Explorer 8+' title='Internet Explorer 8+' /></a></li><li><a target='_blank' href='http://www.opera.com/'><img src='/system/images/opera-icon24.gif' alt='Opera 9.5+' title='Opera 9.5+' /></a></li><li><a target='_blank' href='http://www.apple.com/se/safari/'><img src='/system/images/safari-icon24.gif' alt='Safari 3.0+' title='Safari 3.0+' /></a></li></ul></div>";
                }
                $('body').append(browsermsg);
                $('#browsermsg').delay('1500').slideDown('slow');
                $('.bgcont').click(function () {
                    $('#browsermsg').slideUp('slow');
                });
            }
        });
    }

    $('a.popup').live('click', function (e) {
        var popupLink = $(this);
        $("#splashOverlay").overlay({
            top: 148,
            mask: {
                color: '#fff',
                loadSpeed: 200,
                opacity: 0.8
            },

            onBeforeLoad: function () {
                // grab wrapper element inside content
                var wrap = $('#splashOverlay').find('.content-wrap');
                // load the page specified in the trigger

                var imagestring = "<img src='" + $(popupLink).attr("href") + "' alt='" + $(popupLink).attr("alt") + "' />";
                $(wrap).empty().append(imagestring);

            },
            load: true
        });
        e.preventDefault();
    });

    /*show the menu to modern browsers*/
//    if (ie6 == false) {
//        /*Adjust pos. if submenu extends pagewidth*/
//        $('ul#main-navigation > li').each(function () {
//            var containerRightPos = $('div.page').offset().left + 898;
//            var menuRightPos = $(this).offset().left + $(this).children('ul').width();

//            if (menuRightPos > containerRightPos) {
//                var corrPos = menuRightPos - (containerRightPos - 10);
//                $(this).children('ul').css('left', -corrPos);
//            }
//        });

//        $('ul#main-navigation > li').hover(function () {
//            if ($(this).children('ul').size() > 0) {
//                var curA = $(this).children('a');
//                var ovt = setTimeout(function () { $(curA).removeClass('selected').addClass('ishover'); clearTimeout(ovt); }, 150);

//                $(this).children('ul').stop(true, true).delay(150).slideDown(200);
//            }
//            else {
//                $(this).addClass("active");
//            }
//        },
//        function () {
//            if ($(this).children('ul').size() > 0) {
//                var curA = $(this).children('a');
//                var out = setTimeout(function () { $(curA).removeClass('ishover'); clearTimeout(out); }, 250);

//                $(this).children('ul').stop(true, true).delay(250).slideUp(200);
//            }
//            else {
//                $(this).removeClass("active");
//            }
//        });

//        $('ul.sub li a').hover(function () {
//            $(this).animate({ 'padding-right': '0', 'padding-left': '6px' }, 100);
//        },
//        function () {
//            $(this).animate({ 'padding-right': '6px', 'padding-left': '0px' }, 100);
//        });
//    } // End show menu

    $("input.searchfield").focus(function (e) {
        if ($(this).val() == this.defaultValue) {
            $(this).val("");
        }
    });

    $("input.searchfield").blur(function () {
        if ($(this).val() == "") {
            $(this).val(this.defaultValue);
        }
    });

    /* Event gallery */
    if ($('body').hasClass('eventgallery')) {
        $('#imageul li:first-child').show().addClass("active");

        $('#navul li:first-child').addClass("active");
        var oldIndex = 0;

        $('#navul li').hover(function () {
            var curIndex = $('#navul li').index(this) + 1;

            clearInterval(fadeInterval);
            fadeInterval = '';
            oldIndex = $('#navul li.active').index() + 1;
            $('#imageul li:nth-child(' + curIndex + ')').stop(true, true).fadeIn(300).addClass('active');
            $('#navul li:nth-child(' + curIndex + ')').addClass('active');
            if (curIndex != oldIndex) {
                $('#imageul li:nth-child(' + oldIndex + ')').stop(true, true).fadeOut(300).removeClass('active');
                $('#navul li:nth-child(' + oldIndex + ')').removeClass('active');
            }
            oldIndex = curIndex;

        });

        if ($('#navul li').size() > 1) {
            fadeInterval = setInterval('fadeAnimation()', 2500);
        }
    }

    /* Footer share */
    $('#sharelink').hover(function () {
        $(this).children('ul').stop(true, true).slideDown(300);
        $(this).find('a').stop(true, true).delay(180).fadeIn(100);
    }, function () {
        $(this).children('ul').stop(true, true).slideUp(300);
        $(this).find('a').stop(true, true).fadeOut(80);
    });

    $('a.printlink').click(function (e) {
        e.preventDefault();
        var printheader = "<img id='printheader' class='displaynone' src='/system/images/print_body_bg.jpg' alt='Twinings' />";
        $('body').append(printheader);
        window.print();
    });


});

/*Fade animation used in event-gallery*/
function fadeAnimation(){
    var currentimg = ($('#imageul li.active') ?  $('#imageul li.active') : $('#imageul li:first'));
	var nextimg = ((currentimg.next().length) ? ((currentimg.next().hasClass('active')) ? $('#imageul li:first') : currentimg.next()) : $('#imageul li:first'));
    nextimg.fadeOut(300).addClass('active').fadeIn(300);
	currentimg.fadeOut(300).removeClass('active');

    var currentnav = ($('#navul li.active') ?  $('#navul li.active') : $('#navul li:first'));
	var nextnav = ((currentnav.next().length) ? ((currentnav.next().hasClass('active')) ? $('#navul li:first') :currentnav.next()) : $('#navul li:first'));
    nextnav.addClass('active');
	currentnav.removeClass('active');
}

function OnResponseEnd_showOverlay() {
    
    $("#overlay").overlay({
        top: 100,
        mask: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.5
        },
        load: true
    });
}
