// Namespaces
var Batch19 = (!Batch19) ? {} : Batch19;
Batch19.mod = {}; //for Module specific methods, vars, and objects

(function($){

	var rsid = 'millercoorsbatch19com';	
	
	/*************************************
	Headline Rotator
	*************************************/
	Batch19.mod.headlineRotator = function(){
		//$('.available ul').fader({ 'speed': 1500, 'timeout':6000 }); 
    };
    
    /*************************************
	The Beer Overlay
	*************************************/
    Batch19.mod.beerDescription = function(){
		$(".the-beer").hover(
		  function () {
			$('.beer-description').show();
			var s=s_gi(rsid);
			s.channel='Batch 19 Home';
			s.pageName='Batch 19 Home: Rollover - Beer Quote';
			void(s.t());
		  }, 
		  function () {
			$('.beer-description').hide();
		  }
		);
    };
    /*************************************
	Quote Rotator
	*************************************/
    Batch19.mod.beerBuzz = function() {

		var displayDuration = 6700;
		var transitionDuration = 1200;
		var transitionTimer;
		var currentQuote;
		var quotes = $('#quotes div').get();		
		
		$.each(quotes, function(t) {
			$(quotes[t]).css('opacity', 0).hide();
		});

		rotateQuote();
		
		function rotateQuote() {
		
			if (currentQuote != undefined) {
				$(quotes[currentQuote]).fadeTo(transitionDuration, 0, function() { $(this).hide(); });
				currentQuote = ((currentQuote + 1) < quotes.length) ? currentQuote + 1 : 0;
				$(quotes[currentQuote]).show().fadeTo(transitionDuration, 1);
			} else {
				currentQuote = 0;
				$(quotes[currentQuote]).show().fadeTo(transitionDuration, 1);
			}
		
			transitionTimer = setTimeout(rotateQuote, displayDuration);
		}  
    };
    
    /*************************************
	Twitter
	*************************************/    
    Batch19.mod.twitter = function() {
    
		$.jTwitter('batch19', 3, function(posts){
			var a = "results\n";
			for (var i=0; i<posts.length; i++) {
				a += posts[i].text + "\n\n";
			}
			alert(a);
		});    
    };
    
    /*************************************
	Age Gate Check
	*************************************/
    Batch19.mod.ageGate = function(){
    	// clear input fields on focus
		$('#frm-verify-age input').focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		// populate fields if left empty
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
		$('#frm-verify-age').submit(function () {
			$('#frm-verify-age input:not(input#frm-verify-submit)').css('border', '1px solid #392B21');
			var
				min_age = 21;
				month = $('#frm-verify-month').val();
				day = $('#frm-verify-day').val();
				year = $('#frm-verify-year').val();
				theirDate = new Date(parseInt(year) + min_age, month - 1, day);
				today = new Date();
		
			// check if valid date
			if (year < 1900 || month > 12 || month == 0 || day > 31 || day == 0 || isNaN(month) || isNaN(day) || isNaN(year)) {
				$('.invalid-date').show(); // display error message
				
				// add border to appropriate invalid field
				if (month > 12 || month == 0 || isNaN(month)) {
					$('#frm-verify-month').css('border', '1px solid #fff');
				}
				if (day > 31 || day == 0 || isNaN(day)) {
					$('#frm-verify-day').css('border', '1px solid #fff');
				}
				if (year < 1900 || isNaN(year)) {
					$('#frm-verify-year').css('border', '1px solid #fff');
				}
			}
			// validate age
			else {
				if ((today.getTime() - theirDate.getTime()) >= 0) {
					var s=s_gi(rsid);
					s.channel='Batch 19 AV';
					s.pageName='Batch 19 AV: AV Succes';
					void(s.t());
					$('.invalid-date').hide();
					window.location.href = "landing.html";
				} else {
					$('h2, h3').hide();
					$('.invalid-date').hide();
					$('#frm-verify-age').hide();
					$('p.redirect').show();
					var s=s_gi(rsid);
					s.channel='Batch 19 AV';
					s.pageName='Batch 19 AV: AV Failure';
					void(s.t());
					setTimeout(' Batch19.mod.underAge()', 5000);
				}
			}
			return false;
		});
		Batch19.mod.underAge = function() {
			window.location.href="http://thecoolspot.gov";
		}
    };
    
    /*************************************
	Image Preloader
	*************************************/
    Batch19.mod.preLoad = function () {
		var allImgs = []; //new array for all the image urls 
		var k = 0; //iterator for adding images
		var sheets = document.styleSheets; //array of stylesheets
		for (var i = 0; i < sheets.length; i++) { //loop through each stylesheet
			var cssPile = ''; //create large string of all css rules in sheet
			var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
			var baseURLarr = csshref.split('/'); //split href at / to make array
			baseURLarr.pop(); //remove file path from baseURL array
			var baseURL = baseURLarr.join('/'); //create base url for the images in this sheet (css file's dir)
			if (baseURL != "") baseURL += '/'; //tack on a / if needed
			if (document.styleSheets[i].cssRules) { //w3
				var thisSheetRules = document.styleSheets[i].cssRules; //w3
				for (var j = 0; j < thisSheetRules.length; j++) {
					cssPile += thisSheetRules[j].cssText;
				}
			}
			else {
				cssPile += document.styleSheets[i].cssText;
			}
	
			//parse cssPile for image urls and load them into the DOM
			var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g); //reg ex to get a string of between a "(" and a ".filename"
			if (imgUrls != null && imgUrls.length > 0 && imgUrls != '') { //loop array
				var arr = jQuery.makeArray(imgUrls); //create array from regex obj       
				jQuery(arr).each(function () {
					allImgs[k] = new Image(); //new img obj
					allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this; //set src either absolute or rel to css dir
					k++;
				});
			}
		} //loop
		return allImgs;
	}
	
	/*************************************
	Window Popups
	*************************************/
    Batch19.mod.popUp = function () {
		$('a.popup').bind('click',function() {
			openPopUp(this.getAttribute("href"));
			return false;
		});
		
		$('a.legalpopup').bind('click',function() {
			openLegal(this.getAttribute("href"));
			return false;
		});		
		
		function openPopUp(linkURL) {
			window.open(linkURL, 'popup', 'width=500,height=500,scrollbars=yes')
		}
		
		function openLegal(linkURL) {
			window.open(linkURL, 'popup', 'width=640,height=500,scrollbars=yes,status=yes,menubar=no,resizable=yes,toolbar=no')
		}		
	}
	
	/*************************************
	Omniture
	*************************************/
	Batch19.mod.omniture = function () {
		$('.facebook a').bind('click',function() {
			var s=s_gi(rsid);
			s.channel='Batch 19 Home';
			s.pageName='Batch 19 Home: Facebook Logo';
			void(s.t());
		});
		
		$('.facebook-link').bind('click',function() {
			var s=s_gi(rsid);
			s.channel='Batch 19 Home';
			s.pageName='Batch 19 Home: Facebook Text Link';
			void(s.t());
		});
		
		$('.twitter a').bind('click',function() {
			var s=s_gi(rsid);
			s.channel='Batch 19 Home';
			s.pageName='Batch 19 Home: Twitter';
			void(s.t());
		});
	}
	
})(jQuery);

// Global JS Inits using jQuery
$(document).ready(function() {
	Batch19.mod.preLoad();
	Batch19.mod.headlineRotator();
	Batch19.mod.beerDescription();
	Batch19.mod.beerBuzz();
	//Batch19.mod.twitter();
	Batch19.mod.ageGate();
	Batch19.mod.popUp();
	Batch19.mod.omniture();
});
