/**
* @author    JoomlaShine.com http://www.joomlashine.com
* @copyright Copyright (C) 2008 - 2009 JoomlaShine.com. All rights reserved.
* @license   Copyrighted Commercial Software
* This file may not be redistributed in whole or significant part.
*/

/* Menu */

function efmSetMenuFX(menuId, menuFX, options)
{
	switch(menuFX)
	{
		case 0:
			efmUtils.sfHover(menuId);
			break;

		case 1:
			if (typeof(MooTools) != 'undefined') {
				new MooMenu($(menuId), options)
			} else {
				efmSetMenuFX(menuId, 0, options);
			}
			break;
	}
}

function efmSetMainmenuFX(menuId, menuFX, options)
{
	if (typeof(MooTools) != 'undefined') {
		window.addEvent('domready',function() {
			efmSetMenuFX(menuId, menuFX, options);
		});
	} else {
		efmUtils.addEvent(window, 'load', function() {
			efmSetMenuFX(menuId, menuFX, options);
		});
	}
}

function efmInitSidemenu(menuClass, menuFX, options)
{
	var sidemenus, sidemenu, menuId;

	// Set ids for all side menus base on class
	sidemenus = efmUtils.getElementsByClass(document, "UL", menuClass);
	if (sidemenus == undefined) return;

	for(var i=0;i<sidemenus.length;i++){
		menuId = "base-sidemenu-" + (i+1);
		sidemenu = sidemenus[i];
		sidemenu.id = menuId;

		// Set fx
		efmSetMenuFX(menuId, menuFX, options);
	}
}

function efmInitTreemenu(menuClass)
{
	var treemenus, treemenu, menuId;

	// Set ids for all side menus base on class
	treemenus = efmUtils.getElementsByClass(document, "UL", menuClass);
	if (treemenus == undefined) return;

	for(var i=0;i<treemenus.length;i++){
		menuId = "base-treemenu-" + (i+1);
		treemenu = treemenus[i];
		treemenu.id = menuId;

		// Set fx
		efmSetMenuFX(menuId, 0, {});
	}
}

function efmSetSidemenuFX(menuClass, menuFX, options)
{
	if (typeof(MooTools) != 'undefined') {
		window.addEvent('domready',function() {
			efmInitSidemenu(menuClass, menuFX, options);
		});
	} else {
		efmUtils.addEvent(window, 'load', function() {
			efmInitSidemenu(menuClass, menuFX, options);
		});
	}
}

function efmSetSidemenuLayout(menuClass)
{
	var sidemenus, sidemenu, smChildren, smChild, smSubmenu;
	sidemenus = efmUtils.getElementsByClass(document, "UL", menuClass);
	if (sidemenus != undefined) {
		for(var i=0;i<sidemenus.length;i++){
			sidemenu = sidemenus[i];
			smChildren = efmUtils.getChildren(sidemenu, "LI");
			if (smChildren != undefined) {
				for(var j=0;j<smChildren.length;j++){
					smChild = smChildren[j];
					smSubmenu = efmUtils.getFirstChild(smChild, "UL");
					if (smSubmenu != null) {
						if(enableRTL == true) { smSubmenu.style.marginRight = smChild.offsetWidth+"px"; }
						else { smSubmenu.style.marginLeft = smChild.offsetWidth+"px"; }
					}
				}
			}
		}
	}
}

function efmSetSitetoolsLayout(sitetoolsId, neighbourId)
{
	var sitetoolsContainer, parentItem, sitetoolsPanel, neighbour;
	sitetoolsContainer = document.getElementById(sitetoolsId);
	if (sitetoolsContainer != undefined) {
		parentItem = efmUtils.getFirstChild(sitetoolsContainer, "LI");
		sitetoolsPanel = efmUtils.getFirstChild(parentItem, "UL");
		if (enableRTL == true) {
			sitetoolsPanel.style.marginRight = -1*(sitetoolsPanel.offsetWidth - parentItem.offsetWidth) + "px";
		} else {
			sitetoolsPanel.style.marginLeft = -1*(sitetoolsPanel.offsetWidth - parentItem.offsetWidth) + "px";
		}

		neighbour = document.getElementById(neighbourId);
		if(neighbour != undefined) {
			neighbour.style.right = (parentItem.offsetWidth + 10) + "px";
		}
	}
}

function efmSetSmoothScroll()
{
	// Setup smooth go to top link
	if (typeof(MooTools) != 'undefined') {
		window.addEvent('domready',function() {
			new SmoothScroll({ duration:360 }, window);
		});
	}
}

function efmInitTemplate()
{
	if (enableGotopLink) {
		efmUtils.setLinkNavPrefix('efm-gotoplink', '#top');
	}
	
	efmUtils.createGridLayout("DIV", "grid-layout", "grid-col", "grid-lastcol");

	efmUtils.setInnerLayout(["efm-content_inner3", "efm-leftsidecontent", "efm-rightsidecontent", "efm-pinnerleft", "efm-pinnerright"]);
	
	efmUtils.createExtList("list-number-", "strong", "bulletcount", true);
	efmUtils.createExtList("list-icon", "span", "", false);
	
	efmSetSidemenuLayout("menu-sidemenu");
	efmSetSitetoolsLayout("efm-sitetools-menu", "efm-ptoolbar");
	efmInitTreemenu("menu-treemenu");

	efmUtils.setStickPosition("efm-pstickleft", lspAlignment);
	efmUtils.setStickPosition("efm-pstickright", rspAlignment);
}

function efmInitTemplateNow()
{
	// Setup main menu fx
	efmSetMainmenuFX("base-mainmenu", (enableMMFX?1:0), { duration:250, delay:500, orientation: 'h' });
	efmSetMainmenuFX("efm-sitetools-menu", (enableMMFX?1:0), { duration:250, delay:500, orientation: 'h' });

	// Setup side menu fx
	efmSetSidemenuFX("menu-sidemenu", (enableSMFX?1:0), { duration:250, delay:500, orientation: 'v' });

	if (enableGotopLink) {
		efmSetSmoothScroll();
	}
}

// Call right away
efmInitTemplateNow();

// Call on document load
efmUtils.addEvent(window, 'load', efmInitTemplate);
