//////////////////////////////////////////
//  Delay Plugin
/////////////////////////////////////////
(function($){
	$.fn.delay = function(time, callback){
		jQuery.fx.step.delay = function(){};
		return this.animate({delay:1}, time, callback);
	}
})(jQuery);
//////////////////////////////////////////
//  Thumbnail Hover Effects
/////////////////////////////////////////
(function($){
	$.fn.subtleFade = function(){
		this.css({'opacity' : .65 });
		this.each(function(){
			var $this = $(this);
			$this.hover(function(){
				$this.stop().animate({'opacity': 1});
			}, function(){
				$this.stop().animate({'opacity': .65});
			});
		});
		return this; 
	}
})(jQuery);

$(window).load(function() {
	//////////////////////////////////////////
	//  Home Slideshow
	/////////////////////////////////////////

	$('#home div.slide').cycle({speed: 1000, timeout: 5000}); 
	
	$('#home .slide .first').animate({
      'opacity': 1
  });
/*
  	jQuery('#home div.slide img:first').bind('load', function() {
  		$this = $(this);
  		$('#home div.slide').css('height', $this.height()).css('width', $this.width()).fadeOut(0).fadeIn(1000);
  	});
*/
});

$(document).ready(function(){
	// Open external links in new window
	$('a[rel*="external"]').click(function(){
		window.open(this.href);
		return false;
	});
	//////////////////////////////////////////
	//  Accordian Navigation
	/////////////////////////////////////////
	$('#primeNav>li:has(ul)>a, #primeNav>li>ul>li:has(ul)>a').click(function(){
		if ($(this).hasClass('open')) {
			$(this).removeClass('open').siblings('ul').slideUp();
		} else {
			$(this).addClass('open').siblings('ul').slideDown();
			$(this).parent('li')
			.siblings().children('a')
			.removeClass('open')
			.siblings('ul').delay(500).slideUp();
		}
	});
	
	//////////////////////////////////////////
	//  Stockists Layout
	/////////////////////////////////////////
//	$('ul.stockists li:nth-child(3n+1)').css({
//		'width' : '10.3333em',
//		'margin-right': '5.75em'
//	});
//	$('ul.stockists li:nth-child(3n+2)').css({
//		'width' : '11.5em',
//		'margin-right': '5.75em'
//	});
//	$('ul.stockists li:nth-child(3n+3)').css({
//		'width' : '11em',
//		'margin-right': '0'
//	});	

	//////////////////////////////////////////
	//  Stockists Pager
	/////////////////////////////////////////
/*
	$('#stock #primeContent').cycle({
		fx: 'fade',
		timeout: 0,
		speed: 500
	});
	var stk = $('#primeContent ul.stockists');
	if (stk.length > 1) {
		$('div#primeContent').after('<ul id="pager">');
		$('#pager').after('<a href="#" class="next">&gt;</a>');
		$('#stock div#primeContent').cycle({ 
		    fx:     'scrollHorz', 
		    speed:  500, 
		    timeout: 0,
		 	next: 'a.next',
		    pager:  '#pager',
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#">'+ (idx + 1) +'</a></li>'; 
			}
		});
	}
*/
	
	//////////////////////////////////////////
	//  Gallery
	/////////////////////////////////////////

	var tGroup = $('#secondaryContent ul.thumbs');

	if (tGroup.length > 1) {
		$('div#secondaryContent').after('<div class="controls"><ul id="pager"></div>');
		$('#pager').after('<a href="#" class="next">&gt;</a>');
		$('#secondaryContent').cycle({ 
			fx:     'scrollHorz', 
			speed:  500, 
			timeout: 0,
			next: 'a.next',
			pager:  '#pager',
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#">'+ (idx + 1) +'</a></li>'; 
			}
		});
	}

	var cycleOpts = {
		fx:     'fade', 
		speed:  500, 
 		timeout: 0, 
		next: '.slide img', 
		pager:  'ul.thumbs',
		pagerAnchorBuilder: function(idx, slide) { 
			return 'ul.thumbs li:eq(' + idx + ') a'; 
		}
	}


	$('.slide li:nth-child(10n) img, .slide li:last-child img').click(function() { 
		$('.controls a.next').trigger('click');
		return false; 
	});
	
	$('.gallery .thumbs li').addClass('loader');

	$('.gallery ul.slide').hide().cycle(cycleOpts);

	$('.gallery .thumbs li a img').hide().subtleFade();

	//////////////////////////////////////////
	//  Fancy Box
	/////////////////////////////////////////
	if(jQuery.fn.fancybox) {
		$("a.box").fancybox({ 
			'padding' : 0,
			'hideOnContentClick': true, 
			'zoomOpacity' : 0.5,
			'overlayOpacity' : 0.7,
			'imageScale' : true, 
			'zoomSpeedIn' : 500,
			'zoomSpeedOut' : 400 
		});
	}

});


//////////////////////////////////////////
//  PSUEDO Loader
/////////////////////////////////////////

var i = 0;
var int = 0; // for IE
$(window).bind('load', function() {
	var int = setInterval('loadPic(i)', 100);
});

function loadPic() {

	var thumbs = $('.gallery .thumbs li a img').length;
	var slides = $('.slide img').length;

	if (i >= thumbs) {
		clearInterval(int);
	}
	if (i >= slides) {
		clearInterval(int);
	}

	$('img:hidden').eq(0).fadeIn(500).parents().removeClass('loader');
	$('.slide').fadeIn(800).parents('#primeContent').removeClass('loader');

	i++;
}
