
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27643891-7']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

/* Copyright © Cigars International. All Rights Reserved. */

var cFest = (function($, cf){

var i, l, hrefArray, hrefQuery, key, value, request,
	FEST_DATE =  new Date(2012, 4, 4, 10, 30);

// If the server does not provide a global cFest object, 
// create a module to close over and return that new object.

cf = cf || {};

// Generate empty references if no page data is provided.

if( !cf.page ){
	cf.page = {
		name: '',
		type: '',
		fileName: ''
	}
}

// Generate an associative array of URL variables if the server has not provided one.

if( !cf.page.httpRequest ){
	cf.page.httpRequest = request = {};
	hrefArray = window.location.href.split('?');
	
	if( hrefArray[1] ){
		hrefArray = hrefArray[1].split('&');
		
		for( i = 0, l = hrefArray.length; i < l; i++ ){
			hrefQuery = hrefArray[i].split('=');
			key = hrefQuery[0].toLowerCase();
			value = hrefQuery[1].toLowerCase();
			request[ key ] = value;
		}
	}
}

// Manipulate the DOM as soon as the header elements are created
// using a pseudo DOMContentLoaded event triggered by a script event in the document.

$(document).bind('header.DOMContentLoaded', function( e ){
	var slides, index, cookieValue, timeOut, timeOut2,
		hoverDelay = 60, 
		timeOutDelay = 1000,
		navbar = $('#nav'),
		navSelected = defaultNav = $('.nav-link-selected:first', navbar),
		panelSelected = defaultSubNav = $('.nav-panel-selected:first', navbar),
		defaultNav = navSelected,
		defaultSubNav = panelSelected,
		counter = $('#header-countdown div');
	
	$('#header-email input[type="text"]').inputer({ prompt: 'Type Your Email Here' });
	
	$('> li', navbar).mouseenter(function(){
		var navLink = $('> a.nav-link', this),
			panel = $('> ul.nav-panel', this);
		
		clearTimeout( timeOut2 );
		
		// If a panel exists, switch to that panel.
		
		timeOut = setTimeout( function(){
			if( panel.length > 0 ){
				navSelected.removeClass('nav-link-selected');
				panelSelected.removeClass('nav-panel-selected');
				
				navSelected = navLink.addClass('nav-link-selected');
				panelSelected = panel.addClass('nav-panel-selected');
			};
		}, hoverDelay);
	
	}).mouseleave(function(){
		clearTimeout( timeOut );
		
		timeOut2 = setTimeout( function(){
			navSelected.removeClass('nav-link-selected');
			panelSelected.removeClass('nav-panel-selected');
				
			navSelected = defaultNav.addClass('nav-link-selected');
			panelSelected = defaultSubNav.addClass('nav-panel-selected');
		}, timeOutDelay);
	});
	
	counter.countdown({
		until: FEST_DATE,
		compact: true, 
		description: '',
		onTick: function( d ){
			var textNode = this.firstChild.firstChild,
				dd = (d[3] == 1) ? ' Day, ' : ' Days, ',
				d4 = (d[4] < 10 ? '0' : '') + d[4],
				d5 = (d[5] < 10 ? '0' : '') + d[5],
				d6 = (d[6] < 10 ? '0' : '') + d[6];
			
			textNode.nodeValue = String('').concat(d[3],dd,d4,':',d5,':',d6);
		}
	});
	
	if( cf.page.name != 'Home Page' ){
		
		// Omits any slides that are related to the current page or previous page.
		
		slides = $('#header-slideshow').show().children('[data-pagename!="' + cf.page.name.replace(/\s/g,'_') + '"]');
		
		// Exception so the same image of Fritz's face doesn't appear on the Shop page. 
		
		if( cf.page.name == 'Shop' ){
			slides = slides.filter('[data-pagename!="Pre_Registration"]');
		}
		
		// Check for a session cookie.
		
		cookieValue = $.cookie( 'cfSlideshow' );
		
		if( cookieValue ){
			slides = slides.not('[data-pagename=' + cookieValue + ']');
		};
		
		// Randomly select a new slide.
		
		index = Math.floor( Math.random() * slides.length );
		cookieValue = slides.eq( index ).show().attr( 'data-pagename' );
		
		$.cookie( 'cfSlideshow', cookieValue );
	};
});

// After the footer.DOMContentLoaded event is triggered the last 
// script tag in the document unbinds all the events in the .DOMContentLoaded namespace.

$(document).bind('footer.DOMContentLoaded', function( e ){
	var quotator = $('#footer-quotator').show(),
		quotatorBtn = $('#footer a.btn-footer-festimonials').show();
	
	quotator.quotator({
		quotes: [
			{
				"quote": "This was my Second Cigarfest and it just keeps getting better. Great job CI. I can't wait till next year. Cigarfest Rocks.",
				"author": "- KD of Lewistown, PA"
			},
			{
				"quote": "Again, another great CF! Lots of hot girls and great cigars!! Excellent job CI, cant wait til next year!",
				"author": "- RL of Medford, NJ"
			},
			{
				"quote": "First Cigarfest, went with a friend that has been talking about it for the last year. It definitely lived up to the hype. Great smokes, great booze, and great fun.",
				"author": "- GS of Maple Grove, MN"
			},
			{
				"quote": "CI Nation, another fantastic FEST. Not sure how you keep on getting better but you do. This is my 7th Fest and my expectations were exceeded one more time. The cigars, drinks, food and the girls, OH YES THE GIRLS, made this one for the record books. Can't wait for CigarFest #9, has a date been announce yet? Great job to all at CI and the incredible cigar manufacturers.",
				"author": "- CB of Leicester, MA"
			},
			{
				"quote": "This was my 2nd cigarfest, and as last year my wife and I had a great time! It's like trick or treating for adults. CI you guys did a great job again! Next years fest can't come soon enough!!!!",
				"author": "- JS of Oneonta, NY"
			},
			{
				"quote": "A great big thank you to CI for the wonderful job in putting together CigarFest 2011. It was absolutely fantastic. I cannot wait until next year.",
				"author": "- JS of Easton, PA"
			}
		]
	}).css('cursor', 'pointer').click(function(){
		location.href = quotatorBtn[0].href;
	});
});

return cf;

})(jQuery, cFest);
