var loginDialog = null;
var userObj = null;

$(document).ready(function() {
	$('#nav').droppy();
	$('li a').click(function(e) {
		// if (e.target.id == 'log') document.location.href = "login/login.html";
		if (e.target.id == 'signin') MenuHandler.login(loginDialog);
	});
});

YAHOO.util.Event.onDOMReady(function () { 
	buildLogin();
	
	var onSuccess = function(o) {
		// alert("The response was: " + o.responseText);
		userObj = eval("(" + o.responseText + ")");
		// debugger;
		if (userObj.auth == true) {
			setMenu();
			doCalendar();
			// debugDB();
		} else {
			alert('Your login failed.');
			
		}
	}
	
	var onFailure = function(o) {
		alert("AJAX failure: " + o.status);
	}

	var handleCancel = function() {
		this.cancel();
	}
	var handleSubmit = function() {
		this.submit();
	}


    YAHOO.util.Dom.removeClass("loginDiaglog", "yui-pe-content");


	loginDialog = new YAHOO.widget.Dialog("loginDiaglog",
			{ 					
							width : "20em",
							fixedcenter : true,
							visible : false,
							modal : true,
							// underlay : 'shadow',
							constraintoviewport : true,
							buttons : [ { text:"Sign in", handler:handleSubmit, isDefault:true },
								      { text:"Cancel", handler:handleCancel } ]
			}
	);
	
loginDialog.callback.success = onSuccess;
loginDialog.callback.failure = onFailure;
	

loginDialog.render(); 


}
);





/*
<div id="loginDiaglog" class="yui-pe-content">
    <div class="hd">Please enter your login information</div>
    <div class="bd">
        <form name="dlgForm" method="POST" action="login/login.post.php">
		    <table>
				<tr>
					<td><label for="userName">User Name:</label></td>
					<td><input type="text" name="u" /></td>
				<tr>
					<td><label for="passWord">Password:</label></td>
					<td><input type="password" name="p" /></td>
			</table>
        </form>
    </div>
</div>
*/


function buildLogin() {
	
	var loginContainer = document.createElement('DIV');
	loginContainer.id = 'loginDiaglog';
	loginContainer.className = 'yui-pe-content';
	var d = document.createElement('DIV');
	d.className = 'hd';
	d.innerHTML = 'Please Login below';
	loginContainer.appendChild(d);
	var body = document.createElement('DIV');
	loginContainer.appendChild(body);
	body.className = 'bd';
	var f = document.createElement('FORM');
	body.appendChild(f);
	var t = document.createElement('TABLE');
	f.setAttribute('name','dlgForm');
	f.setAttribute('method','POST');
	f.setAttribute('action','login/login.post.php');
	f.appendChild(t);
	var r = t.insertRow(-1);
	var cLabel = r.insertCell(-1);
	cLabel.innerHTML = "<label for=\"userName\">User Name:</label>";
	var cUserName = r.insertCell(-1);
	cUserName.innerHTML = "<input type=\"text\" name=\"u\"/>";
	var r2 = t.insertRow(-1);
	var cPLabel = r2.insertCell(-1);
	cPLabel.innerHTML = "<label for=\"passWord\">Password:</label>";
	var cPInput = r2.insertCell(-1);
	cPInput.innerHTML = "<input type=\"password\" name=\"p\" />";
	//c = r.insertCell(-1);
	//c.innerHTML = "input type=\"text\" name=\"u\"/>"
	document.body.appendChild(loginContainer);
}

function setMenu() {
	if (userObj.auth) {
		var sie = document.getElementById('signin');
		sie.innerHTML = 'Sign Out';
	}
}
