if(window.ipub) _ipub = ipub;

//global ipub object: loads external JS scripts dynamically based on the existance of corresponding class name in the DOM


ipub = {
	info: 'Infrapublica Base Javascript',
	log : function() {
		try{console.log.apply('',arguments);} catch(e) {}
	},
	scriptsLoc : location.pathname.split('/').slice(0,-1).join('/') + '/js/modules/', // change to absolute path once we integrate into platform
	mod:{
		'buscador':{},
		'buscador2':{}
	},
	loaded : [],
	init: function(){
		var temp;
		
		//remove non-js message from DOM
		$('.enable-js').remove();
		
		//preload images
		
		(function(){
			var imagesToLoad = [
				'css/images/bg-tooltip-left.png',
				'css/images/bg-tooltip-right.png',
				'css/images/bg-tooltip-image-left.png',
				'css/images/bg-tooltip-image-right.png'
			];
			$('<div class="no-print" style="position:absolute;left:-9999px;top:-9999px;height:0;width:0;overflow:hidden;"/>').appendTo('body').html('<img src="'+imagesToLoad.join('"/><img src="')+'"/>');
		})();
		
		// accessibility: [plugin] treat spacebar 'keypress' event the same as the mouse 'click' event
		$.fn.klik = function(f){
			return this.bind('click keypress', function(e) {
				if (!e.keyCode || e.keyCode == 32){
					return f.call(this, e);
				}
			});
		};
		
		// setup and, if needed, load modules & plugins from other files
		$.each(ipub.mod, function(moduleName){
			$.extend(ipub.mod[moduleName],{
				className: '.mod-'+moduleName,
				load: function(m){
					var s = document.createElement('script');
					s.src = ipub.scriptsLoc + 'ipub.' + moduleName.replace(/_/g,'-') + '.js';
					$('head').append(s);
					
					(function(){
						if(!ipub.mod[moduleName].init){
							setTimeout(arguments.callee, 30);
							return;
						}
						ipub.mod[moduleName].init(m);
					})();

					ipub.loaded.push(moduleName);
				}
			});
			
			var module = $('.'+moduleName.replace(/_/g,'-'));
			if (module.size()){
				ipub.mod[moduleName].load(module);
			}
		});
		
		// print entire page - using "print-page" class
		$('.print-page').klik(function(){
			window.print();
		});
	}	
};

// instantiate ipub object
ipub.init();
