/**
 * @jp_global => ACTIONS for PC & EWT
 */


(function($){
    function initMenu(){
        function menuShow(){
            var jElm = $(this);
            if(!$.browser.msie) {
                jElm.stop(true, true)
                    .css({
                        opacity: 0
                    })
                    .animate({
                        opacity: 1
                    }, {
                        duration: 300
                    });
            }
            $('ul:first', this).stop(true, true).css({
                marginTop: '-' + jElm.height() + 'px'
            }).animate({
                marginTop: '-1px'
            }, {
                duration: 300
            });
        }
        function menuHide(ui){
            var jElm = $(this);
            $('ul:first', this).animate({
                marginTop: '-' + jElm.height() + 'px'
            }, {
                duration: 300,
                complete: function(){
                    $(this).css({
                        marginTop: '-1px'
                    });
                    ui.element.removeClass('over');
                }
            });
            if (!$.browser.msie) {
                jElm.animate({
                    opacity: 0
                }, {
                    duration: 300,
                    complete: function(){
                        jElm.css({
                            opacity: 1
                        });
                        ui.element.removeClass('over');
                    }
                });
            }
        }
        function minMaxWidth(type, ui){
            var width = ui.menuitem.menuitem.outerWidth(), minWidth = 0, minWidthIMG = 0;
            ui.menuitem.submenu.css({
                'visibility': 'hidden'
            });
            ui.menuitem.element.addClass('over');
            
            $('img', ui.menuitem.submenu[0]).each(function(){
                var sW = $(this).width();
                if (sW > minWidthIMG) {
                    minWidthIMG = sW;
                }
            });
            $('span:not(.bl,.br)', ui.menuitem.submenu[0]).each(function(){
                var sW = $(this).width();
                if (sW > minWidth) {
                    minWidth = sW;
                }
            });
            
            minWidth += minWidthIMG;
            
            ui.menuitem.element.removeClass('over');
            ui.menuitem.submenu.css({
                'visibility': ''
            });
            
            minWidth += 23;
            if (minWidth > width) {
                width = minWidth;
            }
            ui.menuitem.submenu.css({
                'width': width + 'px'
            });
        }
        $('#nav-global').menubar({
            selSubmenu: 'div',
            needToActivate: false,
            closeOnLeave: true,
            showAnim: menuShow,
            hideAnim: menuHide
        }).bind('menuitembeforeOpen', minMaxWidth);
        
        $('#nav-meta').menubar({
            selSubmenu: 'div',
            needToActivate: false,
            closeOnLeave: true,
            showAnim: menuShow,
            hideAnim: menuHide
        }).bind('menuitembeforeOpen', minMaxWidth);

        $('#nav-global > li > a[class=hasSubmenu]').click( function(e) {
             if( ! $(this).parent().hasClass('clickable') ) {
               e.preventDefault();
             }
         });

    }
    
    
    $.triumpgBgImg = (function(){
        var viewport, imgSize, imgDim, staticImg, imgNum;
        function calcViewport(){
            viewport = {
                height: $(window).height(),
                width: $(window).width()
            };
        }
        
        function calcImgSize(img){
            if (img[0]) {
                imgDim = {
                    height: img.height(),
                    width: img.width()
                };
            }
        }
        
        function adjustBgImg(){
            img = staticImg;
            calcViewport();
            
            var windowRatio = viewport.width / viewport.height, imgRatio = imgDim.width / imgDim.height;
            if (windowRatio < imgRatio) {
                img.css({
                    width: 'auto',
                    height: viewport.height + 'px'
                });
            }
            else {
                img.css({
                    width: '100%',
                    height: 'auto'
                });
            }
        }
        function showNext(){
        
            staticImg = $('#bg-img img:last');
            calcImgSize(staticImg);
            adjustBgImg(staticImg);
            $('#overlay').animate({
                opacity: 0.9,
                backgroundColor: '#ffffff'
            }, {
                duration: 400,
                complete: function(){
                    staticImg.css({
                        opacity: 1
                    });
                    $('#bg-img img:first').remove();
                }
            }).animate({
                opacity: 0.5,
                backgroundColor: '#000000'
            }, {
                duration: 400
            });
        }
        function loadNext(path){
            var img = new Image();
            img.src = path;
            $(img).css({
                opacity: 0
            }).appendTo($('#bg-img')[0]);
            if (img.complete) {
                showNext();
            }
            else {
                $(img).load(showNext);
            }
            
        }
        
        return {
            init: function(){
                staticImg = $('#bg-img img:first');
                if (staticImg && staticImg[0]) {
                    if (staticImg[0].complete) {
                        calcImgSize(staticImg);
                        adjustBgImg(staticImg);
                        $(window).bind('resize', adjustBgImg);
                    }
                    else {
                        staticImg.bind('load', function(){
                            calcImgSize(staticImg);
                            adjustBgImg(staticImg);
                        });
                        $(window).bind('resize', adjustBgImg);
                    }
                }
                
            },
            loadNext: loadNext
        
        };
    })();
    (function($){
        $.imgPreLoad = (function(){
            var srcList = [], ready = false, started = false;
            function loadImg(){
                if (srcList.length) {
                    started = true;
                    var src = srcList.shift(), img = new Image();
                    img.src = src;
                    if (img.complete) {
                        loadImg();
                    }
                    else {
                        $(img).load(loadImg);
                    }
                }
                else {
                    started = false;
                }
            }
            return {
                add: function(src){
                    srcList.push(src);
                    if (ready && !started) {
                        loadImg();
                    }
                },
                ready: function(){
                    ready = true;
                    loadImg();
                }
            };
        })();
        $(window).bind('load', $.imgPreLoad.ready);
    })(jQuery);
    function callGlobal(){
        initMenu();
        $.triumpgBgImg.init();
        /* IhatE U */
        if ($.browser.msie) {
            var overlay = $('#overlay'), opac = (overlay.is('.mod')) ? 0 : 0.5;
            overlay.css({
                'opacity': opac
            });
        }
    }
    $(callGlobal);
})(jQuery);