var initializer = 
{
	initOnPageLoad: function()
	{
		$.ajaxSetup({error:jsUtil.ajaxError,type:'GET'});
		
		jsUtil.tweakTopMargin();
		
		window.onresize = function() 
		{
			jsUtil.tweakTopMargin();
		}
		
		jsTopNav.init();
		jsContentSwapper.init();
		
		$('.button').bindButton();

		$('form#submitAgeVerify').attr('validator','validateAgeVerify').initForm();
		$('form#submitMailingList').initForm();
		$('form#submitHundredWaysSubmit1').initForm();
		$('form#submitHundredWaysSubmit2').initForm();
		$('form#submitHundredWaysInvite').initForm();
		$('form#submitSendSparklism').initForm();

		jsContentSwapper.bind('.pageWindow a');

		this.initOnContentLoad();
	},

	initOnContentLoad: function()
	{	
		// homepage bottle nav dhtml
		$('#flashBottleNav').hover(function() {$('.layoutHero').stop().animate({height:249});},function() {$('.layoutHero').stop().animate({height:283});});

		// our tale accordian dhtml
		$('.toggleHeight').click(function()
		{
			if($(this).text() == 'More')
			{
				$('.toggleHeight').text('More');
				$(this).text('Less').parent('div').stop().animate({height:541},750).siblings('div.item').stop().animate({height:21},750);
			}
			else
			{
				$(this).text('More').parent('div').stop().animate({height:151},750).siblings('div.item').stop().animate({height:151},750);
			}
		});
		
		// downloads page dhtml
		$('ul.nav li').click(function()
		{
			$('div.item').hide();
			$(this).siblings('li').css({color:"#FFC20F"});
			$('#downloads'+$(this).text()).show();
			$(this).css({color:"#FFFFFF"});
		});
		$('#linkWallpapers').trigger('click');
		
		// enjoy responsibly footer link
		if(location.hash.indexOf('enjoy-responsibly') != -1)
		{
			$('.toggleHeight').each(function(i) {if(i == 1) {$(this).click();}});
		}
		
		// modal links
		$('a.jsModal').unbind('click').click(function(e)
		{
			e.preventDefault();
			var path = this.href.split("/");
			
			$("#modal"+path[path.length-1]).openModal();
		});

		// download tracking
		$('a.trackDownload').click(function()
		{
			trackingHandlers.trackDownload($(this).attr('href'));
		});
	}
}

window.onload = function()
{	
	initializer.initOnPageLoad();
}

var trackingHandlers =
{
	trackPage: function(path)
	{
		pageTracker._trackPageview(path);
		
		if(path == "/content/our-tale.php")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytour854;ord=1;num=');
		}
		if(path.search("/news/") != -1)
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytart363;ord=1;num=');
		}
		if(path.search("/fun/flavor-map.php") != -1)
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytfla971;ord=1;num=');
		}
	},
	
	trackModalOpen: function(modal)
	{
		pageTracker._trackPageview("/modalOpen/"+modal);

		if(modal == "modalSendSparklism")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=spark291;cat=ytspk154;ord=1;num=');
		}
	},

	trackModalClose: function(modal)
	{
		pageTracker._trackPageview("/modalClose/"+modal);
	},

	trackFormSubmit: function(form)
	{
		pageTracker._trackPageview("/submitForm/"+form);

		if(form == "submitSendSparklism")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=spark291;cat=ytspk261;ord=1;num=');
		}
		if(form == "submitHundredWaysSubmit2")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=yt100734;ord=1;num=');
		}
		if(form == "submitHundredWaysInvite")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=100fr495;ord=1;num=');
		}
		if(form == "submitMailingList")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=mailp604;ord=1;num=');
		}
		if(form == "submitAgeVerify")
		{
			if(location.href.indexOf("always-sparkle") != -1)
			{
				trackingHandlers.trackSpecific('always-sparkle');
			}
			if(location.href.indexOf("home") != -1)
			{
				trackingHandlers.trackSpecific('home');
			}
		}
	},

	trackVideoPlay: function(video)
	{	
		pageTracker._trackPageview("/videoPlay/"+video);
		this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytvid769;ord=1;num=');
	},

	trackDownload: function(download)
	{
		pageTracker._trackPageview("/downloadAction/"+download);
		this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytdow185;ord=1;num=');
	},

	trackSpecific: function(event)
	{	
		if(event == "home")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ythom642;ord=1;num=');
		}
		if(event == "always-sparkle")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=spark291;cat=ytspk826;ord=1;num=');
		}
		if(event == "sparkle-voted")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=spark291;cat=ytspk924;ord=1;num=');
		}
		if(event == "sparkle-to-vote")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=spark291;cat=ytspk418;ord=1;num=');
		}
		if(event == "sparklism-intro")
		{
			this.fire('http://fls.doubleclick.net/activityi;src=1847372;type=spark291;cat=ytspk418;ord=1;num=');
		}
		if(event == "age-verify")
		{
			this.fire('http://ad.doubleclick.net/activity;src=1847372;type=cores395;cat=ytage217;ord=1;num=');
		}

		if(event == "flavormap-select")
		{
			pageTracker._trackPageview("/flavorMap/clickSelect");
		}
		if(event == "flavormap-reset")
		{
			pageTracker._trackPageview("/flavorMap/clickReset");
		}
		if(event == "flavormap-product")
		{
			pageTracker._trackPageview("/flavorMap/clickWine");
		}
	},

	fire: function(url)
	{
		var axel = Math.random()+"";
		var a = axel * 10000000000000;

		if($('#dcPixel').length == 0)
		{
			$('<img src="img/pixel.png" style="width:0;height:0;" id="dcPixel" />').appendTo("body");
		}

		$('#dcPixel').attr('src',url+a+"?");
	}
}

