var preImgs = new Array();
var preImg = new Image();
$('#home-hero a.img').each(function(){
	$(this).append('<img src="'+$(this).attr('rel')+'"/>').removeAttr('rel');
}).parent().nivoSlider({
	effect:	'fold',
	slices: 10,
	animSpeed: 800,
	pauseTime: 5000,
	directionNav: false,
	controlNav: false,
	pauseOnHover: true,
	manualAdvance: false, 
	captionOpacity: 0
});
/** Product Thumbnail Viewer **/
$('#assets #thumbnails').each(function(){
	if($(this).find('li').length == 0) return;
	var increment = $(this).find('li').width() + 2;
	$(this).width($(this).find('li').length * increment)
		.find('a').click(function(){
			$(this).closest('ul').find('img').css('opacity', '.3').removeAttr('class')
				.end().end().find('img').css('opacity', '1').addClass('selected');
			$('#hero img').stop().clone().appendTo('#hero')
				.addClass('fade')
				.attr('src', $(this).attr('rel'))
				.attr('title', $(this).attr('title'))
				.attr('alt', $(this).attr('title'))
				.fadeIn(function(){
					$(this).removeAttr('style').removeClass('fade').prev().remove();
				});			
			$(this).closest('div').next().find('a').attr('href', $(this).attr('rel').replace(/uploads/, 'uploads/print').replace(/jpg$/, 'tif'));
			if($(this).hasClass('hr'))
				$(this).closest('div').next().find('a').css('visibility', 'visible');
			else
				$(this).closest('div').next().find('a').css('visibility', 'hidden');
			
		}).each(function(){
			preImgs.push($(this).attr('href'));
			$(this).attr('rel', $(this).attr('href')).removeAttr('href');
		}).closest('ul').wrap('<div id="thumbnails-window"/>')
			.parent().height($(this).find('li').height())
			.after('<div id="thumbnails-control">'
				+'<span class="l" title="Scroll Left">&lt;</span>'
				+'<span class="r" title="Scroll Right">&gt;</span>'
				+ ( $('body').hasClass('single-product') ? '<a>Download Hi-Res Image &gt;</a></div>' : '')
			).next().find('.l').click(function(){
				l = $(this).parent().prev().find('ul');
				l.find('li:gt('+(l.find('li').length-3)+')').prependTo(l)
					.parent().css('left', (increment*-2)+'px').animate({left : 0}, 300, 'swing')			
			}).end().find('.r').click(function(){
				$(this).parent().prev().find('ul')
					.animate({left: increment*-2}, 300, 'swing', function(){
						$(this).find('li:lt(2)').appendTo($(this))
						$(this).css('left', '0px');				
					});	
			}).parent().prev().find('img').hover(
				function(){
					$(this).addClass('hover').closest('ul').find('img').stop().attr('rel', 'no')
						.filter('.hover').removeAttr('rel').animate({opacity: 1}, 'fast').end()
						.filter('[rel=no]').css('opacity', .3);
				},
				function(){
					$(this).removeClass('hover').closest('ul').find('img').stop()
						.filter(function(){
							return $(this).hasClass('selected') ? 0 : 1;
						}).css('opacity', .3).removeAttr('rel').end()
						.filter('img.selected').animate({opacity: 1}, 'slow');
				}
			).filter(':first').parent().click();
	if($(this).find('li').length <= 4) 
		$('#thumbnails-control span').remove();
		
	$('a.all').appendTo('#thumbnails-control');
});
/** Product Asset Viewer **/
$('.product .options').prepend('<ul id="options-nav"/>')
	.find('ul.swatches').each(function(){
		var increment = $(this).find('li').width() + 2;
		var name = $(this).prev().text();
		$(this).wrap('<div class="swatches-window"/>')
			.parent().wrap('<div class="swatches-holder"/>')
			.parent().prepend('<div class="preview"><div><img/></div><span/></div>')
			.find('a').click(function(){
				$(this).closest('.swatches-holder').find('.preview img')
					.attr('src', $(this).attr('rel')).parent().next().html($(this).attr('title'));
			}).each(function(){
				if($(this).closest('ul').attr('id') == 'resources') return;			
				preImgs.push($(this).attr('href'));
				$(this).attr('rel', $(this).attr('href')).removeAttr('href');
			}).filter(':first').click();			
		$(this).closest('.swatches-holder').attr('id', $(this).attr('id'))
			.prev().wrapInner('<a href="#'+$(this).attr('id')+'"/>')
			.wrap('<li/>').parent().appendTo('#options-nav');			
		if($(this).find('li').length <= 12) return;
		$(this).width(Math.ceil($(this).find('li').length / 3) * increment);
		$(this).parent().parent().append(
			'<div class="swatches-control">'
			+'<span class="l" title="Scroll Left">&lt;</span>'
			+'<span class="r" title="Scroll Right">&gt;</span>'
			+'More '+name+'</div>'
		).find('.l').click(function(){
			u = $(this).closest('.swatches-holder').find('ul');
			u.stop();
			d = parseInt(u.css('left')) - u.parent().width();
			u.animate({left: d});
			d -= u.parent().width();
			if(u.width() < d*-1) $(this).hide();		
			$(this).parent().find('.r').show();
		}).end().find('.r').click(function(){
			u = $(this).closest('.swatches-holder').find('ul');
			u.stop();		
			d = parseInt(u.css('left')) + u.parent().width();
			u.animate({left: d});
			if(d == 0) $(this).hide();
			$(this).parent().find('.l').show();
		}).hide();
	}).end().tabs();
/** Preload Images **/
for(i=0; i<preImgs.length; i++){
	s = preImgs[i];
	preImgs[i] = new Image();
	preImgs[i].src = s;
}
/** Locator **/
$('.locate .col ul').hide().prev().css('cursor', 'pointer').click(
	function(){$(this).next().toggle(200);},
	function(){$(this).next().toggle(200);}
)
/** FAQ **/
$('body.faq h3').next().hide().prev().css('cursor', 'pointer').click(
	function(){$(this).next().toggle(200);},
	function(){$(this).next().toggle(200);}
);
/** Menu Image Rollovers **/
/*
for(u in menuImages){
	i = preImgs.length;
	preImgs[i] = new Image();
	preImgs[i].src = menuImages[u];
}
$('#menu-mega li:first>ul').append('<img id="menu-preview" src="'+HTPATH+'/style/img/trans.gif" alt=""/>').find('ul ul a').hover(
	function(){$('#menu-preview').attr('src', menuImages[$(this).attr('href').replace(/\W+/g, '-')]);},
	function(){$('#menu-preview').attr('src', HTPATH+'/style/img/trans.gif');}
);
*/
/** Menu Tabs **/
$('#menu-mega>li>ul.sub-menu').each(function(){
	$(this).prepend('<a class="tab" href="'+$(this).prev().attr('href')+'">'+$(this).prev().text()+'<span/></a>');
});
/** Search Form **/
$('#header input')
	.focus(function(){ $(this).addClass('focus').prev().addClass('focus');})
	.blur(function(){ $(this).removeClass('focus').prev().removeClass('focus');})
