if(typeof(Meteor) == 'undefined'){ Meteor = {}; }
if(typeof(Meteor.Addons) == 'undefined'){ Meteor.Addons = {}; }


/**
 * Bazowa klasa narzedzi
 */
 if(typeof(Meteor.Addons.Utils) == "undefined"){
	Meteor.Addons.Utils = {
		
		JqueryScriptOutputed :false, 
		JqueryLoadedFromThis : false ,
		
		PathHeler : {
			MainJsPath : 'http://add.meteor24.pl/add/JS/AddsManager',
			AddonsPath : 'http://add.meteor24.pl/add/JS/AddsManager',
			ModulesPath :'http://add.meteor24.pl/add/JS/AddsManager/Modules',
			ResourcesPath :'http://add.meteor24.pl/add/JS/AddsManager/Resources'
		},	
		
		Load : function(moduleName){	
			var scriptElem = document.createElement('script');	
				scriptElem.setAttribute('src',this.PathHeler.ModulesPath+'/MeteorExt' + moduleName + '.js');
				scriptElem.setAttribute('type','text/javascript');
			document.getElementsByTagName('head')[0].appendChild(scriptElem)
		},
		
		LoadJquery: function initJQuery(fCallback) {
			//if the jQuery object isn't available
			if (typeof($) == 'undefined' && typeof(jQuery) == 'undefined') {
				if (!Meteor.Addons.Utils.JqueryScriptOutputed) {		
					//only output the script once..
					Meteor.Addons.Utils.JqueryScriptOutputed = true;
					Meteor.Addons.Utils.JqueryLoadedFromThis = true;
					Meteor.Addons.Utils.LoadJs("http://meteor24.pl/add/JS/JQuery/jquery-1.4.1.min.js");
				}	
				setTimeout(function(){Meteor.Addons.Utils.LoadJquery(fCallback);}, 50);
				
			} else {	
				if (Meteor.Addons.Utils.JqueryLoadedFromThis){
					jQuery.noConflict();
				}
				fCallback.apply(this);
			}
		},
            
		LoadJs : function(scriptPath){	
			var scriptElem = document.createElement('script');	
				scriptElem.setAttribute('src',scriptPath);
				scriptElem.setAttribute('type','text/javascript');
			document.getElementsByTagName('head')[0].appendChild(scriptElem)
		},
		
		LoadCss : function(fileName){	
			var linkElem = document.createElement('link');	
				linkElem.setAttribute('href',this.PathHeler.ResourcesPath+'/' + fileName + '.css');
				linkElem.setAttribute('type','text/css');
				linkElem.setAttribute('rel','stylesheet');
				linkElem.setAttribute('id','link-element-' + fileName);
			document.getElementsByTagName('head')[0].appendChild(linkElem)
		},
		
		showOverlay : function(oOptions){
			this.LoadCss('overlay');
			
			var overlayElement = document.createElement('div');
				overlayElement.setAttribute('id','overlay-container');
				
			var overlayContentElement = document.createElement('div');		
				overlayContentElement.setAttribute('id','overlay-content');	
			overlayElement.appendChild(overlayContentElement);
			
			var overlayContentElementDIV = document.createElement('div');	
				overlayContentElementDIV.setAttribute('id','overlay-content-div');			
				overlayContentElementDIV.innerHTML = oOptions.Html;
			overlayContentElement.appendChild(overlayContentElementDIV);
			
				
			document.getElementsByTagName('body')[0].appendChild(overlayContentElement);
			document.getElementsByTagName('body')[0].appendChild(overlayElement);	
			this.addEvent(overlayElement, 'click', this.removeOverlay);
			this.addEvent(overlayContentElement, 'click', this.removeOverlay);	
		},
		
		removeOverlay : function(){
			if (document.getElementById('link-element-overlay')){
				document.getElementsByTagName('head')[0].removeChild(document.getElementById('link-element-overlay'));
			}
			if (document.getElementById('overlay-container')){
				document.getElementsByTagName('body')[0].removeChild(document.getElementById('overlay-container'));
			}
			if (document.getElementById('overlay-content')){
				document.getElementsByTagName('body')[0].removeChild(document.getElementById('overlay-content'));
			}
		},
		
		addEvent : function(obj, type, fn)
		{
			if (obj.addEventListener)
			  obj.addEventListener(type, fn, false);
			else if (obj.attachEvent) {
			  obj["e" + type + fn] = fn;
			  obj[type + fn] = function() { obj["e" + type + fn]( window.event ); }
			  obj.attachEvent("on" + type, obj[type + fn]);
			}
		},
		
		/**
		 * Wygenerowanie funkcji skrotu
		 */
		universalHash : function(s, tableSize) {
		  var b = 27183, h = 0, a = 31415;
		  return ((a * s) * (b % tableSize));
		}
	}
}
/**
 *	Meteor Addons Core
 *  Glowna klasa zarzadzajaca dodatkami (mapy, dyplomy, bwm )
 */
