var cycle = {
  init: function() {
    $('.cycle').each(function(){

      var $cycle = $(this);

      var id = $(this).attr('id');

      // Effects
      switch(id){
        case 'gallery-thumbs':
          var fx = 'fade';
          break;
        case 'spotlight':
        case 'catch-of-the-day':
        case 'look-of-the-day':
          var fx = 'none';
          break;
      }

      // Remove empty li
      $(this).find('.pager li').remove();

      // Initialise the cycle
      var $cycleItems = $(this).find('.items')
        .cycle({
          next: $(this).find('.next'),
          prev: $(this).find('.prev'),
          pause: 1,
          fx: fx,
          pagerEvent: 'mouseover',
          activePagerClass: 'active',
          pager: $(this).find('.pager'),
          pagerAnchorBuilder: function(id, slide) {
            return '<li><a class="title" href="' +  $cycle.find('.items li:eq(' + id + ') a').attr('href') + '">' + $cycle.find('.items li:eq(' + id + ') .title').text() + '</a><p class="text">' + $cycle.find('.items li:eq(' + id + ') .desciption').text() + '</p></li>';
          }
        });

      // If less or equal then 1 item hide the nav links
      if($(this).find('.items li').length <= 1){
        $(this).find('.prev, .next').remove();
      }

      // Custom pager click
      $('#subspotlight .pager li').bind('click', function() {
        window.location = $(this).find('.title').attr('href');
      });

      // Pause the sub spotlights
      switch(id){
        case 'catch-of-the-day':
        case 'look-of-the-day':
        case 'gallery-thumbs':
          $cycleItems.cycle('pause');
          break;
        case 'spotlight':
          // Pause on prev/next hover
          $('.prev, .next', $(this))
            .data('cycle', $cycleItems)
            .hover(
              function() {
                $(this).data('cycle').cycle('pause');
              },
              function() {
                $(this).data('cycle').cycle('resume');
              }
            );
          break;
      }
    });
  }
}

var jumpTo = {
  init: function() {
    $('.jump-to')
      .bind('click', function(e){
        e.preventDefault();
        $('html, body').animate({scrollTop:$($(this).attr('href')).offset().top}, 400);
      });
  }
}

var testResults = {
  init: function() {
    $('.testresults')
      .find('li .text')
      .hide()
      .end()
      .find('li:first-child .text')
      .show()
      .end()
      .find('li .toggle')
      .bind('click', function(e) {
        e.preventDefault();
        $(this).parent().parent().parent().find('.text').toggle();
      });
  }
}

var fancybox = {
  init: function() {
    if(typeof(openFancybox) != 'undefined'){
      $.fancybox({
        'href': openFancybox,
        'type': 'iframe'
      });
    }

    $('a[rel*=fancybox]').each(function(){
      if($(this).hasClass('iframe')){
        $(this).attr('href', $(this).attr('href') + '?fancybox=true');
      }

      if($(this).hasClass('fancybox-external')){
        var boxScrolling = 'auto';
        var width = 600;
        var height = 400;
      } else {
        var boxScrolling = 'no';
        var width = 600;
        var height = 400;
      }

      $(this).fancybox({
        'overlayOpacity': 0.8,
        'overlayColor': '#0c0c09',
        'scrolling': boxScrolling,
        'width': width,
        'height': height,
        'showNavArrows': false
      });
    });

    $(function() {
      fancybox.outerHeight();
    });

  },

  outerHeight: function() {
    if($('#iframe-popup').length > 0){
      parent.fancybox.resize($('#iframe-popup').outerHeight(), 600);
    }
    if($('#popup-iframe').length > 0){
      parent.fancybox.resize($('#popup-iframe').outerHeight(), 600);
    }
    if($('#popup-gallery').length > 0){
      parent.fancybox.resize($('#popup-gallery').outerHeight(), 770);
    }
  },

  resize: function(height, width){
    $('#fancybox-inner').css({
      'height': height + 'px',
      'left': '0',
      'top': '0',
      'width': width + 'px'
    });
    $('#fancybox-wrap').css({
      'height': height + 'px',
      'width': width + 'px'
    });

    /* Native fancybox center function */
    $.fancybox.center();
  }
}

var faq = {
  init: function() {
    $('#faq ul')
    .find('.title')
    .bind('click', function() {
      $(this)
        .parent()
        .find('.text')
        .slideToggle(100);
    });
  }
}

var popupAd = {
  init: function() {
    $('#popup-advertisement').delay(6000).fadeOut();
  }
}

var imagePoll = {
  init: function() {
    $('#image-poll .vote li').live('click', function() {
      $.ajax({
        type: 'POST',
        url: $('#image-poll form').attr('action'),
        data: 'opt=' + $(this).find('input').attr('value'),
        cache: false,
        success: function(data) {
          $('#image-poll .block').replaceWith(data);
        }
      });
    });
    $('#image-poll .btn-more').live('click', function(e) {
      e.preventDefault();
      $('#image-poll').load($(this).attr('href'));
    });
  }
}

$(function() {

  $('body').addClass('js');

  $('.list, .list-secondary')
    .not('.fhNoHover')
    .find('li')
    .fhHover();

  jumpTo.init();
  cycle.init();
  testResults.init();
  fancybox.init();
  faq.init();
  popupAd.init();
  imagePoll.init();

  $('#tag_selection select').bind('change', function() {
    $('#tag_selection').submit();
  });

  $('form input[type=file], input[type=text]').gzPrettyForms();

  $('a[rel*=external]').attr('target', '_blank');

});
