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


/**
 * 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, moduleType){

			if(typeof(moduleType) === 'undefined'){
				moduleType = 'Noclegi';
			}
			this.debug("Meteor: Loading " + moduleType + "." + moduleName);
			var scriptElem = document.createElement('script');

			scriptElem.setAttribute('src',this.PathHeler.ModulesPath + '/' + moduleType + '/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(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]);
			}
		},
		debug : function(x){
			try {
				console.log(x);
			} catch (e){}
		},
		assert : function(condition, message, data){
			if(condition === false){
				throw new Meteor.Addons.Exception(message, data);
			}
		},
		
		/**
		 * Wygenerowanie funkcji skrotu
		 */
		universalHash : function(s, tableSize) {
		  var b = 27183, h = 0, a = 31415;
		  return ((a * s) * (b % tableSize));
		},
		/**
		 * informacje o stronie
		 */
		hostPageInfo : {
			getLocation : function(){
				var loc = "no-data";
				try {
					loc = window.location + "";	
				} catch(e){
					
				}
				return loc;
			}
		}
	}
}
/**
 *	Meteor Addons Core
 *  Glowna klasa zarzadzajaca dodatkami (mapy, dyplomy, bwm )
 */
if(typeof(Meteor.Addons.Core) == 'undefined'){
	Meteor.Addons.Core = {
		_moduleList : new Array(),
		
		/**
		 * czy moduy zostały już zainicjalizowane
		 *
		 * @property _modulesInitialized
		 * @type bool
		 */
		_modulesInitialized : false,
		
		/**
		 * 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 ładuje wybrane moduły
		 */
		LoadModule : function(moduleName, moduleType){
			Meteor.Addons.Utils.Load(moduleName, moduleType);
		},
		/**
		 * Metoda rejstruje modułu w wewnetrznej tablicy aplikacji
		 */
		RegisterModule : function(oObject){
			this._moduleList.push(oObject);

			//jeśli odpalono już _initializeModules to inicjujemy moduł przy rejestracji
			if(this._modulesInitialized){
				oObject._init();
			}
		},
		
		/**
		 * zainicjalizowanie modulow
		 */
		_initializeModules : function(){
			for (var i=0; i< this._moduleList.length; i++){
				this._moduleList[i]._init();
			}
			this._modulesInitialized = true;
		}
	};

}

/**
 * Bazowa klasa dodatkow
 */
 if(typeof(Meteor.Addons.Core.MeteorExt) == 'undefined'){
	Meteor.Addons.Core.MeteorExt = {
		options : {
			MeteorService : false,
			MeteorLinksClass : 'MeteorAddLinki',
			ObiektNr : null,
			MeteorLinksPattern : {
				'Noclegi' : /(href="http:\/\/meteor24.pl")|(href="http:\/\/meteor.turystyka.pl")|(href="http:\/\/ibed.pl")|(href="http:\/\/emeteor.pl")|(href="http:\/\/e-meteor.pl")/,
				'NoclegiStrict' : /href="http:\/\/meteor.turystyka.pl"/,
				'Specjalisci' : /(href="http:\/\/swiaturody\.com)|(href="http:\/\/znowzdrow.pl)|(href="http:\/\/dentysta\-stomatolog\.com)/
			}
		},
		
		/**
		 * Funckcja spradza czy linki zostaly umieszczone w kontenerze dodatku
		 * @param {string} htmlContent
		 * @param {string} pattern [Noclegi|Specjalisci]
		 */
		checkLinks : function(htmlContent, pattern) {
			if(typeof(pattern) == 'undefined'){
				pattern = "Noclegi";
			}

			if (typeof(MeteorSerwis) != "undefined" && MeteorSerwis == 1){
				this.options.MeteorService = true;
				return true;
			}

			if(typeof(htmlContent) == 'undefined'){
				return false;
			}

			var status = false;
			if(htmlContent.match(this.options.MeteorLinksPattern[pattern])){
				status = true;				
			}
			/*
			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;
		}
	};
}


if(typeof(Meteor.Addons.Exception) == 'undefined'){
	/**
	 * wyjątek
	 * @contructor
	 * @param {string} message
	 * @param {any} data dodatkowe dane związane z wyjątkiem
	 */
	Meteor.Addons.Exception = function(message, data) {
		this.__exceptionMessage = message || {};
		this.__exceptionData = data || {};
	};

	/**
	 *
	 * @return {string} komunikat błędu
	 */
	Meteor.Addons.Exception.prototype.getMessage = function(){
		return this.__exceptionMessage;
	};

	/**
	 *
	 * @return {any} dane powiązane z wyjątkiem
	 */
	Meteor.Addons.Exception.prototype.getData = function(){
		return this.__exceptionData;
	};

	/**
	 *
	 * @return {string}
	 */
	Meteor.Addons.Exception.prototype.toString = function(){
		return this.getMessage();
	};
}


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

