var tmw = {};

$(document).ready(function(){  
	if(typeof tmw.galeria != "undefined")
		tmw.galeria.init();
	if(typeof tmw.popup != "undefined")
		tmw.popup.init();
	if(typeof tmw.notasImagenes != "undefined")
		tmw.notasImagenes.init();
});

/* Fondo popup */
tmw.fondoPopup = {
	callback: null,
	
	muestraFondoPopup: function(){
		if(document.getElementById("TMW_overlay") !== null && $("#TMW_overlay").css('display')!='none')
			return;
			
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TMW_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TMW_HideSelect'></iframe><div id='TMW_overlay'></div>");
				$("#TMW_overlay").addClass('TMW_overlayBG');
				$("#TMW_overlay").click(tmw.fondoPopup.ocultaFondoPopup);
			}
		}else{//all others
			if(document.getElementById("TMW_overlay") === null){
				$("body").append("<div id='TMW_overlay'></div>");
				$("#TMW_overlay").addClass('TMW_overlayBG');
				$("#TMW_overlay").click(tmw.fondoPopup.ocultaFondoPopup);
			}
		}
		$("#TMW_overlay").hide();
		$("#TMW_overlay").fadeIn('fast');
	},
	
	ocultaFondoPopup: function(){
		$("#TMW_overlay").fadeOut("fast", function(){
			$('#TMW_overlay,#TMW_HideSelect')
				.trigger("unload")
				.unbind()
				.remove();
			if(tmw.fondoPopup.callback != null && typeof tmw.fondoPopup.callback == 'function')
				tmw.fondoPopup.callback();
		});
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
		}
	},
	
	desactivaCerrarClick: function(){
		$("#TMW_overlay").unbind();
	}
};

/* Loading */
tmw.loading = {
	imgLoader: null,
	pathImg: "../includes/img/iconos/loadingAnimation.gif",
	
	muestraLoading: function(){
		tmw.loading.imgLoader = new Image();
		tmw.loading.imgLoader.path = tmw.loading.pathImg;
		$("body").append("<div id='TMW_load'><img src='"+tmw.loading.imgLoader.path+"' /></div>");
		$('#TMW_load').show();
	},
	
	ocultaLoading: function(){
		$("#TMW_load").remove();
	}
};

/* Bibliotecas generales */
tmw.commons = {
	getTeclaPulsada: function(e){
		if(e == null)
			return event.keyCode;
		else
			return e.which;
	},
	
	getEvento: function(e){
		if(e == null)
			return event;
		else
			return e;
	},
	
	cancelaEvento: function(e){
		var evento = tmw.commons.getEvento(e);
		if(evento.preventDefault)
			evento.preventDefault();
		else
			evento.returnValue = false;
	},
	
	tamanioPagina: function(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		arrayPageSize = [w,h];
		return arrayPageSize;
	},
	
	parseaQuery: function(query){
		var Params = {};
		if (!query) 
			return Params;
		var Pairs = query.split(/[;&]/);
		for ( var i = 0; i < Pairs.length; i++ ) {
			var KeyVal = Pairs[i].split('=');
			if (!KeyVal || KeyVal.length != 2 )
				continue;
			var key = unescape( KeyVal[0] );
			var val = unescape( KeyVal[1] );
			val = val.replace(/\+/g, ' ');
			Params[key] = val;
		}
		return Params;
	},
	
	includeJS: function(srcScript){
		var funcion = arguments[1];
		var head = document.getElementsByTagName('head').item(0); 
		var sct = document.createElement("script");
		sct.setAttribute("type","text/javascript");
		sct.setAttribute("src",srcScript);
		head.appendChild(sct);
		
		return sct;
	},
	
	includeCSS: function(fileUrl) {
		var head = document.getElementsByTagName('head').item(0); 
		var css = document.createElement("link");
		css.setAttribute("rel", "stylesheet");
		css.setAttribute("type", "text/css");
		css.setAttribute("media", "screen");
		css.setAttribute("href", fileUrl);
		head.appendChild(css); 

		return css;
	},
	
	removeJS: function(script) {
		var head = document.getElementsByTagName('head').item(0); 
		head.removeChild(script); 
	}
};