var Application =  {
	lastId: 0,
	currentSampleNb: 0,
	
	  getNewId: function() {
		Application.lastId++;
		return "window_id_" + Application.lastId;
	  },
	  
	addLoginWindow: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<a class=\"welcome\" style=\"cursor: pointer;\" onclick=\"Application.openLoginWindow('" + theme + "')\">Sign in here.</a>";
	document.write(html)
	},
	
	addForumLoginWindow: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<a class=\"darkBlue\" style=\"cursor: pointer; font-size: 100%;\" onclick=\"Application.openLoginWindow('" + theme + "')\">signed in</a>";
	document.write(html)
	},
	
	loginLink: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<a class=\"darkBlueUnderline\" style=\"cursor: pointer; font-size: 100%;\" onclick=\"Application.openLoginWindow('" + theme + "')\">login</a>";
	document.write(html)
	},
	
	registerLink: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<a class=\"darkBlueUnderline\" style=\"cursor: pointer; font-size: 100%;\" onclick=\"Application.openCreateProfileWindow('" + theme + "')\">register</a>";
	document.write(html)
	},
	
	addLogoutWindow: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<span class='login' onclick=\"Application.openLoginWindow('" + theme + "')\">Logout</span>";
	document.write(html)
	},
	
	addCreateProfileWindow: function(theme, modal) {
	  idButton = theme + '_theme_button';
		html = "<a href=\"javascript: Application.openCreateProfileWindow('" + theme + "')\" id=\"join\"><i>Join GameHuddle</i></a>";
	document.write(html)
	},
	
	openCreateProfileWindow: function(theme, modal) {
	  var win = new Window(Application.getNewId(), {className: theme, width:400, height:300, resizable: false, minimizable: false, maximizable: false, title: "<span style='color: #414141;  font-size: 120%; font-family: arial, helvetica, sans-serif; font-weight: bold;'>Create a Profile</span>"});
	  win.getContent().innerHTML= "<form id='windowRegisterForm'><p style='margin: 15px 0 15px 0; color: #555555; font-family: Arial, Helvetica, sans-serif; font-size: 100%;' align='center'>Enter your email, username, and password to create a profile.</p><table style='margin-left: 13px;'><tr><td class='createProfile' width='100'>Email</td><td><input class='signInTextbox' type='text' name='regEmail' id='regEmail' size='35' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Username</td><td><input size='35' class='signInTextbox' type='text' name='regUsername' id='regUsername' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Password</td><td><input size='35' class='signInTextbox' type='password' name='regPass' id='regPass' /></td></tr><tr><td colspan='2'><div style='height: 8px; display: block'></div></td></tr><tr><td></td><td><input type='button' class='button' value='Create Profile' name='Register' id='Register' OnClick=\"windowAjaxCall('register.aspx', 'windowRegisterForm');\" /></td></tr><tr><td colspan='2'><a class='darkBlueUnderline block marginTop20' href=\"javascript:changeWindow('login')\" style='font-size:100%; width: 185px;'>Already a member?  Sign in here.</a></td></tr><tr><td colspan='2'><span id='errorMessage'></span></td></tr></table></form>";
	  win.showCenter(modal);
	},

	openLoginWindow: function(theme, modal) {
	  var win = new Window(Application.getNewId(), {className: theme, width:400, height:300, resizable: false, minimizable: false, maximizable: false, title: "<span style='color: #414141; font-size: 120%; font-family: arial, helvetica, sans-serif; font-weight: bold;'>Sign In</span>"});
	  win.getContent().innerHTML= "<form id='windowLoginForm'><p style='margin: 15px 0 15px 0; color: #555555; font-family: Arial, Helvetica, sans-serif; font-size: 100%;' align='center'>Enter your email and password to login.</p><table style='margin-left: 13px;'><tr><td class='createProfile' width='100'>Email</td><td><input size='35' class='signInTextbox' type='text' name='loginEmail' id='loginEmail' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Password</td><td><input size='35' class='signInTextbox' type='password' name='loginPass' id='loginPass' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td colspan='2'><div style='height: 8px; display: block'></div></td></tr><tr><td></td><td><input type='button' class='button' value='Sign In' name='login' id='login' OnClick=\"windowAjaxCall('register.aspx', 'windowLoginForm');\" /></td></tr></table></form><br/><br/><a class='darkBlueUnderline block marginTop20 marginLeft15' style='font-size:100%; width: 185px;' href=\"javascript:changeWindow('register')\">Not Registered?  Create A Profile.</a>";
	  win.showCenter(modal);
	},
		
	addRightColumn: function() {
    document.write();    
    
    setTimeout(Application.moveFrame, 100);
	},
	
	moveFrame: function() {
	  var f =$$("iframe")[0]; 
    if (f) {
      $("g").appendChild(f)
    }
    else
      setTimeout(Application.moveFrame, 100)
	}
}

