var app = {
  init: function() {
    this.searchInputSetup();
    this.setupNavDrawer();
    this.setupSlides();
    $('.has-simple-tip').each(function() {
        var tipHaver = $(this)
           ,txt = tipHaver.attr('title');
        
        tipHaver.hover(function() {
            var tip = $('<div/>', {id: 'simple-tip', text: txt}).appendTo('body');
            
            tipHaver.attr('title', '');
            
            tip.position({
                my    : 'center bottom'
               ,at    : 'center top'
               ,of    : tipHaver
               ,offset: '0 -5'
            });
        }, function() {
            tipHaver.attr('title', txt);
            
            $('#simple-tip').remove();
        });
    });
    $('.datepicker').datepicker();
    if(swfobject.hasFlashPlayerVersion('10')) {
        $('.youtube-video').click(function() {
            $.fancybox({
                'padding'       : 0
               ,'autoScale'     : false
               ,'transitionIn'  : 'none'
               ,'transitionOut' : 'none'
               ,'title'         : this.title
               ,'width'         : 680
               ,'height'        : 495
               ,'overlayColor'  : '#000'
               ,'overlayOpacity': 0.8
               ,'href'          : ($.data(this, 'href.tabs') || this.href).replace(new RegExp("watch\\?v=", "i"), 'v/')
               ,'type'          : 'swf'
               ,'swf' : { 'wmode' : 'transparent', 'allowfullscreen' : 'true' }
            });
            return false;
        });
        $('.vimeo-video').click(function() {
            $.fancybox({
                'padding'       : 0
               ,'autoScale'     : false
               ,'transitionIn'  : 'none'
               ,'transitionOut' : 'none'
               ,'title'         : this.title
               ,'width'         : 680
               ,'height'        : 495
               ,'overlayColor'  : '#000'
               ,'overlayOpacity': 0.8
			   ,'href'          : ($.data(this, 'href.tabs') || this.href).replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1')
               ,'type'          : 'swf'
            });
            return false;
        });
    }
    $('.show-image').fancybox({
        'overlayColor'  : '#000'
       ,'overlayOpacity': 0.8
    });
    var catDialog = $('#cat-dialog').dialog({
        autoOpen: false
       ,dialogClass: 'cat-dialog panel'
       ,modal: true
       ,resizable: false
       ,width: 400
    });
    $('.cat-dialog-open').click(function(event) {
        event.preventDefault();
        catDialog.dialog('open');
    });
    $('#background').each(function(index, background) {
        var imageHeight = 600
           ,minHeight = $('#container').outerHeight()
           ,minImages = Math.round(minHeight/imageHeight);
        
        if($(background).outerHeight() > minHeight) return;
        
        if(minImages*imageHeight < minHeight) minImages++;
        
        minImages--
        
        $(background).css({
            'height': minHeight
           ,'overflow': 'hidden'
        });
        
        for (var i=0; i < minImages; i++) {
            $('img', background).clone().appendTo(background);
        };
    });
  },
  searchInputSetup: function() {
    $('.search-input').focus(function() {
      $(this).parent().addClass('not-empty');
    });

    $('.search-input').blur(function() {
      var class_name = 'not-empty';
      if ($(this).val().length > 0) {
        $(this).parent().addClass(class_name);
      } else {
        $(this).parent().removeClass(class_name);
      };
    });
  },
  setupNavDrawer: function() {
    $('.nav-drawer-handle').click(function() {
      $(this).parent().toggleClass('closed');
      $(this).parent().parent().toggleClass('has-closed-drawer');
    });
  },
  setupScroll: function(pane) {
    var pane = $(pane);
    if (pane.hasClass('setup')) return;
    var paneHight = pane.height(),
        content = $('.scroll-content', pane),
        contentHeight = content.height(),
        heightDiff = contentHeight - paneHight;
    if (heightDiff > 0) {
      var scrollBar = $('.scroll-bar', pane),
          scrollBarHandle = $('.ui-slider-handle', pane),
          factor = paneHight / contentHeight,
          scrollBarHandleHeight = Math.round(paneHight * factor),
          scrollBarHeight = paneHight - scrollBarHandleHeight;
      scrollBar.css({
        'height': scrollBarHeight,
        'top': (paneHight - scrollBarHeight) / 2
      });
      scrollBarHandle.css({
        'height': scrollBarHandleHeight,
        'margin-bottom': 0 - (scrollBarHandleHeight / 2)
      });
      scrollBar.slider({
        orientation: 'vertical',
        range: 'min',
        min: 0,
        max: 100,
        value: 100,
        slide: function( event, ui ) {
          content.css('top', 0 - (heightDiff - ((heightDiff * ui.value) / 100)));
        }
      });
    };
    pane.addClass('setup');
  },
  setupSlides: function() {
    $('.slides-horizontal-container').each(function(index, container) {
        var container = $(container)
           ,mask = $('.slides-mask', container)
           ,maskWidth = mask.width()
           ,shifters = $('.slides-shifter', container)
           ,shifterNext = $('.slides-shifter-next', container)
           ,shifterPrevious = $('.slides-shifter-previous', container)
           ,shelf = $('.slides', container)
           ,shelfWidth = shelf.width()
           ,slides = $('.slide', container)
           ,maxLeft = 0
           ,current = 0
           ,positions = [];
        shelf.each(function(index, element) {
            var clone = $(this).clone().css({
                'width'     : 'auto'
               ,'position'  : 'absolute'
               ,'visibility': 'hidden'
               ,'display'   : 'block'
            }).appendTo('body');
            shelfWidth = clone.width();
            $(this).width(shelfWidth);
            maxLeft = maskWidth - shelfWidth;
            clone.remove();
        });
        if (shelfWidth > maskWidth) {
            slides.each(function(index, slide) {
                var left = $(this).position().left
                   ,marginLeft = parseInt($(this).css('margin-left').replace('px',''))
                   ,pos = 0 - (left + marginLeft);
                positions.push(pos);
            });
            shifterNext.addClass('active');
            shifters.click(function() {
                if ($(this).hasClass('active')) {
                    if ($(this).hasClass('slides-shifter-next')) {
                        var next = current + 1
                           ,positionNext = positions[next];
                        if (parseInt(shelf.css('margin-left')) + shelfWidth >= maskWidth) {
                            current = next;
                            shelf.css('margin-left', positionNext > maxLeft ? positionNext : maxLeft);
                            if (!shifterPrevious.hasClass('active')) shifterPrevious.addClass('active');
                            if (positionNext < maxLeft) $(this).removeClass('active');
                        } else {
                            $(this).removeClass('active');
                        };
                    } else if ($(this).hasClass('slides-shifter-previous')) {
                        if (shelf.css('margin-left').replace('px','') < 0) {
                            current = current - 1;
                            shelf.css('margin-left', positions[current]);
                            if (!shifterNext.hasClass('active')) shifterNext.addClass('active');
                            if (positions[current] == 0) $(this).removeClass('active');
                        } else {
                            $(this).removeClass('active');
                        };
                    };
                };
                return false;
            });
        };
    });
    $('.slides-vertical-container').each(function(index, container) {
        var container = $(container)
           ,mask = $('.slides-mask', container)
           ,maskHeight = mask.height()
           ,shifters = $('.slides-shifter', container)
           ,shifterNext = $('.slides-shifter-next', container)
           ,shifterPrevious = $('.slides-shifter-previous', container)
           ,shelf = $('.slides', container)
           ,shelfHeight = shelf.height()
           ,slides = $('.slide', container)
           ,maxTop = maskHeight - shelfHeight
           ,current = 0
           ,positions = [];
        console.log(positions);
        if (shelfHeight > maskHeight) {
            slides.each(function(index, slide) {
                var position = 0 - $(this).position().top;
                if (index > 0 && position == positions[positions.length - 1]) return;
                positions.push(position);
            });
            shifterNext.addClass('active');
            shifters.click(function() {
                if ($(this).hasClass('active')) {
                    if ($(this).hasClass('slides-shifter-next')) {
                        var next = current + 1
                           ,positionNext = positions[next];
                        if (parseInt(shelf.css('margin-top')) + shelfHeight >= maskHeight) {
                            current = next;
                            shelf.css('margin-top', positionNext > maxTop ? positionNext : maxTop);
                            if (!shifterPrevious.hasClass('active')) shifterPrevious.addClass('active');
                            if (positionNext < maxTop) $(this).removeClass('active');
                        } else {
                            $(this).removeClass('active');
                        };
                    } else if ($(this).hasClass('slides-shifter-previous')) {
                        if (shelf.css('margin-top').replace('px','') < 0) {
                            current = current - 1;
                            shelf.css('margin-top', positions[current]);
                            if (!shifterNext.hasClass('active')) shifterNext.addClass('active');
                            if (positions[current] == 0) $(this).removeClass('active');
                        } else {
                            $(this).removeClass('active');
                        };
                    };
                };
                return false;
            });
        };
    });
  }
};