if(typeof(Meteor.Addons.Core) == 'undefined'){
	Meteor.Addons.Core = {
		_moduleList : new Array(),
		
		/**
		 * Konstruktor
		 */
		_init : function(){
			this._initializeModules();
		},
		
		/**
		 * Metoda ładuje wybrane moduły
		 */
		LoadModules : function(){		
			var args = arguments;
			for( var i = 0; i < args.length; i++ ) {	
				Meteor.Addons.Utils.Load(args[i]);
			}
		},
		
		/**
		 * Metoda rejstruje modułu w wewnetrznej tablicy aplikacji
		 */
		RegisterModule : function(oObject){	
			this._moduleList.push(oObject);
		},
		
		/**
		 * zainicjalizowanie modulow
		 */
		_initializeModules : function(){
			for (var i=0; i< this._moduleList.length; i++){
				this._moduleList[i]._init();
			}
		}
	};
}

/**
 * Bazowa klasa dodatkow
 */
 if(typeof(Meteor.Addons.MeteorExt) == 'undefined'){
	Meteor.Addons.Core.MeteorExt = {
		options : {
			MeteorService : false,
			MeteorLinksClass : 'MeteorAddLinki',
			ObiektNr : null
		},
		
		/**
		 * Funckcja spradza czy linki zostaly umieszczone w kontenerze dodatku
		 */
		checkLinks : function(htmlContent) {
			if (typeof(MeteorSerwis) != "undefined" && MeteorSerwis == 1){
				this.options.MeteorService = true;
				return true;
			}
			
			var status = false;
			if(htmlContent.match(/(href="http:\/\/meteor24.pl")|(href="http:\/\/meteor.turystyka.pl")|(href="http:\/\/ibed.pl")|(href="http:\/\/emeteor.pl")|(href="http:\/\/e-meteor.pl")/)){
				status = true;				
			}
			
			if (!status) 
				return false;
			else
				return true;
		},
		
		hideLinks : function(AddContainer){
			var allPageTags=document.getElementsByTagName("DIV"); 
			 //Cycle through the tags using a for loop 
			 for (i=0; i<allPageTags.length; i++) { 
				//Pick out the tags with our class name 
				 if (allPageTags[i].className==this.options.MeteorLinksClass) { 
					if (typeof(allPageTags[i].parentNode.getAttribute("id")) != 'undefined' && allPageTags[i].parentNode.getAttribute("id") == AddContainer){
						 //Manipulate this in whatever way you want 		 
						 allPageTags[i].style.display='none'; 
					 }
				 } 
			 }
		},
		
		/**
		 * Metoda wstawia Iframe do drzewa DOM
		 * @exception {string} Cannot find required HTML Tag in DOM
		 * @exception {string} Cannot find required attribute 'obiektNr'
		 */
		appendHtmlObject : function(oOptions){			
			requiredElement = document.getElementById(oOptions.addContainerId);		
			if (requiredElement != null){
				this.options.ObiektNr = requiredElement.getAttribute(oOptions.obiektIDAttribute);
				if (this.options.ObiektNr != null){
					// ukrycie linkow 
					if (!this.options.MeteorService) {
						this.hideLinks(oOptions.addContainerId);
					}			
					// wklejenie obiektu
					var width =  (requiredElement.style.width != '')?requiredElement.style.width:oOptions.css.width;
					var height = (requiredElement.style.height != '')?requiredElement.style.height:oOptions.css.height;
					requiredElement.innerHTML = '<iframe border="0" frameborder="0" style="border: none; width: '+width+'px; height:'+height+'px;" src="'+oOptions.source+'" width="'+width+'" height="'+height+'"></iframe>';
				} else {
					throw {name: "", message: "Cannot find required attribute '"+oOptions.obiektIDAttribute+"'."}
				}
			} else {
				// Brak wymaganego elementu w DOM
				throw {name: "", message: "Cannot find required HTML Tag in DOM."}
			}
		},
		
		/**
		 * Metoda tworzy obiekt Iframe
		 * @param {Object} width wysokość
		 * @param {Object} height wysokość
		 * @param {Object} src źródło
		 */
		createIframeElement : function(width, height, src){
			var iframeElement = document.createElement('iframe');
			iframeElement.setAttribute('src',src);
			iframeElement.setAttribute('width',width);
			iframeElement.setAttribute('height',height);
			iframeElement.setAttribute('border' ,0);
			iframeElement.style.border = 'none';
			iframeElement.setAttribute('frameborder', 0);
			return iframeElement;
		}
	};
}

window.onload = function(){
	Meteor.Addons.Utils.LoadJquery(function(){
		// inicjalizacja modulow
		jQuery(document).ready(function($){
			Meteor.Addons.Core._init();
		});
	});
}
