(function($){
	var origDiv = '#origDiv';
	var origImg = '#origDiv img';
	//resize the image on brower load
	$(document).ready(function() {
		$(origDiv).resizeImg();
	});
	//resize the image on browser resize
	$(window).bind("resize", function() {
		$(origDiv).resizeImg();
	});
	$.fn.resizeImg = function() {
		//define original width and height of the image
		var imgWidth = $(origImg).attr('width');
		var imgHeight = $(origImg).attr('height');
		//define image ratio
		var ratio = imgHeight/imgWidth;
		//get browser dimensions
		var winWidth = $(window).width();
		var winHeight = $(window).height();
		var winRatio = winHeight/winWidth;
		//resize the image
		if (winRatio > ratio) {
			$(origDiv).height(winHeight);
			$(origDiv).width(winHeight / ratio);
			$(origImg).height(winHeight);
			$(origImg).width(winHeight / ratio);
		} else {
			$(origDiv).width(winWidth);
			$(origDiv).height(winWidth * ratio);
			$(origImg).width(winWidth);
			$(origImg).height(winWidth * ratio);
		}
		var browserwidth = $(window).width();
		$(origDiv).css('left', (browserwidth - $(origImg).width())/2);
	};
})(jQuery);

(function($){
	$.fn.hint=function(blurClass){
		if(!blurClass){
			blurClass='blur';
		}
		return this.each(function(){
			var $input=$(this),title=$input.attr('title'),$form=$(this.form),$win=$(window);
			function remove(){
			if($input.realval()===title&&$input.hasClass(blurClass)){
				$input.val('').removeClass(blurClass);
			}
		}
		if(title){
			$input.blur(function(){
				if(this.value===''){
					$input.val(title).addClass(blurClass);
				}
			}).focus(remove).blur();
			$form.submit(remove);
			$win.unload(remove);
		}
		});
	};
	$.fn.realval=$.fn.val;
	$.fn.val=function(value){
		var i=$(this);
		if(value===undefined){
			return(i.realval()===i.attr('title'))?'':i.realval();
		} else {
			return i.realval(value);
		}
	}
})(jQuery);

function switchCountry() {
	if($("#country").val()=="uk") {
		$("#country").val("us");
	} else {
		$("#country").val("uk");
	}
	$("#country-image").css("background-image", "url('country-"+$("#country").val()+".png')");
}

function setCookieSession() {
  document.cookie = "age_verification="+escape("passed") + "; path=/";
  document.location.href='/';
}

function setCookiePermanent() {
  var today = new Date();
  var expire = new Date();
  expire.setTime(today.getTime() + 3600000*24*365);
  document.cookie = "age_verification="+escape("passed") + ";expires="+expire.toGMTString() + "; path=/";
  document.location.href='/';
}

function checkAgeVerificationForm() {   
	if($("#age-verification-dd").val()=="" || $("#age-verification-dd").val()=="DD"){
		alert("Please enter your day of birth");
		return false;
	}
	if($("#age-verification-mm").val()=="" || $("#age-verification-mm").val()=="MM"){
		alert("Please enter your month of birth");
		return false;
	}
	if($("#age-verification-yy").val()=="" || $("#age-verification-yy").val()=="YY"){
		alert("Please enter your year of birth");
		return false;
	}		 
	// check age
	var db = parseInt($('#age-verification-dd').val());
	var mb = (parseInt($('#age-verification-mm').val(), 10) -1); 
	var yb = parseInt($('#age-verification-yy').val());
	var dob = new Date(yb, mb, db);
	var now = new Date();
	var tday = now.getDate();
	var yearNow = now.getFullYear();
	var yearThen = dob.getFullYear();
	var monthNow = now.getMonth();
	var age;				
	if((monthNow > mb)||(monthNow==mb & tday>=db)) {
		age=yearThen;
	} else {
		age=yearThen+1;
	}
	myAge = yearNow - age;
	if (myAge >= 18){
		if($('#remember-me').is(':checked')){
			setCookiePermanent();
		}						
		else {
			setCookieSession();
		}
	}						 
	else
	{
		alert('You are not legally old enough to view this site')
	}
	return false;
}

