/*
	Nobilo JS
	========================================
	Version:	0.1
	Date:		19/07/10
	Website:	http://wearesuper.co.nz
	Author:		Matthew Ravenhall
*/

var jsNobilo = window.jsNobilo || {};

/*	Makes the init run at page load
============================================*/
$(window).load(function() {
	jsNobilo.initialize();
});

jsNobilo.initialize = function() {
	if ($("#splash").is(":visible")) {
		jsNobilo.Common().verifyAge();
		
		$("#age").validate({
			rules: {
				day: {
				  required: true,
				  number: true
				},
				year: {
				  required: true,
				  number: true
				}
			}
		});
	}

	jsNobilo.Common().showContent();
}


/*	Common functions, that can be re-called at any point.
============================================*/
jsNobilo.Common = function() {
	
	var showContent = function() {
		var country =  $.cookie("NobiloCountry");
		
		$('.defaultcontent').hide();
		
		if (country == 'au') {
			$('.defaultcontent').hide();
			$('.aucontent').show();
			
		} else if (country == 'ca'){
			$('.defaultcontent').hide();
			$('.cacontent').show();	
			
		} else if (country == 'uk'){
			$('.defaultcontent').hide();
			$('.ukcontent').show();
			
		} else if (country == 'usa'){
			window.location = 'http://nobilowines.cbrands.com';
			
		} else {
			$('.defaultcontent').show();
		}
		
	
	}

	// Verify Age
	var verifyAge = function() {
		var country, day, month, year, newyear, myDate, today, age;
		var myCookie = $.cookie("Nobilo");

		if (myCookie != null && myCookie == 'Over') {
			parent.location = 'index.html';
		} else if (myCookie != null) {
			alert('Sorry you are under age and cannot view this site.');
		}

		$('div#splash #enter').click(function(){
			country 	= $('#country :selected').val();
			day 		= $('#day').val();
			month 		= $('#month :selected').val();
			year 		= $('#year').val();			
			newyear 	= parseInt(year) + parseInt(country);
			myDate		= new Date();
			myDate.setFullYear(newyear, month, day);
			today 		= new Date();
			
			$.cookie('NobiloCountry', country, { expires: 365 });
		
			if(country == 'usa'){
				age = 21;
			} else {
				age = 18;
			}
			
			//alert('mydate: ' + myDate + '   today:'+ today)
			
			if (myDate > today) {
				alert('Sorry you are under ' + age + ' and cannot view this site.');
				$.cookie('Nobilo', 'Under', { expires: 365 });
				return false;
			} else {
				$.cookie('Nobilo', 'Over', { expires: 365 });
				return true;
			}
		});
	}
	

	
	return {
		showContent : showContent,
		verifyAge : verifyAge
	}
}