var modalHandlers =
{
	modalOpenHundredWaysSubmit: function()
	{
		$('#bigNumber1,#bigNumber2,#bigNumber3').show();
		return true;
	},

	modalOpenHundredWaysInvite: function()
	{
		if($('#pairing').val() == '83' || $('#pairing').val() == '78')
		{
			return false;
		}
		return true;
	}
}

var buttonHandlers =
{
	buttonCloseFallPromotionRules: function()
	{
		$('#modalFallPromotionRules').closeModal();
		//$('#modalFallPromotion').openModal();
	},
	
	buttonCloseModalFallPromotion: function()
	{
		$('#modalFallPromotion').closeModal();
	},

	buttonCloseModalContactUs: function()
	{
		$('#modalContactUs').closeModal();
	},

	buttonCloseModalMailingList: function()
	{
		$('#modalMailingList div.confirm').hide();
		$('#modalMailingList div.form').show();
		$('form#submitMailingList').resetForm();
		$('#modalMailingList').closeModal();
	},

	buttonCloseModalHundredWaysSubmit: function()
	{
		$('#modalHundredWaysSubmit div.container').css('height',200);
		$('#modalHundredWaysSubmit').closeModal();
		$('#modalHundredWaysSubmit div.form1').show();
		$('#modalHundredWaysSubmit div.form2,#modalHundredWaysSubmit div.confirm,#bigNumber1,#bigNumber2,#bigNumber3,#bigNumber4').hide();
		$('form#submitHundredWaysSubmit1,form#submitHundredWaysSubmit2').resetForm();
	},

	buttonHundredWaysSubmitEdit: function()
	{
		$('#modalHundredWaysSubmit div.form2,#bigNumber4').hide();
		$('#modalHundredWaysSubmit div.form1,#bigNumber1,#bigNumber2,#bigNumber3').show();
		$('#modalHundredWaysSubmit div.container').css('height',200);
	},

	buttonCloseModalHundredWaysInvite: function()
	{
		$('#modalHundredWaysInvite').closeModal();
		$('#modalHundredWaysInvite div.confirm').hide();
		$('#modalHundredWaysInvite div.form').show();
		$('form#submitHundredWaysInvite').resetForm();
	},

	buttonOpenModalHundredWaysInvite: function()
	{
		$('#modalHundredWaysInvite div.confirm').hide();
		$('#modalHundredWaysInvite div.form').show();
	},

	buttonCloseModalSendSparklism: function()
	{
		$('#modalSendSparklism').closeModal();
		$('#modalSendSparklism div.confirm').hide();
		$('#modalSendSparklism div.form').show();
		$('form#submitSendSparklism').resetForm();
	},

	buttonOpenModalSendSparklism: function()
	{
		$('#modalSendSparklism div.confirm').hide();
		$('#modalSendSparklism div.form').show();
	}
}

