Event.observe(window, 'load', ct_load, false);

function ct_load() {
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','ct-overlay');
	objOverlay.style.display = 'none';

	objOverlay.onclick = function() { ct_close(); }
	document.body.appendChild(objOverlay);

	var objLoginbox = document.createElement("div");
	objLoginbox.setAttribute('id','login-box');
	objLoginbox.style.display = 'none';
	objLoginbox.onkeypress = function(e) {	// close Lightbox is user clicks shadow overlay
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}

		if((keycode == escapeKey)){	// close lightbox
			ct_close();
		}
	}
	objLoginbox.onclick = function(e) {	// close Lightbox is user clicks shadow overlay
		if (!e) var e = window.event;
		var clickObj = Event.element(e).id;
		if ( clickObj == 'login-box') {ct_close();}
	};
//	objLoginbox.innerHTML = '<input id="login" type="text" /><br /><input type="password" /><br /><input type="button" value="&raquo;" onclick="ct_login()" />';
	var t = '<div class="side-centre" style="position: relative; width: 369px; height: 100%; margin: 0 auto;"><div class="side-top"><'+'/div>';
	t += '<div style="padding:15px 10px" >';
t += '<h3 style="margin:10px">Authorization</h3>';
	t += '<input class="login" type="text" name="log" id="log"/>';
	t += '<input class="pass" type="password" value="" name="pwd" id="pwd"/>';
	t += '<input class="btn" type="submit" value="&raquo; &raquo;" name="submit" onclick="ct_login()"/>';
	t += '<'+'/div>';
    t += '<div class="side-bott"><'+'/div><'+'/div>';
	objLoginbox.innerHTML = t;

	document.body.appendChild(objLoginbox);
}

function ct_login() {
	var mysack = new sack(templDir+'/login.js.php');

	mysack.execute = 1;
	mysack.method = 'POST';
	mysack.setVar( "login", $('log').value);
	mysack.setVar( "pass", $('pwd').value);
	mysack.onError = function() { alert('AJAX error in voting' )};
	mysack.runAJAX();
}

function ct_close() {
	Element.hide('login-box');
	var overlayDuration = 0.2;	// shadow fade in/out duration
	new Effect.Fade('ct-overlay', { duration: overlayDuration});
}

function ct_open() {
	var overlayOpacity = 0.8;	// controls transparency of shadow overlay
	var overlayDuration = 0.2;	// shadow fade in/out duration

	var arrayPageSize = getPageSize();
	$('ct-overlay').style.width = arrayPageSize[0] +"px";
	$('ct-overlay').style.height = arrayPageSize[1] +"px";
	new Effect.Appear('ct-overlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });

	var arrayPageScroll = getPageScroll();
	$('login-box').style.top = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	$('login-box').style.left = arrayPageScroll[0];

	Element.show('login-box');
	$('log').focus();
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
}