function changeWindow(type)
{
	if(type == 'register'){
		new Effect.Fade($('windowLoginForm') , {duration: 1});
		var div = "<form id='windowRegisterForm'><p style='margin: 15px 0 15px 0; color: #555555; font-family: Arial, Helvetica, sans-serif; font-size: 12px;' align='center'>Enter your email, username, and password to create a profile.</p><table style='margin-left: 13px;'><tr><td class='createProfile' width='100'>Email</td><td><input class='signInTextbox' type='text' name='regEmail' id='regEmail' size='35' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Username</td><td><input size='35' class='signInTextbox' type='text' name='regUsername' id='regUsername' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Password</td><td><input size='35' class='signInTextbox' type='password' name='regPass' id='regPass' /></td></tr><tr><td colspan='2'><div style='height: 8px; display: block'></div></td></tr><tr><td></td><td><input type='button' class='button' value='Create Profile' name='Register' id='Register' OnClick=\"windowAjaxCall('register.aspx', 'windowRegisterForm');\" /></td></tr><tr><td colspan='2'><a class='darkBlueUnderline block marginTop20' style='font-size: 100%; width: 185px;' href=\"javascript:changeWindow('login')\" style='font-size:100%'>Already a member?  Sign in here.</a></td></tr><tr><td colspan='2'><span id='errorMessage'></span></td></tr></table></form>";
		
		Windows.focusedWindow.getContent().innerHTML= div;
		Windows.focusedWindow.setTitle("<span style='color: #414141; font-size: 12px; font-size: 120%; font-family: arial, helvetica, sans-serif; font-weight: bold;'>Create a Profile</span>");
		
		$('windowRegisterForm').style.visibility = 'visible';
		$('windowRegisterForm').style.display = 'block';
		$('windowRegisterForm').hide();
		
		Effect.Appear($('windowRegisterForm') , {duration: 1});
	}else{
		new Effect.Fade($('windowRegisterForm') , {duration: 1});
		var div = "<form id='windowLoginForm'><p style='margin: 15px 0 15px 0; color: #555555; font-family: Arial, Helvetica, sans-serif; font-size: 12px;' align='center'>Enter your email and password to login.</p><table style='margin-left: 13px;'><tr><td class='createProfile' width='100'>Email</td><td><input size='35' class='signInTextbox' type='text' name='loginEmail' id='loginEmail' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td class='createProfile'>Password</td><td><input size='35' class='signInTextbox' type='password' name='loginPass' id='loginPass' /></td></tr><tr><td colspan='2'><div style='height: 4px; display: block;'></div></td></tr><tr><td colspan='2'><div style='height: 8px; display: block'></div></td></tr><tr><td></td><td><input type='button' class='button' value='Login' name='login' id='login' OnClick=\"windowAjaxCall('register.aspx', 'windowLoginForm');\" /></td></tr><tr><td colspan='2'><a class='darkBlueUnderline block marginTop20' style='font-size:100%; width: 185px;' href=\"javascript:changeWindow('register')\">Not Registered?  Create A Profile.</a></td></tr><tr><td colspan='2'><span id='errorMessage'></span></td></tr></table></form>";
		
		Windows.focusedWindow.getContent().innerHTML= div;
		Windows.focusedWindow.setTitle("<span style='color: #414141; font-size: 12px; font-size: 120%; font-family: arial, helvetica, sans-serif; font-weight: bold;'>Sign In</span>");
		
		$('windowLoginForm').style.visibility = 'visible';
		$('windowLoginForm').style.display = 'block';
		$('windowLoginForm').hide();
		
		Effect.Appear($('windowLoginForm') , {duration: 1});
	}
}

function windowAjaxCall(requestPage, form)
{
	method = 'post';
	params = Form.serialize($(form));

	new Ajax.Request(
		requestPage,
		{
			method: method,
			postBody: params,
			onLoading: windowLoadingHandler,
			onSuccess: windowSuccessHandler,
			onFailure: windowFailureHandler
		}
	);	

}

function windowFailureHandler (response)
{
	alert(response.responseText);
}

function windowLoadingHandler ()
{
	$('windowIcon').addClassName('loadingIcon');
	new Effect.Appear($('windowIcon'));	
}

function windowSuccessHandler (response)
{
	response = response.responseText.split('|');

	if(response[1] == 'success')
	{
		//$('windowIcon').removeClassName('loadingIcon');
		//$('windowIcon').removeClassName('errorIcon');
		//$('windowIcon').addClassName('successIcon');
		window.location = response[2];
	}

}