var formHandlers = 
{
	validateAgeVerify: function()
	{	
		var day = $('form#submitAgeVerify #day').val();
		var month = $('form#submitAgeVerify #month').val();
		var year = $('form#submitAgeVerify #year').val();

		var c = new Date(year,month-1,day);
		if(year.length == 4)
		{
			if((day == c.getDate()) && ((month-1) == c.getMonth()) && (year == c.getFullYear()))
			{
				if(((Date.parse(new Date()) - Date.parse(c))/86400000) > 7671)
				{
					return true;
				}
			}
		}
		return false;
	},

	submitAgeVerify: function()
	{
		($('form#submitAgeVerify #remember_me').attr('checked')) ? document.cookie = "age_verified=1;max-age="+(60*60*24*30) : document.cookie = "age_verified=1";
		location.reload(true);
		<!--$('#modalAgeVerify').closeModal();-->
	},

	submitMailingList: function(response)
	{
		$('#modalMailingList div.form').hide();
		$('#modalMailingList div.confirm').show();

		if(location.href.search('sparkle-voting') != -1)
		{
			$('#modalMailingList div.confirm h3#confirmDefault').hide();
			$('#modalMailingList div.confirm h3#confirmVoting').show();
		}
		else
		{
			$('#modalMailingList div.confirm h3#confirmDefault').show();
			$('#modalMailingList div.confirm h3#confirmVoting').hide();
		}
	},
	
	submitHundredWaysSubmit1: function()
	{
		$('#modalHundredWaysSubmit div.form1,#bigNumber1,#bigNumber2,#bigNumber3').hide();
		$('#modalHundredWaysSubmit div.form2,#bigNumber4').show();
		$('#modalHundredWaysSubmit div.container').css('height',300);

		$('#showDescription').text($('#dummy_description').val());
		$('#description').val($('#dummy_description').val());

		$('#showWine').text($('#dummy_wine').val());
		$('#wine').val($('#dummy_wine').val());

		if($('#dummy_explanation').val() != "")
		{
			$('#showExplanation').parent('p').show().end().text($('#dummy_explanation').val());
		}
		else
		{
			$('#showExplanation').parent('p').hide().end().text("");
		}
		$('#explanation').val($('#dummy_explanation').val());
	},

	submitHundredWaysSubmit2: function()
	{
		$('#modalHundredWaysSubmit div.form2,#bigNumber1,#bigNumber2,#bigNumber3,#bigNumber4').hide();
		$('#modalHundredWaysSubmit div.confirm').show();
		$('#modalHundredWaysSubmit div.container').css('height',200);
	},

	submitHundredWaysInvite: function()
	{
		$('#modalHundredWaysInvite div.form').hide();
		$('#modalHundredWaysInvite div.confirm').show();
	},

	submitSendSparklism: function()
	{
		$('#modalSendSparklism div.form').hide();
		$('#modalSendSparklism div.confirm').show();
	}
}

var flashInterface = 
{
	pairing: null,
	
	trackTab: function(tab)
	{
		//alert(tab);
	},

	trackVideo: function(video)
	{
		trackingHandlers.trackVideoPlay(video);
	},

	vote: function(vote,ident)
	{
		trackingHandlers.trackSpecific('sparkle-voted');
		$.post("/controller.php",{ident:ident,vote:vote,action:'vote'});
	},

	open: function(param)
	{	
		$('.slide').stop().animate({height:310},250);
		//$('.fun').css({height:325});
	},

	close: function(param)
	{
		$('.slide').stop().animate({height:480},250);
		//$('.fun').css({height:158});
	},

	sparklism: function(text)
	{
		//$('#showSparklism').text(text);
		$('#sparklism').val(text);
		$('#modalSendSparklism').openModal();
	},
	
	track: function(id)
	{	
		var pairingsA = new Array(1,3,4,6,9,12,15,17,18,20,21,22,23,24,25,29,34,36,37,39,40,44,45,46,47,48,49,50,51,53,54,55,56,57,58,60,64,66,67,68,70,71,73,74,80);
		var pairingsB = new Array(16,28,33,35,81);
		var pairingsC = new Array(2,5,7,8,10,11,13,14,19,26,27,30,31,32,38,41,42,43,52,59,61,62,63,65,69,72,75,76,77,79,82);
		//var pairingsNull = new Array(78,83);

		$.post("/controller.php",{pairing:id,action:'track'},function(response)
		{
			var pairing = eval("("+response+")");
			
			$('.pairingTitle').text(pairing.title.replace(/&apos;/g,"'"));
			$('.pairingSubtitle').text(pairing.subtitle.replace(/&apos;/g,"'"));

			$('#pairing').val(pairing.pairing);
			$('#pairing_title').val(pairing.title.replace(/&apos;/g,"'"));
			$('#pairing_subtitle').val(pairing.subtitle.replace(/&apos;/g,"'"));
			
			$('#inviteA,#inviteB,#inviteC').hide();
			
			if(jsUtil.inArray(pairingsA,pairing.pairing))
			{
				$('#inviteA').show();
				$('#email_type').val('invite-a');
			}
			if(jsUtil.inArray(pairingsB,pairing.pairing))
			{
				$('#inviteB').show();
				$('#email_type').val('invite-b');
			}
			if(jsUtil.inArray(pairingsC,pairing.pairing))
			{
				$('#inviteC').show();
				$('#email_type').val('invite-c');
			}
		});
	},

	goToPairing: function(pairing)
	{
		this.pairing = pairing;
		this.goTo('/fun/100-ways-to-pair-wine.php/');
	},

	goTo: function(path)
	{
		if(path.indexOf('/modal/') != -1)
		{
			if(path.indexOf('print') != -1)
			{
				if($('iframe#printable').length == 0)
				{
					$('<iframe style="visibility:hidden;" id="printable" src=""></iframe>').appendTo('body');
				}
				if(location == 'http://discoveryellowtail.com/sparkling/white.php')
				{alert('white');
					$('iframe#printable').attr('src',"/content/sparkling/white-recipes.php");
				}
				if(location.hash.indexOf('rose') != -1)
				{
					$('iframe#printable').attr('src',"/content/sparkling/rose-recipes.php");
				}
			}
			else
			{
				path = path.replace('/modal/','').replace('/','');
				$("#modal"+path.substr(0,1).toUpperCase()+path.substr(1,path.length-1)).openModal();
			}
		}
		else
		{
			path = path.substr(0,path.length-1);
			window.location.href = path;
			//SWFAddress.setValue(path);
		}
	},
	goToReserve: function(path)
	{
		path = path.substr(0,path.length-1);
		path = path+'.php';
		window.location.href = path;
		//SWFAddress.setValue(path);
		
	}
}

