/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Varien
 * @package     js
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * @classDescription simple Navigation with replacing old handlers
 * @param {String} id id of ul element with navigation lists
 * @param {Object} settings object with settings
 */

var leftNav = function() {
	var nav = $$('.level1')[0];
	nav.id = "mainnav";

	var nav_childs_lev2 = $$('#' + nav.id + ' li.level2-item');
	
	for(var x=0;x<nav_childs_lev2.length;x++){
		nav_childs_lev2[x].id = "nav-lev2-" + x; 	
		nav_sublist_lev3 = $$('#' + nav_childs_lev2[x].id + ' ul')[0];
		if (nav_sublist_lev3){
			//has childrens?
			nav_childs_lev2[x].className = nav_childs_lev2[x].className + " mother";
			
			nav_sublist_lev3.id = "sub-lev3-"+x;
			height = nav_sublist_lev3.offsetHeight;
			nav_sublist_lev3.style.height = height + "px";
			if(!nav_childs_lev2[x].hasClassName('active')){
				nav_sublist_lev3.style.display = "none";
				//onclick-event
				nav_child_link = $$('#' + nav_childs_lev2[x].id + ' a')[0];
				nav_child_link.onclick = function(){
					var level = this.parentNode.parentNode.className;
						
					showSubnav(level,this);
					return false;
				}
			}
		}		
	}
	
	
	var nav_childs = $$('#' + nav.id + ' li.level1-item');
	
	for(var x=0;x<nav_childs.length;x++){
		nav_childs[x].id = "nav-top-" + x; 
		nav_sublist = $$('#' + nav_childs[x].id + ' ul')[0];
		if (nav_sublist){
			//has childrens?
			nav_childs[x].className = nav_childs[x].className + " mother";
			
			nav_sublist.id = "sub"+x;
			height = nav_sublist.offsetHeight;
			nav_sublist.style.height = height + "px";
			if(!nav_childs[x].hasClassName('active')){
				nav_sublist.style.display = "none";
				//onclick-event
				nav_child_link = $$('#' + nav_childs[x].id + ' a')[0];
				nav_child_link.onclick = function(){
					var level = this.parentNode.parentNode.className;
						
					showSubnav(level,this);
					return false;
				}
			}
		}		
	}
	
};

var showSubnav = function(level,el){
	var level_sub = parseInt(level.replace(/level/g, ""));	
	level_sub = level_sub + 1;
	//nav_sub = $$('.level' + level_sub);
	nav_sub = $$('#' + el.parentNode.id + ' ul')[0];
	var subs_to_hide = $$('.level' + level_sub);
	
	if (level=="level2"){
		var nav_top = $(el.parentNode.parentNode.id);		
		temp_height = parseInt(nav_top.style.height);
		nav_top.style.height = "auto";
	}
	
	//hide all sub-navs
	for(var x=0;x<subs_to_hide.length;x++){
		if(!subs_to_hide[x].parentNode.hasClassName('active') && subs_to_hide[x].id!=nav_sub.id){

			new Effect.BlindUp(subs_to_hide[x].id, {
				  duration: 0.8,
				  afterFinish: function () {
						//$(subs_to_hide[x].id).style.display = "none";
				  }
			});
			
			new_class = subs_to_hide[x].parentNode.className;
			new_class = new_class.replace(/temp_active/g, "");
			subs_to_hide[x].parentNode.className = new_class;	
			el_link = $$('#' + subs_to_hide[x].parentNode.id + ' a')[0];
			el_link.className = el_link.className.replace(/temp_current/g, "");
		}
	}	
	
	var act_nav_child = el.parentNode;
	act_nav_child = $$('#' + act_nav_child.id + ' .level' + level_sub)[0];
	if (act_nav_child.style.display == "block"){
		//act_nav_child.style.display = "none";		
		if (level=="level2"){
			new_height = temp_height - parseInt($(act_nav_child.id).style.height);
				new Effect.BlindUp(act_nav_child.id, {
				  duration: 0.8,
				  afterFinish: function () {
						$(act_nav_child.id).style.display = "none";
						//nav_top.style.height = new_height + "px";
						new_class = el.parentNode.className;
						new_class = new_class.replace(/temp_active/g, "");
						el.parentNode.className = new_class;	
				  }
			});			
			
		} else {
			
			new Effect.BlindUp(act_nav_child.id, {
				  duration: 0.8,
				  afterFinish: function () {
						$(act_nav_child.id).style.display = "none";
				  }
			});
			
		}
		el.className = "";
	} else {
		//act_nav_child.style.display = "block";		
		if (level=="level2"){		
			new_height = temp_height + parseInt($(act_nav_child.id).style.height);
			el.parentNode.className = el.parentNode.className + " temp_active";
			new Effect.BlindDown(act_nav_child.id, {
				  duration: 0.8,
				  afterFinish: function () {
						$(act_nav_child.id).style.display = "block";
						//nav_top.style.height = new_height + "px";					
				  }			  
			});
			
		} else {
		
			new Effect.BlindDown(act_nav_child.id, {
				  duration: 0.8,
				  afterFinish: function () {
						$(act_nav_child.id).style.display = "block";
				  }			  
			});			
			
		}
		
		el.className = "temp_current";
	}
}

document.observe("dom:loaded", function() {
	leftNav();
});

