/*
	functions.site.js
	JQuery / DOM Interaction Functions
	Created: July 6, 2009
	Creator: Matt Kircher
*/

/* GENERAL */
function setupPage(){
	
	applyIE6FlickerFix();	//IE6 Flickering issue
	translateEmails();		//changes unlinked email address to usable ones (spam protection)
	
	//cufon
		Cufon.replace(' #copyright, #footer-nav li a', { fontFamily: 'Usherwood Medium', hover:true });
		Cufon.replace('#functional-nav li a, #main-nav li a, #content :header, #sub-nav li a', { fontFamily: 'Usherwood Black', hover:true });
	
	//navigation
		$('#main-nav > ul, #functional-nav > ul, #sub-nav > ul, #product-toolbar').find('li:last').addClass('end_nav');
	
		$('.products #sub-nav ul').prepend($('.products #sub-nav ul li:last'));
		if(!$.browser.msie){
			$('.products #sub-nav ul li').hover(
				function(){ $(this).siblings().not('.selected').find('a').css({ opacity:0.4 }); },
				function(){ $(this).siblings().not('.selected').find('a').css({ opacity:1 }); }
			);
			$('.products #sub-nav ul li.selected').css({ opacity:0.25 });
		}
	
	//buttons & grids
		$('p.button, .grid2, .grid2_offset_left, .grid2_offset_right, .grid3, .ingredients li, #news-product-line-listing > div').addClass('clearfix');
	
	//proximity + spacing
		$(':header + :header, :header + p, :header + ul, :header + ol, :header + div, :header + blockquote').prev().css({ marginBottom:'10px' });
		$('.subhead + p, .subhead + ul, .subhead + ol, .subhead + div, .subhead + blockquote').prev().css({ marginBottom:'5px' });
		$('p + :header, ul + :header, ol + :header, blockquote + :header, div + :header').prev().css({ marginBottom:'25px' });
		
		$('.subpage #sub-content, #product-information-pane .grid2').equalHeights();
	
	//misc
		$('#ice-age-promo, #home-product-links > div').css({ cursor:'pointer' })
		.click(function(){ document.location = $(this).find('a').attr('href'); });
		
		//$('ul.ingredients li a b').append('&nbsp;<img src="images/info_icon.png" alt="info" border="0" />');
		
		$('#product-toolbar')
		.find('li.print a').click(function(){ printProductInformation(); return false; }).end()
		.prependTo('#product-information-pane');
}

function printProductInformation(){
	if($('#product-information-pane')) {
		
		//format content
		var $image 	= $('#sucrets-product-image');
		var $info 	= $('#product-information-pane').clone();
		$info.find('canvas, #product-toolbar').remove().end().find('*').attr('style', '');
		
		//finished content
		var c	= '<img src="'+$image.attr('src')+'" /><hr />'+$info.html();
		
		//print
		var pframe = $('<iframe id="pframe" name="pframe"></iframe>');
		pframe.css({
		    width:"1px",
		    height:"1px",
		    position:"absolute",
		    left:"-9999px"
		});
		$('body').find('iframe').remove().end().append(pframe);
		
		var doc = null;  
		if(pframe[0].contentDocument){
			// Firefox, Opera  
			doc = pframe[0].contentDocument;  
			
		} else if(pframe[0].contentWindow){
			// Internet Explorer  
			doc = pframe[0].contentWindow.document;  
			
		} else if(pframe[0].document){
			// Others?  
			doc = pframe[0].document;
		}

		doc.open();
		doc.write('<html><head></head><body>'+c+'</body></html>');
		doc.close();
		
		if($.browser.msie){
			w = doc.execCommand('print', false, null);
		} else {
			pframe[0].contentWindow.print();
			//pframe[0].window.print();
		}
	}
}

//interactive accordian
function initInfoAccordian(){
		
	$('.info_accordian').each(function(index){
				
		var iaNavIndex = index;
		$(this).find('dt').each(function(index){
			
			$(this).wrapInner('<a href="#" title="Click to view..."></a>');
			
			//interaction
			$(this)
			.attr('id', 'ia-'+iaNavIndex+'-'+index)
			.hover(
				function(){ $(this).addClass('hover'); $(this).siblings().removeClass('hover') },
				function(){ $(this).removeClass('hover'); }
			)
			.click(function(){
				if(!$(this).hasClass('open')){
					$(this).find('a > span.desc').text('Click to close [x]');
					$(this).addClass('open').next('dd').slideDown(700, 'easeOutCubic');
				} else {
					$(this).find('a > span.desc').text($(this).find('a').attr('title'));
					$(this).removeClass('open').next('dd').slideUp(700, 'easeOutCubic');
				}				
				return false;
			})
			.find('a')
			.addClass('clearfix').wrapInner('<span class="title"></span>');
			
			//add description if available
			if($.trim($(this).find('a').attr('title')).length < 1){
				$(this).find('span.title').css({ width:'100%' });
			} else {
				$(this).find('a').append('<span class="desc">'+$(this).find('a').attr('title')+'</span>');
			}
		});
	});
	
	if(window.location.hash){
		var sections = window.location.hash.substr(1).split(",");
		for(x=0; x<sections.length; x++){
			var section = '#'+sections[x];
			if($(section).length){
				$(section).prev().find('> a').trigger('click');
			}
		}
	}
}

//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();
	});
}

function initProductHomeSwapper(){
	if($('#products-jumper')){
		
		$('#sub-nav ul a').hover(
			function(){
				var c = $(this).attr('class').split(' ')[0];
				$(this).addClass('selected');
				$('#products-jumper > div').hide();
				$('#products-jumper').find('.'+c).fadeIn(200);
			},
			function(){
				$('#sub-nav a').removeClass('selected');
			}
		);
		$('#products-jumper .sucrets_throat').fadeIn(200);
	}
}

/* IE RELATED */
function applyIE6FlickerFix(){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}


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