$(document).ready( function () {
	$('.home #video a, #video').fancybox({
		padding : 0,
		margin : 0,
		overlayColor : '#000',
		overlayOpacity : 0.5,
		width : 750, 
		height : 422,
		type : 'iframe',
		href : '/video.jsp',
		showCloseButton : true,
		scrolling : 'no',
		onComplete: function() { _gaq.push(['_trackEvent', "Video", "Start", "Celebrate Snacktivity_Art of Seeding"]); },
		onClosed: function() { _gaq.push(['_trackEvent', "Video", "Complete", "Celebrate Snacktivity_Art of Seeding"]); }
	});
	
	$(function() {
	
		$('div.seed').scrollingParallax({
			staticSpeed : 1.7,
			loopIt : false,
			staticScrollLimit : false,
			disableIE6 : true
		});
		
		$('div.background').scrollingParallax({
			staticSpeed : .17,
			loopIt : false,
			staticScrollLimit : false,
	        disableIE6 : true
	    });

	});
	
	$.ajaxSetup ({
		cache: false
	});
	
	$('body').append('<div id="result"></div>')
	
	$("a[href$='/products'], #products").click(function(e){
		e.preventDefault();
		loadResult('http://' + document.domain + '/products.jsp', 745);
	});
	
	$("a[href$='/fun-snacking'], #why_seeds").click(function(e){
		e.preventDefault();
		loadResult('http://' + document.domain + '/fun-snacking.jsp', 750);
	});
	
	$("a[href$='/official-sunflower-seed-of-little-league'], #baseball").click(function(e){
		e.preventDefault();
		loadResult('http://' + document.domain + '/official-sunflower-seed-of-little-league.jsp', 745);
	});
	
	$("a.nutrition").live("click", function(e){
		e.preventDefault();
		var upc = $(this).attr('rel');
		displayLabelByUPC(upc, this);
	});
	
	$("#footer_logo a").live("click", function(e){
		e.preventDefault();
		clickTracking("Footer", "Outbound Link", "ConAgra Foods", this.href);
	});
	
	$("a[href^='http://www.amazon.com']").live("click", function(e){
		e.preventDefault();
		var classList = $(this).attr('class').split(/\s+/);
		var label = classList[0];
		clickTracking("Amazon", "OutBound Link", label, this.href);
	});
	
});

function showFancybox(content_width){
	$.fancybox({
		padding: 0,
		width: content_width,
		height: 'auto',
		autoDimensions: false,
		autoScale: false,
		overlayColor : '#000',
		overlayOpacity : 0.5,
		content: $("#result").html(),
		scrolling: 'no',
		onClosed: function() { $("#result").html(""); hideLabel(); },
		onComplete: function () {
			// the height of this box is being set to 100px too high
			$("#fancybox-content").height($("#fancybox-content").height() - 100);
		}
	});
}

function loadResult (url, content_width) {
	$('#result').load(url + ' #wrapper', function() { showFancybox(content_width); });
}

function clickTracking(category, action, label, url)
{
	var labelVal = '';
	if (label != null)
		labelVal = label;

	_gaq.push(['_trackEvent', category, action, label]);
	if (url != null && typeof (url) != "undefined" && url != "") {
		if (url.indexOf(document.domain) == -1 || endsWith(url, ".pdf")) {
			window.open(url);
		} else 
			setTimeout("window.location.href='" + url + "'", 250);
	}
}

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
