//change className of html from .noJS to .hasJS.
(function (el) { el.className = el.className.replace("noJS", "hasJS") })(document.documentElement);

/* device detection */
var userAgent = navigator.userAgent;
var device = {
    iPad: userAgent.match(/iPad/),
    iPhone: userAgent.match(/iPhone/),
    iPod: userAgent.match(/iPod/),
    iOS: userAgent.match(/(iPhone|iPod|iPad)/),
    blackberry: userAgent.match(/BlackBerry/),
    android: userAgent.match(/Android/),
    webOS: userAgent.match(/webOS/)
};
isMobileDevice = false;

var tabs = null;

//Initial bind
$(document).ready(function () {

    /* add class to body if mobile*/
    if (device.iOS || device.android || device.webOS) {
        isMobileDevice = true;
        $("body").addClass("mobile-device");
    } else if (device.iPhone || device.iPod) {
        setTimeout(scrollTo, 0, 0, 1);
    }

    $("#header .search-bar").each(function () {
        $(this).hover(function () {
            $(this).addClass("hover");
        }, function () {
            $(this).removeClass("hover");
        });
    });


    /* search */
    $(".search-bar").each(function () {
        var self = $(this),
				searchScope = self.find(".scope"),
				searchQuery = self.find(".query"),
				searchSubmit = self.find(".submit");

        searchQuery.keydown(function (e) {
            if ((e.keyCode || e.which) === 13) {
                searchSubmit.trigger("click");
                return false;
            }
        });

        searchSubmit.bind('click', function () {
            if (searchScope.length) {
                if (searchScope.val() != "THIS") {
                    window.location = self.attr("data-action") + '?k=' + encodeURI(searchQuery.val()) + '&s=' + searchScope.val();
                }
                else {
                    window.location = self.attr("data-action") + '?k=' + encodeURI(searchQuery.val()) + '&cs=This Site&u=' + self.attr("data-url-scope");
                }
            }
            else {
                if (self.attr("data-scope")) {
                    window.location = self.attr("data-action") + '?k=' + encodeURI(searchQuery.val()) + '&s=' + self.attr("data-scope");

                }
                else {
                    window.location = self.attr("data-action") + '?k=' + encodeURI(searchQuery.val()) + '&cs=This Site&u=' + self.attr("data-url-scope");
                }

            }
            return false;

        });
    });

    /* site navigation */
    function megamenu(menuItem, cmd) {
        if (menuItem.hasClass("has-megamenu")) {
            $thisMenu = menuItem.find(".megamenu");
        } else {
            $thisMenu = menuItem.find(".dropdown-nav");
        }

        if (cmd == "show") {
            menuItem.find("a:first").addClass("menuhover");
            $thisMenu.slideDown();
            var megamenuHeight = menuItem.find(".megamenu:first .megamenu-subnav").height();
            menuItem.find(".megamenu:first .megamenu-content .pane").css('min-height', megamenuHeight);
        } else {
            $thisMenu.slideUp();
            menuItem.find("a:first").removeClass("menuhover");
        }
    }
    if (!isMobileDevice) {
        $(".menu-item").hoverIntent({
            over: function () { megamenu($(this), "show"); },
            timeout: 100,
            out: function () { megamenu($(this), "hide"); }
        });
    } else {
        $(".menu-item").hover(function () {
            megamenu($(this), "show");
        }, function () {
            megamenu($(this), "hide");
        });
    }



    $(".megamenu .nav").each(function () {
        //$(this).tabs($(this).parent().next().find(".pane"), { event: "mouseover" });
        $(this).tabs($(this).parent().next().find(".pane"), { event: "dblclick" });
        $(this).find("a").hoverIntent({
            sensitivity: 50,
            over: function () { $(this).dblclick(); },
            timeout: 50,
            out: function () { }
        });

    });



    $('.text-tabs').each(function () {
        $this = $(this);
        if (document.location.hash == "") {
            var a = $this.find('li[data-initial-tab] a');
            if (a.length) {
                document.location.hash = a.attr('href');
            }
        }
        tabs = $this.tabs(".textcontent-wrap", { history: true, onClick: function () {
            if (typeof (s) != "undefined" && typeof (s.pageName) != "undefined") {
                s.pageName = s.pageName.split("#")[0] + "#" + this.getCurrentTab().text();
                s.t();
            }
        }
        }).data('tabs');
    });


    //Link tracking
    $(".sc-link").click(function () {
        $this = $(this);
        varType = $this.attr('data-sc-vartype');
        varValue = $this.attr('data-sc-varvalue');
        eventType = $this.attr('data-sc-eventtype');
        if (typeof (s_account) != "undefined" && eventType) {
            var s = s_gi(s_account);
            s.linkTrackEvents = eventType;
            s.events = eventType;
            if (varType && varValue) {
                s.linkTrackVars = varType + ',events';
                eval("s." + varType + " = varValue;");
                s.tl(this, 'o', eventType + ' : ' + varValue);
            }
            else {
                s.linkTrackVars = 'events';
                s.tl(this, 'o', eventType);
            }
        }
        return true;
    });



    if (!isMobileDevice) {
        /* country select box in the header */
        $("#country-selector .dropdown").hoverIntent({
            over: function () {
                dropdownSelect = $(this).parent().parent().find("dt a");
                dropdownSelect.addClass("hover");
                dropdown = $(this).parent().parent().find("dd ul");
                dropdown.slideDown(400);
            },
            timeout: 200,
            out: function () {
                dropdown.slideUp(400, function () {
                    dropdownSelect.removeClass("hover");
                });
            }
        });


        /* country select box on corporate */
        $("#country-selector-big .dropdown").hoverIntent(function () {
            dropdownSelect = $(this).parent().parent().find("dt a");
            dropdownSelect.addClass("hover");
            dropdown = $(this).parent().parent().find("dd ul");
            dropdownHeight = dropdown.height();
            dropdown.height(0).show().animate({ 'height': dropdownHeight + 'px', 'top': '-' + Number(dropdownHeight + 75) + 'px' });
        }, function () {
            dropdown.animate({ 'height': "0px", 'top': "-75px" }, function () {
                dropdownSelect.removeClass("hover");
                $(this).hide().removeAttr("style");
            });
        });


        function shortcutSelect(thisItem, cmd) {
            dropdown = thisItem.parent().parent().find("dd ul");

            if (cmd == "show") {
                dropdown.slideDown(400);
            } else {
                dropdown.slideUp(400);
            }
        }
        /* shortcuts dropdown */
        $(".shortcut-select").hoverIntent({
            over: function () {
                shortcutSelect($(this), "show");
            },
            timeout: 200,
            out: function () {
                shortcutSelect($(this), "hide");
            }
        });

    } else {
        $(".shortcut-select").hover(function () {
            shortcutSelect($(this), "show");
        }, function () {
            shortcutSelect($(this), "hide");
        });


        $(".country-select, #country-selector-big .dropdown").parent().find("select").focusin(function () {
            dropdownSelect = $(this).parent().parent().find("dt a");
            dropdownSelect.addClass("hover");
        }).focusout(function () {
            dropdownSelect.removeClass("hover");
        }).change(function () {
            window.location.href = $(this).val();
        });

    }


    /* accordion-list hover fix */
    $(".accordion-list .head a:first-child").hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });


    /* light box*/
    $(".lightbox-image, .ntElement-lightbox-image").fancybox({
        padding: 0,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        'titlePosition': 'inside'
    });

    $(".lightbox-video, .lightbox-iframe, .ntElement-lightbox-iframe, .ntElement-lightbox-video").each(function () {
        $lightbox = {};
        $lightbox.width = parseInt($(this).attr("data-lightbox-width") ? $(this).attr("data-lightbox-width") : 640);
        $lightbox.height = parseInt($(this).attr("data-lightbox-height") ? $(this).attr("data-lightbox-height") : 360);

        if ($(this).attr("data-lightbox-width") == undefined && $(this).attr("data-lightbox-hight") == undefined) {
            $lightbox.width = parseInt($.urlParam("width", $(this).attr("href")));
            $lightbox.height = parseInt($.urlParam("height", $(this).attr("href")));
        }

        $(this).fancybox({
            'width': $lightbox.width,
            'height': $lightbox.height,
            'autoScale': false,
            'padding': 0,
            'type': 'iframe',
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'easingIn': 'easeOutBack',
            'easingOut': 'easeInBack',
            'titlePosition': 'inside'
        });

    });

    $statusIframe = $("#status-iframe");
    if ($statusIframe.length) {
        $statusIframe.after("<iframe id='status-iframe-info' frameborder='0' scrolling='no' src='" + $statusIframe.attr("data-statusinfo-url") + "'></iframe>");
        $statusIframeInfo = $("#status-iframe-info");
        $statusIframe.hover(function () {
            $statusIframeInfo.fadeIn();
        }, function () {
            $statusIframeInfo.fadeOut();
        });
    }


});

