var login_text = 'Login to get your own or <a id="sup_button" class="fake_link">Sign Up</a>';
var signup_text = 'Sign up to get your own or <a id="sup_button" class="fake_link">Log In</a>';

var ns_signup_text = 'Already a member? <a id="sup_button" class="fake_link">Log In</a> | <a id="skip_button" class="fake_link">Skip</a>';
var ns_login_text = 'Not a member yet? <a id="sup_button" class="fake_link">Sign Up</a> | <a id="skip_button" class="fake_link">Skip</a>';

var get_button_ind_html = '<img src="http://www.pyzam.com/images/v2/pageGraphics/creating.gif">';
var get_button_html = '<img src="http://static.pyzam.com/images/v2/pageGraphics/getyours_blue.gif">';

var toy_embed;
var working = false;

var greeting_state = 0; /* 1:signup,2:login,3:ns_signup,4:ns_login */ 

// mmp, 6-22-2008
// needed for AS3 ExternalInterface crap to work properly in cranky browsers such as IE. 
// we'll change this to true when the container (that contains the embed/object) is done loading using onload or just at the end of the page
// then flash will automagically set up its crap and be happy
var _jsReady = false;
var _swfReady = false;
function jsReady() { return _jsReady; }
function swfReady() { _swfReady = true; }
function isSwfReady() { return _swfReady; }

/*new PeriodicalExecuter(function(pe) {
	if (signupSetup()) {
		pe.stop();
	}
}, 1);*/

function gf(varName) {
	if (toy_embed)
		return toy_embed.GetVariable("/:"+varName);
	else
		return false;
}

function login() {
	if (typeof before_ajax == 'function') {
		if (!before_ajax()) {
			return;
		}
	}

	if (!working) { 
		working = true;
		$('get_button').innerHTML = get_button_ind_html;
		new Ajax.Request('/newajax/login', {
			method: 'post',
			parameters: 'toy_py_ea='+$('toy_py_ea').value+'&toy_py_pw='+$('toy_py_pw').value+'&toy_py_pw_cf='+$('toy_py_pw_cf').value,
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '-') {
					$('su_message').innerHTML = res[1];
					$('get_button').innerHTML = get_button_html;
					working = false;
				} else {
					$('su_message').innerHTML = 'login successful';

					if (typeof customGetToy == 'function')
						customGetToy();
					else
						getToy();
				}
			},
			onFailure: function() {
				$('su_message').innerHTML = 'failure during login';
				$('get_button').innerHTML = get_button_html;
				working = false;
			}
		});
	}
}

function signup() {
	if (typeof before_ajax == 'function') {
		if (!before_ajax()) {
			return;
		}
	}

	if (!working) { 
		working = true;
		$('get_button').innerHTML = get_button_ind_html;
		new Ajax.Request('/newajax/signup', {
			method: 'post',
			parameters: 'toy_py_ea='+$('toy_py_ea').value+'&toy_py_pw='+$('toy_py_pw').value+'&toy_py_pw_cf='+$('toy_py_pw_cf').value+'&toy_py_un='+$('toy_py_un').value+'&toy_py_ts=yes',
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '-') {
					$('su_message').innerHTML = res[1];
					$('get_button').innerHTML = get_button_html;
					working = false;
					if (res[1] == 'That email exists in our system. Try logging in.') {
						login();
					}
				} else {
					$('su_message').innerHTML = 'signup successful';
					$('get_button').innerHTML = get_button_html;
					working = false;
					login();
				}
			},
			onFailure: function() {
				$('su_message').innerHTML = 'failure during signup';
				$('get_button').innerHTML = get_button_html;
				working = false;
			}
		});
	}
}

