(function($) {
    
    var global =  this, carousel, cursor, drag = false, nLastVisualTop, nLastMoveTop, jvisual, nCoreHeight, nVisualHeight,
        box = global.box,
        loe = global.loe,
        timeNav;
    /*var AUTOPLAY = true;*/
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
    
    var pagination = {
            bottom:0,
            add:function(){
            var html = '<ul id="mainPagin" class="pagination">'+
            '<li><a href="#" class="prev"><span>Previous</span></a></li>'+
            '<li class="counter"></li>'+
            '<li><a href="#" class="next"><span>Next</span></a></li>'+
            '</ul>';
            $('#visual').append(html);
            pagination.bottom = parseInt($('.pagination').css('bottom').replace('px',''),10);
        },
        move:function(){
            var fHeight = parseInt($('#footer').css('bottom').replace('px',''),10);
            $('#mainPagin').css('bottom',fHeight+ pagination.bottom );
        }
    };
    
    var moveVisual = function(oEvt) {
        var nTop = nLastVisualTop + oEvt.pageY - nLastMoveTop;
           if(nTop > 0) { nTop = 0; }
        else if(nTop - nCoreHeight < - nVisualHeight) { nTop = nCoreHeight - nVisualHeight; }
        nLastVisualTop = nTop;
        nLastMoveTop = oEvt.pageY;
        jvisual.css({ top: nTop });
    };
    
    var endMoveVisual = function(oEvt) {
        oEvt.preventDefault();
        loe.jDoc.unbind('mousemove mouseup');
    };
    
    var moveCursor = function(oEvt){
        cursor.css({
          top:oEvt.pageY+2,
          left:oEvt.pageX+2 
       });
    };
    
    var startMoveVisual = function(oEvt) {
        oEvt.preventDefault();
        if(oEvt.type ===  'mousemove'){
            setTimeout(function(){
            moveCursor(oEvt);
            },33);
        }
        if(oEvt.type === 'mousedown'){
            nLastVisualTop = parseInt(jvisual.css('top'), 10) || 0;
            nLastMoveTop = oEvt.pageY; 
            drag = true;
        }
        if(oEvt.type === 'mouseup') {drag = false;}
        
        if(drag){
            setTimeout(function(){
            moveVisual(oEvt);
            },33);
        }
    };
     
    var setSlide = function(){
       var current = box.ui('carousel.slider').current,
            slide = $('#carousel li.slide:eq('+current+')');
        if(slide.hasClass('drag')){
            if($.browser.opera || $.browser.chrome){
                $(document.body).addClass('noCursor');
            }
            
            if(!slide.find('img').parent().is('div')){
                slide.find('img, ul').wrapAll('<div />');
            }
            if(!document.getElementById('cursor')){
                $(document.body).append('<img src="' + BASE_SKIN_URL + 'images/common/drag-cursor.png" alt="" id="cursor" />');
            }
            cursor = $('#cursor');
            jvisual = slide.find('div');
            nVisualHeight = slide.find('img').height();
            nCoreHeight = $('#carouselInner').height();

            jvisual.bind('mousemove mousedown mouseup',startMoveVisual);
        }
        loe.checkLogoColor(current);
    };
    
    var activeScroll = {
        
        create:function(){
            $('.scroll').each(function(){     
                box.ui('scroll').create({
                    element: $(this),
                    bar: true,
                    buttons: true
                });
                            
                activeScroll.set($(this));
            });
            
         },
        set:function(obj){
           var mainHeight = Math.round($('#carousel').height()),
               wrapperArticleHeight = obj.parent().height(), 
             
            scrollHeight = activeScroll.getArticleHeight(obj);              
            obj.css({
            'height':scrollHeight
            });      
            obj.find('.scrollbar').height(scrollHeight);
                    
            box.ui('scroll.'+ obj.getBoxDatas('id')).compute(); 
            
            activeScroll.centerScroll(obj,mainHeight,wrapperArticleHeight);       
        },
        getArticleHeight:function(obj){
             var articleHeight = Math.round(obj.height()),
                reviewHeight = Math.round($('#carousel').height() * 0.6);
                            
             if(articleHeight > reviewHeight){
                 //active scroll
                 return reviewHeight;
             }
        },
        centerScroll:function(obj,mainHeight,wrapperArticleHeight){           
             obj.parent().parent().css({
                    top: Math.round((mainHeight - wrapperArticleHeight)/2)
             });
         }
    };        
    
    var checkUrlAnchor = function(){
        var urlHash = window.location.hash;
        if(urlHash.indexOf('slide') !== -1){
            
            //$('#visual').append('<div style="background-color:white;position:absolute;top:0;left:0;z-index:2;width:'+$('#visual').width()+'px;height:'+$('#visual').height()+'px" id="maskAnchor"></div>');
            //loe.loader.add($('#maskAnchor'),false);
            
            if(typeof carousel !== 'object'){
                return;
            }
            
            var id = urlHash.replace('#','');
            var slide = carousel.objCarousel.element.find(id);

            //setTimeout(function(){
                var nDuration = carousel.objCarousel.duration;
                delete carousel.objCarousel.duration;
                carousel.objCarousel.moveToItem(slide.index()+1);
                carousel.objCarousel.duration = nDuration;
                carousel.preloadImg(true);
                
                /*if(slide.index()===0){
                    loe.loader.remove($('#maskAnchor'),false);
                    $('#maskAnchor').remove();
                }*/
            //},300);
        }
    };
    
    var showMenu = function() {
        loe.headerMainNav.open();

        timeNav = setTimeout(function() {
            loe.headerMainNav.close();
        }, 4000);
        
        if($('#contextual a.next').size() === 1 && !ie6){
            setTimeout(function() {
                if(AUTOPLAY){
                    window.location = $('#contextual a.next').attr('href');
                }
            }, 15000);
        }
        
        $('#header-main-nav').mouseover(function(){
            clearTimeout(timeNav);
            AUTOPLAY = false;
        });
    };
    
    loe.display = {
        resize:function(nCoreWidth,nCoreHeight,jVisual){
            if(typeof carousel === 'object'){
                if(AUTOPLAY && carousel.objCarousel.autoplaying){
                	AUTOPLAY = false;
                    carousel.objCarousel.endAutoplay();
                }
                carousel.resize(loe.nCoreWidth,loe.nCoreHeight,loe.nCoreWidth);
                setSlide();
            }
            
            //resize scrolls
            $('.scroll').each(function(){
                activeScroll.set($(this));
            });
            if($('#carouselCampaign').length){
                box.fire({ type: 'resizePopin' });
            }
        },
        ready:function(){
            loe.preloadVisual($('#visual #carousel li.slide:first img'));
            
            if($('#carousel li.slide').size() > 1) {
                pagination.add();
            }
            
            $('#carousel .scroll').each(function(){
                box.ui('scroll').create({
                    element: this,
                    bar:true,
                    buttons:true,
                    moveBy:20
                });
            });
            
            box.bind({
                'mainCarouselReady': function() {

                    
                    checkUrlAnchor();
                }
            });
            
            if($('body').hasClass('cms-what-s-new-master-of-leather')){
                loe.carousel = carousel = new loe.CarouselElastic($('#carousel'),loe.nCoreWidth,loe.nCoreHeight,loe.nCoreWidth,'slider',{
                    horizontal: true,
                    display: 1,
                    circular:true,
                    paginate:false,
                    duration: 600,
                    buttons: false,
                    counter:true,
                    outPagination:'#mainPagin',
                    autoPreload: false
                });
            }else{
                loe.carousel = carousel = new loe.CarouselElastic($('#carousel'),loe.nCoreWidth,loe.nCoreHeight,loe.nCoreWidth,'slider',{
                    horizontal: true,
                    display: 1,
                    circular:true,
                    paginate:false,
                    duration: 600,
                    buttons: false,
                    counter:true,
                    outPagination:'#mainPagin'
                });
            }
            loe.bulletsManager.init();
            loe.setContextualInCenter();
            //activeScroll.create();
            
            $('.slide a.video').click(function(e){
                e.preventDefault();
                loe.popinManager.open($(this));
            });
            
            
            if($(document.body).hasClass('cms-index-index')){
                showMenu();
            }
        }
    };
    
    box.bind({
        'endmove.carousel.slider': function(evt){
            if(/^#slide\d+$/.test(window.location.hash)) {
                window.location.hash = '#slide' + (box.ui('carousel.slider').current + 1);
            }

            //loe.setCounter();
            setSlide();
            /*if(document.getElementById('maskAnchor')){
                setTimeout(function(){
                    loe.loader.remove($('#maskAnchor'),false);
                    $('#maskAnchor').remove();
                },1000);
            }*/
        },
        'startmove.carousel.slider': function(){
        	AUTOPLAY = false;
        },
        'visualready': function(){
            
        },
        'bulletsopen':function(){
        	AUTOPLAY = false;
        }
    });
}(jQuery));