var jsContentSwapper = 
{
	last: "",
	locked: false,
	ignoreOnce: false,
	shutterHeight: 250,
	
	bind: function(selector)
	{
		/*$(selector).each(function()
		{	
			var a = $(this);
			if(a.attr('target') != '_blank' && a.attr('href').indexOf('/modal/') == -1)
			{
				if(a.attr('href') != '#' && a.attr('href').indexOf('mailto:') == -1)
				{
					a.attr('content',a.attr('href').replace(location.protocol+'//'+location.hostname,'')).click(function(e) 
					{
						e.preventDefault();
						var page = $(this).attr('content');
						SWFAddress.setValue(page);
						$('.dropdown').hide();
						this.blur();
					});
				}
			}
		});*/
	},

	init: function()
	{	
		this.bind('div.pageHeader a,ul.dropdown li a,div.pageFooter ul li a');
		
		if($('div#contentHome').length > 0)
		{
			if(location.hash != '#/home/')
			{
				var path = location.hash.replace('#','');
				jsContentSwapper.swap({path:path});
			}
			else
			{
				jsContentSwapper.reveal();
				jsTopNav.lock(page.replace('.php','/').replace('/content/','/'));
			}
		}
		else
		{
			jsContentSwapper.reveal();
			jsTopNav.lock(page.replace('.php','/').replace('/content/','/'));
		}

		SWFAddress.addEventListener(SWFAddressEvent.CHANGE,jsContentSwapper.swap);
	},
	
	lock: function()
	{
		$('.pageContainer').css({cursor:'wait'});
		this.locked = true;
	},

	unlock: function()
	{
		$('.pageContainer').css({cursor:'default'});
		this.locked = false;
	},

	debug: function(event)
	{
		console.log('params: '+event.parameters);
		console.log('path: '+event.path);
		console.log('target: '+event.target);
		console.log('type: '+event.type);
		console.log('value: '+event.value);
	},

	swap: function(event)
	{	
		if(typeof(event) != 'undefined' && !jsContentSwapper.ignoreOnce)
		{	
			var path = '/content'+event.path.substr(0,event.path.length-1)+'.php';

			var isProductToProduct = false;

			var from = jsContentSwapper.last.replace('/content','').replace('.php','/');
			var to = event.path;

			if(from != to)
			{	
				var fromIsProduct = (from.indexOf('/wine/') != -1 || from.indexOf('/sparkling/') != -1) ? true : false;
				var toIsProduct = (to.indexOf('/wine/') != -1 || to.indexOf('/sparkling/') != -1) ? true : false;
				isProductToProduct = (fromIsProduct && toIsProduct) ? true : false;
			}

			if(!jsContentSwapper.locked)
			{	
				jsContentSwapper.lock();
				trackingHandlers.trackPage(path);
				jsTopNav.lock(path.replace('.php','/').replace('/content/','/'));
				
				jsContentSwapper.last = path;

				if(isProductToProduct)
				{
					var product = path.replace('/content','/flash/products').replace('.php','.swf');
					var so = new SWFObject(product, "product", "880", "376", "8", "#FFFFFF");
					so.addParam("wmode", "transparent");
					so.write("flashProduct");
					jsContentSwapper.unlock();
				}
				else
				{
					$.get(path,{randomizer:Math.round(Math.random()*100000000)},function(response)
					{
						$('.pageContent').remove();
						$(response).prependTo('.pageWindow');
						jsContentSwapper.bind('.pageWindow a');
						initializer.initOnContentLoad();
						if(!$.browser.msie)
						{
							SWFAddress.setTitle($('div.pageContent').attr('title'));
						}
						$('div.pageContent').attr('title','');
						jsContentSwapper.reveal();
						jsContentSwapper.unlock();
					});
				}
			}
		}

		jsContentSwapper.ignoreOnce = false;
	},

	reveal: function()
	{
		if($('div.pageContent').height() > 524)
		{
			this.shutterHeight = Math.round($('div.pageContent').height()/2);
		}
		else
		{
			this.shutterHeight = 262;
		}
		
		$('#shutterMaster').hide();
		$('.pageWindow').animate({height:(this.shutterHeight*2)},500,'easeInOutQuint');
	}
	
}

