/**
 *
 * @access public
 * @return void
 **/

function onloadGeneral(current_page) {
	if(current_page == "who_i_am") {
		//Get JSON array with Twitter data (particularly the most recent feed(s))
		//Pass this array to the (locally-stored) processing function to package the tweet and relevant info into an LI and place it in the DOM
		var twitter_JSON = document.createElement("script");
		twitter_JSON.type="text/javascript"
		twitter_JSON.src="http://twitter.com/statuses/user_timeline/nathanaeldew.json?callback=twitterCallback&count=20"
		document.getElementsByTagName("head")[0].appendChild(twitter_JSON);
	}
}

//Accepts a string designating the current page, which is used to determine (among other things) which menu item will not be assigned event handlers
function setupEvents(current_page) {
	// Find the menu list, and create mouseover event handlers for all list elements in the menu
	var menu_LIs = document.getElementById('page_titles_menu').getElementsByTagName("LI");
	for(i = 0; i < menu_LIs.length; i++) {
		var li = menu_LIs[i];
		//If this particular LI element does not correspond to the currently selected page, then setup hover events 
		//	(we don't want the tab for the current page to have any mouseover effects)
		if(li.className.indexOf('current_page') < 0) {	
			li.onmouseover = menu_hover;
			if (li.captureEvents) li.captureEvents(Event.MOUSEOVER);
			li.onmouseout = menu_hover;
			if (li.captureEvents) li.captureEvents(Event.MOUSEOUT);
			
/*			anchors = li.getElementsByTagName("A");
			
			for(j = 0; j < anchors.length; j++) {
				anchor = anchors[j];
				anchor.onmouseover = hover;
				if (anchor.captureEvents) anchor.captureEvents(Event.MOUSEOVER);
				anchor.onmouseout = hover;
				if (anchor.captureEvents) anchor.captureEvents(Event.MOUSEOUT);
			}
*/			
		}
	}
	
	if(current_page == "index") {
		var vellum1 = document.getElementById('index_vellum1');
		vellum1.style.marginLeft = "11px";
		vellum1.style.width = "128px";
		indexVellum1SlideAnim = new YAHOO.util.Anim('index_vellum1', "", 3, YAHOO.util.Easing.easeBoth);
				indexVellum1SlideAnim.attributes.marginLeft = {
					from: 11,
					to: 62
				};
				indexVellum1SlideAnim.attributes.width = {
						from: 128,
						to: 240
					};
		setTimeout('indexVellum1SlideAnim.animate()', 1000);
		indexVellum2SlideAnim = new YAHOO.util.Anim('index_vellum2', "", 3, YAHOO.util.Easing.easeBoth);
		indexVellum2SlideAnim.attributes.marginTop = {
			from: 0,
			to: 29
		};
		setTimeout('indexVellum2SlideAnim.animate()', 3000);
	}
	else if(current_page == "where_im_going") {
		//Setup collapsable elements (i.e. form subsections (fieldsets))
		basicCollapseBarSetup("prosp_client");
		
		YAHOO.util.Event.addListener(document.getElementById('prosp_client_anchor'), "click", function() {
			var showFormAnim = new YAHOO.util.Anim('prosp_client', "", 2, YAHOO.util.Easing.easeOut);
					showFormAnim.attributes.height = {
						from: 0,
						to: 276
					};
			carrySubtitle = new YAHOO.util.Anim('subtitle1', "", 2, YAHOO.util.Easing.easeBoth);
					carrySubtitle.attributes.bottom = {
						from: 192,
						to: 34
					};		
			document.getElementById('prosp_client').style.height = "0px";
			document.getElementById('prosp_client').style.display = "block";
			showFormAnim.animate();
			setTimeout("document.getElementById('subtitle1').style.zIndex = 4; carrySubtitle.animate()", 300);
			showFormAnim.onComplete.subscribe(function(){
						setTimeout("document.getElementById('prosp_client').style.height = 'auto';  document.getElementById('prosp_client').style.overflowY = 'visible';", 500);
						//Neuter form-showing link, now that form is visible
						YAHOO.util.Event.removeListener(document.getElementById('prosp_client_anchor'), "click");
			});
		});
		
		//Setup events to display form sub-sections based on selected value of the project type ("projtype") menu
		function showOneFieldset(menu_val){
			//If this has not yet been done, we'll start by clearing some space by minimizing (and moving) the current clients form,
			//	then widening the prospective clients form to utilize the vacated space.
			if(!document.getElementById('prosp_client').className.match(/\bbig\b/)) {
				//Re-structure the styling to a less-flexible but in this case visually identical presentation that will allow me to 
				//	hide overflow in the vellum content area (hiding overflow in any way also hides the left_img_holder div that is absolutely
				//	positioned and hanging off the left side of the content div)
				var cc_content_style = document.getElementById('where_im_going_vellum1_content').style;
//				cc_content_style.backgroundPosition = "left center";
//				cc_content_style.marginLeft = "-38px";
//				cc_content_style.paddingLeft = "38px";
//				cc_content_style.width = "220px";
				cc_content_style.overflowY = "hidden";
				
				//Prepare to gently collapse the Current clients vellum... 
				shrinkCCVellumAnim = new YAHOO.util.Anim('where_im_going_vellum1_content', "", 1, YAHOO.util.Easing.easeOut);
					shrinkCCVellumAnim.attributes.height = {
						from: 250,
						to: 0
					};
				//...an action that will be accompanied by a gaudy show of animating power: swooping the Current clients vellum into 
				//	the lower left corner of the main image area	
				var attributes = { 
					points: { by: [-680, 350], control: [ [100, 100], [-200, -300] ] } 
				}; 
				var swoopCCVellum = new YAHOO.util.Motion('where_im_going_vellum1', attributes, 1.5, YAHOO.util.Easing.easeOut);
/*
				swoopCCVellum.onComplete.subscribe(function() {
					swoopCCVellum.attributes = { 
						points: {
							to: [500, 350]
						}
					}
				});
*/
				
				//Prepare to expand and shift the prosp_client vellum
				shiftPCVellumAnim = new YAHOO.util.Anim('prosp_client_vellum', "", 1, YAHOO.util.Easing.easeOut);
					shiftPCVellumAnim.attributes.width = {
						from: 300,
						to: 549
					};
					shiftPCVellumAnim.attributes.marginRight = {
						from: 24,
						to: -244
					};
				
				swoopCCVellum.animate();
				shrinkCCVellumAnim.animate();
//				shrinkCCVellumAnim.onComplete.subscribe(function() {shiftPCVellumAnim.animate();});
				shiftPCVellumAnim.animate();
				//Add a class to the form element to summon styles designed for the larger layout
				document.getElementById('prosp_client').className += " big";
				
				//Add a mechanism to revive the Current clients vellum (by clicking on the header thereof)
				document.getElementById('where_im_going_vellum1_header').className += " link";
				YAHOO.util.Event.addListener(document.getElementById('where_im_going_vellum1_header'), "click", function() {
					shrinkCCVellumAnim.attributes.height = {
						from: 0,
						to: 250
					};
					swoopCCVellum.attributes = { 
						points: {
							by: [0, -188]
						}
					}
					shrinkCCVellumAnim.animate();
					swoopCCVellum.animate();
					var cc_head = document.getElementById('where_im_going_vellum1_header');
					cc_head.style.height = "auto";
					cc_head.className = cc_head.className.replace(/\blink\b/, "nolink");
					YAHOO.util.Event.removeListener(cc_head, "click");
					
					var cc_content_style = document.getElementById('where_im_going_vellum1_content').style;
					cc_content_style.backgroundPosition = "right center";
					cc_content_style.marginLeft = "0";
					cc_content_style.paddingLeft = "0";
					cc_content_style.width = "220px";
					cc_content_style.overflowY = "visible";
				});
			}
			
			//Now show only the selected fieldset:
			//First, make sure all top-level fieldsets are hidden, in case the user has previously selected a different option and is now reconsidering
			var children = document.getElementById('prosp_client').childNodes;
			for (var i = 0; i < children.length; i++) {
				var child = children[i];
				if (child.tagName) {
					if (child.tagName.match(/fieldset/i)) {
						if (!child.id.match(/fs_personal_info/i)) {
							child.style.display = "none";
						}
					}
				}
			}
			//Then display the fieldset corresponding to the selected option
			fs_id = "fs_" + menu_val + "_details";
			document.getElementById(fs_id).style.display = "block";
		}
		
		//Setup events to dispay certain field(set)s based on the value of another control
		//TODO: Create a generalized function (or object method-based procedure, etc) to handle all of these genitor-progeny relationships
		//TODO: Deselect all progeny controls in the event that their genitor is deselected
		YAHOO.util.Event.addListener(document.getElementById('projtype'), "change", function(){
				var select = document.getElementById('projtype');
				var sel_index = select.selectedIndex;
				if(sel_index > 0) {
					var val = select.options[sel_index].value;			
					showOneFieldset(val);
				}
			});
		
		chex_toggle = function(genitor, progeny){
			alert(genitor);
			if(document.getElementById(genitor).checked) {
				display(progeny);
			}
			else {
				disappear(progeny);
			}
		}
			
//		YAHOO.util.Event.addListener(document.getElementById('redesign_logo'), "change", "chex_toggle('redesign_logo', 'redesign_fs_logo')");	
		
//		YAHOO.util.Event.addListener(document.getElementById('redesign_logo'), "click", chex_toggle('redesign_logo', 'redesign_fs_logo'));
/*
		function(){
				if(document.getElementById('redesign_logo').checked) {
					display('redesign_fs_logo');
				}
				else {
					disappear('redesign_fs_logo');
				}
			});	

*/
		YAHOO.util.Event.addListener(document.getElementById('redesign_logo'), "change", function(){
				if(document.getElementById('redesign_logo').checked) {
					display('redesign_fs_logo');
				}
				else {
					disappear('redesign_fs_logo');
				}
			});	

		YAHOO.util.Event.addListener(document.getElementById('redesign_logo'), "click", function(){
				if(document.getElementById('redesign_logo').checked) {
					display('redesign_fs_logo');
				}
				else {
					disappear('redesign_fs_logo');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_logo'), "change", function(){
				if(document.getElementById('add-on_logo').checked) {
					display('add-on_fs_logo');
				}
				else {
					disappear('add-on_fs_logo');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_logo'), "click", function(){
				if(document.getElementById('add-on_logo').checked) {
					display('add-on_fs_logo');
				}
				else {
					disappear('add-on_fs_logo');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('new_site_db_login'), "change", function(){
				if(document.getElementById('new_site_db_login').checked) {
					display('new_site_fs_login');
				}
				else {
					disappear('new_site_fs_login');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('new_site_db_login'), "click", function(){
				if(document.getElementById('new_site_db_login').checked) {
					display('new_site_fs_login');
				}
				else {
					disappear('new_site_fs_login');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_db_login'), "change", function(){
				if(document.getElementById('add-on_db_login').checked) {
					display('add-on_fs_login');
				}
				else {
					disappear('add-on_fs_login');
				}
			});	
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_db_login'), "click", function(){
				if(document.getElementById('add-on_db_login').checked) {
					display('add-on_fs_login');
				}
				else {
					disappear('add-on_fs_login');
				}
			});	
			
		YAHOO.util.Event.addListener(document.getElementById('new_site_db_other'), "change", function(){
				if(document.getElementById('new_site_db_other').checked) {
					display('new_site_db_other_desc_set');
				}
				else {
					disappear('new_site_db_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('new_site_db_other'), "click", function(){
				if(document.getElementById('new_site_db_other').checked) {
					display('new_site_db_other_desc_set');
				}
				else {
					disappear('new_site_db_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('redesign_func_newtech_other'), "change", function(){
				if(document.getElementById('redesign_func_newtech_other').checked) {
					display('redesign_func_newtech_other_desc_set');
				}
				else {
					disappear('redesign_func_newtech_other_desc_set');
				}
			});

		YAHOO.util.Event.addListener(document.getElementById('redesign_func_newtech_other'), "click", function(){
			if(document.getElementById('redesign_func_newtech_other').checked) {
				display('redesign_func_newtech_other_desc_set');
			}
			else {
				disappear('redesign_func_newtech_other_desc_set');
			}
		});
		
		YAHOO.util.Event.addListener(document.getElementById('redesign_other'), "change", function(){
				if(document.getElementById('redesign_other').checked) {
					display('redesign_other_desc_set');
				}
				else {
					disappear('redesign_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('redesign_other'), "click", function(){
				if(document.getElementById('redesign_other').checked) {
					display('redesign_other_desc_set');
				}
				else {
					disappear('redesign_other_desc_set');
				}
			});
			
		YAHOO.util.Event.addListener(document.getElementById('add-on_db_other'), "change", function(){
				if(document.getElementById('add-on_db_other').checked) {
					display('add-on_db_other_desc_set');
				}
				else {
					disappear('add-on_db_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_db_other'), "click", function(){
				if(document.getElementById('add-on_db_other').checked) {
					display('add-on_db_other_desc_set');
				}
				else {
					disappear('add-on_db_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_other'), "change", function(){
				if(document.getElementById('add-on_other').checked) {
					display('add-on_other_desc_set');
				}
				else {
					disappear('add-on_other_desc_set');
				}
			});
		
		YAHOO.util.Event.addListener(document.getElementById('add-on_other'), "click", function(){
				if(document.getElementById('add-on_other').checked) {
					display('add-on_other_desc_set');
				}
				else {
					disappear('add-on_other_desc_set');
				}
			});
				
		//Temporary solution for IE's lack of support for attribute selectors (e.g. input[type="checkbox"])
		//Give every input element a class with the same name as its type attribute (default is text, 
		//	for broswers and therefore in my css - no need to set "text" class here if type attribute is null)
		var inputs = document.getElementById('prosp_client').getElementsByTagName("INPUT");
		var num_inputs = inputs.length;
		var input;
		for(var i = 0; i < num_inputs; i++) {
			input = inputs[i];
			if(input.type) input.className += " " + input.type;
		} 		
				
		//Create ajax object to handle form submission
		var prosp_client_submit_ajax = new ajaxObject('prosp_client_internal', 'process_form.php');
//		var prosp_client_submit_ajax = new ajaxObject('prosp_client_internal', '_lib/phpmailer-fe.php');
		prosp_client_submit_ajax.callback = function() {
			document.getElementById('subtitle1').style.bottom = "192px";
		}
		
		invalid_email = 0;
		
		 function submitForm(){
			//TODO: Validate form data
			var email_el_1 = document.getElementById('email');
			var email_1 = email_el_1.value;
			var email_1_label = document.getElementById('email_label');
			var email_el_2 = document.getElementById('email_confirm');
			var email_2 = email_el_2.value;
			var email_2_label = document.getElementById('email_confirm_label');
			if(email_1.length > 0) {
				//TODO: modify regex to prevent two consecutive dot (".") charachters in the local-name 
				if(email_1.match(/^([a-zA-Z0-9_\-])([a-zA-Z0-9_\.\-])*\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
					//There is no longer a problem with email_1
					email_el_1.className = email_el_1.className.replace(/\bform_error\b/, "");
					email_1_label.className = email_1_label.className.replace(/\bform_error\b/, "");
					if(email_1 == email_2) {
						//There is no longer a problem with email_2
						email_el_2.className = email_el_2.className.replace(/\bform_error\b/, "");
						email_2_label.className = email_2_label.className.replace(/\bform_error\b/, "");
						
						var projtype_el = document.getElementById('projtype');
						var projtype = projtype_el.options[projtype_el.selectedIndex].value;
						var projtype_label = document.getElementById('projtype_label');
						if(projtype != 0) {
							//Submit the form
							var form_params = getFormParamString(document.getElementById('prosp_client'));
							prosp_client_submit_ajax.update(form_params, "POST");
							//Display a "processing your data" graphic
							var img_str = "";
							for(var i = 1; i < 5; i++) {
								img_str += "<img id=\"processing_img_" + i + "\" class=\"processing_img\" src=\"graphics/processing_rect_" + i + ".png\"/> \n";
							}
							document.getElementById('prosp_client_internal').innerHTML = "<div id=\"processing_images\"/>" + img_str + "</div>Processing your data...";
							proc_images = document.getElementById('processing_images').getElementsByTagName("IMG");
							i = 0;
							function animateProcessingImages(){
								proc_images[i].style.visibility = "visible";
								proc_images[(i+3) % 4].style.visibility = "hidden";
								i = (i + 1) % 4;
							}
							setInterval(animateProcessingImages, "150");
						}
						else {
							alert("Please select a project type.");
							projtype_el.focus();
							if(!projtype_el.className.match(/\bform_error\b/)) projtype_el.className += " form_error";
							if(!projtype_label.className.match(/\bform_error\b/)) projtype_label.className += " form_error";
						}
					}
					else {
						alert("Please enter the same email address in both fields, to ensure that I can communicate with you.");
						email_el_2.focus();
						if(!email_el_2.className.match(/\bform_error\b/)) email_el_2.className += " form_error";
						if(!email_2_label.className.match(/\bform_error\b/)) email_2_label.className += " form_error";
					}
				}
				else {
					var alert_str = "Please enter a valid email address.";
					if(invalid_email > 0) alert_str += " The following obscure characters are not allowed: ! # $ % * / ? | ^ { } ` ~ & ' + = ";  
					alert(alert_str);
					invalid_email++;
					email_el_1.focus();
					if(!email_el_1.className.match(/\bform_error\b/)) email_el_1.className += " form_error";
					if(!email_1_label.className.match(/\bform_error\b/)) email_1_label.className += " form_error";
				}
			}	
			else {
				alert("Please enter an email address so that I can contact you to discuss this project.");
				email_el_1.focus();
				if(!email_el_1.className.match(/\bform_error\b/)) email_el_1.className += " form_error";
				if(!email_1_label.className.match(/\bform_error\b/)) email_1_label.className += " form_error";
			}		
		}	
		
		YAHOO.util.Event.addListener(document.getElementById('prosp_client_submit'), "click", submitForm);
	}
	
	//Setup events to display helptext elements (e.g. to explain form fields)
	setupHelptext();
}

function menu_hover(the_event) {

	var targ;
	if (!the_event) var the_event = window.event;
	// the_event gives access to the event in all browsers
	if (the_event.target) targ = the_event.target; //DOM standard
	else if (the_event.srcElement) targ = the_event.srcElement; //IE
	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}	
	// 'targ' now refers to the original target element regardless of what major browser is being used
	
	// targ may be the menu LI or any element beneath it
	// iterate upward until getting to the actual LI element, so that we can then proceed on solid footing
	while(targ.tagName != "LI") {
		targ = targ.parentNode;
	}
	//Then, look for the only image descendant of the LI
	var images = targ.getElementsByTagName("IMG");
	//If more (or less) than one image appears to be beneath the LI element, then something was misaligned.  Otherwise, proceed to show/hide image.
	if(images.length == 1) {
		targ = images[0];
		if(the_event.type == "mouseover") {
			targ.style.visibility = "visible";
		}
		else if(the_event.type == "mouseout") {
			targ.style.visibility = "hidden";
		}
	}
}

function show(elementId) {
	if(document.getElementById(elementId) != null) {
  		document.getElementById(elementId).style.visibility = 'visible';
  	}
}

function hide(elementId) {
	if(document.getElementById(elementId) != null) {
  		document.getElementById(elementId).style.visibility = 'hidden';
  	}
}

function switchChildImages(parentId){
	var childImages = document.getElementById(parentId).getElementsByTagName('img');
	var image1 = childImages[0];
	var image2 = childImages[1];
	z_index1 = image1.style.zIndex;
	z_index2 = image2.style.zIndex;
	if ( z_index1 == null || z_index2 == null ) {
		image1.style.zIndex = 1;
		image2.style.zIndex = 2;
	}
	else {
		if (z_index1 >= z_index2) {
			image1.style.zIndex = 1;
			image2.style.zIndex = 2;
		}
		else {
			image1.style.zIndex = 2;
			image2.style.zIndex = 1;
		}
	}
}

function preloadImages(page_name) {
	if (document.images) {
		if (page_name == "where_im_going") {
			proc_images = new Array();
			for (var i = 1; i < 5; i++) {
				proc_img = new Image(107, 11);
				proc_img.src = "graphics/processing_rect_" + i + ".png";
				proc_images[i - 1] = proc_img;
			}
		}
    }
}

/**
 *
 * @access public
 * @return void
 **
function init(){
	var page_titles_menu = document.getElementById('page_titles_menu');
	if (!page_titles_menu) return;
	var page_title_list_elements = page_titles_menu.getElementsByTagName('li');
	var page_title_names = new Array('what_i_do', 'where_ive_been', 'where_im_going', 'who_i_am');
	for (var i = 0; i < page_title_list_elements.length && i < page_title_names.length; i++) {
		page_title_list_elements[i].onmouseover = switchChildImages(page_title_names[i] + '_anchor');
		page_title_list_elements[i].onmouseout  = switchChildImages(page_title_names[i] + '_anchor');
	}
}
*
*/
