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

var under_menu_box = new Class({
	Implements: [Options],
	/* standart egenskap */
	options: {
		under_menu						: null,
		frame_id						: 'under_meny_frame',
		frame_contetnt_id			    : 'under_meny_frame_content',
		limit_box_id					: 'top_menu_cont',
		dummy_a_id						: 'dummy_under_menu_item',
		menu_per_col					: 6,
		top_offset						: 0,
		box_visible						: false,
		box_hide_delay					: -1,
		box_hide_show_delay				: -1		
	},  	
	frame_DOM						: null,
	frame_tween						: null,
	frame_top_arrow_DOM				: null,
	frame_content_DOM				: null,
	dummy_link_DOM					: null,
	current_meny_ind 				: null,	
	left_menu_DOM					: null,
	left_menu_pos					: null,
	left_menu_size					: null,	
	/* -- constructor -- */
	initialize: function($options){
		this.setOptions($options);		
		
		for (menu_id in this.options.under_menu){
			// '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.under_menu[menu_id].elem_DOM = $('menu_td_'+menu_id);
			this.options.under_menu[menu_id].elem_DOM.addEvent('mouseover', this.show_box_for_delay.bind(this, [menu_id]));			
			this.options.under_menu[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).adopt(this.frame_DOM);
		this.frame_DOM.setStyle('visibility', 'hidden');
		this.frame_content_DOM = $(this.options.frame_contetnt_id);		
		this.h_limits = $(this.options.limit_box_id);
		this.h_limits_pos = this.h_limits.getPosition();
		this.h_limits_size = this.h_limits.getSize();				
		//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.dummy_link_DOM = $(this.options.dummy_a_id);
		this.frame_tween = new Fx.Morph(this.frame_DOM, {duration: 'short'});
	},
	show_box_for_delay: function(ind){
		this.options.box_hide_show_delay = this.show_box_for.delay(250, this, ind);
	},
	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.under_menu[ind].elem_DOM;
		var target_pos = target_elem.getPosition();
		var target_size = target_elem.getSize();				
		this.frame_content_DOM.empty();
		var current_content_size = this.frame_content_DOM.getSize();		
		this.frame_content_DOM.setStyles({'width': 'auto', 'height':'auto'});		
		var cont_dic = new Element('div', {styles:{'float':'left','margin': '5px 0px'}});
		cont_dic.inject(this.frame_content_DOM);
		this.options.menu_per_col = Math.round(this.options.under_menu[ind]['count'] / 5 + 1);
		//alert(this.options.menu_per_col);
		for (var i = 0; i < this.options.under_menu[ind]['count']; i++){			
			var menu_info = this.options.under_menu[ind]['menu'][i];
			var new_menu = this.dummy_link_DOM.clone();	
			new_menu.setProperty('href', menu_info.link)
			new_menu.setProperty('title', menu_info.title)
			new_menu.set('html', menu_info.text);
			new_menu.inject(cont_dic);
			if ((i + 1) % this.options.menu_per_col == 0){
				cont_dic = new Element('div', {styles:{'float':'left', 'border-left':'1px solid #b4b4b4', 'padding-left':'5px', 'margin': '5px 0px'}});
				cont_dic.inject(this.frame_content_DOM);
			}//console.log(i);
		}
		//console.log(this.options.under_menu[ind]['menu'].length + ' ' + ['count']);
		this.frame_DOM.setStyles({'display': 'block'});
		var content_size = this.frame_content_DOM.getSize();		
		var box_pos = {
			x:target_pos.x - (content_size.x / 2) + (target_size.x / 2) - 21,
			y:this.options.top_offset
		};				
		var box_size = {
			x: content_size.x, 
			y: content_size.y
		};		
		// too mutch to the left
		//console.log(box_pos.x+ ' ' + this.h_limits_size.x + ' ' + box_size.x);
		if (this.h_limits_pos.x > box_pos.x && this.h_limits_size.x  > box_size.x){
			//var box_dx = parseInt(this.h_limits_pos.x) + 5 - parseInt(box_pos.x);
			box_pos.x = parseInt(this.h_limits_pos.x) + 5;							
		}
		// too mutch to the right
		if (this.h_limits_pos.x + this.h_limits_size.x < box_pos.x + box_size.x && this.h_limits_size.x  > box_size.x){
			//var box_dx = parseInt(this.h_limits_pos.x) + parseInt(this.h_limits_size.x) - parseInt(box_pos.x);
			box_pos.x = parseInt(this.h_limits_pos.x) + parseInt(this.h_limits_size.x) - parseInt(box_size.x);							
		}	
		// too wide anyway
		if (box_size.x > this.h_limits_size.x){
			var sizeDx = (box_size.x - this.h_limits_size.x) / 2;
			box_pos.x = parseInt(this.h_limits_pos.x) - sizeDx;							
		}		
		/*var child = this.frame_content_DOM.getFirst();
		if (child) {
			$(document.body).grab(child);
			child.setStyle('display', 'none');
		}*/		
		//this.frame_top_arrow_DOM.setStyle('background-position', arrow_pos.x+' '+arrow_pos.y);				
		if (this.options.box_visible){			
			this.frame_DOM.set('tween', {'duration':'short'});				
			this.frame_DOM.tween('left', box_pos.x);
			this.frame_content_DOM.setStyles({'width':current_content_size.x, 'height':current_content_size.y});		
			this.frame_content_DOM.morph({
				'width':box_size.x,
				'height':box_size.y
				/*onComplete: function($content){
					//$content.setStyles({'display':'block'});
					//this.frame_content_DOM.grab($content);										
				}.bind(this, [this.frame_content_DOM])*/
			}); 
			//this.frame_DOM.setPosition({'x':target_pos.x - (content_size.x / 2) + (target_size.x / 2), 'y':this.options.top_offset});
		} else {			
			//content_elem.setStyle('display', 'block');			
			//this.frame_content_DOM.adopt(content_elem);			
			///this.frame_content_DOM.setStyles({'width':content_size.x, 'height':content_size.y});
			this.frame_content_DOM.setStyles({'width':box_size.x, 'height':box_size.y});			
			//this.frame_DOM.setPosition({'x':target_pos.x - (content_size.x / 2) + (target_size.x / 2) - 21, 'y':this.options.top_offset});			
			this.frame_DOM.setPosition({'x':box_pos.x, 'y':box_pos.y});
			this.frame_DOM.setStyle('display','block');			
			this.frame_DOM.setStyle('visibility', 'visible');
			this.frame_tween.cancel();
			this.frame_DOM.setStyle('opacity','1');
			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});
		/*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;
	}
});
/* main menu text */
var menu_text_wrapper = new Class({
	Implements: [Chain, Options],
	/* standart egenskap */
	options: {
		meny_info							: null,
		flash_wrapper_src					: 'site/main/swf/meny_text.swf',
		wraping_div							: 'meny_div_cont_',
		place_holder 						: 'a_1l_',
		anchor								: 'a_1l_',
		menu_js_object_name					: 'menu_JS_obj'		
	},  	  	
	/* -- constructor -- */
	initialize: function($options){
		this.setOptions($options);								
		for (meny in this.options.meny_info){			
			var menu_info = this.options.meny_info[meny];
			var menu_id = menu_info.MENU_ID;			
			var flashvars = this.create_swf_var();						
			var a_DOM = $(this.options.anchor + menu_id);
			flashvars.menu_text = a_DOM.get('html');
			flashvars.JS_click_eval = 'menu_JS_obj.menu_mouse_click(\''+meny+'\')';
			flashvars.JS_over_eval = 'menu_JS_obj.menu_mouse_over(\''+meny+'\')';
			flashvars.JS_out_eval = 'menu_JS_obj.menu_mouse_out(\''+meny+'\')';
			//console.log(menu_info.sel);
			if (menu_info.sel == true) flashvars.meny_selected = menu_info.sel; 
			$extend(flashvars, menu_info.flashvars);			
			var attributes = this.create_swf_attr();
			attributes.name = a_DOM.getProperty('name');
			attributes.styleclass = a_DOM.getProperty('class');						
			var new_swf_size = a_DOM.getSize();
			a_DOM.getParent().setStyle('width', new_swf_size.x+'px');
			this.options.meny_info[meny].achor_DOM = a_DOM;
			//$('meny_div_cont_'+menu_id).setStyles({'width':new_swf_size.x+'px', 'height':new_swf_size.y+'px', 'overflow':'hidden'});
			//a_DOM.setStyle('display', 'none');		
			//attributes.id = $(pic_info.id).getProperty('id');		
			//console.log(new_swf_size.x);
			swfobject.embedSWF(EDIT_CORE.JSSiteRoot + this.options.flash_wrapper_src, 
				this.options.place_holder + menu_id, 
				new_swf_size.x, 
				new_swf_size.y, 
				"10.0.0", 
				EDIT_CORE.JSSiteRoot + "site/pages/html/flash_expressInstall", 
				flashvars, 
				this.swf_params, 
				attributes
			);
		}		
	},
	create_swf_var : function (){
		return {		
		}
	},
	create_swf_attr : function(){
		return {	  		
		styleclass :"flash_fade_class_1",
		standby:"Laster ned",
		align:"left"
		};
	},
	swf_params : {
		  bgcolor:"#ffffff",
		  wmode  :"transparent",
		  allowNetworking  :"all",
		  allowScriptAccess  :"always"
	  },
	menu_mouse_click: function($menu){
		var menu_info = this.options.meny_info[$menu];
		var menu_id = menu_info.MENU_ID;					
		//var a_DOM = $(this.options.anchor + menu_id);
		window.location = this.options.meny_info[$menu].achor_DOM.getProperty('href');
	},
	menu_mouse_over: function($menu){
		
	},
	menu_mouse_out: function($menu){
		
	}
});
