dojo.declare("MGA_Menu", null, {
	baseUrl : "",
	repository : new Object(),
	rep : new Object(),
	lastimg : "",
	hold : null,
	last : "",
	
	constructor: function(json_ext, inner_div_id, baseUrl){
		this.baseUrl = baseUrl;		
		dojo.byId(inner_div_id).innerHTML = this.createMenu(json_ext);
		
		dojo.connect(dojo.byId('sec_menu'), 'onmouseover', dojo.hitch(this, "holdSubMenu"));
		dojo.connect(dojo.byId('sec_menu'), 'onmouseout', dojo.hitch(this, "onMoOut2"));
	},
	
	createMenu: function(jsonObj){
		var html = "";
		for(var i in jsonObj){
			html += '<div style="cursor:pointer;float:left; width: ' + jsonObj[i]['width']+ '; height: ' + jsonObj[i]['height'] + ';" id="item_' + (i*1+1) + '" onmouseout="mgamenu.onMoOut(this.id);" onmouseover="mgamenu.onMoOver(this);" onclick="window.location.href =\'' + jsonObj[i]['link'] + '\'">';
			html += '<img style="border: 0px solid #fff;" id="item_' + (i*1+1) + '_" src="' + this.baseUrl + '/images/' + jsonObj[i]['image'][0] + '"/>'
			html += '<img style="display: none; border: 0px solid #fff;" id="item_' + (i*1+1) + '__" src="' + this.baseUrl + '/images/' + jsonObj[i]['image'][1] + '"/>';
			html += '</div>';		
			if((typeof jsonObj[i]['sec_menu']) == "object"){
			this.repository['item_'+(i*1+1)] = new Object();
				this.repository['item_'+(i*1+1)] = jsonObj[i]['sec_menu'];
			}
		}
		return html;
	},
	
	onMoOver: function(obj){
	
		if(this.hold != null){
			clearTimeout(this.hold);
			this.hold = null;
			this.afterTimeout(this.last);
		}
		this.last = obj.id;
		
		dojo.byId(obj.id+'_').style.display = "none";
		dojo.byId(obj.id+'__').style.display = "";

		if((typeof this.repository[obj.id]) == "object" ){
			var cont = '<div class="sec_menu">';
			for(var sec in this.repository[obj.id]){
				cont += '<a href="' + this.repository[obj.id][sec]['link'] + '">&raquo;&nbsp;' + this.repository[obj.id][sec]['name'] + '</a><br/>';
			}
			cont += '</div>';
			dojo.byId('sec_menu').innerHTML = cont;
			dojo.byId('sec_menu').style.left = (55+obj.offsetLeft-2) + 'px';
			dojo.byId('sec_menu').style.display = "";
		}
	},
	
	onMoOut: function(id){
		this.hold = setTimeout(dojo.hitch(this, "afterTimeout", id), 300);
	},
	
	onMoOut2: function(id){
		var tmplast = this.last;
		this.hold = setTimeout(dojo.hitch(this, "afterTimeout", tmplast), 300);
	},
	
	afterTimeout : function(obj){
		if(obj != null){
			dojo.byId(obj+'_').style.display = "";
			dojo.byId(obj+'__').style.display = "none";
			dojo.byId('sec_menu').style.display = "none";
		}
	},
	
	holdSubMenu : function(){
		if(this.hold != null){
			clearTimeout(this.hold);
		}
	}
});
