﻿var GlueProject = function() {
	function init() {
		$(document.body).removeClass('no-js').addClass('js');
		attachSlideShow();
		attachPauseSlideShow();
		attachColorBox();
		attachBuyPopup();
		attachTabs();
		attachSlickBox();
		attachTrackOutboundLink();
		pngFix();
	}
	

	function pngFix() {
		if (typeof(jQuery.fn.pngFix) == 'function') {
			$(document).pngFix();
		}	
	}


	/*
	Attach a slideshow to the approprate elements
	*/
	function attachSlideShow() {
		if($('#slideshow').length == 0) {
			return false;
		}

		$('#slideshow').before('<div id="nav" class="nav">').cycle({
			fx:     'scrollRight',
			speed:  'slow',
			timeout: 9000,
			pager:  '#nav',
			before: function() { 
					if (window.console) console.log(this.src); 
				}
		});		
	}


	/*
	Pause the slideshow when a navigation link is clicked
	*/
	function attachPauseSlideShow() {
		$('#nav').click(function() { 
			$('#slideshow').cycle('pause'); 
		});		
	}


	/*
	Attach a modal window
	*/
	function attachColorBox() {
		if($('.color-box').length == 0) {
			return false;
		}

		var options = {
			iframe:true, 
			innerWidth:425, 
			innerHeight:344
		}
		$('.color-box').colorbox(options);
	}


	/*
	Attach a popup to the buy button
	*/
	function attachBuyPopup() {
		$('#buy-all-bran').click(function(e) {
			$('#slideshow').cycle('pause');

			$('#buy-all-bran-popup').lightbox_me({
				centered: true, 
				onLoad: function() { 
					$('#slideshow').cycle('resume');
					$('#buy-all-bran-popup').find('input:first').focus()
					}
			});
			e.preventDefault();
		});	
	}


	/*
	Attach jQueryUI tabs to the appropriate elements
	*/
	function attachTabs() {
		if($('#recipes-tabs').length == 0) {
			return false;
		}

		$('#recipes-tabs').tabs();
		$('#breakfast').tabs();
		$('#lunch').tabs();
		$('#dinner').tabs();
		$('#deserts').tabs();
		$('#snacks').tabs();

	}


	/*
	Attach an opening / closing panel
	*/
	function attachSlickBox() {
		if($('.slickbox').length == 0) {
			return false;
		}

		// $('.slickbox').hide();

		$('.slickbox-open').click(function() {
			$(this).parents().siblings('.slickbox').show();
			$(this).css('display','none');
			$(this).siblings('.slickbox-close').css('display','inline');
			return false;
		});

		$('.slickbox-close').click(function() {
			$(this).parents().siblings('.slickbox').hide();
			$(this).css('display','none');
			$(this).siblings('.slickbox-open').css('display','inline');
			return false;
		});
	}

	/*
	Track outbound links
	http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55527
	*/
	function attachTrackOutboundLink() {
		//recordOutboundLink(this, 'Outbound Links', 'example.com');

		$('.track-outbound').click(function() {
			var category = 'Outbound Links';
			var action = $(this).attr('href');
			var target = $(this).attr('target');
			//console.debug(category + ' : ' + action);
			try {
				var pageTracker=_gat._getTracker('UA-23178087-10');
				pageTracker._trackEvent(category, action);
				// set target for the link (if there is one)
				if(target != '') {
					$(this).target = target;
				} else {
					setTimeout('document.location = "' + action + '"', 100)									
				}

			} catch(err) {}
		});
	}	

	return {
		init: init,
		attachSlideShow: attachSlideShow,
		attachPauseSlideShow: attachPauseSlideShow,
		attachColorBox: attachColorBox,
		attachBuyPopup: attachBuyPopup,
		attachTabs: attachTabs,
		attachSlickBox: attachSlickBox,
		attachTrackOutboundLink: attachTrackOutboundLink
	}
}();

$(document).ready(function(){
	GlueProject.init();
});
