/*



Custom Functions

Uses http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
// (Throws security errors if using https in IE7 )
Event.onDOMReady(function(){
	init();
});
*/
window.onload = function(){
	init();
};
/*
	


This is the function that attached the Javascript functions to the page
*/
function init(){


	/*
	
	
	
	Embed Flash
	EmbedFlash(EF_object, EF_width, EF_height, EF_filename, true);
	*/



	/*
	
	
	
	Makes Internet Explorer render 32 bit PNGs properly
	IE_PNG_Hack("#head h1 img");
	*/
	/*
	
	
	#PrimaryNav Drop Down show's the first ul child
	*/
	$$("#PrimaryNav li").each(function(node, i){

		node = $(node);

		if($A(node.getElementsByTagName("ul")).length > 0){

			var ul = $($A(node.getElementsByTagName("ul")).first());

			node.onmouseover = node.onmouseout = function(){
				
				ToggleClassName(node, "hover");

				ul.toggle();
			};

		}
		
	});
	/*
	
	
	
	Clear the email input on the Newsletter form
	*/
	$$("form.newsletter input[name=email]").each(function(node, i){

		node = $(node);

		AlreadyReset = false;
		
		node.onclick = function(){

			if (AlreadyReset != true){
				node.value = "";
				AlreadyReset = true;
			}

		};

	});




	/*
	
	
	
	Forms
	*/
		/*
		
		
		
		Focus on an input
		*/
		$$("form input").each(function(node, i){
			node = $(node);
			node.onfocus = node.onblur = function(){

				ToggleClassName(node, "focus");

			};
	
		});
		/*
		
		
		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){
			node = $(node);
			
			/*
			
			clean white space text node elements!
			node.cleanWhitespace(); // breaks in ie
			*/
			node.onsubmit = function(){
				/*
				
				an array of id's that will always be validated
				*/
				FormIDArray = new Array(
					"customer_name_first",
					"customer_name_last",
					"customer_phone_daytime",
					"customer_email",
					"address_1_address",
					"address_1_move_in_date",
					"address_1_city",
					"address_1_state",
					"address_1_zip",
					"address_2_address",
					"address_2_city",
					"address_2_state",
					"address_2_zip"
				);
				/*
				
				validate form, cancel submission if it vails validation
				*/
				if(ValidateForm(FormIDArray) == false){
					this.blur;
					return false;
				}
			};
		});

	
}

/*



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);
		}

	});

}

/*



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

*/
function EmbedFlash(EF_object, EF_width, EF_height, EF_filename, EF_transparency){

/*
	var EF_object = "FlashFeature";
	var EF_width = "100%";
	var EF_height = "100%";
	var EF_filename = "FlashFeature";
*/

	if($(EF_object)){
		
		if (EF_transparency){
			var EF_transparency_ParamMarkup = '<param name="wmode" value="transparent" />';
			var EF_transparency_EmbedMarkup = 'wmode="transparent"';
		} else {
			var EF_transparency_ParamMarkup = '';
			var EF_transparency_EmbedMarkup = '';
		}

		FlashHTML =   '<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_object + '" align="middle">'
						+ '<param name="allowScriptAccess" value="sameDomain" />'
						+ EF_transparency_ParamMarkup
						+ '<param name="movie" value="interface/flash/' + EF_filename + '" />'
						+ '<param name="quality" value="high" />'
						+ '<embed src="interface/flash/' + EF_filename + '" ' + EF_transparency_EmbedMarkup + ' quality="high" width="' + EF_width + '" height="' + EF_height + '" name="' + EF_object + '" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
					+ '</object>';

		$(EF_object).innerHTML = FlashHTML;
	}

}

/*

This function makes 32 bit PNG's with transparency work
*/	
function IE_PNG_Hack(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')";
			
		});
		
	}
	
}

/*


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_FormIDArray){
	ValidationError = $A();
	$A(vf_FormIDArray).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;
				/*
				
				if the parent div doesn't already have the "ValidationError" class, add it
				*/
				if(!parent.hasClassName("ValidationError")){
					parent.addClassName("ValidationError");
				}
			/*

			else it's not empty 
			*/
			} else {
				ValidationError[i] = false;
			}
			
			/*
			
			when the input is blurred, remove the ValidationError class if ...
			*/
			node.onblur = 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){
					if(parent.hasClassName("ValidationError")){
						parent.removeClassName("ValidationError");
					}
				}
				
				/*
				
				Toggles the "focusing" class
				*/
				if(node.hasClassName("focus")){
					node.removeClassName("focus");
				} else {
					node.addClassName("focus");
				}
			};
		}
	});
	
	/*
	
	if validation failed
	*/
	if(ValidationError.any()){
		alert("Please fill out the required form fields.");
		this.blur();
		location.href = "#totop";
		return false;
	}

}

function ToggleClassName(TCN_element, TCN_ClassName){
	node = $(TCN_element);
	if (node.hasClassName(TCN_ClassName)) {
		node.removeClassName(TCN_ClassName);
	} else {
		node.addClassName(TCN_ClassName);
	}
}

