/* Thompson JavaScript */

window.addEvent('domready', function() {
		
	// Smooth Scroll page links
	var mySmoothScroll = new SmoothScroll();
	
	
	// Open External Links in a new window
	externalLinks = function() {
		var allAnchors = $(document.body).getElements('a');
		for (var i=0; i<allAnchors.length; i++) {
			var myCurrentAnchor = allAnchors[i];
			if (myCurrentAnchor.get('href') && myCurrentAnchor.get('rel') == "externalLink") {
				myCurrentAnchor.target = "_blank";
			}
		}
	}
	externalLinks();

	
	
	// Rotate images on home page
	if ($('rotatorContainer')) {
		var rotater = new Rotater('.rotateThis',{ 	//Class of elements that should rotate.
			slideInterval:4500, 					//Length of showing each element, in milliseconds
			transitionDuration:800 					//Length crossfading transition, in milliseconds
		});
	}
	
	
	// Rotate logos on glasses & sunglasses page
	if ($('logosRotatorContainer')) {
		var logosRotater = new Rotater('.rotateThisLogo',{ 	//Class of elements that should rotate.
			slideInterval:2800, 							//Length of showing each element, in milliseconds
			transitionDuration:750 							//Length crossfading transition, in milliseconds
		});
	}
	
	
	// Process Glasses Search Form
	if ($('eyeglassesFieldSet')) {
		
		processEyeglassesSearch = function() {
			
			if ($('selectedColor').value != "anyColor" && $('selectedBrandName').value != "anyBrand" && $('selectedType').value != "anyType") {
				
				location.href = "/searchEyeglasses/" + $('selectedColor').value + "/" + $('selectedBrandName').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedBrandName').value == "anyBrand" && $('selectedType').value == "anyType") {
				
				alert("Please select at least one criteria to filter your search by.");
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedBrandName').value == "anyBrand") {
				
				location.href = "/searchEyeglasses/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedType').value == "anyType") {
				
				location.href = "/searchEyeglasses/" + $('selectedBrandName').value;
				return false;
				
			}
			
			if ($('selectedBrandName').value == "anyBrand" && $('selectedType').value == "anyType") {
				
				location.href = "/searchEyeglasses/" + $('selectedColor').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor") {
				
				location.href = "/searchEyeglasses/" + $('selectedBrandName').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedBrandName').value == "anyBrand") {
				
				location.href = "/searchEyeglasses/" + $('selectedColor').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedType').value == "anyType") {
				
				location.href = "/searchEyeglasses/" + $('selectedColor').value + "/" + $('selectedBrandName').value;
				return false;
				
			}
			
			
		} //End process Glasses Search function
		
	} // End if eyeglassesFieldSet
	
	
	
	
	// Process Sunglasses Search Form
	if ($('sunglassesFieldSet')) {
		
		processSunglassesSearch = function() {
			
			if ($('selectedColor').value != "anyColor" && $('selectedBrandName').value != "anyBrand" && $('selectedType').value != "anyType") {
				
				location.href = "/searchSunglasses/" + $('selectedColor').value + "/" + $('selectedBrandName').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedBrandName').value == "anyBrand" && $('selectedType').value == "anyType") {
				
				alert("Please select at least one criteria to filter your search by.");
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedBrandName').value == "anyBrand") {
				
				location.href = "/searchSunglasses/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor" && $('selectedType').value == "anyType") {
				
				location.href = "/searchSunglasses/" + $('selectedBrandName').value;
				return false;
				
			}
			
			if ($('selectedBrandName').value == "anyBrand" && $('selectedType').value == "anyType") {
				
				location.href = "/searchSunglasses/" + $('selectedColor').value;
				return false;
				
			}
			
			if ($('selectedColor').value == "anyColor") {
				
				location.href = "/searchSunglasses/" + $('selectedBrandName').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedBrandName').value == "anyBrand") {
				
				location.href = "/searchSunglasses/" + $('selectedColor').value + "/" + $('selectedType').value;
				return false;
				
			}
			
			if ($('selectedType').value == "anyType") {
				
				location.href = "/searchSunglasses/" + $('selectedColor').value + "/" + $('selectedBrandName').value;
				return false;
				
			}
			
			
		} //End process Sunglasses Search function
		
	} // End if sunglassesFieldSet
	
	
	
	
	// Enter To Win Form Validation
	if ($('subscribeForm')) { 
		
		checkSubscribeForm = function() {
			
			var original = $('name').value;
			var o_split = original.split(" ");
			//this probably isn't a complete list of words that shouldn't be capitalized
			var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');


			if ($('name').value ==""){
				alert("Please provide your name.");
				$('name').focus();
				return false;
			}
			
			if ($('name').value !==""){
				for (i=0;i<o_split.length;i++) {
					if (i == 0) {
						//always capitalize the first word
						o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
					}
					else if(special_words.indexOf(o_split[i]) < 0) { 
					  	o_split[i] = (o_split[i].substring(0,1)).toUpperCase() + o_split[i].substring(1);
					}
				}
				retval = o_split.join(' ');
				$('name').value = retval;
			}

		
			if ($('emailAddress').value ==""){
				alert("Please enter your email address.");
				$('emailAddress').focus();
				return false;
			}
		
			if ($('emailAddress').value !=""){
				var x = $('emailAddress').value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (!filter.test(x)) {
						alert("Oops, looks like there's a problem with your email address.");
						$('emailAddress').focus();
						return false;
					}
			}
		
			if ($('postalCode').value ==""){
				alert("Please provide your Postal Code.");
				$('postalCode').focus();
				return false;
			}
			
			if ($('postalCode').value !=""){
				var myPC = $('postalCode').value;
				var filter  = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
					if (!filter.test(myPC)) {
						alert("Oops, looks like your postal code is not formatted correctly. A1B 2C3 or A1B2C3");
						$('postalCode').focus();
						return false;
					}
			}
			
			if ($('permission').checked !== true){
				alert("You must agree to receive our newsletter to be eligible for the draw.");
				return false;
			}
			
			return (true);
			
		} //end checkForm function
		
	} //end if subscribeForm
	
	
	
}); // Close DomReady function




	
	




