var oneIdolTabber = {
	
	ToggleDivInProcess : null,
	ToggleCodenameInProgress : null,
	ActiveTab : null,
	HTMLcache : Array,	
	
	doToggle : function(whichDiv, whichURL, whichCodename) {
	
		oneIdolTabber.ToggleDivInProcess = whichDiv;
		oneIdolTabber.ToggleCodenameInProgress = whichCodename;
		
		YAHOO.util.Dom.removeClass(oneIdolTabber.ActiveTab + '-tab', 'tab-selected');		
		document.getElementById(oneIdolTabber.ActiveTab + '-content').style.display = 'none';
		document.getElementById(whichCodename + '-content').style.display = 'block';
		oneIdolTabber.ActiveTab = whichCodename;		
		YAHOO.util.Dom.addClass(whichCodename + '-tab', 'tab-selected');
		
		return;
		
		if(typeof oneIdolTabber.HTMLcache[whichCodename] != 'undefined') {
			document.getElementById(whichDiv).innerHTML = oneIdolTabber.HTMLcache[whichCodename];
			//alert(oneIdolTabber.HTMLcache[whichCodename]);
		}
		else {
					var myAjax = new Ajax.Request( whichURL,
					{
						method: 'get',
						onSuccess: oneIdolTabber.handleToggleSuccessResponse,
						onFailure: oneIdolTabber.ajaxFailure
					});	
		}
		

	
	},
	
	handleToggleSuccessResponse : function(response) {
		//alert(response.responseText);
		//alert("oneIdolTabber.HTMLcache." + oneIdolTabber.ToggleCodenameInProgress + " = " + response.responseText);
		eval("oneIdolTabber.HTMLcache['" + oneIdolTabber.ToggleCodenameInProgress + "'] = response.responseText;");
		//alert("oneIdolTabber.HTMLcache['" + oneIdolTabber.ToggleCodenameInProgress + "'] = " + "escape('test storage');");
		//alert("eval done");
		document.getElementById(oneIdolTabber.ToggleDivInProcess).innerHTML = response.responseText;
	
	},
	
	ajaxFailure : function() {
		alert("Ajax problem...");
	}
	
}

var idolBookmark = {
	addURL: 'http://myidol.americanidol.com/go/ajax/_addBookmark?output_method=ajax',
	delURL: 'http://myidol.americanidol.com/go/ajax/_removeBookmark?output_method=ajax',
	moduleURL: null,
	currentlyLoading: false,
	
	addBookmark: function()
	{
		if (idolBookmark.currentlyLoading) {
			return false;
		}
		
		var theName = document.getElementById('bookmarkName').value;
		var theURL = document.getElementById('bookmarkURL').value;
		
		if (!theURL) {
			alert("Please make sure you enter a URL for your bookmark.");
			return false;
		}
		
		var params = "name=" + theName + "&URL=" + theURL;
		
		idolBookmark.currentlyLoading = true;
		YAHOO.util.Connect.asyncRequest("POST", this.addURL, {success: idolBookmark.bookmarkAdded, failure: idolBookmark.bookmarkFailed}, params);
	},
	
	deleteBookmark: function(bookmarkId)
	{
		if (!bookmarkId || idolBookmark.currentlyLoading) {
			return false;
		}
		
		var params = "bookmarkID=" + bookmarkId;
		
		idolBookmark.currentlyLoading = true;
		YAHOO.util.Connect.asyncRequest("POST", this.delURL, {success: idolBookmark.bookmarkRemoved, failure: idolBookmark.bookmarkFailed}, params);
	},
	
	bookmarkAdded: function(o)
	{
		idolBookmark.currentlyLoading = false;
		
		var res = eval('(' + o.responseText + ')');
		
		if (!res['success']) {
			alert("Sorry, adding that bookmark failed.");
			return false;
		}
		
		var theDiv = document.createElement('div');
		theDiv.className = 'bookmarkItem';
		theDiv.id = 'bookmark_' + res['new_id'];
		
		var itemNameDiv = document.createElement('div');
		itemNameDiv.className = 'bookmarkItemNameLink';
		
		var bookmarkAnchor = document.createElement('a');
		bookmarkAnchor.href = res['URL'];
		
		var name = res['URL'];
		
		if (res['name']) {
			name = res['name'];
		}
		
		bookmarkAnchor.innerHTML = name;
		
		itemNameDiv.appendChild(bookmarkAnchor);
		
		var itemContsDiv = document.createElement('div');
		itemContsDiv.className = 'bookmarkItemTools';
		
		var deleteAnchor = document.createElement('a');
		deleteAnchor.href = "#";
		deleteAnchor.onclick = function() {
			idolBookmark.deleteBookmark(res['new_id']);
			return false;
		}
		deleteAnchor.innerHTML = "<span>Delete</span>";
		
		itemContsDiv.appendChild(deleteAnchor);
		
		theDiv.appendChild(itemNameDiv);
		theDiv.appendChild(itemContsDiv);
		
		var remMsg = null;
		
		if (remMsg = document.getElementById('bookmarkEmpty')) {
			remMsg.parentNode.removeChild(remMsg);
		}
		
		document.getElementById('bookmarksContainer').appendChild(theDiv);
		
		idolBookmark.hideAddForm();
	},
	
	bookmarkRemoved: function(o)
	{
		idolBookmark.currentlyLoading = false;
		
		var res = eval('(' + o.responseText + ')');
		
		if (!res['success']) {
			alert("Sorry, removing that bookmark failed.");
			return false;
		}
		
		var deadMeat = document.getElementById('bookmark_' + res['rem_id']);
		
		deadMeat.parentNode.removeChild(deadMeat);
	},
	
	bookmarkFailed: function()
	{
		return false;
	},
	
	showAddForm: function()
	{
		var theFormDiv = document.getElementById('bookmarkAddForm');
		theFormDiv.style.display = 'block';
	},
	
	hideAddForm: function()
	{
		var theFormDiv = document.getElementById('bookmarkAddForm');
		theFormDiv.style.display = 'none';
	},
	
	goToPage: function(pageNumber)
	{
		if (!pageNumber) {
			return false;
		}
		
		if (!this.moduleURL) {
			return false;
		}
		
		this.moduleURL +="&pg=" + pageNumber;
		
		idolBookmark.currentlyLoading = true;
		
		YAHOO.util.Connect.asyncRequest("GET", this.moduleURL, {success: idolBookmark.reloadModule, failure: idolBookmark.bookmarkFailed});
		
		return false;
	},
	
	reloadModule: function(o)
	{
		idolBookmark.currentlyLoading = false;
		
		if (!o.responseText) {
			alert("Sorry, removing that bookmark failed.");
			return false;
		}
		
		document.getElementById('mod_user_bookmarks').parentNode.innerHTML = o.responseText;
	}
}

