var dipstart,
	diplast,
	diptotal,
	rename,
	carousel,
	prefix,
	defaultTab;

prefix = 'eggs-tab-';
defaultTab = 'quick-easy';

carousel = function () {
	$('#egg-ideas-swatch a').click(function () {
		this.blur();
	});
	
	$('.egg-idea-content').each(function () {
		$(this).css({
			paddingTop: (130 - $(this).height()) / 2
		});
	});
	
	$('.egg-idea').css({width: '517px'});
		
	stepcarousel.setup({
		galleryid: 'egg-ideas-carousel', //id of carousel DIV
		beltclass: 'egg-ideas-carousel-belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'egg-idea', //class of panel DIVs each holding content
		autostep: {enable:false},
		panelbehavior: {speed:500, wraparound:true, persist:true},
		defaultbuttons: {enable: false},
		statusvars: ['dipstart', 'diplast', 'diptotal'],
		contenttype: ['inline'],
		onslide: function () {
			var thumbs = $('#egg-ideas-swatch div');
			thumbs.removeClass('current');
			$(thumbs[dipstart-1]).addClass('current');
		}
	});
};

rename = function () {
	$('.eggs-tab').each(function () {
		this.id = prefix + this.id;
	});
};
	

// set up tabs
$(function () {
	
	var pick,
		tabs,
		panes,
		init,
		height,
		fragment;
		
	init = function () {
		tabs = {};
		panes = {};
		height = 0;

		// set up each tab
		$('.eggs-tab-hotspot').each(function () {
			var key;
			
			// remove the prefix added inline
			key = this.parentNode.id.replace(prefix, '');
			
			// keep the tab and the pane for picking later
			tabs[key] = this;
			panes[key] = this.parentNode;
			
			// transplant the tab
			this.parentNode.removeChild(this);
			$('#eggs-tabs').append(this);
			
			// make it clickable
			$(this).click(function () {
				pick(key);
			})
		});

		// size tab images here because Safari doesn't obey height in css file
		// until after the height-equalizing code below is finished.
		$('.eggs-tab-thumb').css({
			height: 100,
			lineHeight: 0,
			paddingBottom: '.5em'
		});

		// equalize tab height
		$('.eggs-tab-hotspot-inner').each(function () {
			height = Math.max(height, $(this).height());
		});
		$('.eggs-tab-hotspot-inner').height(height);
		
		// pick the tab from the url fragment
		pick(fragment(window.location.href));

	};

	fragment = function (url) {
		var hash;
		hash = url.split('#');
		return hash[1] || defaultTab;
	};
	
	pick = function (key) {
		// select the tab
		$('.eggs-tab-hotspot').addClass('hidden');
		$(tabs[key]).removeClass('hidden');
		
		// select the pane
		$('.eggs-tab').css({display: 'none'});
		$(panes[key]).css({display: 'block'});
		
		// set the hash
		window.location.href = '#' + key;
		
		// track the event
		jsTrack.trackEvent('egg-recipes-' + key);
	};

	init();


});


// set up quote rotation
$(function () {
	var height,
		offset,
		width,
		quotes,
		index,
		scroll;
	
	offset = $('#eggs-what-say-middle').outerWidth();
	height = 0;
	quotes = [];
	index = 0;
	
	scroll = function () {
		var next;

		next = (index + 1) % quotes.length;

		$(quotes[index]).css({
			left: 0,
			opacity: 1
		}).animate({
			left: -offset,
			opacity: 0
		}, 400);

		$(quotes[next]).css({
			left: offset,
			opacity: 0
		}).animate({
			left: 0,
			opacity: 1
		}, 600);
		
		index = next;
		
	};
	
	$('.eggs-what-say-quote').
	each(function () {
		quotes.push(this);
		$(this).css({
			padding: 0,
			margin: 0,
			position: 'absolute',
			width: '148px', // do it here 'cause of safari
			top: 0,
			left: offset
		});
	});
	
	// for ie7
	$('#eggs-what-say-quotes').css({
		display: 'block'
	});
	
	// defer this part for safari
	$(window).load(function () {
	
		$('.eggs-what-say-quote').
		each(function () {
			height = Math.max(height, $(this).height());
		}).
		each(function () {
			$(this).css({
				paddingTop: ((height - $(this).height()) / 2) + 'px'
			});
		});
		
		$('#eggs-what-say-quotes').height(height);
		
		$(quotes[0]).css({
			left: 0
		});
	
	
		setInterval(scroll, 7000);
		
	});
	
});

