
//  Overlabel
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.not('.readonly').css("display", "none");
            }
            control.focus(function () {label.not('.readonly').css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};

// Cufon 
var helvetica = [
  '#primaryNavigation a',
  'h1, h2, h3, h4',
  'legend',
  '#galleryPage a.submit',
  'a.more',
  'a.donate',
  'a.return',
  '.sizes th:not(.select-option), #shoppingCart th',
  '#miniCartTotals .checkout',
  '#checkout #cartTools a'
];
Cufon.replace(helvetica, {
	hover: true
});

// Cycle
function initCycle(elem, type) {
  var len = $(elem).find('>*').length;
  
  var opts = {};
  opts.fx = 'fade';
  if (type === 'auto') {
    opts.timeout = 3000; 
  }
  if (type === 'thumbs') {
    var id = $(elem).parents('.product').attr('id');
    opts.timeout = 0;
    opts.pager = $('#'+ id +' .thumbs ul');
    opts.pagerAnchorBuilder = function buildPager(idx, slide) {
        return '#'+ id +' .thumbs ul li:eq(' + idx + ') a';
    }; 
  }
  
  if (len > 1) {
    $(elem).cycle(opts);  
  }

}

// Fancybox
function initFancybox(elem, type) {
  var opts = {};
  opts.padding = 0;
  if (type = 'signup') {
    opts.autoScale = false;
    opts.autoDimensions = false;
    opts.titleShow = false;
    opts.scrolling = 'no';
    opts.height = 785;
    opts.width = 420;
    opts.onComplete = function() {
      Cufon.replace(helvetica);
      $('#gallerySubmit label').overlabel();
      $('#gallerySubmit form').validate();   
    };
    
  }
  $(elem).fancybox(opts);
}

//  Document Ready
$(document).ready(function(){
  
  // Open Links in New Window
  $('a[rel*=external]').click(function(){
     window.open(this.href);
     return false;
   });
   
  // Tooltips
  $('#learnMore a').each(function(){
    var $this = $(this),
        title = $this.attr('title');
    
    var x = $this.find('img').width();
    
    var html = '';  
    html += '<div class="tooltip">';
    html += '<div class="inner">';
    html += '<div class="cap left"></div>';
    html += '<div class="content">'+title+'</div>';
    html += '<div class="cap right"></div>'; 
    html += '</div>';
    html += '</div>';
    
    $this.hover(function() {
        $this.parents('li').append(html);
        $this.parents('li').find('.tooltip').css({
            'left': -75     
        });
    },
    function() {
        $('.tooltip').remove();
    });
  
  });
  
  // Cycles
  if ($('body:not(#shopPage) .slide').length) {
    initCycle('.slide', 'auto');
  } 
  
  // Fancybox
  if (jQuery.fn.fancybox) {
    $('a.box').each(function(){
      elem = this;
      type = $(this).attr('rel');
      initFancybox(elem, type);
    });
  }

  //  Contact Form
  $('#contactForm label').overlabel();
  $('#contactForm form').validate();
  
  
  // Lazyload
  $("#galleryArchive img.lazy").lazyload({ placeholder : "/media/images/king.jpg", "effect" : "fadeIn" });
  
});