var aistatus = new Object();

	/**
	 *	Function:					aistatus.init
	 *	Purpose:					initializes by adding a click listener to a container
	 */	

	aistatus.init = function () {
		if(document.getElementById("idolStatusUpdaterDefault")) {
			YAHOO.util.Event.addListener("idolStatusUpdaterDefault", "click", aistatus.initEditBox);
		}
	}
	
	/**
	 *	Function:					aistatus.createEditBox
	 *	Purpose:					Creates and returns the field necessary for the user to edit status
	 */	

	aistatus.createEditBox = function () {
		this.box = document.createElement('input');
		this.box.setAttribute('type', 'text');
		this.box.id = "statusUpdateInputBox";
		this.box.setAttribute("maxlength", "100");
		this.box.setAttribute('value', 'is ');
		this.keyer = new YAHOO.util.KeyListener(this.box, {keys:13},{fn:aistatus.submitStatus});
		this.keyer.enable();
		YAHOO.util.Event.addListener(document.body, "click", aistatus.killEditBox);
		return this.box;
	}
	
	/**
	 *	Function:					aistatus.createEditButton
	 *	Purpose:					Creates and returns the button to initiate the update call
	 */	

	aistatus.createEditButton = function () {
		this.button = document.createElement('button');
		this.button.id = 'statusUpdateSubmitButton';
		this.button.innerHTML = '<span>Post</span>';
		YAHOO.util.Event.addListener(this.button, 'click', aistatus.submitStatus);
		return this.button;
	}
	
	/**
	 *	Function:					aistatus.checkForEnter
	 *	Purpose:					Checks to see if the enter key was pressed
	 */	

	aistatus.checkForEnter = function () {
		
	}
	
	/**
	 *	Function:					aistatus.killEditBox
	 *	Purpose:					Removes the update field and button from the DOM, unhides default text
	 */	

	aistatus.killEditBox = function () {
		this.editBox = document.getElementById('statusUpdateInputBox');
		this.editButton = document.getElementById('statusUpdateSubmitButton');
		this.editBox.parentNode.removeChild(this.editBox);
		this.editButton.parentNode.removeChild(this.editButton);
		YAHOO.util.Dom.setStyle('idolStatusUpdaterDefault', 'display', '');
		YAHOO.util.Event.removeListener(document.body, "click", aistatus.killEditBox);
	}
	
	/**
	 *	Function:					aistatus.initEditBox
	 *	Purpose:					Calls methods to create input field and button, inserts them into the DOM
	 */	

	aistatus.initEditBox = function (e) {
		YAHOO.util.Event.stopEvent(e);
		this.statusBox = aistatus.createEditBox();
		this.statusButton = aistatus.createEditButton();	
		YAHOO.util.Dom.setStyle('idolStatusUpdaterDefault', 'display', 'none');
		document.getElementById('idolStatusUpdater').appendChild(this.statusBox);
		document.getElementById('idolStatusUpdater').appendChild(this.statusButton);
		this.statusBox.focus();
	}
	
	/**
	 *	Function:					aistatus.submitStatus
	 *	Purpose:					Gets status message from input field, initiates AJAX call
	 */	

	aistatus.submitStatus = function (e) {
		YAHOO.util.Event.stopEvent(e);
		var params = new Array();
		params['responseFunction'] = aistatus.updateStatusText;
		params['status'] = document.getElementById('statusUpdateInputBox').value;
		params['type'] = 'both';
		params['action'] = 'updateStatus';
		params['responseType'] = 'JSON';
		params['responseFormat'] = 'JSON';
		params['handlerName'] = 'one_ajax_statusHandler';
		params['requestType'] = 'class';
		params['sdb'] = 1;
		this.editBox = document.getElementById('statusUpdateInputBox');
		this.editButton = document.getElementById('statusUpdateSubmitButton');
		
		this.editBox.setAttribute('disabled', 'disabled');
		this.editButton.innerHTML = '<span>Updating</span>';
		this.editButton.setAttribute('disabled', 'disabled');

		//Make the request
		OneAjax.request(params);	
	}
	
	/**
	 *	Function:					aistatus.updateStatusText
	 *	Purpose:					updates user's status message in the DOM, initiates aistatus.killEditBox method
	 *
	 *	@param object response		returned from AJAX call, JSON array containing success/failure message and updated status message
	 */	

	aistatus.updateStatusText = function (response) {
		document.getElementById('ownerStatusMessage').innerHTML = response.status_text;
		document.getElementById('ownerStatusUpdated').innerHTML = response.time;
		aistatus.killEditBox();
	}

