/****  MENU ******/

var under_menu_box = new Class({
	Implements: [Options],
	/* standart egenskap */
	options: {
		under_menu						: null,
		frame_id						: 'undermeny_frame',
		undermenu_col_1_id				: 'mo_undermenu_col_1',
		undermenu_col_2_id				: 'mo_undermenu_col_2',
		undermenu_col_3_id				: 'mo_undermenu_col_3',
		undermenu_col_4_id				: 'mo_undermenu_col_4',
		main_page_name_id				: 'main_page_name',
		main_page_link_id				: 'main_page_link',
		dm_sub_page_id 					: 'dummy_sub_page',
		dm_sub_sub_page_id				: 'dummy_sub_sub_page',
		dm_sub_page_spacer_id			: 'dummy_sub_page_spacer',
		undermenu_news_cont_id			: 'undermenu_news_cont',
		undermenu_news_id				: 'undermenu_news',		
		box_visible						: false,
		box_hide_delay					: -1,
		box_hide_show_delay				: -1		
	},  	
	frame_DOM						: null,
	frame_tween						: null,
	frame_top_arrow_DOM				: null,
	
	under_menu_col					: new Array(),
	current_col						: 0,
	main_page_name					: null,
	main_page_link 					: null,
	dummy_sub_page 					: null,
	dummy_sub_sub_page  			: null,
	dummy_sub_page_spacer  			: null,
	under_menu_news_cont  			: null,
	under_menu_news  				: null,
	under_menu_news_img 			: null,
	under_menu_news_name 			: null,
	under_menu_news_ingress 		: null,
	
	
	current_meny_ind 				: null,	
	
	/* math info 8 main pages, 16 small, 0.5:2 */
	/* -- constructor -- */
	initialize: function($options){
		this.setOptions($options);				
		for (menu_id in this.options.menu_1l){			
			// 'elem_id'=> 'search_field', 'under_box_id'=>'search_under_box', 'event'=>'blur'
			/*this.options.under_menu[i].elem_DOM = $(this.options.under_menu[i].elem_id);
			this.options.under_menu[i].under_box_DOM = $(this.options.box_ids[i].under_box_id);
			this.options.box_ids[i].under_box_DOM.setStyles({'display': 'block', 'visibility':'hidden'});
			this.options.box_ids[i].under_box_size = this.options.box_ids[i].under_box_DOM.getSize();			
			this.options.box_ids[i].under_box_DOM.setStyles({'width':this.options.box_ids[i].under_box_size.x, 'height':this.options.box_ids[i].under_box_size.y});
			this.options.box_ids[i].under_box_DOM.setStyles({'display': 'none', 'visibility':'visible'});		
			this.options.box_ids[i].position_limits = true;			*/					
			this.options.menu_1l[menu_id].elem_DOM = $('a_main_'+menu_id);						
			this.options.menu_1l[menu_id].elem_DOM.addEvent('mouseover', this.show_box_for_delay.bind(this, [menu_id]));						
			this.options.menu_1l[menu_id].elem_DOM.addEvent('mouseout', this.set_delay_hide_box_for.bind(this, [menu_id]));									
		}			
		this.frame_DOM = $(this.options.frame_id);				
		$(document.body).grab(this.frame_DOM);
		this.frame_DOM.setStyles({'visibility': 'hidden', 'display':'none'});		
		this.frame_top_arrow_DOM = $('undermeny_frame_top_pil');
		
		this.under_menu_col.push({'dom':$(this.options.undermenu_col_1_id), 'amount':0});
		this.under_menu_col.push({'dom':$(this.options.undermenu_col_2_id), 'amount':0});
		this.under_menu_col.push({'dom':$(this.options.undermenu_col_3_id), 'amount':0});
		this.under_menu_col.push({'dom':$(this.options.undermenu_col_4_id), 'amount':0});
		
		this.main_page_name = $(this.options.main_page_name_id);
		this.main_page_link = $(this.options.main_page_link_id);
		this.dummy_sub_page = $(this.options.dm_sub_page_id);
		this.dummy_sub_sub_page = $(this.options.dm_sub_sub_page_id);
		this.dummy_sub_page_spacer = $(this.options.dm_sub_page_spacer_id);
		this.under_menu_news_cont = $(this.options.undermenu_news_cont_id);
		this.under_menu_news = $(this.options.undermenu_news_id);			
		this.under_menu_news_img = this.under_menu_news.getElement('.image img');
		this.under_menu_news_name = this.under_menu_news.getElement('.name');
		this.under_menu_news_ingress = this.under_menu_news.getElement('.ingress');
		
		if ($('content-front') != null){
			var box_pos = $('content-front').getPosition();
			this.frame_DOM.setStyles({'left':box_pos.x, 'top':box_pos.y - 2});
		} else {
			var box_pos = $('content').getPosition();
			this.frame_DOM.setStyles({'left':box_pos.x, 'top':box_pos.y - 2});
		}
		
		//this.frame_top_arrow_DOM = this.frame_DOM.getElement('#under_meny_frame_arrow');
		//this.options.top_offset = this.h_limits.getPosition().y + this.h_limits.getSize().y + 0;		
		this.frame_DOM.addEvent('mouseover', this.reset_delay.bind(this));
		this.frame_DOM.addEvent('mousemove', this.reset_delay.bind(this));
		this.frame_DOM.addEvent('click', this.reset_delay.bind(this));
		this.frame_DOM.addEvent('mouseout', this.set_delay_hide_box_for.bind(this));								
		
		
		this.frame_tween = new Fx.Morph(this.frame_top_arrow_DOM, {duration: 'short', transition:'quart:out'});			
	},
	show_box_for_delay: function(ind){
		this.options.box_hide_show_delay = this.show_box_for.delay(250, this, ind);
	},
	fill_menu_box_with:function(ind){		
		for (var i = 0; i < this.under_menu_col.length; i ++) {
			this.under_menu_col[i].dom.empty();
			this.under_menu_col[i].amount = 0;
		}		
		if (this.options.news[ind] != undefined){
			this.under_menu_news_cont.setStyle('display', 'block');			
			if (this.options.news[ind].pic != '') {
				this.under_menu_news_img.setStyle('display', 'block');
				this.under_menu_news_img.setProperty('src', this.options.news[ind].pic);
			} else this.under_menu_news_img.setStyle('display', 'none');								
			this.under_menu_news_name.set('html', this.options.news[ind].name);
			this.under_menu_news_ingress.set('html', this.options.news[ind].ingress);
			this.under_menu_news_cont.removeEvents('click');
			this.under_menu_news_cont.addEvent('click', function(){
				//alert(this.page_id);
				location.href = EDIT_CORE.JSSiteRoot + 'index.php?page_id='+this.page_id;
			}.bind({'page_id':this.options.news[ind].id}));
		} else this.under_menu_news_cont.setStyle('display', 'none');
		this.main_page_name.set('html', this.options.menu_1l[ind].name+':');
		this.main_page_link.setProperty('href', this.options.menu_1l[ind].link);
		this.main_page_link.setProperty('target', this.options.menu_1l[ind].target);	
		this.current_col = 0;				
		for (var i = 0; i < this.options.menu_1l[ind]['count']; i++){			
			var menu_info = this.options.menu_1l[ind]['menu_2l'][i];
			/* sub pages */
			var tmp_menu = this.dummy_sub_page.clone().setStyle('display', 'block');			
			tmp_menu.inject(this.under_menu_col[this.current_col].dom);			
			this.under_menu_col[this.current_col].amount += 1.2;
			
			if (this.under_menu_col[this.current_col].amount >= 16 && this.current_col < this.under_menu_col.length - 1) this.current_col ++;
			var main_page_name = tmp_menu.getElement('.name').empty();
			if (menu_info.link != ''){
				var tmp_link = new Element('a', {'html':menu_info.text, href:menu_info.link, target:menu_info.target});
				tmp_link.inject(main_page_name);
			} else main_page_name.set('html', menu_info.text);
			if (menu_info.selection == 'true') main_page_name.addClass('selected');			
			/* sub sub pages */						
			for (var j = 0; j < menu_info.menu_3l.length; j ++){				
				var tmp_sub_menu = this.dummy_sub_sub_page.clone().setStyle('display', 'block').empty();
				var tmp_sub_menu_info = menu_info.menu_3l[j];
				tmp_sub_menu.inject(this.under_menu_col[this.current_col].dom);
				this.under_menu_col[this.current_col].amount += 0.9;				
				if (this.under_menu_col[this.current_col].amount >= 16 && this.current_col < this.under_menu_col.length - 1) this.current_col ++;
				//var menu_text_elem = tmp_sub_menu.getElement('.sub_sub_page');							
				if (tmp_sub_menu_info.selection == 'true') tmp_sub_menu.addClass('selected');											
				if (tmp_sub_menu_info.link != ''){
					var tmp_link = new Element('a', {'html':tmp_sub_menu_info.text, 'href':tmp_sub_menu_info.link, 'target':tmp_sub_menu_info.target});
					tmp_link.inject(tmp_sub_menu);
				} else tmp_sub_menu.set('html', tmp_sub_menu_info.text);								
			}							
			this.dummy_sub_page_spacer.clone().setStyle('display', 'block').inject(this.under_menu_col[this.current_col].dom);
			this.under_menu_col[this.current_col].amount += 1.2;								
			if (this.under_menu_col[this.current_col].amount >= 16 && this.current_col < this.under_menu_col.length - 1) this.current_col ++;			
			
		}		
		//console.log(this.options.under_menu[ind]['menu'].length + ' ' + ['count']);
	},
	show_box_for: function(ind){				
		this.options.box_hide_show_delay = -1;
		this.reset_delay(ind);
		this.current_meny_ind = ind;
		var target_elem = this.options.menu_1l[ind].elem_DOM;
		var target_pos = target_elem.getPosition();
		var target_size = target_elem.getSize();						
		this.fill_menu_box_with(ind);
		this.frame_DOM.setStyles({'display': 'block'});
		var frame_box_pos = this.frame_DOM.getPosition();
		//var content_size = this.frame_content_DOM.getSize();		
		var arrow_pos = {
			x:target_pos.x - (frame_box_pos.x) + (target_size.x / 2) - 11,
			y:this.options.top_offset
		};										
		if (this.options.box_visible){			
			this.frame_DOM.setStyle('display','block');
			this.frame_tween.start({'background-position': arrow_pos.x+'px 0px'});
		} else {						
			this.frame_top_arrow_DOM.setStyle('background-position', arrow_pos.x+'px 0px');			
			this.frame_DOM.setStyle('display','block');			
			this.frame_DOM.setStyle('visibility', 'visible');			
			this.frame_tween.cancel();
			
			this.options.box_visible = true;
		}				
	}, 
	set_delay_hide_box_for: function(ind){
		if (this.options.box_hide_show_delay != -1) {
			$clear(this.options.box_hide_show_delay);
			this.options.box_hide_show_delay = -1;
		} else this.options.box_hide_delay = this.hide_box_for.delay(750, this);
	},
	hide_box_for: function(){		
		//return;
		//return ;
		//this.frame_DOM.setStyle('display', 'none');
		//this.frame_tween.start({'opacity': 0});
		this.frame_DOM.setStyles({'display':'none'});
		/*var child = this.frame_content_DOM.getFirst();
		if (child) {
			child.setStyle('display', 'none');
			$(document.body).grab(child);
		}*/
		this.options.box_hide_delay = -1;
		this.options.box_visible = false;
		this.current_meny_ind = -1;
	},
	reset_delay: function(ind){								
		$clear(this.options.box_hide_delay);
		this.options.box_hide_delay = -1;
	},	
	reset_delay_2 : function(ind){
		if (this.current_meny_ind != ind) return;
		$clear(this.options.box_hide_delay);
		this.options.box_hide_delay = -1;
	}
});