// name:  SubMenuObject
// desc:  sliding the sub menu
// param: obj - submenu div 

var SubMenuObject = new Class ({

	initialize: function(obj){
		this.element = obj;
		this.submenuwidth = 0;
		this.submenuheight = 0;
		this.navElements = "";
		this.navHeight = 0;
		this.oldMargin = 0;
		var self = this;
	
		// get width of submenu
		//$(this.element).getElements('li').each(function(item) {self.submenuwidth += item.getStyle('width').toInt();});
		$(this.element).getElements('li').each(function(item) {if (self.submenuwidth < item.getStyle('width').toInt()) self.submenuwidth = item.getStyle('width').toInt();});
		$(this.element).getElement('ul').setStyle('width', this.submenuwidth);
		
		this.navHeight = $("nav").getStyle('height');
		this.submenuheight = $(this.element).getElement('ul').getStyle('height').toInt() + ($(this.element).getParent().getStyle('paddingTop').toInt() * 2);		
		//$(this.element).hide();
		
		// add Events
		$(this.element).getParent().addEvent('mouseover', function(){self.open();});
		$(this.element).getParent().addEvent('mouseout', function(){self.close();});
		
		// get menu elements & calculate new menu element margin
		this.navElements = $(this.element).getParent().getSiblings("li");
		this.oldMargin =  $(this.element).getParent().getStyle('margin-left').toInt();
		this.newMargin = Math.floor(this.oldMargin - (this.submenuwidth / (((this.navElements.length))*2)));
    },
	
	open: function() {
	 //alert($(this.element).getParent().getStyle('margin-left')+" "+this.newMargin);
		$(this.element).getParent().morph({'margin-left': this.newMargin, 'margin-right': this.newMargin});

		for (i=0;i<this.navElements.length;i++) {
			this.navElements[i].morph({'margin-left': this.newMargin, 'margin-right': this.newMargin});
		}
		//$(this.element).show();
		$(this.element).tween('width', this.submenuwidth);
		$("nav").tween('height', this.submenuheight);
	},
	
	close: function () {
		$("nav").tween('height', this.navHeight);
		$(this.element).tween('width', 1);
		//$(this.element).hide();
		
		$(this.element).getParent().morph({'margin-left': this.oldMargin, 'margin-right': this.oldMargin});
		
		for (i=0;i<this.navElements.length;i++) {
			this.navElements[i].morph({'margin-left': this.oldMargin, 'margin-right': this.oldMargin});
		}
	}
});
		