var jsTopNav = 
{
	
	menuItemPadLeft: 28,
	dropdownItemPadLeft: 20,

	current: null,

	init: function()
	{
		var left = 0;
		var last = 0;

		$('div.pageHeader ul li.navItem').each(function(i)
		{	
			var li = $(this);
			var depth = li.parents('ul').length;

			if(depth == 1 && li.parents('.pageHeader').length == 1)
			{				
				if(li.text().indexOf('Fun') != -1)
				{
					li.children('ul').css({left:150});
				}
				else
				{
					li.children('ul').css({left:left+4});
				}
				last = left;
				left += li.width()+jsTopNav.menuItemPadLeft;
				
				li.hover(
					function()
					{
						li.add(li.find('> a')).css({color:'#FFC20F',backgroundColor:'#000000'}).children('ul').show();
						li.find('strong img').each(function()
						{
							$(this).attr('src',$(this).attr('src').replace('-off','-over'));
						});
					},
					function()
					{
						li.add(li.find('> a')).css({color:'#000000',backgroundColor:'#FFC20F'}).children('ul').hide();
						li.find('strong img').each(function()
						{
							$(this).attr('src',$(this).attr('src').replace('-over','-off'));
						});
					}
				);
			}

			if(depth == 2)
			{	
				var id = jsUtil.genid('nav');
				
				//var offset = Math.round((jsUtil.windowWidth()-880)/2);
				//(offset < 0) ? offset = 0 : null;

				li.attr('navId',id).attr('id','anchor-'+id).children('ul').remove().clone().attr('id',id).addClass('dropdown').css({left:last+228}).appendTo('div.pageWindow');

				li.hover(
					function()
					{
						li.add(li.find('> a')).stop().animate({color:'#FFFFFF'});
						
						if($('#'+li.attr('navId')).css('paddingLeft') == '0px')
						{
							var offset = 0;
							li.parent('ul').find('li:lt('+jsUtil.pos(li)+')').each(function()
							{
								offset += $(this).width()+jsTopNav.dropdownItemPadLeft;
							});
							$('#'+li.attr('navId')).css({paddingLeft:offset});
						}
						$('#'+li.attr('navId')).show();
					},
					function()
					{
						li.add(li.find('> a')).stop().animate({color:'#FFC20F'});
						$('#'+li.attr('navId')).hide();
					}
				);
			}
		});

		$('.dropdown').hover(
			function()
			{
				$(this).show();
				$('#anchor-'+$(this).attr('id')).trigger('mouseover').parent('ul').parent('li').trigger('mouseover');
			},
			function()
			{
				$(this).hide();
				$('#anchor-'+$(this).attr('id')).trigger('mouseout').parent('ul').parent('li').trigger('mouseout');
			}
		);
		
		$('.dropdown li').hover(
			function()
			{	
				if($(this).text().indexOf('Chardonnay') != -1)
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#FFC20F'});
				}
				else if($(this).text().indexOf('Riesling') != -1 || $(this).text().indexOf('Pinot Grigio') != -1 || $(this).text().indexOf('Sauvignon Blanc') != -1)
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#ffffff'});
				}
				else
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#000000'});
				}
			},
			function()
			{	
				if($(this).text().indexOf('Riesling') != -1 || $(this).text().indexOf('Pinot Grigio') != -1 || $(this).text().indexOf('Sauvignon Blanc') != -1)
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#000000'});
				}
				else
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#ffffff'});
				}
			}
		);
		
		$('.dropdown li.treefree').hover(
			function()
			{	
				$(this).add($(this).find('> a')).stop().animate({color:'#ffffff'});
			},
			function()
			{	
				$(this).add($(this).find('> a')).stop().animate({color:'#000000'});
			}
		);
		
		
	},

	lock: function(path)
	{
		var a = $('.pageHeader a[@href='+path+']');
		var el = null;

		if(a.length == 1)
		{
			if(a.parents('.dropdown').length == 1)
			{
				el = $('#anchor-'+$(a).parents('ul').attr('id')).parents('li');
			}

			if(a.parents('div.pageHeader').length == 1)
			{
				var depth = a.parents('ul').length;

				if(depth == 1)
				{
					el = a.parent('li');
				}

				if(depth == 2)
				{
					el = a.parents('ul').parent('li');
				}
			}
		}

		if(jsTopNav.current != null)
		{
			jsTopNav.current.unbind().add(jsTopNav.current.find('> a')).stop().css({color:'#000000',backgroundColor:'#FFC20F'});
			jsTopNav.current.hover(
				function()
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#FFC20F',backgroundColor:'#000000'}).children('ul').show();
				},
				function()
				{
					$(this).add($(this).find('> a')).stop().animate({color:'#000000',backgroundColor:'#FFC20F'}).children('ul').hide();
				}
			);
			jsTopNav.current = null;
		}

		if(el != null && el.length == 1)
		{
			el.trigger('mouseout').unbind().add(el.find('> a')).stop().css({color:'#FFFFFF',backgroundColor:'#000000'});
			el.hover(
				function()
				{
					el.children('ul').show();
				},
				function()
				{
					el.children('ul').hide();
				}
			);
			jsTopNav.current = el;
		}
	}
}

