var slideSwitcher;

document.observe('dom:loaded', function() {

	/**
	* common to all pages
	*/
	$$('a[rel=external]').each(function(elm) {
		elm.target = '_blank';
	});
	
	$('txtDobMonth').observe('focus', function(e) {
		var dobMonth = $('txtDobMonth');
		if(!/[0-9]{1,2}/.test(dobMonth.value)) {
			dobMonth.value = '';
		}
	});
	
	$('txtDobMonth').observe('blur', function(e) {
		var dobMonth = $('txtDobMonth');
		if(!/[0-9]{1,2}/.test(dobMonth.value)) {
			dobMonth.value = 'MM';
		}
	});
	
	$('txtDobDay').observe('focus', function(e) {
		var dobDay = $('txtDobDay');
		if(!/[0-9]{1,2}/.test(dobDay.value)) {
			dobDay.value = '';
		}
	});
	
	$('txtDobDay').observe('blur', function(e) {
		var dobDay = $('txtDobDay');
		if(!/[0-9]{1,2}/.test(dobDay.value)) {
			dobDay.value = 'DD';
		}
	});
	
	$('txtDobYear').observe('focus', function(e) {
		var dobYear = $('txtDobYear');
		if(!/[0-9]{4}/.test(dobYear.value)) {
			dobYear.value = '';
		}
	});
	
	$('txtDobYear').observe('blur', function(e) {
		var dobYear = $('txtDobYear');
		if(!/[0-9]{4}/.test(dobYear.value)) {
			dobYear.value = 'YYYY';
		}
	});

	/**
	* snugride product preview
	*/
	$$('#productPageNav a').each(function(elm) {
		elm.observe('click', function(e) {
			e.stop();
			
			$$('#productPageNav a').each(function(linkElm) {
				if(e.target.innerHTML == linkElm.innerHTML) {
					linkElm.addClassName('activeLink');
				} else {
					linkElm.removeClassName('activeLink');
				}
			});
			switch(e.target.innerHTML) {
				case '1':
					$('page2').hide();
					$('page1').show();
					break;
				case '2':
					$('page1').hide();
					$('page2').show();
					break;
			}
		});
	});

	$$('#products .productExpand').each(function(elm) {
		elm.observe('click', function(e) {
			var preview = e.target.up().select('.productPreviewContainer')[0];
			if(preview.visible()) {
				Effect.BlindUp(preview, {duration: 0.4});
			} else {
				Effect.BlindDown(preview, {duration: 0.4});
			}
		});
	});
	
	
	/**
	* collections product slider
	*/
	var productListContainer = $('productList');
	if(productListContainer) {
		var productList = productListContainer.childElements();
		var isSliding = false;
		var isAtStart = true;
		var isAtEnd = false;
		
		productList.first().siblings().each(function(elm) {
			prevSibling = elm.previous();
			prevSiblingPos = prevSibling.positionedOffset();
			elm.setStyle({left: prevSiblingPos.left + prevSibling.getWidth() + 'px', top: prevSiblingPos.top + 'px'});
		});
		
		productList.each(function(elm) {
			elm.setStyle({display: 'none', visibility: 'visible'});
		});
		
		Effect.multiple(productList, Effect.Appear, {speed: 0.1, delay: 0});
		
		var leftArrow = $('productsLeft');
		var rightArrow = $('productsRight');
		
		function productSlide(slideCount) {
			if(!isSliding) {
				var slideAmount = productList.first().getWidth() * slideCount;
				Effect.multiple(productList, Effect.Move, {
					speed: 0, delay: 0,
					x: slideAmount, y: 0, mode: 'relative',
					transition: Effect.Transitions.sinoidal,
					beforeStart: function(ef) {
						if(!isSliding) {
							if(productList.last().positionedOffset().left + slideAmount < productListContainer.getWidth()) {
								isAtStart = false;
								isAtEnd = true
							} else if(productList.first().positionedOffset().left + slideAmount >= 0) {
								isAtStart = true;
								isAtEnd = false;
							} else {
								isAtStart = false;
								isAtEnd = false;
							}
						}
						isSliding = true;
					},
					afterFinish: function(ef) {
						if(ef.element == productList.last()) {
							isSliding = false;
						}
					}
				});
			}
		}
		
		//un observe when on last item, add new observe to other function to jump the full list, once that's clicked un observe and add the original observe back on
		
		leftArrow.observe('click', function(e) {
			if(isAtStart) {
				var total = $$('#productList > li').size();
				productSlide(-total + total%5);
				isAtStart = false;
				isAtEnd = true;
			} else {
				productSlide(5);
			}
		});
		
		rightArrow.observe('click', function(e) {
			if(isAtEnd) {
				var total = $$('#productList > li').size();
				productSlide(total - total%5);
				isAtStart = true;
				isAtEnd = false;
			} else {
				productSlide(-5);
			}
		});
	}
	
	if($('testButton') != null) {
		$('testButton').observe('click', function(e) {
			var total = $$('#productList > li').size();
			productSlide(total - total%5);
		});
	}
	
	if($('newsletter-signup') != null) {
		$('newsletter-signup').observe('submit', function(e){
			e.stop();
			if($('txtYourName') != null && $('txtYourName').value == "" && $('txtFirstName').value != "" && $('txtLastName').value != "")
				$('txtYourName').value = $('txtFirstName').value+" "+$('txtLastName').value;
			new Ajax.Request('/newsletter-signup.php',
			{
				method:'post',
				parameters: $('newsletter-signup').serialize(true),
				onSuccess: function(transport)
				{
					var response = transport.responseText.evalJSON() || "no response text";
					switch(response.status)
					{
						case "FAIL":
							Modalbox.show('<div class="warning"><p>Please make sure your first and last name are filled out, and you have input a valid email address and date of birth.</p><input type="image" src="images/btn-close.gif" onclick="Modalbox.hide()" /></div>',{title: "There was an Error", width: 300});
						break;
						case "success":
							if($('newsletterCid') != null)
								var linkCid = $('newsletterCid').value;
							else
								var linkCid = "";
							Modalbox.show('<div class="warning"><p>Thank you for signing up for "Wisdom from the Heart". If you would like to create an account on GracoBaby.com, please click <a target="_blank" href="http://www.gracobaby.com/CreateAccount/Pages/CreateAccount.aspx?cid='+linkCid+'">here.</a></p><input type="image" src="images/btn-close.gif" onclick="Modalbox.hide()" /></div>',{title: "Thank You", width: 300});
							$('newsletter-signup').stopObserving();
							$('newsletter-message').hide();
							$('newsletter-thankyou').show();
							s.campaign=linkCid;
							s.events='event22';
							var s_code=s.t();if(s_code)document.write(s_code);
							$('newsletterSignupContent').down('form').remove();
						break;
					}
				},
				onFailure: function()
				{
				}
		  });
		});
	}
	
	friendMessageShown = false;
	alreadyForwardedToFriend = false;
	if($('recommend-friends') != null) {
		$('recommend-friends').observe('submit', function(e){
			e.stop();
			new Ajax.Request('/recommend-friends.php',
			{
				method:'post',
				parameters: $('recommend-friends').serialize(true),
				onSuccess: function(transport)
				{
					var response = transport.responseText.evalJSON() || "no response text";
					switch(response.status)
					{
						case "FAIL":
							Modalbox.show('<div class="warning"><p>Please make sure you have entered your name, your friends name, and a valid email address for your friend.</p><input type="image" src="images/btn-close.gif" onclick="Modalbox.hide()" /></div>',{title: "There was an Error", width: 300});
						break;
						case "success":
							s.campaign=s.getQueryParam('cid');
							if (alreadyForwardedToFriend)
							{
								s.events='event15';
							} else {
								alreadyForwardedToFriend = true;
								s.events='event15,event24';
							}
							s.products=';Friends Referred;;;event15=1';
							var s_code=s.t();if(s_code)document.write(s_code);
							
							$('txtFriendsName').value = "";
							$('txtFriendsEmailAddress').value = "";
							if(!friendMessageShown)
							{
								friendMessageShown = true;
								Modalbox.show('<div class="warning"><p>Thank you for recommending a friend!  To recommend another friend simply put in another name.  This message will not be shown again.</p><input type="image" src="images/btn-close.gif" onclick="Modalbox.hide()" /></div>',{title: "Thank You", width: 300});
							}
						break;
					}
				},
				onFailure: function()
				{
				}
		  });
		});
	}
	/**
	* myride tip slider
	*/
	var tipListContainer = $('tipList');
	if(tipListContainer) {
		var tipList = tipListContainer.childElements();
		var tipIsSliding = false;
		var tipIsAtStart = true;
		var tipIsAtEnd = false;
		tipList.first().setStyle({left:'0px',top:'0px'});
		tipList.first().siblings().each(function(elm) {
			tipPrevSibling = elm.previous();
  		tipPrevSiblingPos = tipPrevSibling.positionedOffset();
			elm.setStyle({left: tipPrevSiblingPos.left + tipPrevSibling.getWidth() + 'px', top: tipPrevSiblingPos.top + 'px'});
		}); 
		tipList.each(function(elm) {
			elm.setStyle({display: 'none', visibility: 'visible'});
		});
		
		Effect.multiple(tipList, Effect.Appear, {speed: 0.1, delay: 0});
		var tipLeft = $('tipsLeft');
		var tipRight = $('tipsRight');
		function tipSlide(slideCount) {
			if(!tipIsSliding) {
				var tipSlideAmount = tipList.first().getWidth() * slideCount;
				Effect.multiple(tipList, Effect.Move, {
					speed: 0, delay: 0,
					x: tipSlideAmount, y: 0, mode: 'relative',
					transition: Effect.Transitions.sinoidal,
					beforeStart: function(ef) {
						if(!tipIsSliding) {
							if(tipList.last().positionedOffset().left + tipSlideAmount < tipListContainer.getWidth()) {
								tipIsAtStart = false;
								tipIsAtEnd = true
							} else if(tipList.first().positionedOffset().left + tipSlideAmount >= 0) {
								tipIsAtStart = true;
								tipIsAtEnd = false;
							} else {
								tipIsAtStart = false;
								tipIsAtEnd = false;
							}
						}
						tipIsSliding = true;
					},
					afterFinish: function(ef) {
						if(ef.element == tipList.last()) {
							tipIsSliding = false;
						}
					}
				});
			}
		}
		
		//un observe when on last item, add new observe to other function to jump the full list, once that's clicked un observe and add the original observe back on
		
		tipLeft.observe('click', function(e) {
			if(tipIsAtStart) {
				var total = $$('#tipList > li').size();
				tipSlide(-3);
				tipIsAtStart = false;
				tipIsAtEnd = true;
			} else {
				tipSlide(1);
			}
		});
		
		tipRight.observe('click', function(e) {
			if(tipIsAtEnd) {
				var total = $$('#tipList > li').size();
        tipSlide(3);
				tipIsAtStart = true;
				tipIsAtEnd = false;
			} else {
				tipSlide(-1);
			}
		});
	}

});

function trackLink(eventName, propName, linkName) {
	var o_s=s_gi(s_account);
	o_s.linkTrackVars='campaign,events,prop3';
	o_s.linkTrackEvents=eventName;
	o_s.events=eventName;
	o_s.campaign=s.getQueryParam('cid');
	o_s.prop3=propName;
	o_s.linkLeaveQueryString = true;
	o_s.tl(this,'d',linkName);
}