var JS_DUMP_ENABLE = true;

var navayo =
{
	currentTopMenu			: null,
	currentSubMenu			: null,
	currentMenuItemBound	: null,
	currentMenuGroupBound	: null,
	ajax					: new Ajax('index.php', 'POST', true, 'URL'),
	
	onload: function()
	{
		addEvent(document, 'mousemove', navayo, 'mouseMove');
		var menuElmList = $('menu').XPath('/ul[@class=links]/li[@sub]');
		
		menuElmList.map(function(elm)
		{
			addEvent(elm, 'mouseover', navayo, 'onMouseOver');
		});
	},
	
	mouseMove: function(e)
	{
		var evt = e || window.event;
		var mmc = getMouseCoords(evt);
		
		if(!pointInBox(this.currentMenuGroupBound, mmc) && isElement(this.currentSubMenu))
		{
			this.currentSubMenu.del();
			this.currentSubMenu = null;
		}
				
	},
	
	onMouseOver: function(e)
	{
		var evt 				= e || window.event;
		var target				= evt.srcElement || evt.target;
		var cont				= getParentBy(target, 'tagName', 'LI');
		
		if(cont.getAttribute('sub') && this.currentTopMenu != cont && isElement(this.currentSubMenu))
		{
			this.currentSubMenu.del();
			this.currentSubMenu = null;
		}
		
		if(this.currentSubMenu == null)
		{
			var subMenuId				= 'submenu-'+cont.getAttribute('sub');
			if(!isElement($(subMenuId)))
				return false;
			
			this.createIframe($(subMenuId), 'menu-bg');
			var subMenu					= $(subMenuId).copyTo(document.getElementsByTagName('body').item(0));		
		
			
			this.currentTopMenu			= cont;
			this.currentSubMenu 		= subMenu;
			this.currentMenuItemBound 	= getBounds(cont);
			
			this.currentSubMenu.setStyle({
				display			: 'block',
				left			: this.currentMenuItemBound._x + 'px',
				top				: this.currentMenuItemBound._y + this.currentMenuItemBound.height + 1 + 'px'
			});
			
			this.currentMenuGroupBound =
			{
				_x		: this.currentMenuItemBound._x,
				_y		: this.currentMenuItemBound._y,
				width	: this.currentMenuItemBound.width,
				height	: this.currentMenuItemBound.height + getBounds(this.currentSubMenu).height
			}
			this.currentSubMenu.iframe = this.createIframe(subMenu, 'menu-bg');
			this.currentSubMenu.del = function()
			{
				this.iframe.parentNode.removeChild(this.iframe);
				this.parentNode.removeChild(this);
			}
		}
	},
	
	switchProductImage: function(number)
	{
		var container 	= $('product-full-size-image');
		var imgList		= container.getElementsByTagName('img');
		
		for(var i=0 ; i<imgList.length ; i++)
		{
			if( i+1 == number)
				imgList[i].style.display = 'block';
			else
				imgList[i].style.display = 'none';
		}
	},
	
	showPopUp: function(name)
	{
		var id 		= 'popup-'+name;
		var popup	= $(id);
		
		if(isElement(popup))
		{
			var width	= getWindowWidth();
			var height	= getWindowHeight();
			var body	= document.getElementsByTagName('body').item(0);
			var bg		= create.div(id+'-bg', null, null, {position: 'absolute', left:'0px', top:'0px', width:width+'px', height:height+'px', backgroundColor:'#000000', filter:'alpha(opacity=80)', opacity: 0.8}, body);
			clone		= popup.copyTo(body);
			
			clone.id	= id+'-clone';
			clone.setStyle({display:'block', visibility:'hidden'});
			clone.setStyle({position:'absolute', left:width/2-clone.offsetWidth/2+'px', top:height/2-clone.offsetHeight/2+'px'});
			clone.setStyle({visibility:'visible'});
		}
	},
	
	hidePopup: function(name)
	{
		var id = 'popup-'+name;
		if( (e = $(id+'-bg')) )		e.del();
		if( (e = $(id+'-clone')) )	e.del();
	},
	
	sendForm: function(form, popupName)
	{
		var popupId		= 'popup-'+popupName+'-clone';
		var param		= new Object();
		var events		= new Object();
		var p 			= $(popupId);
		var loading		= create.div(null, null, null, {position:'absolute', left:'0px', top:'0px', width:p.offsetWidth+'px', height:p.offsetHeight+'px', backgroundColor:'#000000', filter:'alpha(opacity=80)', opacity: 0.8}, p);
		create.img(null, './images/loading.gif', null, {position: 'relative', left:'50%', top:'50%', margin: '-15px 0 0 -15px'}, loading);
		
		for(var k in form)
		{
			if(['cmd', 'username', 'pass', 'repass', 'email', 'name_first', 'name_last', 'company', 'position', 'address', 'city', 'id_country', 'tel', 'fax', 'company_profile', 'employees', 'comments', 'redirect_uri'].exists(k))
				param[k] = form[k].value;
		}		
		
		events[AJAX.EVENT_COMPLETE] = function(ajax)
		{
			var p = $(popupId);
			if(isElement(p))
			{
				p.innerHTML = ajax.getResponseText();
				p.setStyle({
					left	: getWindowWidth() / 2 - p.offsetWidth / 2 + 'px',
					top		: getWindowHeight() / 2 - p.offsetHeight / 2 + 'px'
				});
				
				ajax.parent.evalScriptTags(ajax.getResponseText());
			}
		}
		
		this.ajax.request(param, events, popupName);
		
		return false;
	},
	
	createIframe: function(obj, name)
	{
		var id = 'iframe-'+name;
		if( !(frame = $(id)) )
		{
			var frame = create.iframe(id, true, null, {position: 'absolute', border:'0px solid #FFFFFF'}, document.getElementsByTagName('body').item(0));
		}
		
		if(isElement(frame))
		{
			var b = getBounds(obj);
			frame.setStyle({left:b._x+'px', top:b._y+'px', width:b.width+'px', height:b.height+'px'});
		}
		
		return frame;
	}
}

addEvent(window, 'load', navayo, 'onload');

function nogo(){};