/**
 * Nana DC jQuery scripts
 *
 */
$(document).ready(function() {
	
	// homepage slideshow
	$('#rotate').nivoSlider({
		effect:'fade',
		directionNav:false,
		controlNav:true
	});
	
	// add featured banner to .featured images
	$("#tiles img.featured").each(function(index){
		$('<img src="'+$(this).attr('rel')+'assets/images/featured.png" class="featured-banner" alt="Featured" />').insertAfter($(this));

		//this.append('<img src="'+$(this).attr('rel')+'assets/images/featured.png" class="featured-banner" alt="Featured" />');
	});
	
	// add buttons to homepage slideshow
	$("#rotate .nivo-control").each(function(){
		$(this).attr('id', 'button'+$(this).attr('rel'));
	});

	// open all external links in new window
	$('html a:not([rel]=noleave)').filter(function(){
		var theHref = this;
		var shopurl = "bode-graham-and-kertzmann7860.myshopify.com";

		if (theHref.hostname && theHref.hostname !== location.hostname && theHref.hostname !== shopurl) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)){
					
					// Build tracking url
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;

					// Track 
					
				};
			});
		};
	});

	// product details swap thumbnails
	$("#product-details #thumbs .thumb").click(function() {
		// set large photo to thumb
		$("#product-details .large").attr('src', $(this).attr('rel'));
	});

	// store current links
	$('#shopify .subnav a').each(function() {
		if( $(this).attr('href').indexOf('?q=') == -1)
		{
			if ($(this).attr('href')  ===  window.location.pathname) {
				$(this).addClass('current');
			}
		}
		else
		{
			// search page
			var searchTerm = location.search.replace('?q=','');
			var title = $(this).attr('title');

			if( searchTerm == title)
			{
				$(this).addClass('current');
			}
			
		}
	});
	
	// jquery validation
	$("#add-comment").validate({
		errorPlacement: function(error,element) {
			return true;
		},
		rules:
		{
			name:
			{
				required: true
			},
			email:
			{
				required: true,
				email: true
			},
			
			comment:
			{
				required: true
			}
		},
		errorClass: "invalid"
	});

});
