var theShim = new Element('iframe');

// JavaScript Document
function showMe(p){
	// the sub_menu exists so lets set its x and y
	//alert(this.getParent().getStyle('position'));
	// Set left to 0 to figure out the real current position
	// we have two splits for it depending on various things
	this.setStyle('width','auto');
	this.setStyle('display','block');
	if(!this.getParent().hasClass('sub_menu_item')){
		if(this.getParent().getPosition()['x']+this.getSize()['size']['x']<window.getWidth()){
			this.setStyle('left',this.getParent().getPosition()['x']);
		}else{
			this.setStyle('left',this.getParent().getPosition()['x']-this.getSize()['size']['x']+this.getParent().getSize()['size']['x']);
		}
		this.setStyle('top',147);
		
	}else{
		if((this.getParent().getLeft() + this.getParent().getSize()['size']['x'] + this.getSize()['size']['x'])<window.getWidth()){
			// Position Element
			this.setStyle('top',this.getParent().getPosition()['y']-148);	
			this.setStyle('left',this.getParent().getSize()['size']['x']);
			
		}else{
			// Position Element
			this.setStyle('top',this.getParent().getPosition()['y']-148);
			this.setStyle('left',0-this.getSize()['size']['x']);
		}
	}
	
	// Get the width and height of the panel
	var bigds = this.getDimensions();
	var bigpos = this.getPosition();
	
	// Set the shims properties
	theShim.setStyles({
				'display': 'block',
				'top':bigpos.y+'px',
				'left':bigpos.x+'px',
				'width':bigds.width+'px',
				'height':bigds.height+'px',
				'zIndex': '4'
	});
	
	//$('pageIF').setStyle('display','block');
	//this.iShim.show.bind(this.iShim);
}

function hideMe(){
	this.setStyle('display','none');
	theShim.setStyle('display','none');
	//this.iShim.hide.bind(this.iShim);
	// Hide the IFRAME covering everything
	//$('pageIF').setStyle('display','none');
}

function BuildSubMenu(menu_el){
	// look for the submenu
	var gparent = menu_el.getParent(); // used to link events
	
	// attempt to get the sub menu
	var sub_menu = $E('div.sub_menu',menu_el);
	if($chk(sub_menu)){	
	
		// Create the IFRAME shim
		/*sub_menu.iShim = new IframeShim({
		   element: sub_menu
		});*/
		//sub_menu.iShim.shim.setStyle('border', '8px solid #333');
		
		// Bind the show and the hide
		menu_el.addEvent('mouseenter',showMe.bindWithEvent(sub_menu));
		menu_el.addEvent('mouseleave',hideMe.bindWithEvent(sub_menu));
		
		// We want to go through the divs that are inside and bind them too
		$ES('div.sub_menu_item',sub_menu).each(BuildSubMenu);
	}
}

function BuildMenu(){
	// Get an array of all of the menu_item in menu_items
	var top_items = $ES('*.menu_item',$('menu_items'));
	top_items.each(BuildSubMenu);
	// Set the shims properties
	theShim.setStyles({
				'display': 'none',
				'position': 'absolute',
				'top':'200px',
				'left':'0px',
				'width':'80px',
				'height':'80px',
				'zIndex': '4',
				'border': 'none',
				'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
	}).setProperties({
				'src':'javascript:void(0);',
				'frameborder':'0',
				'scrolling':'no',
				'id':'dashim'});
	
	theShim.injectInside(document.body);
}

window.addEvent('domready',BuildMenu);