/*



Dynamic Application of Functions to DOM
*/

Event.observe(window, "load", init);



/*



This is the function that applies the functions to the DOM
*/
function init(){


	/*
	
	
	
	Embed Flash
	IEHacks.EmbedFlash(EF_object, EF_width, EF_height, EF_filename, true);
	*/



	/*
	
	
	
	Makes Internet Explorer render 32 bit PNGs properly
	IEHacks.MakePNGTransparent("#head h1 img");
	*/
	/*
	
	
	Round Corners
	Corners.apply({
		"Section" : "tl tr",
		"SideSection" : "tl tr br bl",
		"UploadImages" : "tl tr br bl"
	});
	*/
	/*
	
	
	#PrimaryNav Drop Down show's the first ul child
	*/
	$$("#PrimaryNav li").each(function(node, i){
		if(node.getElementsBySelector("ul").length > 0){
			var ul = node.getElementsBySelector("ul")[0];
			node.observe(
				"mouseover",
				function(){
					node.toggleClassName("hover");
					ul.toggle();
				}
			);
			node.observe(
				"mouseout",
				function(){
					node.toggleClassName("hover");
					ul.toggle();
				}
			);
		}
	});
	/*
	
	
	
	Forms
	*/
		/*
		
		
		
		Focus on an input
		*/
		$$("input").each(function(node, i){
			node.observe("focus", function(){node.toggleClassName("focus");});
			node.observe("blur", function(){node.toggleClassName("focus");});
		});
		/*
		
		
		
		Clear the email input on the Newsletter form
		*/
		if($("NewsletterEmail")){
			var node = $("NewsletterEmail");
			AlreadyReset = false;
			node.observe(
				"click",
				function(){
					if (!AlreadyReset){
						node.clear();
						AlreadyReset = true;
					}
				}
			);
		}
		/*
		
		
		Simple form validation
			onsubmit, feed the ValidateForm() function an array containing id's of form inputs you wish to validate
			
		*/
		$$(".CustomerForm").each(function(node, i){
			
			/*
			
			clean white space text node elements!
			node.cleanWhitespace(); // breaks in ie
			*/
			node.observe(
				"submit",
				function(){
					/*
					
					an array of elements that will always be validated
					*/
					ValidateMe = $$(".Validate");
					/*
					
					validate form, cancel submission if it vails validation
					*/
					if(ValidateForm(ValidateMe) == false){
						this.blur;
						return false;
					}
				}
			);
		});


}






/*



Library of Functions
*/



/*



Called from within init() and rounds the corners of elements with the class "rounded"
*/
function RoundCorners(){

	RoundedCornersHTML =  "<div class=\"roundedTR\"></div><div class=\"roundedBR\"></div><div class=\"roundedBL\"></div><div class=\"roundedTL\"></div>";

	$$(".rounded").each(function(node, i){
		node = $(node);
		// Test if the element is already rounded by looking at the innerHTML
		if(node.innerHTML.substr(0, RoundedCornersHTML.length) != RoundedCornersHTML){
			new Insertion.Top(node, RoundedCornersHTML);
		}

	});

}


var IEHacks = {

	/*
	
	This function makes 32 bit PNG's with transparency work
	*/	
	MakePNGTransparent: function(selector) {

		if (navigator.userAgent.indexOf("MSIE ") == -1){
			
			return false;
			
		} else {
	
			$$(selector).each(function(node, i){
						
				var node = $(node);
				
				if (node.src.indexOf("png") == -1) {
					
					node.runtimeStyle.filter = "";
					
					return;
				}
				
				var oldSrc = node.src;
				
				node.src = "interface/images/transparent.gif";
				
				node.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldSrc + "',sizingMethod='scale')";
				
			});
			
		}

	},
	/*
	
	
	
	This function is used to embed the Flash into the page
	
	Variables:
	
		Variable Name   : Laman        : Data Type
	
		EF_object       : id of object : String
		EF_width        : width        : String
		EF_height       : height       : String
		EF_filename     : filename     : String
		EF_transparency : transparency : Boolean
	
	*/
	EmbedFlash: function(EF_object, EF_name, EF_width, EF_height, EF_filename, EF_transparency) {
		/*
			var EF_object = "FlashFeature";
			var EF_name = SomethingUnique;
			var EF_width = "100%";
			var EF_height = "100%";
			var EF_filename = "/interface/flash/FlashFeature.swf";
			var EF_filename = true;
		*/
	
		if($(EF_object)){
			
			if (EF_transparency){
				var EF_transparency_ParamMarkup = new String('<param name="wmode" value="transparent" />');
				var EF_transparency_EmbedMarkup = new String('wmode="transparent"');
			} else {
				var EF_transparency_ParamMarkup = new String('');
				var EF_transparency_EmbedMarkup = new String('');
			}
	
			var FlashHTML =   new String('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + EF_width + '" height="' + EF_height + '" id="' + EF_name + '" align="middle" style="height: ' + EF_height + '; width: ' + EF_width + ';">'
							+ '<param name="allowScriptAccess" value="sameDomain" />'
							+ EF_transparency_ParamMarkup
							+ '<param name="movie" value="' + EF_filename + '" />'
							+ '<param name="quality" value="high" />'
							+ '<embed src="' + EF_filename + '" '+ EF_transparency_EmbedMarkup + ' quality="high" width="' + EF_width + '" height="' + EF_height + '" name="' + EF_name + '" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
						+ '</object>');

			$(EF_object).update(FlashHTML);
		}
	}
}
/*


Returns the Flash object,
allowing you to use methods and functions from within the ActionScript itself
*/
function $Flash(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
/*



Called from within init() and rounds the corners of elements with the class "rounded"
*/
var Corners = {
	
	build: function(corners){
		var RoundedCornersHTML = new String();
		$w(corners).each(function(node, i){
			RoundedCornersHTML += "<div class=\"rounded" + node.toUpperCase() + "\"></div>";
		});
		return RoundedCornersHTML;
	},
	
	apply: function(ToBeRounded){
		$H(ToBeRounded).each(function(node, i){
			if ($(node.key)){
				new Insertion.Top(node.key, Corners.build(node.value));
			}
		});
	}

};




/*


A Form Validation Function
That decorates the page with feedback
	<div>
		<label for="asdf">asdf</label>
		<input id="asdf" name="asdf" /><br />
	</div>
*/
function ValidateForm(vf_ValidateMe){
	ValidationError = $A();
	$A(vf_ValidateMe).each(function(node, i){
		/*
		
		If it's an actual node
		*/
		if($(node)){
			node = $(node);
			parent = $(node.parentNode);
			/*
			
			if it's empty
			*/
			if((node.value == null) || (node.value == "")){
				ValidationError[i] = true;
				parent.toggleClassName("ValidationError");
			/*

			else it's not empty 
			*/
			} else {
				ValidationError[i] = false;
			}
			
			/*
			
			when the input is blurred, remove the ValidationError class if ...
			*/
			node.observe(
				"blur",
				function(){
					/*
					
					if the input has something in it and parent div has the "ValidationError" class, remove it
					*/
					parent = $(node.parentNode);
					if (node.value.length > 0){
						parent.toggleClassName("ValidationError");
					}
					
					/*
					
					Toggles the "focusing" class
					*/
					node.toggleClassName("focus");
				}
			);
		}
	});
	
	/*
	
	if validation failed
	*/
	if(ValidationError.any()){
		alert("Please fill out the required form fields.");
		this.blur();
		location.href = "#totop";
		return false;
	}

}


