/*****************Menu General********************/

(function($){
	$.fn.stxMenu = function(ops){
			var stxOps;
		    var defaults = {'directorio':'', 'id':'0', 'orientacion':'horizontal', 'hojaEstilo':'stxMenu', 'elementos':[]
		   // , 'css':{'color':'#FFDE00', 'fondo':'transparent', 'colorEncima': '#2F358B', 'fondoEncima':'#FFDE00', 'tamanoFuente':'medium', 'ancho':'todo'}
		    };
		    
			var elemento = $(this);
			this.each(function(){
				stxOps = $.extend(defaults,ops);
				//Agrego el elemento que contiene el menu.
		    	elemento.prepend("<div class='stxMenu'></div>");

				devElementos();
		    	//Ini();
			});
			
			function Ini(){

				var m = elemento.find('.stxMenu').first();
				if(stxOps.elementos != undefined){
					agregarHijos(m, stxOps.elementos);							
					$(m).find('li').bind('mouseover',function(){ 
						$(this).find('ul').first().css('visibility','visible'); 
						
					}).bind('mouseout',function(){
						$(this).find('ul').first().css('visibility','hidden');
					});

					if(stxOps.orientacion == 'vertical'){
						elemento.find('ul > li').css('text-align','left').css('display',"inline").css('width','100%');
					}
					if(stxOps.css){
						if(stxOps.css.color) elemento.find('.stxMenu li a').css('color',stxOps.css.color);
						if(stxOps.css.color) elemento.find('.stxMenu li a').css('background-color',stxOps.css.fondo);
						//Hover: color fondo.
						if(stxOps.css.fondo) elemento.find('.stxMenu li a').hover( function(){
							$(this).css('background-color',stxOps.css.fondoEncima);},function(){$(this).css('background-color',stxOps.css.fondo);}
						);
						if(stxOps.css.tamanoFuente){ 
							elemento.find('.stxMenu li a').css('font-size',stxOps.css.tamanoFuente );
						}
						//Hover: color fuente.
						if(stxOps.css.fondo) elemento.find('.stxMenu li a').hover( function(){
							$(this).css('color',stxOps.css.colorEncima);},function(){$(this).css('color',stxOps.css.color);}
						);
						if(stxOps.css.ancho){
							if(stxOps.css.ancho=='todo') {
								elemento.find('.stxMenu ul li').css({'width':'100%','float':'left', 'text-align':'left'});
							}
						}
					}
					
					var obj=$('.stxMenu > ul > li > ul > li').find('ul');
					if(obj != undefined){
						//var _top =  obj.parent().parent().css('height').replace('px','');
						//var _left = obj.parent().width();
						//obj.css('top',_top).css('left','0');
					}
				}
			}
			
			function agregarHijos(lista,eles){
				$(lista).append('<ul></ul>');
				var m = $(lista).find('ul').last();
				
				var i;
				for(i=0; i < eles.length; i++){
					if(eles[i].descripcion == null) eles[i].descripcion ='';
					var target = "_self"; if(eles[i].esExt == true) target = '_blank';
					if(eles[i].enlace != '')
						m.append('<li><a href="'+ eles[i].enlace +'" target="'+target+'" title="'+eles[i].descripcion+'" >'+ eles[i].titulo +'</a></li>');
					else
						m.append('<li><a target="'+target+'" >'+ eles[i].titulo +'</a></li>');
						
					if((eles[i].elementos != undefined) && (eles[i].elementos.length > 0)){
						var men = $(m).find('li').last();
						men.find('a').append('<span style="text-align:right;"> '+'<img style="margin-left: 5px; border-width: 0px;" width="10" height="8"  src="'+ stxOps.directorio + 'media/azul-abajo.png"'+' </span>');
						agregarHijos(men, eles[i].elementos);
					}
				}	
			}
			
			function devElementos(){
				$.post(	
						stxOps.directorio + 'stxMenu.php',
						{"func":"devElesMenu",'id':stxOps.id},
						function(eles){
							if(eles){
								stxOps.elementos = $.extend(stxOps.elementos,eval(eles));//stxOps.elementos = $.extend(stxOps.elementos,eval([{'titulo':'Recursos','enlace':'http://www.ipl.edu.do/?sec1=recs'},{'titulo':'Recursos','enlace':'http://www.ipl.edu.do/?sec1=recs'},{'titulo':'Recursos','enlace':'http://www.ipl.edu.do/?sec1=recs'}]));
								Ini();
							}
							else
								alert('Ha ocurrido un error al validar los datos en el servidor o el mismo no esta disponible.');
						}
					);
			}
			

			this.Finalizar = function(){
				elemento.find('.stxMenu').remove();
			};
			
			return this;
	};
})(jQuery);