$(window).load(function () {
    $scrollable = $(".scrollable");
    if ($scrollable.length > 0) {
        $scrollable.each(function () {
            $el = $(this);

            var $panels = $el.find(".panels");
            $panels.find(".panel:first").addClass("firstPanel");
            var hasPaging = $el.hasClass("has-paging");
            $panel = $el.find(".panel")

            var conf = {
                speed: 500
					, autoplay: false
					, paging: hasPaging
					, interval: 5000
					, loop: true
					, width: null
					, height: null
            }

            var meta = $el.metadata();
            conf = $.extend(conf, meta);

            if ($el.hasClass("scrollable-vertical")) {
                $el = $el.find(".panels-container");

                var api = $el.scrollable({ items: '.panels', vertical: true, speed: conf.speed });

                if (conf.paging) {
                    api.navigator({ navi: ".scrollable-vertical .nav", activeClass: 'current', indexed: false })
                }
            } else {
                if (!conf.width) {
                    conf.width = $el.width();
                }
                if (!conf.height) {
                    conf.height = $panels.height();
                }

                $el.height(conf.height).width(conf.width);

                $panelPadding = $panel.width() - $panel.innerWidth();
                if ($.browser.msie) {
                    $panelPadding = $panelPadding + 1; // IE8 Fix: 1px float panel roundup bug 
                }
                $panel.width(conf.width + $panelPadding + 1);

                var api = $el.scrollable({ items: '.panels', circular: conf.loop, speed: conf.speed, easing: conf.easing });

                if (conf.paging) {
                    // page counter fix for ie7
                    if ($.browser.msie && parseInt($.browser.version, 10) == 7) {
                        var $panelCount = $el.find(".panel-count");
                        $panel.each(function () {
                            $panelCount.append("<a href='#'>&nbsp;</a>");
                        });
                    }
                    api.navigator({ navi: ".panel-count", naviItem: 'a', activeClass: 'current', indexed: false })
                }

            }

            if (conf.autoplay) {
                api.autoscroll({ autoplay: true, interval: conf.interval, api: true })
            }
        });
    }
});

// get querystring
$.urlParam = function (name, searchString) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(searchString);
    if (!results) { return 0; }
    return decodeURIComponent(results[1].replace(/\+/g, " ")) || 0;
}
