function getRefreshArgs()
{
	return { closeCallback: AdminWindows.reloadWhenOk };
}

function newTemplateSnippet()
{
	AdminWindows.openNewTemplateSnippet({ closeCallback: AdminWindows.reloadAlways });
}

function newPageTemplate()
{
	AdminWindows.openNewPageTemplate({ closeCallback: AdminWindows.reloadAlways });
}

function newEntry()
{
	AdminWindows.openNewEntry({ closeCallback: AdminWindows.reloadWhenOk });
}

function newCategory()
{
	AdminWindows.openNewCategory(getRefreshArgs());
}

function newInvitation()
{
	AdminWindows.openNewInvitation(getRefreshArgs());
}

function newBlog()
{
	GlobalWindows.openNewBlog();
}

var SlidingMenu = Class.create();
SlidingMenu._nop = function()
	{
		return false;
	};

SlidingMenu.prototype = 
{
	_lastDiv: false,
	_currentDiv: false,
	_mniToShow: [],
	_mniToHide: [],
	_isLoaded: false,
	_initAnchor: null,
	_menuId: null,
	
	initialize: function(menuId)
	{
		this._menuId = menuId;
		
		var rule = {};
		menuId = "#" + menuId;
		
		rule[menuId + " li.main:click"] = this._li_click.bindAsEventListener(this);
		rule[menuId + " li.main > a"] = function(a) { a.onclick = SlidingMenu._nop; };
		rule[menuId + " div, .startsHidden"] = function(div) { $(div).hide(); };
		rule["window:load"] = this._window_onload.bindAsEventListener(this);

		Blossom.Behavior.add(rule);
	},
	
	_window_onload: function()
	{
		this._isLoaded = true;
		
		var menu = $(this._menuId);
		var menuId = "#" + menu.id;
		
		var liToDiv = {};
		var divToLi = {};
		
		var i = 1;
		
		$$(menuId + " li.main").each(function(li)
		{
			var div = $$("div", li)[0];
			
			li.id = "SLIDING_LI_" + i;
			div.id = "SLIDING_DIV_" + i;
			
			liToDiv[li.id] = div.id;
			divToLi[div.id] = li.id;
			
			i ++;
			
			// menu.parentNode.appendChild(div);
		});
		
		this._liToDiv = liToDiv;
		this._divToLi = divToLi;
		
		if (this._initAnchor)
			this.setActive(this._initAnchor);
		
		if (this._mniToShow.length > 0) 
		{
			this._mniToShow.each(this.showMenuItem.bind(this));
			this._mniToShow = [];
		}

		if (this._mniToHide.length > 0) 
		{
			this._mniToHide.each(this.hideMenuItem.bind(this));
			this._mniToHide = [];
		}
	},
	
	_fixup: function(e)
	{
		if (this._lastDiv && Element.visible(this._lastDiv) && this._lastDiv != e.element)
		{
			this._hideMenu(this._lastDiv);
		}
		
		this._lastDiv = e.element;
	},
	
	_getDefaultOptions: function(element, callback)
	{
		return { duration: 0.25, fps: 20, queue: {position: 'end', scope: element.id, limit: 1}, afterFinish: callback};
	},
	
	_getSubMenu: function(mainMenu)
	{
		if (typeof(mainMenu) != "String")
			mainMenu = mainMenu.id;
			
		return $(this._liToDiv[mainMenu]);
	},
	
	_getMainMenu: function(subMenu)
	{
		if (typeof(subMenu) != "String")
			subMenu = subMenu.id;
			
		return $(this._divToLi[subMenu]);
	},

	_li_click: function(e)
	{
		e = Event.findElement(e, "LI");
			
		this._showMenu(this._getSubMenu(e));
	},
	
	_hideMenu: function(subMenuDiv)
	{
		if (!subMenuDiv)
			return;

		subMenuDiv = $(subMenuDiv);

		this._getMainMenu(subMenuDiv).removeClassName("selected");
		
		subMenuDiv.hide();
	},

	_showMenu: function(subMenuDiv)
	{
		if (!subMenuDiv)
			return;

		subMenuDiv = $(subMenuDiv);

		this._getMainMenu(subMenuDiv).addClassName("selected");
		
		if (subMenuDiv != this._currentDiv)
		{
			this._currentDiv = subMenuDiv;
			
			if (this._lastDiv && this._lastDiv != subMenuDiv)
			{
				this._hideMenu(this._lastDiv);
			}
			
			if (browser.isSafari)
			{
				$(subMenuDiv).show();
				this._fixup({ element: subMenuDiv });
			}
			else
			{
				new Effect.SlideDown(subMenuDiv, this._getDefaultOptions(subMenuDiv, this._fixup.bind(this)));
			}
		}
	},

	setActive: function(href)
	{
		if (!this._isLoaded)
		{
			this._initAnchor = href;
			return;
		}
		
		href = $$("a[href=" + href + "]");
		
		if (href && href.length > 0)
		{
			href = href[0];
			
			this._showMenu(this._getSubMenu(Utils.findParent(href, "LI")));
		}
	},

	showMenuItem: function(menuItem)
	{
		if (!this._isLoaded)
		{
			this._mniToShow.push(menuItem);
			
			return;
		}
	
		menuItem = $(menuItem);
		if (!menuItem)
			return;
			
		menuItem.show();
		
/*
		window.setTimeout(function(){
			new Effect.Pulsate(menuItem,
			{
				duration: 1,	
				pulses: 1
			}
			
			
			); //, { startcolor: "#4e8cf0" });
			}, 200);
*/
	},
	
	hideMenuItem: function(menuItem)
	{
		if (!this._isLoaded)
		{
			this._mniToHide.push(menuItem);
			
			return;
		}
	
		if (menuItem = $(menuItem))
			menuItem.hide();
	}
};

if (typeof(MENU_ID) != "undefined")
	slidingMenu = new SlidingMenu(MENU_ID);
