
/****************************************************************************

  This file is part of the GamesCast.tv Website

	GamesCast.tv coded by Steve "GreasedScotsman" Huff, 
	Copyright 2010 Steve Huff
	
	It is distributed under the terms of the 
	GNU General Public License
		
***************************************************************************/

function submitLogout() {
		$.get($(".logout").attr("href"), 
			function(data) { 
				$("#logoutErrors").html("Reloading the page to complete the logout request..."); 
				setTimeout("window.location.reload()", 2000);
			} 
		);
}

function submitLogin() {
	if ($("#lUser").val() == "" || $("#lPass").val() == "") {
		var bValid = false;
		$("#loginErrors").html("<b>Error:</b> You need to specify a username and password.");
		$("#loginErrors").fadeIn(500);
	}
	else {
		var bValid = true;
		$("#loginErrors").html("");
	}
						
	if (bValid) {

		var theloginform = document.getElementById("loginform");
		//theloginform.submit();
		$.post("/forums/ucp.php?mode=login", { sid: $("#loginsid").val(), redirect: $("#loginredir").val(), login: $("#loginaction").val(), 
								username: $("#lUser").val(), password: $("#lPass").val(), autologin: $("input[name='autologin']:checked").val() }, 
			function(data) { 
				$("#loginErrors").html("Reloading the page to complete login..."); 
				setTimeout("window.location.reload()", 2000);
			} 
		);
		

		//alert("User: "+$("#lUser").val());

/*		$.post("http://www.gamescast.tv/forums/ucp.php?mode=login", { username: $("#lUser").val(), 
										password: $("#lPass").val(),
										autologin: $("input[name='autologin']:checked").val() },
			function(data){
				$("#loginDialog").dialog('close');
				window.location.reload();
			}
 		);
*/
	}
}

$(document).ready(function(){

/*	if ($.browser.msie) {

		$("#loginDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 'auto',
			width: 300,
			position: ['right','top'],
			modal: false,
			buttons: {
				//'Login': function () { $(this).click( submitLogin() ) },
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
					$("#pDialogSuccessMsg").html('<p>Login was <b>cancelled</b>.</p>');
					$("#pDialogSuccessMsg").fadeIn(500);
					$("#pDialogSuccessMsg").fadeOut(5000);
			}
		});
	} else {
*/
		$("#ielogin").hide();

		$("#loginDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 'auto',
			width: 300,
			position: ['right','top'],
			modal: false,
			buttons: {
				'Login': function () { $(this).click( submitLogin() ) },
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
					$("#pDialogSuccessMsg").html('<p>Login was <b>cancelled</b>.</p>');
					$("#pDialogSuccessMsg").fadeIn(500);
					$("#pDialogSuccessMsg").fadeOut(5000);
			}
		});


//	}


		$("#logoutDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 'auto',
			width: 300,
			position: ['right','top'],
			modal: true,
			buttons: {
				'Logout': function () { $(this).click( submitLogout() ) },
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
					$("#pDialogSuccessMsg").html('<p>Logout was <b>cancelled</b>.</p>');
					$("#pDialogSuccessMsg").fadeIn(500);
					$("#pDialogSuccessMsg").fadeOut(5000);
			}
		});	

		
		
	$('#userLogin').click(function() {
		$('#loginDialog').dialog('open');
		LoginDialogTheme(0);
		$("#loginErrors").html("");		
	});
	
	$('#userLoginContent').click(function() {
		$('#loginDialog').dialog('open');
		LoginDialogTheme(0);
		$("#loginErrors").html("");		
	})
	
	.hover(
		function(){ 
			//$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			//$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
	$("input[class='login']").bind("keydown", function(event) {
		// track enter key
		var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
		if (keycode == 13) { // keycode for enter key
			// force the 'Enter Key' to implicitly click the Update button
			submitLogin();
			return false;
		} else {
			return true;
		}
	}); 

	$(".logout").click(function() {
		$('#logoutDialog').dialog('open');
		LogoutDialogTheme(0);
		return false;				
	});

var dwrap = [false, false];

// fix for dialogs not getting the class-scoped dialog UI theme
function LoginDialogTheme(dialog){
	switch (dialog){
		case 0 :
			if( !dwrap[0] ){ 
				$("div[aria-labelledby*='ui-dialog-title-loginDialog']").wrap("<div class='gctvtheme'></div>"); 
				dwrap[0] = true; 
			}

			//for overlay no good way but i dont see another
			$(".ui-widget-overlay").wrap("<div class='gctvtheme'></div>"); 
		break; 
		case 1 : 
			if( !dwrap[1] ){
				$("div[aria-labelledby*='ui-dialog-title-loginDialog']").wrap("<div class='gctvtheme'></div>"); 
				dwrap[1] = true; 
			}
		break; 
		default : 
		break; 
	}
}

function LogoutDialogTheme(dialog){
	switch (dialog){
		case 0 :
			if( !dwrap[0] ){ 
				$("div[aria-labelledby*='ui-dialog-title-logoutDialog']").wrap("<div class='gctvtheme'></div>"); 
				dwrap[0] = true; 
			}

			//for overlay no good way but i dont see another
			$(".ui-widget-overlay").wrap("<div class='gctvtheme'></div>"); 
		break; 
		case 1 : 
			if( !dwrap[1] ){
				$("div[aria-labelledby*='ui-dialog-title-logoutDialog']").wrap("<div class='gctvtheme'></div>"); 
				dwrap[1] = true; 
			}
		break; 
		default : 
		break; 
	}
}

});
 
// EOF
