/*
	Add the bind_scope to the function prototype since jQuery doesn't have what it takes!
*/
Function.prototype.bind_scope = function(o_object)
{
	var method = this;
	var temp = function() 
	{
		return method.apply(o_object, arguments);
	};

	return temp; 
}

/*
	Create our onepage module
*/
function onepage (o_mainMenuItem, s_onepageName)
{
	/*
		Define our objects and other settings/globals
	*/
	this.b_displayed = false;
	this.b_hide = true;
	this.s_onepageName = s_onepageName;
	this.o_wrapper = $(document.createElement('div'));
	this.o_wrapper.attr('id', this.s_onepageName);
	$('#onepage_wrapper').append(this.o_wrapper);	
	this.o_mainMenuItem = o_mainMenuItem;
	this.o_hideTimer = false;
	this.o_menuList = false;
	this.o_contentList = false;
	
	/*
		Define debug object, counter and boolean to turn it on/off
	*/
	this.b_debug = true;
	this.o_debug = $('#content');
	this.i_debug = 0;

	/*
		Debug function, if you don't  want debugging, use the global to turn it off!
	*/
	this.debug = function (s_debug)
	{
		if(this.b_debug)
		{
			this.o_debug.html('['+this.i_debug+']' + s_debug + '<br />' + this.o_debug.html());
			this.i_debug ++;
		}
	};
	
	/*
		Open the menu (or keep it open if it's already open)
	*/
	this.menu_display = function ()
	{
		// Cancel the hiding process
		this.b_hide = false;
		this._stop_timer();
		
		// Only continue if we're curently closed
		if(!this.b_displayed)
		{
			this.b_displayed = true;
			this.o_wrapper.css('display', 'block');
			
			// Open the first item		
			this.item_display($(this.o_menuList).children(":first-child"));
		}
	};
	
	/*
		Start closing the menu
	*/
	this.menu_hide = function ()
	{
		// if the timer is not already running, run it!
		if(this.o_hideTimer == false)
		{
			this.b_hide = true;
			// Start a timer which will actually close the menu if not canceled before then
			this._start_timer();
			return;
		}
	};
	
	/*
		When the timer ends we check if the menu_display hasn't been used in the mean time (b_hide is set to false in menu_display)
		Close the menu if we are allowed
	*/
	this._menu_hide = function ()
	{
		this._stop_timer();
		
		if(this.b_hide)
		{
			this.b_displayed = false;
			this.b_hide = false;
			this.o_wrapper.css('display', 'none');
		}
	};
	
	this._stop_timer = function ()
	{
		clearTimeout(this.o_hideTimer);
		this.o_hideTimer = false;	
	};
	
	this._start_timer = function ()
	{
		this._stop_timer();
		this.o_hideTimer = setTimeout(this._menu_hide.bind_scope(this), 200);	
	}
	
	/*
		Display one item and close the others
	*/	
	this.item_display = function (o_menuItem)
	{
		// Create the contentId we're looking for from the id of the menuItem
		s_displayId = $(o_menuItem).attr('id').replace('menu', 'content');
		
		// Loop through the items
		$.each(this.o_contentList.children('li'), function(i_key,o_contentItem) 
		{
			// Check if they're the item we're looking for
			if($(o_contentItem).attr('id') != s_displayId)
			{
				// If they're not we hide them and unselected their menu item
				$(o_contentItem).css('display', 'none');
				$(o_contentItem).removeClass('selected');	
				s_menuId = $(o_contentItem).attr('id').replace('content', 'menu');
				$('#'+s_menuId).removeClass('selected');			
			}
			else
			{
				// If they are we show them and select their menu item
				$(o_contentItem).css('display', 'block');	
				$(o_contentItem).addClass('selected');	
				s_menuId = $(o_contentItem).attr('id').replace('content', 'menu');
				$('#'+s_menuId).addClass('selected');	
			}
		}.bind_scope(this));	
	};
	
	// Assign the mouseover/out for the wrapper div
	this.o_wrapper.bind('mouseover', function()
	{
		this.menu_display();
	}.bind_scope(this));
	
	this.o_wrapper.bind('mouseout', function()
	{
		this.menu_hide();
	}.bind_scope(this));

	/*
		Bind our onload event to window
	*/
	$(window).bind('load',function()
	{
		// Assign the mouseover/out for the menu item
		this.o_mainMenuItem.bind('mouseover', function()
		{
			this.menu_display();
		}.bind_scope(this));	
		this.o_mainMenuItem.bind('mouseout', function()
		{
			this.menu_hide();
		}.bind_scope(this));	
		
		// Load our data with AJAX	
		this.o_wrapper.load('/'+this.s_onepageName+'/?ajax=1', function()
		{
			// Set our objects in the globals
			this.o_menuList = $('#'+this.s_onepageName+' .onepage_menulist');
			this.o_contentList = $('#'+this.s_onepageName+' .onepage_contentlist');

			// Loop over the content to hide them
			$.each(this.o_contentList.children('li'), function(i_key,o_contentItem) 
			{
				$(o_contentItem).css('display', 'none');
			}.bind_scope(this));
			
			// Loop over the menu items to assign their mouseover event			
			$.each(this.o_menuList.children('li'), function(i_key,o_menuItem) 
			{
				o_menuItem.o_onepage = this;
				$(o_menuItem).bind('mouseover', function()
				{
					this.o_onepage.item_display(this);
				});
				$(o_menuItem).bind('mouseout', function()
				{
					this.o_onepage.menu_hide();
				});
			}.bind_scope(this));			
		}.bind_scope(this));
	}.bind_scope(this));
}

/*
onepage_projects = new onepage($('.menu-path-projectgroups'), 'onepage_projectgroups');
onepage_about = new onepage($('.menu-path-node-86'), 'onepage_about');
onepage_about = new onepage($('.menu-path-node-248'), 'onepage_courses');
onepage_about = new onepage($('.menu-path-blogs'), 'onepage_blogs');
*/

onepage_projects = new onepage($('.menu-path-projectgroups'), 'onepage_projectgroups');
onepage_about = new onepage($('.menu-path-node-86'), 'onepage_about');
onepage_courses = new onepage($('.menu-path-node-4'), 'onepage_courses');
onepage_blogs = new onepage($('.menu-path-blogs'), 'onepage_blogs');