/*
	tabSlideOUt v1.3
	
	By William Paoli: http://wpaoli.building58.com
	To use you must have an image ready to go as your tab
	Make sure to pass in at minimum the path to the image and its dimensions:
	
	example:
	
		$('.slide-out-div').tabSlideOut({
				tabHandle: '.handle',						 //class of the element that will be your tab -doesnt have to be an anchor
				pathToTabImage: 'images/contact_tab.gif',	 //relative path to the image for the tab
				imageHeight: '133px',						 //height of tab image
				imageWidth: '44px',						   //width of tab image   
		});
	or you can leave out these options
	and set the image properties using css
	
*/
(function($){
	$.fn.tabSlideOut = function(callerSettings) {
		var settings = $.extend({
			tabHandle: '.handle',
			speed: 300, 
			action: 'click',
			tabLocation: 'left',
			topPos: '200px',
			leftPos: '20px',
			fixedPosition: false,
			positioning: 'absolute',
			pathToTabImage: null,
			imageHeight: null,
			imageWidth: null,
			onLoadSlideOut: false					   
		}, callerSettings||{});
		settings.tabHandle = $(settings.tabHandle);
		var obj = this;
		if (settings.fixedPosition === true) {
			settings.positioning = 'fixed';
		} else {
			settings.positioning = 'absolute';
		}
		//ie6 doesn't do well with the fixed option
		if (document.all && !window.opera && !window.XMLHttpRequest) {
			settings.positioning = 'absolute';
		}
		//set initial tabHandle css
		if (settings.pathToTabImage != null) {
			settings.tabHandle.css({
			'background' : 'url('+settings.pathToTabImage+') no-repeat',
			'width' : settings.imageWidth,
			'height': settings.imageHeight
			});
		}
		settings.tabHandle.css({ 
			'display': 'block',
			'textIndent' : '-99999px',
			'outline' : 'none',
			'position' : 'absolute'
		});
		obj.css({
			'line-height' : '1',
			'position' : settings.positioning
		});
		var properties = {
			containerWidth: parseInt(obj.outerWidth(), 10) + 'px',
			containerHeight: parseInt(obj.outerHeight(), 10) + 'px',
			tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
			tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
		};
		//set calculated css
		if(settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
			obj.css({'left' : settings.leftPos});
			settings.tabHandle.css({'right' : 0});
		}
		if(settings.tabLocation === 'top') {
			obj.css({'top' : '-' + properties.containerHeight});
			settings.tabHandle.css({'bottom' : '-' + properties.tabHeight});
		}
		if(settings.tabLocation === 'bottom') {
			obj.css({'bottom' : '-' + properties.containerHeight, 'position' : 'fixed'});
			settings.tabHandle.css({'top' : '-' + properties.tabHeight});
			
		}
		if(settings.tabLocation === 'left' || settings.tabLocation === 'right') {
			obj.css({
				'height' : properties.containerHeight,
				'top' : settings.topPos
			});
			settings.tabHandle.css({'top' : 0});
		}
		if(settings.tabLocation === 'left') {
			obj.css({ 'left': '-' + properties.containerWidth});
			settings.tabHandle.css({'right' : '-' + properties.tabWidth});
		}
		if(settings.tabLocation === 'right') {
			obj.css({ 'right': '-' + properties.containerWidth});
			settings.tabHandle.css({'left' : '-' + properties.tabWidth});
			$('html').css('overflow-x', 'hidden');
		}
		//functions for animation events
		settings.tabHandle.click(function(event){
			event.preventDefault();
		});
		var slideIn = function() {
			if (settings.tabLocation === 'top') {
				obj.animate({top:'-' + properties.containerHeight}, settings.speed).removeClass('open');
			} else if (settings.tabLocation === 'left') {
				obj.animate({left: '-' + properties.containerWidth}, settings.speed).removeClass('open');
			} else if (settings.tabLocation === 'right') {
				obj.animate({right: '-' + properties.containerWidth}, settings.speed).removeClass('open');
			} else if (settings.tabLocation === 'bottom') {
				obj.animate({bottom: '-' + properties.containerHeight}, settings.speed).removeClass('open');
			}	
		};
		var slideOut = function() {
			if (settings.tabLocation == 'top') {
				obj.animate({top:'-3px'},  settings.speed).addClass('open');
			} else if (settings.tabLocation == 'left') {
				obj.animate({left:'-3px'},  settings.speed).addClass('open');
			} else if (settings.tabLocation == 'right') {
				obj.animate({right:'-3px'},  settings.speed).addClass('open');
			} else if (settings.tabLocation == 'bottom') {
				obj.animate({bottom:'-3px'},  settings.speed).addClass('open');
			}
		};
		var clickScreenToClose = function() {
			obj.click(function(event){
				event.stopPropagation();
			});
			$(document).click(function(){
				slideIn();
			});
		};
		var clickAction = function(){
			settings.tabHandle.click(function(event){
				if (obj.hasClass('open')) {
					slideIn();
				} else {
					slideOut();
				}
			});
			clickScreenToClose();
		};
		var hoverAction = function(){
			obj.hover(
				function(){
					slideOut();
				},
				function(){
					slideIn();
				});
				settings.tabHandle.click(function(event){
					if (obj.hasClass('open')) {
						slideIn();
					}
				});
				clickScreenToClose();
		};
		var slideOutOnLoad = function(){
			slideIn();
			setTimeout(slideOut, 2000);
		};
		//choose which type of action to bind
		if (settings.action === 'click') {
			clickAction();
		}
		if (settings.action === 'hover') {
			hoverAction();
		}
		if (settings.onLoadSlideOut) {
			slideOutOnLoad();
		};
	};
})(jQuery);

