/******************** rotate object **********************/
/*
	The rotate object controls the rotating content found on some department
	home pages and the mu home page.  It displays one of several <div>s located
	in the body of the document for 10 seconds at a time.  Additional
	code needs to be included in the head tag of the document to implement
	the rotate object see the mu home page for an example.
*/
var Rotate = function() {
	var that = this; // for scoping issues
	this._construct = function() {
		that.f = 0;
		that.size = $("div.event").length;
		that.seconds = 10000;
	}
	this.init = function() {
		var current = ++that.f % that.size;
		els = $("div.event");
		lis = $("ul#events_list li");
		for(var i = 0; i < that.size; i++) {
			el = els.get(i);
			li = lis.get(i);
			if(i == current) {
				$(el).fadeIn("slow");
				$(li).addClass("selected");
			} else {
				$(el).hide();
				$(li).removeClass();
			}
		}
	}
	this.start = function() {
		that.interval = setInterval(that.init, that.seconds)
	}
	this._stop = function() {
		clearInterval(that.interval);
		that.f = 0;
	}
	this.restart = function(i) {
		that._stop();
		that.f = --i;
		that.init();
		that.start();
	}

	// create the object
	this._construct();
}
/******************** end rotate object *******************/


/************************ menu object *********************/
/*
	The munu object controls the top and left hand navigation bars
	all menus for all pages are defined in the menus array within the 
	menu object.  Menu needs to be instantiated in the head of all pages
	and the .show() function must also be called in the head to display
	the top menu when the page loads.
	
	todo: factor out the menu definitions
*/
var Menu = function() {
	//Used in the timer function
	var timer=0;
	var timerEnabled=0; //0=Off  1=Open  -1=CloseAll
	var tmpObj = new Object();
	
	var that = this; //scoping issues
	this._construct = function() {
		that.baseHref = "http://www.asu.edu/mu/";
		that.section = "";
		
		/*
		// factor out the menu definitions, not working yet
		 that.menus = $.ajax({   type: "GET",
					url: that.baseHref +"script/menu_definition.js",
					dataType: "json",
					async: "false",
					success: function(menu_def){
						 return that.menus;
   					}
				}); 
		alert(that.menus);
		*/
		 
		/*
		*	decalare menues
		*	edit the following section to add remove or change menu items
		*/
		
		that.menus = {
			"menu root" : {
				"Student Resources" : "student_resources.htm",
				"Student Involvement" : "student_involvement.htm",
				"Event \&amp\; Meeting Services" : "event_meeting_services.htm",
				"MU Services" : "mu_services.htm",
				"Memorial Union" : "index.htm"
			},
			"resources" : {
				"name" : "Student Resources",
				"section" : "menu root",
				"Computer Lab \&amp\; Wireless" : "computers.htm",
				"Learning Resource Center" : "http://www.asu.edu/lrc",
				"Child \&amp\; Family Services" : "family/index.htm",
				"Adult Reentry" : "http://www.asu.edu/studentlife/reentry/",
				"Student Legal Assistance" : "legal/index.htm",
				"Safety Escort Service" : "safety_escort.htm"
			},
			"involvement" : {
				"name" : "Student Involvement",
				"section" : "menu root",
				"Sun Devil Involvement Center" : "sdic/index.htm",
				"Student Government ASASU" : "http://asasu.net",
				"Transitions \&amp\; Parent Programs" : "transitions/index.htm",
				"Student Organizations" : "clubs/index.htm",
				"Fraternity \&amp\; Sorority Life" : "greeklife/index.htm",
				"Community Service" : "community/index.htm",
				"MU Programs \&amp\; Activities" : "http://www.asu.edu/mu/mupa",
				"Student Coalitions" : "http://www.asu.edu/studentaffairs/mss/mcc/studentinvolvement.htm"
			},
			"ems" : {
				"name" : "Event \&amp\; Meeting Services",
				"section" : "menu root",
				"Event Planning" : "events/index.htm",
				"Reservations" : "events/reservations.htm",
				"Meeting Rooms" : "events/meeting_rooms.htm",
				"Rates \&amp\; Fees" : "events/fees.htm",
				"Lawn/Mall Spaces" : "events/lawn_mall_space.htm",
				"Catering" : "catering.htm",
				"Preferred Caterers List" : "caterers.htm",
				"Contact Us" : "contact.htm#ems"
			},
			"services" : {
				"name" : "MU Services",
				"section" : "menu root",
				"Dining" : "dining.htm",
				"Meal Plans" : "http://www.asu.campusdish.com/",
				"Businesses, Banks/ATMs" : "businesses.htm",
				"Sparky's Den" : "sparkys_den.htm",
				"Information Desk, Lost \&amp\; Found" : "information_desk.htm",
				"Advertising, Banners, Display Cases" : "advertising_banners.htm",
				"Air Pump Station" : "air_pump_station.htm"
			},
			"mu" : {
				"name" : "Memorial Union",
				"section" : "menu root",
				"About the MU" : "about.htm",
				"MU Advisory Board" : "advisory_board.htm",
				"Mission, Staff" : "staff.htm",
				"Art Cafe" : "art_cafe.htm",
				"Comedy Troupes" : "comedy_troupes.htm",
				"Art Collection" : "art_collection.htm",
				"Employment" : "student_employment.htm",
				"Policies" : "http://www.asu.edu/aad/manuals/sta/index.html#500"
			},
			"sorc" : {
				"name" : "Student Organizations",
				"section" : "involvement",
				"Search for an Organization" : "clubs/search.htm",
				"Registration" : "clubs/register.htm",
				"Policies" : "clubs/policies.htm",
				"Resources" : "javascript:menu.show('sorc_resources', '#other-menu')",
				"Forms" : "clubs/resources.htm",
				"Hall of Fame" : "clubs/hall.htm",
				//"Hall of Fame" : "javascript:menu.show('sorc_hall', '#other-menu')",
				"Student Risk Management" : "clubs/risk/index.htm",
				"About SORC" : "clubs/team.htm"
			},
			"sorc_policies" : {
				"name" : "Policies",
				"section" : "sorc",
				"Handbook" : "clubs/policies.htm",
				"Sales \&amp\; Solicitation" : "clubs/sales.htm",
				"Advertising \&amp\; Posting" : "clubs/advertising.htm",
				"Freedom of Expression" : "clubs/freedom.htm",
				"Student Risk Management" : "clubs/studentriskmanagement.htm",
				"Trademark" : "clubs/trademark.htm"
			},
			"sorc_resources" : {
				"name" : "Resources",
				"section" : "sorc",
				"Best Practices" : "http://www.asu.edu/studentaffairs/mu/bestpractices/index.htm",
				"Copies \&amp\; Posters" : "clubs/copies.htm",
				"Equipment" : "clubs/equipment.htm",
				"Mail Boxes" : "clubs/storage.htm",
				"Funding Options" : "clubs/funding.htm",
				"University Facility Use" : "clubs/facility.htm",
				"Mall \&amp\; Vendor Information" : "clubs/mall.htm",
				"Web \&amp\; Email" : "clubs/webspace.htm"
			},
			"sorc_hall" : {
				"name" : "Hall of Fame",
				"section" : "sorc",
				"Hall of Fame Nominations" : "clubs/hall_nomination.htm",
				"Hall of Fame Awards" : "clubs/hall.htm",
				"Past Recipients" : "clubs/hallawards.htm"
			},
			"greeklife" : {
				"name" : "Fraternity \&amp\; Sorority Life",
				"section" : "involvement",
				"How to Join" : "javascript:menu.show('greek_join', '#other-menu')",
				"Councils" : "javascript:menu.show('greek_councils', '#other-menu')",
				"Chapters at ASU" : "greeklife/chapters_asu.htm",
				"Current Members" : "javascript:menu.show('greek_members', '#other-menu')",
				"Policies, Procedures & Forms" : "greeklife/policies_and_procedures.htm",
				"Reports" : "greeklife/reports.htm",
				"Advisors" : "greeklife/advisors.htm",
				"Alumni" : "http://www.asu.edu/alumni/",
				"Parents" : "javascript:menu.show('greek_parents', '#other-menu')",
				"Reserve Alpha Drive Fields" : "greeklife/alphadrive.htm",
				"Contact Us" : "javascript:menu.show('greek_contact', '#other-menu')"
			},
			"greek_join" : {
				"Recruitment" : "greeklife/recruitment.htm",
				"Housing" : "greeklife/housing.htm",
				"Glossary" : "greeklife/glossary.htm"
			},
			"greek_councils" : {
				"section" : "greeklife",
				"Interfraternity Council" : "greeklife/ifc.htm",
				"Multicultural Greek Council" : "greeklife/mgc.htm",
				"National Pan-Hellenic Council" : "greeklife/nphc.htm",
				"National Association of Latino Fraternal Organizations" : "greeklife/nalfo.htm",
				"Panhellenic Council" : "greeklife/pan.htm"
			},
			"greek_members" : {
				"Adelphi Commons \&amp\; Adelphi Commons II" : "http://www.campushousing.com/asum/",
				"Housing" : "greeklife/housing.htm",
				"Greek Awards" : "greeklife/awards_application.htm",
				"Greek Life Classes" : "greeklife/greek_life_classes.htm",
				"Scholarships" : "greeklife/scholarships.htm",
				"Greek Life Classes" : "greeklife/greek_life_classes.htm",
				"Violations" : "greeklife/violations.htm",
				"Due Dates" : "greeklife/due_dates.htm"
			},
			"greek_parents" : {
				"FAQ" : "greeklife/parents.htm",
				"Myths &amp; Facts About Hazing" : "greeklife/parents.htm#hazing_myths"
			},
			"greek_contact" : {
				"Our Staff" : "greeklife/contact_us.htm",
				"Council Officers" : "greeklife/council_officers.htm"
			},
			"transitions" : {
				"name" : "Transitions \&amp\; Parent Programs",
				"section" : "involvement",
				"About Us" : "transitions/about_us.htm",
				"University New Student Convocation" : "transitions/newstudent.htm",
				"TPP Programs" : "javascript:menu.show('transitions_programs', '#other-menu')",
				"Staff" : "transitions/staff.htm",
				"Contact Us" : "transitions/contact_us.htm"
			},
			"transitions_programs" : {
				"name" : "TPP Programs",
				"section" : "transitions",
				"Fall Welcome" : "transitions/fallwelcome/index.htm",
				"More to Explore" : "transitions/moretoexplore/index.htm",
				"Family Weekend" : "transitions/Family_Weekend/index.htm",
				"Spring Welcome" : "transitions/Spring_Welcome/index.htm",
				"Parent \&amp\; Family Resources" : "transitions/Parents/index.htm"				
			},
			"transitions_parents" : {
				"Home" : "transitions/Parents/",
				"Welcome ASU Parents" : "transitions/Parents/",
				"Advice From Parents" : "transitions/Sundevil101",
				"Important Dates" : "transitions/Sundevil102",
				"Parents Association" : "transitions/Spring_Welcome",
				"Quick Links" : "transitions/Spring_Welcome",				
				"Contact Us" : "transitions/Spring_Welcome"
			},
			"moretoexplore" : {
				"name" : "More to Explore",
				"section" : "transitions_programs",
				"Home" : "transitions/moretoexplore/index.htm",
				"102 Things Every Sun Devil Experience" : "transitions/moretoexplore/102_things_Sundevil_Should_Know.htm",
				"This Semester's More to Explore Schedule" : "javascript:menu.show('moretoexplore_schedule', '#other-menu')",
				"Contact Us" : "transitions/moretoexplore/contact_us.htm"
			},
			"moretoexplore_schedule" : {
				"name" : "This Semester's More to Explore Schedule",
				"section" : "moretoexplore",
				"Week 1: January 14 - January 20" : "transitions/moretoexplore/complete_schedule_week1.htm",
				"Week 2: January 21 - January 27" : "transitions/moretoexplore/complete_schedule_week2.htm",
				"Week 3: January 28 - February 3" : "transitions/moretoexplore/complete_schedule_week3.htm"
			},
			"parentresources" : {
				"name" : "Parent \&amp\; Family Resources",
				"section" : "transitions_programs",
				"Home" : "transitions/Parents/index.htm",
				"Welcome ASU Parents" : "transitions/Parents/welcome.htm",
				"Advice From Parents" : "transitions/Parents/advice.htm",
				"Calendar of Important Dates" : "http://www.asuparentsassociation.com/eventcalendar.html",
				"Parents Association" : "http://www.asu.edu/parents",
				"Contact Us" : "transitions/Parents/contact_us.htm"
			}

		};
		// end menu item declaration	
		
		that._checkTimer();
	}	
	/***** end constructor *****/
	
	/***** start top menu bar functions *****/
		
	this.get = function(menu_id) {
		return that.menus[menu_id];
	}
	
	/*	show:  
	*	displays the top menu
	*/
	this.show = function(menu_id, submenu_id) {
		var i = 0;
		var _submenu = that.get(menu_id);
		if(_submenu["section"]) {
			_name = _submenu["name"];
			_section = that.get(_submenu["section"]);
			_href = that.baseHref + _section[_name];
		} else {
			_href = '';
		}
		// create a link to return to the homepage
		if(submenu_id == "#submenu") {
			if(_href) {
				_menu_title = "<a href=\"" + _href + "\">" + _submenu["name"] + "</a>";
			} else {
				_menu_title = _submenu["name"];
			}
		}
		var _html = "<h2>" + ((submenu_id == "#submenu")?_menu_title:'') + "</h2>";
		for(li in _submenu) {
			if(li != "name" && li != "section") {
				_html += "<a href=\"";
				_html += (_submenu[li].indexOf('http') == -1 && _submenu[li].indexOf('javascript') == -1)?that.baseHref:'';
				_html += _submenu[li] + "\">" + li.replace(/\s/g, "\&nbsp\;") + "</a>" + " ";
			}
		}
		_html += " ";
		if(submenu_id != "#submenu" && $(submenu_id).size() == 0) {
			$("#submenu").after("<div id=\"" + submenu_id.replace(/#/, "") + "\"></div>");
		}
		$(submenu_id).html(_html);
		$(submenu_id).css("height", ""); // clear the height 
		// only slide down when the submenu is hidden
		if($(submenu_id).is(":hidden")) {
			$(submenu_id).slideDown("slow").css("margin", 0);
		} else {
			$(submenu_id).fadeIn();
		}
	}
	/****** end top menu functions ******/

	/****** start left hand menu functions ******/	
	
	/*	addSubs
	*	required for accordian function
	*	adds the submenus dynamicly
	*/
	this.addSubs = function(menu_id) {
		//get submenu nodes
		var _submenu = that.get(menu_id);
		
		//build submenu
		var _html ="<ul>";
		for(li in _submenu) {
			if(li != "name" && li != "section") {
				_html += "<li><a href=\"";
				_html += (_submenu[li].indexOf('http') == -1 && _submenu[li].indexOf('javascript') == -1)?that.baseHref:'';
				_html += _submenu[li] + "\">" + li + "</a></li>";
			}
		}
		_html += "</ul>";
		
		// add  submenus and hide them
		$("#" + menu_id).parent().append(_html);
		$("#" + menu_id).siblings("ul").hide();		
	}
	
	/*	accordian
	*	displays mouseover effect of the left hand menu, 
	*	not the top menu
	*/
	this.accordian = function(element, closeAll) {
		if (closeAll==false){
			$(element).siblings("ul:hidden").slideDown("normal");
			$(element).parent().siblings().children("ul:visible").slideUp("normal");
		}else
			$(element).parent().children("ul:visible").slideUp("normal");
	}
	
	/*Recursive timer check
	*	increases usability of the mouseover accordian effect
	*	pauses the mouseover function briefly to 
	*/
	this._checkTimer = function (){
		w = setTimeout("menu._checkTimer();",100);
		if (that.timerEnabled!=0){
			that.timer--;
			if (that.timer<=0){
				if (that.timerEnabled==1) {
					that.accordian(that.tmpObj, false);
					}
				else
					that.accordian($("#asuint-left-column ul li a"), true);
					
				that.timerEnabled=0;
			}
		}
	}
	
	//Recieves input from the current page to implement the _checkTimer
    this.accordionOpenMenu = function(element) {
		that.timerEnabled=1;
		that.timer=5;
		that.tmpObj = element;
	}
	
	//Recieves input from the current page
	this.accordionCloseMenu  = function() {
		that.timerEnabled=-1;
		that.timer=5;
	}
	
	/****** end left hand menu functions ******/
	
	// create the menu object
	this._construct();
}
/********************* end menu object *************************/

/************** extra functions for textboxes ******************/
function calculateWords(textbox, divName, wordlimit){
	wordsDiv = document.all ? document.all[divName] : document.getElementById(divName);
	wordsLeft = wordlimit - textbox.value.split(' ').length;
	wordsDiv.innerHTML = "<strong>You have "+ wordsLeft +" words left. </strong>";
	if (wordsLeft <=0)
		return false;

	return true;
}

function autoFormatPhone(textbox, evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	if (textbox.value.length == 0 )
		textbox.value = "(";
	if (textbox.value.length == 4 )
		textbox.value += ") ";
	if (textbox.value.length == 9 )
		textbox.value += "-";
	if (textbox.value.length > 13 )
		return false;

	textbox.focus(); textbox.value = textbox.value;
	return true;
}

/************** end of extra functions for textboxes ******************/

	function parseParameter(varName){
		var ret = null;
		if (window.location.toString().split("?").length>1){
			var allParams = window.location.toString().split("?")[1];
			var params = allParams.split("&");
			for (var i=0; i<params.length;i++){
				if (params[i].split("=")[0]==varName)
					ret = params[i].split("=")[1];
			}
		}
		return ret;
	}