/*
	functions.site.js
	JQuery / DOM Interaction Functions
	Creator: Matt Kircher
*/

/* GENERAL */
function setupPage(){
	translateEmails();		//changes unlinked email address to usable ones (spam protection)	
	
	//nav
	$('#main-nav, #functional-nav, #sub-nav, #footer-nav').find('> ul > li:last').addClass('end_nav');
	
	//clearing / heights / proximity
	$('.home #main-content, .home .collection_area, .carousel').addClass('clearfix');
	$('.button, .flourish_button').parent().addClass('clearfix');
	
	$('.grid2, .grid2_offset_left, .grid2_offset_right, .grid3, #footer, .double_list').addClass('clearfix').equalHeights();
	$('.home #main-content').equalHeights(565);
	
	$('p, ul, ol, bloackquote, div').find('+ :header').prev().css({ marginBottom:'25px' });
	
	//misc
		//borders
		$('#functional-links').corner("7px bottom");
		
		//home stuff
		$('.flourish_button').wrap('<span class="flourish_button_wrap"></span>');
		$('.flourish_button_wrap').addClass('clearfix')
		.parent().addClass('clearfix').css({ textAlign:'center' });
		
		if($('.home').length){
			var src = $('.home #main-content .col_l img').attr('src');
			$('.home #main-content .col_l').css({ background:'url('+src+') bottom left no-repeat transparent' });
		}
		
		//subscriber form
		$('#subscriberForm #subscriberEmail')
		.bind('focus', function(){
			if($.trim($(this).val()) ==  'Join our mailing list...'){ $(this).val(''); }
		}).bind('blur', function(){
			if($.trim($(this).val()) ==  ''){ $(this).val('Join our mailing list...'); }
		});
}

function setupCarousels(){
	
	//inventory specific
	if($('#inventory-carousel')){
		$('.inventory_image').colorbox({ current:"{current} of {total}", maxWidth:'80%', maxHeight:'90%' });
		
		//carousel interactivity
		$('#inventory-carousel .carousel > li').each(function(){
			if(!$(this).find('.blank_inventory_item').length){
				$(this).hover(
					function(){
						$(this).prepend('<div class="closer_view_overlay">View</div>');
						$(this).find('.closer_view_overlay').click(function(){ $(this).parent().find('.inventory_image').trigger('click'); })
						$(this).addClass('selected');
						$(this).stop().animate({ opacity:1 }, 300);
						
						$(this).siblings().removeClass('selected');
						$(this).siblings().stop().animate({ opacity:0.50 }, 300);
					},
					function(){ 
						$('.closer_view_overlay').remove();
						$(this).siblings().stop().animate({ opacity:1 }, 300);
					}
				);
			}
		});
		
		//carousel sub nav (sets and items)
		if($('#inventory-carousel .carousel > li').length){
			$('#sets-nav li.selected').each(function(index){			
				
				$(this).append('<ul id="items-nav"></ul>');				
				$('#inventory-carousel .carousel > li').each(function(){
					var id = $(this).attr('id');
					$('#items-nav').append('<li><a href="#'+id+'">'+$(this).find('.item_name').text()+'</a></li>');
				});
				
				$('#items-nav a').click(function(){
					var itm = $('#inventory-carousel .carousel > li'+$(this).attr('href'));
					
					$(itm).css({ opacity:0 }).prependTo($('#inventory-carousel .carousel')).addClass('selected').animate({ opacity:1 }, 300);
					$(itm).siblings().stop().removeClass('selected').animate({ opacity:0.50 }, 300);
					
					$(this).parent().addClass('.selected');
					$(this).parent().siblings().removeClass('.selected');
					return false;
				});
			});
		}
	}
	
	//collections specific
	if($('#collections-carousel')){
		if($('#collections-carousel').length > 0){
			$('#main-content').append('<div class="nav_runner clearfix"><h3 class="subhead">All Available Collections</h3><ul id="collections_listing_runner" class="runner_list clearfix"></ul></div>');
			
			$('#collections-carousel li').each(function(){
				var itm = $(this).clone();
				$(itm).find('img').remove().end()
				$(itm).find('a').html($(itm).find('.title').text());
				$('#collections_listing_runner').append($(itm));
			});
		}
	}
	
	//sets specific
	if($('#sets-carousel')){
		if($('#sets-carousel').length > 0){
			$('#main-content').append('<div class="nav_runner clearfix"><h3 class="subhead">All Available Sets</h3><ul id="sets_listing_runner" class="runner_list clearfix"></ul></div>');
			
			$('#sets-carousel li').each(function(){
				var itm = $(this).clone();
				$(itm).find('img').remove().end()
				$(itm).find('a').html($(itm).find('.title').text());
				$('#sets_listing_runner').append($(itm));
			});
		}
	}
	
	$('#collections-carousel, #sets-carousel, #inventory-carousel').each(function(){
		
		//get total width of the carousel
		var accumulated_width = $('.carousel > li').outerWidth(true) * $('.carousel > li').length;
		$(this).find('.carousel').width(accumulated_width);
		
		$('#inventory_content .col_l, #inventory_content .col_r').removeAttr('style');
		$('#inventory_content').equalHeights();
		
		//insert controls
		var controls = $('<a id="carousel-control-left" href="#" title="Previous item...">Previous</a><a id="carousel-control-right" href="#" title="Next item...">Next</a>');
		
		//if more than the container, create controls
		if(accumulated_width > $(this).outerWidth(false)){
			$(controls).css({ opacity:0 }).prependTo($(this));
			
			//left, 'previous' controls
			$('#carousel-control-left').click(function(){
				
				var container = $(this).parent();
				var carousel = $(container).find('.carousel');
				$(carousel).animate({ left:'200px' }, 500, function(){
					$(carousel).find('> li:last').css({ opacity:0 }).prependTo($(carousel)).animate({ opacity:1 }, 200);
					$(carousel).css({ left:'0px' });
				});
				
				return false;
			});
			
			//right, 'next' controls
			$('#carousel-control-right').click(function(){
				
				var container = $(this).parent();
				var carousel = $(container).find('.carousel');
				$(carousel).animate({ left:'-200px' }, 500, function(){
					$(carousel).find('> li:first').appendTo($(carousel));
					$(carousel).css({ left:'0px' });
				});
				
				return false;
			});
			
			//show hide controls on rollover
			$(this).hover(
					function(){ $('#carousel-control-left, #carousel-control-right').stop().animate({ opacity:1 }, 200); }, 
					function(){ $('#carousel-control-left, #carousel-control-right').stop().animate({ opacity:0 }, 200); }
			);
		}
	});
}

//makes email tags invisible to spiders / spammers
function translateEmails(){
	$('span.email, address.email').each(function(){
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		//EXAMPLE: <span class="email" title="link title | email address | email subject"> link content </span>		
		
		var inner_content = $(spt).html();						//inner HTML of span tag
		var t = $(spt).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
		
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@").replace(dot,".");				//replace words with chars
		
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed
		
		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});