function signupSetup() {
	if (!$('sup_button') || !$('toy_greeting') || !$('get_button')) {
		return false;
	}

	if (!$('nosup_sup') || !$('nosup_lin') || !$('skip_button') || !$('ns_get_button')) {
		return false;
	}
	
	
	toy_embed = getFlashMovieObject("mt_embed");
	
	greeting_state = 1;
	

	$('toy_greeting').innerHTML = signup_text;
	//$('password_confirm').style.display='block';
	$('sup_button').onclick = function() { switchGreeting(); }

	$('nosup_sup').onclick = function() { showSignup(); };
	$('nosup_lin').onclick = function() { showLogin(); };
		
	$('skip_button').onclick = function() { 
		if (typeof customSkipSave == 'function') {
			customSkipSave();
		} else if (typeof skipSave == 'function') {
			skipSave();
		} else {
			try {
				toy_embed.sendData(); 
			} catch (err) {
			}

		}
	}

	Event.observe($('ns_get_button'), 'click', function() {
		if (typeof customSkipSave == 'function') {
			customSkipSave();
		} else if (typeof skipSave == 'function') {
			skipSave();
		} else {
			try {
				toy_embed.sendData();
			} catch (err) {
			}
		}
	});

	Event.observe($('get_button'), 'click', function() {
		get_button();
	});

	if ($('password_confirm'))
	Event.observe($('password_confirm'), 'keydown', function(event) {
		if (event.keyCode == 13)
			get_button();
	});
	
	if ($('toy_py_pw'))
	Event.observe($('toy_py_pw'), 'keydown', function(event) {
		if (event.keyCode == 13)
			get_button();
	});
	
	if (typeof toyform_default_to_signup != 'undefined')
		addEvent(window,'load',showSignup);

	return true;
}

function get_button() {
	if (!$('password_confirm')) { // signed in, I hope
		if (typeof before_ajax == 'function') {
			if (!before_ajax()) {
				return;
			}
		}
		if (typeof customGetToy == 'function')
			customGetToy();
		else
			getToy();
	} else { 
		if ($('password_confirm').style.display != 'none') { // if we're in "sign up" mode
			signup();
		} else {
			login();
		}
	}
}

function updateGreeting(new_state) {
	greeting_state = new_state;
	switch (greeting_state) {
		case 1:
			$('toy_greeting').innerHTML = signup_text;
			$('password_confirm').style.display='block';
			$('sup_button').onclick = function() { switchGreeting(); }
			break;
		case 2:
			$('toy_greeting').innerHTML = login_text;
			$('password_confirm').style.display='none';
			$('sup_button').onclick = function() { switchGreeting(); }
			break;
		case 3:
			$('toy_greeting').innerHTML = ns_signup_text;
			$('password_confirm').style.display='block';
			$('nosup_contain').style.display='none';
			$('sup_contain').style.display='block';
			$('sup_button').onclick = function() { NS_switchGreeting(); }
	//		$('skip_button').onclick = function() { toy_embed.sendData(); }
			$('skip_button').onclick = function() { 
				if (typeof customSkipSave == 'function') {
					customSkipSave();
				} else if (typeof skipSave == 'function') {
					skipSave();
				} else {
					try {
						toy_embed.sendData(); 
					} catch (err) {
					}
				}
			}
			break;
		case 4:
			$('toy_greeting').innerHTML = ns_login_text;
			$('password_confirm').style.display='none';
			$('nosup_contain').style.display='none';
			$('sup_contain').style.display='block';
			$('sup_button').onclick = function() { NS_switchGreeting(); }
			//$('skip_button').onclick = function() { toy_embed.sendData(); }
			$('skip_button').onclick = function() { 
				if (typeof customSkipSave == 'function') {
					customSkipSave();
				} else if (typeof skipSave == 'function') {
					skipSave();
				} else {
					try {
						toy_embed.sendData(); 
					} catch (err) {
					}
				}
			}
			break;
		default:
	}

}

function showSignup() {
	updateGreeting(3); /*ns_signup*/
}

function showLogin() {
	updateGreeting(4);
}

function NS_switchGreeting() {
	if (greeting_state == 4 ) { /*ns_login*/
		updateGreeting(3); /*ns_signup*/
	} else if (greeting_state == 3) {
		updateGreeting(4);
	}
}

function switchGreeting() {
	if (greeting_state == 2) { /*login*/
		updateGreeting(1); /*signup*/
	} else {
		updateGreeting(2);
	}
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	} else {
		return document.getElementById(movieName);
	}
}

signupSetup();
