var menu = new function() {
	this.sections = [];
	this.index = -1;
	this.padding_right = 0;
	this.hover_animate_time = 500;
	this.click_animate_time = 500;
	this.link_current_color = '#c3c7cb';
	this.link_normal_color = 'White';

	this.setup = function(current_id, total, slug) {
		for(var i=1; i<=total; i++) {
			this.sections.push({'jquery' : jQuery("#menu_s" + i), 'status' : 'closed'});
		}
			
		this.index = current_id - 1;
		this.setup_sections();
		
		if (slug == 'home') {
			this.sections[0].status = 'open';
			this.sections[0].slider_left.stop().animate({'right' : this.sections[0].position_open}, this.click_animate_time);
			this.index = 0;
			this.sections[0].button_left.stop().animate({'opacity' : 1}, this.hover_animate_time);
			this.sections[0].slider_right.stop().animate({'opacity' : 1}, this.hover_animate_time);
		}
	};

	this.action = function(action, index) {
		if (action == 'click') {
			if (this.sections[index].status == 'closed') {
				this.sections[index].status = 'open';
				this.sections[index].slider_left.stop().animate({'right' : this.sections[index].position_open}, this.click_animate_time);
				
				if (this.index != index && this.index > -1) {
					this.action('click', this.index);
				}
				this.action('hover_over', index);
				this.index = index;
			} else {
				this.sections[index].status = 'closed';
				this.sections[index].slider_left.stop().animate({'right' : this.sections[index].position_closed}, this.click_animate_time);
				if (this.index == index) {
					this.index = -1;
					this.action('hover_out', index);
				}
			}
			return false;
		} else if (action == 'hover_over') {
			this.sections[index].button_left.stop().animate({'opacity' : 1}, this.hover_animate_time);
			this.sections[index].slider_right.stop().animate({'opacity' : 1}, this.hover_animate_time);
			this.sections[index].section_link.css({'color' : this.link_current_color});
		} else if (action == 'hover_out') {
			if (this.index != index) {
				this.sections[index].slider_right.stop().animate({'opacity' : 0}, this.hover_animate_time);
				this.sections[index].button_left.stop().animate({'opacity' : 0}, this.hover_animate_time);
				this.sections[index].section_link.css({'color' : this.link_normal_color});
			}
		}
	}
	
	this.setup_sections = function() {	
		if (this.index > -1) {
			this.sections[this.index].jquery.attr('class','section');
		}

		for(var i=0; i<this.sections.length; i++) {
			var section_object = this.sections[i].jquery;

			var list = section_object.find('ul');
			list.css({'display': 'inline'});
			
			section_object.children('div.section_sub').before('<div class="section_left"><span></span></div>');
			section_object.children('div.section_sub').after('<div class="section_right"></div>');

			this.sections[i].slider_right = section_object.find('div.section_right');
			this.sections[i].slider_right.css({'opacity' : 0});
			
			var label = section_object.find('div.section_main');
			this.sections[i].position_open = label.width() + this.padding_right + list.width();
			this.sections[i].position_closed = label.width() + this.padding_right;
			
			this.sections[i].slider_left = section_object.find('div.section_left');
			this.sections[i].slider_left.css({'display':'block', 'right' : this.sections[i].position_closed});
			
			this.sections[i].button_left = this.sections[i].slider_left.find('span');
			this.sections[i].button_left.css({'opacity' : 0});
			
			
			var section_link = label.find('a');
			this.sections[i].section_link = section_link;
			section_link.data('index', i);
			section_link.hover(function() {
				menu.action('hover_over', jQuery(this).data('index'));
			}, function() {
				menu.action('hover_out', jQuery(this).data('index'));					
			});
			section_link.click(function() {
				return menu.action('click', jQuery(this).data('index'));	
			});
		}
		if (this.index > -1) {
			this.action('click', this.index);
			this.action('hover_over', this.index);
		}
	};
}