var aimininav = new Object();

	/**
	 *	Function:					aimininav.toggle
	 *	Purpose:					toggles visibility of submenus in the mininav
	 *
	 *	@param object caller		the parent menu item being clicked
	 */	

	aimininav.toggle = function (caller) {
		this.hideUs = YAHOO.util.Dom.getElementsByClassName('miniNavSubMenu');
		YAHOO.util.Dom.setStyle(this.hideUs, 'display', 'none');
		this.showMe = YAHOO.util.Dom.getElementsByClassName(caller.id);
		YAHOO.util.Dom.setStyle(this.showMe, 'display', '');
		return false;
	}
	
var aitooyoung = new Object();

	aitooyoung.init = function () {
		this.bodyId = document.body.id;
		if (this.bodyId == "signup1_index" || this.bodyId == "signup_signup") {
			aitooyoung.checkAge();
			return true;
		} else {
			return false;
		}
	}

	aitooyoung.checkAge = function () {

		var gdate = new Date();
		var udate = new Date();
		var mindate = new Date();
		var maxdate = new Date();
		
		gdate.setHours(23, 59, 59);
		
		var birth_fields = document.getElementById('signupFieldbirth').getElementsByTagName('select');
		
		for (var i = 0; i < birth_fields.length; i++) {
			if (birth_fields[i].name == 'SUbirth_Year') {
				var uyear = Number(birth_fields[i].value);
				continue;
			}
			if (birth_fields[i].name == 'SUbirth_Month') {
				var umonth = Number(birth_fields[i].value);
				continue;
			}
			if (birth_fields[i].name == 'SUbirth_Day') {
				var uday = Number(birth_fields[i].value);
				continue;
			}
		}
		
		udate.setFullYear(uyear, umonth - 1, uday);
		udate.setHours(0, 0, 1);
		
		var thisYear = gdate.getFullYear();
		
		mindate.setFullYear(thisYear - 13);
		maxdate.setFullYear(thisYear - 18);
		
		if (udate > maxdate && udate < mindate) {
			
			var params = new Array();
			
			params['responseFunction'] = aitooyoung.displayResults;
			params['templateName'] = 'template/safetyTips';
			params['action'] = 'loadTemplate';
			params['responseType'] = 'JSON';
			params['handlerName'] = 'one_ajax_templateLoader';
			params['requestType'] = 'class';
			
			OneAjax.request(params);
			
			return false;
			
		}
		
		return true;
	}


	aitooyoung.displayResults = function (resp) {

		aitooyoung.myPanel = new YAHOO.widget.SimpleDialog(
			'myPanel',
			{
				width: "600px",
				modal : true,
				close : false,
				zIndex : 600,
				fixedCenter : true,
				buttons: [ { text:"Accept", handler:aitooyoung.handleAccept, isDefault:true} ]
			}
		);
		
		//Set the header from parameter and set body from response text
		aitooyoung.myPanel.setHeader('<span>Safety Tips</span>');
		aitooyoung.myPanel.setBody(resp['output']);
		
		aitooyoung.myPanel.render(document.body);
		aitooyoung.myPanel.show();
		
	}

	aitooyoung.handleAccept = function() {
		this.hide();
		document.signupForm.submit();
	}
	
YAHOO.util.Event.onDOMReady(aistatus.init);