var jsUtil = 
{
	
	id: new Array(),

	pos: function(el)
	{
		var p = 0;

		while(el.prev().length > 0)
		{
			p++;
			el = el.prev();
		}

		return p;
	},
	
	genid: function(prefix)
	{
		if(this.id[prefix] != null)
		{
			return prefix+(this.id[prefix]++);
		}
		else
		{
			this.id[prefix] = 1;
			return prefix+0;
		}
	},
	
	ajaxError: function(request,settings,exception)
	{
		if(!$.browser.msie)
		{
			//console.log("AJAX request failed.");
		}
	},

	windowWidth: function() 
	{
		var myWidth = 0;
		if( typeof( window.innerWidth ) == 'number' ) 
		{
			myWidth = window.innerWidth;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
		{
			myWidth = document.documentElement.clientWidth;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
		{
			myWidth = document.body.clientWidth;
		}
		return myWidth;
	},

	windowHeight: function() 
	{
		var myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) 
		{
			myHeight = window.innerHeight;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
		{
			myHeight = document.documentElement.clientHeight;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
		{
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	},

	inArray: function(a,n)
	{
		for(var i=0; i < a.length; i++)
		{
			if(a[i] == n)
			{
				return true;
			}
		}
		return false;
	},

	tweakTopMargin: function()
	{
		var margin = Math.round((jsUtil.windowHeight()-600)/2);
		if(margin > 0)
		{
			$('.pageContainer,').css('marginTop',margin);
			$('.modalContainer div.container').css('marginTop',margin+150);
			$('#modalHundredWaysSubmit div.container').css('marginTop',margin+265);
		}
		else 
		{
			$('.pageContainer').css('marginTop',0);
			$('.modalContainer div.container').css('marginTop',150);
			$('#modalHundredWaysSubmit div.container').css('marginTop',265);
		}	
	}
}

$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 2000, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
		
		
		
        
       // $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function () {
  $('.infiniteCarousel').infiniteCarousel();
  $('#noSeconds').countdown({until: new Date(2010,5,1,12), format: 'DHM'});
  $("#slider").easySlider({
		auto: true,
		continuous: true 
	});
	

  
});



