$(document).ready(function(){
  
    if(!window.location.href.match(/home/i)) return ;
  
      var home = (function(){
        
              var h = {
                     dom : {
                        hSlider : $('#homeSlider'),
                        hiPad : $('#sliderIpad'),
                        hLi : $('#homeSlider li'),
                        hItem : $('.homeItem'),
                        input : $('#input'),
                        hFooter : $('#footerMenu'),
                        titleFooter : $('#titleFooter'),
                        thumb : $('.thumb'),
                        homeItem : $('#homeSlider li .homeItem'),
                        nav : $('#homeSlider'),
                        video : $('#video'),
                        myVimeo : $('#footerMenu .myVimeoHome'),
                        jScrollbar_mask : $('.jScrollbar_mask'),
                        jScrollbar : $('.jScrollbar')
                      },
                      
                      lth : {
                       items : $('#homeSlider').find('li').length
                      },
                      
                      w : {
                        wItem : $('#homeSlider').find('li:eq(0)').outerWidth(true),
                        wSlide :($('.homeItem').get().length * $('#homeSlider').find('li:eq(0)').outerWidth(true)) - getWidth(),
                        wDrag :($('.homeItem').get().length * $('#homeSlider').find('li:eq(0)').outerWidth(true))
                      },
                      
                      bool : {
                            hover : true,
                            move : true,
                            click : true
                      },
                      
                      size : {
                        callapse : 160,
                        expand : 400,
                        diff : 240
                      }
                      
              }
    
            //
            var arr = [], cVideo = 0 ;      
            
            h.dom.myVimeo.each(function(index,elm){
                  arr.push($(elm).html());
                  $(elm).empty();
            });
            
            //fire fn
            play(arr[0],setCurrent(0));
            move(h.dom.hSlider);
             
            h.dom.hItem.each(function(index,elm){
              var self = $(this),vimeo;
              $(this).bind('click',function(e){
                    e.preventDefault();
                    //h.bool.move = false;
                    //h.dom.hSlider.draggable('disable');
                    kill();
                    play(arr[index]);
                    setCurrent(index);
               });                     
            });
              
            //put items into Array to use Later
            var _items = [];
            var _itemReverse = [];
            h.dom.hLi.each(function(index,elm){
                _items.push(index);
                _itemReverse.push(index);
                $('#homeSlider li:eq('+index+')').css({left:(h.w.wItem*index),marginTop:h.size.expand});
            });
            
            //Display Items Now
            showItems();
            
            //
            h.dom.hLi.bind('mousedown',function(e){
              h.bool.move = true;    
              move(h.dom.hSlider);
              console.log(' mousedown :: ready');
            });
            
            /*
              when the document is loaded you can not hide Slider 
            */
            _sHide = true;
            
            h.dom.hSlider.bind('mouseup',function(){
                //h.bool.move = false;
                h.bool.hover = true;
                _sHide = false;
                //move(h.dom.hSlider);
                console.log('hSlider  mouseup ::  ready can hide : ', _sHide);
            }).bind('mouseleave',function(){
                //you can hide 
                _sHide = true;
                hideSlider();
                console.log(' hSlider mouseleave :: ready can hide : ', _sHide);
            }).bind('mouseover',function(){
                //you can not hide 
                _sHide = false;
                console.log(' hSlider mouseover :: ready can hide : ', _sHide);
            });
            
            
            if(!isiPad() || !isiPhone()){
                var _xPos = log = 0, _fadeDuration = 5000;
                var _timer = 0;
                
                $('h3.title').bind('mouseover',function(e){
                  _timer =  0;
                    if(_sHide){
                        //show slider 
                        clearTimeout(_timer);
                        toggleShow();
                    }
                    console.log('click footer can hide ' , _sHide);
                    
                });
                
                
                function hideSlider(){
                  console.log('hideSlider() : hover : ', h.bool.hover, ' Hide Slider : ', _sHide );
                  if(!_sHide) return;
                  _timer = setTimeout(function(){
                    //hide Slider
                    toggleShow(true);
                  },_fadeDuration);
               }
                
                
                var _backTo = -211;
                function toggleShow(y){
                  if(y){
                      //hide
                      hideItems(function(){
                        h.dom.hFooter.stop(true,true).animate({bottom:_backTo},'slow');
                        h.dom.titleFooter.animate({bottom:_backTo},500,'easeInOutCubic');
                        h.dom.nav.stop(true,true).animate({bottom:-420},'slow');  
                      });
                      _sHide = true;
                      console.log('toggleShow() :  Hide Slider : true' );
                  }else{
                    //show
                      h.dom.hFooter.stop().animate({bottom:0},{queue:false,duration:0});
                      h.dom.nav.stop().animate({bottom:25},{queue:false,duration:0});
                      h.dom.titleFooter.stop().animate({bottom:0},300,function(){
                        showItems();  
                      }); 
                      _sHide = false;
                      console.log('toggleShow() :  Hide Slider : false Timer :: ', _timer );
                  }
                }  
            }
            
            //
            h.dom.hLi.bind('mouseover',function(e){
                var index = $(this).index();
                h.bool.hover = true;
                _sHide = false;
                mHover();
                expand(index);
                hilight(index,true);
                scroll();
            });
            
            
            
            
            //
            h.dom.hLi.bind('mouseleave',function(){
              mLeave();
              collapse();
            });
          
          
          var hideTimer = 0;
          function showItems(){
                clearTimeout(hideTimer);
                $.each(_items,function(index){
                  $('#homeSlider li:eq('+index+')').stop(true,false).delay(100*index).animate({marginTop:h.size.diff},600,function(){
                    if(!isiPad()){
                        //hide Slider After few Sec
                        hideSlider();   
                    }
                    else console.log('i am iPad,  you can not hide slider');
                  }); 
                });
                
            }
            
            
            _itemReverse.reverse();
            function hideItems(callBack){
              if(!_sHide) return;
              $.each(_itemReverse,function(index){
                    $('#homeSlider li:eq('+_itemReverse[index]+')').delay(200*index).animate({marginTop:h.size.expand},800,'easeInOutCubic',function(){
                      //Reset Slider's Position
                      h.dom.hSlider.animate({left:0},'slow',function(){  
                    });
              });
                  
                });
                if(callBack){
                  var t = (_itemReverse.length - 1) * 200;
                  hideTimer = setTimeout(function(){
                    callBack();
                  },t);
                }
            }
              
           
          function play(vimeo,callBack){
                kill();  
                h.dom.video.html(vimeo);
                setWidth($('iframe'),getWidth());
                setHeight($('iframe'),getHeight());
                var iframe = document.querySelector('iframe');
                isReady(iframe);
                if(callBack) callBack();
          }
          
          
          
          function isReady(iframe){
               var self = iframe;
               $f(self).addEvent('ready', function(){
                    cVideo++;
                    $f(self).addEvent('finish',function(){
                        if(cVideo > (arr.length-1)) cVideo = 0;
                        play(arr[cVideo]);
                        setCurrent(cVideo);
                    });
                });
          }
           
           
           function kill(){h.dom.video.empty();}
           
           
           
            function scroll(){
              $('.jScrollbar').jScrollbar();
            }
 
      
               //
            function move(elm){
                  console.log('move :: ' + h.bool.move);   
              if(h.w.wDrag <= getWidth() || !h.bool.move) return;
              
              h.bool.hover = false;
              h.bool.move = true;
              _sHide = false;
              
              $(elm).addClass('move')
                .draggable({
                    axis: "x",
                    start : function(event, ui){
                        //console.log('drag starts move = ' + h.bool.move) ;
                    },
                    stop : function(event, ui){
                        var w = (parseInt($('#homeSlider').css('width')) - getWidth()) + 40;  
                        if(ui.position.left >= 0) $(this).animate({left:0},'slow');
                        else if(ui.position.left < -(h.w.wSlide)) $(this).animate({left:-w},'slow');  
                        switchClass();
                        h.bool.move = false;
                        
                        if(isiPad() || isiPhone()){      
                              $(elm).draggable('disable');
                        }
                    }
                });
            }
            
            
            
            
            function addEvent(element, eventName, callback) {
                  if (element.addEventListener) {
                      element.addEventListener(eventName, callback, false);
                  }
                  else {
                      element.attachEvent(eventName, callback, false);
                  }
            }
              
            
            function updatePosSlider(){
              $(window).resize(function(){
                  OuterSpace = (getWidth() - (h.lth.items * h.w.wItem));
                  marginleft = parseInt($('#homeSlider').css('left'));
                  percentLeft = marginleft/marginleft;
                  if(marginleft<0) h.dom.hSlider.css('left',(OuterSpace*percentLeft) - 40);
              });
            }
            
            
            
            function expand(index){
              if(!h.bool.hover || isiPad() || isiPhone()) return;
              
              h.dom.hFooter.stop().animate({height:h.size.expand},'slow',function(){});
              h.dom.hLi.stop().animate({height:h.size.callapse,marginTop:h.size.diff},'slow');
              $('#homeSlider li:eq('+index+')').stop().animate({height:h.size.expand,marginTop:0},'slow');
            }
            
            
            
            function collapse(){
              if(!h.bool.hover) return;
              h.dom.hLi.stop()
                      .animate({height:h.size.callapse,marginTop:h.size.diff},400,function(){})
                      .queue(function(){
                        $(this).dequeue();  // only for buffer
                      });
              h.dom.hFooter.stop().animate({height:174},400,function(){});
            }
            
            
            
            //
            function mHover(){
              if(!h.bool.hover) return;
              switchClass();
              _sHide = false;
            }
            
            
            //
            function mLeave(){
                if(!h.bool.hover) return;
                switchClass(true);
                hilight(-1);
            }
            
            
            
              
            //
            function focusMe(){
                  h.dom.input.focus(function(){h.dom.hLi.live('hover')}).click();
                  //console.log('focusMe :: ready');
            }
            
            
            
            function switchClass(c){
              if(c) h.dom.hSlider.addClass('move');
              else h.dom.hSlider.removeClass('move');  
            }
            //
            
            
            //
            function hilight(index,c){ 
              if(c) $('#homeSlider li:eq('+index+') .homeItem').stop().fadeTo('slow',1);
              else h.dom.homeItem.stop().fadeTo('slow',.4);
              
              //console.log('hilight : ready  index = ' + index);
            }
           //
           
           
            function restWidth(){
              h.dom.hSlider.css({width:(h.lth.items * h.w.wItem)});
              h.dom.hiPad.css({width:(h.lth.items * h.w.wItem)}); 
            }
            
            
            function setCurrent(index){
                h.dom.homeItem.removeClass('currentVideo');
                $('#homeSlider li:eq('+index+') .homeItem').addClass('currentVideo');
                //console.log('setCurrent : index = ' + index);
            }
            
            
            h.init = function(){
              restWidth();
              updatePosSlider();
            }
              
              
       
            return h;       
                            
        })();
      
      home.init(); 
});
