/*

Written by Ali Çevik for Bilkent University	Online Courses (MOODLE)

a_cevik@ug.bilkent.edu.tr

07.02.08

http://www.alicevik.net

http://www.bilkent.edu.tr

*/

function Lightbox()
{
  	
	// property fields


	this.menuClass = "white_content";


	// menu context -- its dynamic content
	this.context = null;				
	
	// content div (needed to show menu)
	this.contentDiv = null;
	
	// fader div that will make window fade, when menu is opened
	this.faderdiv = null;

	this.closingCallback = null;

	this.isHovered = false;

	// accessor and mutator methods

	this.setContext = function(_context)
	{
		this.context = _context;
		if (this.contentDiv != null)
			this.contentDiv.innerHTML = this.context;
	}

	this.getContext = function()
	{
		return this.context;
	}
	
	this.setClosingCallback = function(callback)
	{
		this.closingCallback = callback;
	}

	this.getPageSize = function()
	{
		 var viewportwidth;
		 var viewportheight;
		 
		 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		 
		 if (typeof window.innerWidth != 'undefined')
		 {
			  viewportwidth = document.body.offsetWidth + document.body.scrollLeft;
			  
			  var h = document.body.offsetHeight;
			  if(h == 0)
				  h = document.documentElement.clientHeight;
			  
			  viewportheight = h + document.body.scrollTop;
			  
		 }
		 
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

		 else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0)
		 {
			   viewportwidth = document.documentElement.clientWidth + document.documentElement.scrollLeft;
			   viewportheight = document.documentElement.clientHeight +  document.documentElement.scrollTop;
		 }
		 
		 // older versions of IE
		 
		 else
		 {
			   viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
			   viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		 }



		var size = new Object;

		size.width = viewportwidth;
		size.height = viewportheight;

		return size;
	}

	// generate the menu
	this.display = function()
	{
		// create content div
		this.contentDiv = Lightbox.createDiv("menucontent",this.menuClass);
		
		// create fader div
		this.faderDiv = Lightbox.createDiv("menufader","black_overlay");
		
		this.faderDiv.style.height = this.getPageSize().height + "px";
		this.faderDiv.style.width = this.getPageSize().width + 15 + "px";

		var instance = this;
		this.faderDiv.onclick = function() { if(instance.closingCallback != null) { instance.closingCallback(); } Lightbox.removeDiv("menucontent"); Lightbox.removeDiv("menufader");};

		// set context
		this.contentDiv.innerHTML += this.getContext();
		
		this.faderDiv.focus();
		

	}

	// static method to create a div and append it to page
	Lightbox.createDiv = function(id,className)
	{
		var div = document.getElementById(id);
		if (div == null)
		{
			div = document.createElement("div");

			div.setAttribute("id",id);
			
			div.className = className;

			div.style.display = "block";
			
			document.body.appendChild(div);
		}
		else
		{
			Lightbox.removeDiv(id);
			div = null;
			div = Lightbox.createDiv(id,className);
		}

		return div;
	}

	// static method to remove a div from page
	Lightbox.removeDiv = function(id)
	{
		var div = document.getElementById(id);

		if(div == null || div.style.display == "none")
			return false;
		div.style.display = "none";

		document.body.removeChild(div);

		return true;
	}
	// static method, called to close the menu
	Lightbox.closeAny = function()
	{
		if(Lightbox.removeDiv("menucontent") && Lightbox.removeDiv("menufader"))
			Lightbox.lastClosingDate = new Date();
	}

	
	Lightbox.lastClosingDate = null;
}

