/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Namespace and globals
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
window.ThelisResa = {
	_typeof: '[Namespace ThelisResa]'
	,toString: function() { return ThelisResa._typeof; }

	,host: '//ajax.webcamp.fr/'
	,base: '//thelisresa.webcamp.fr/'
	,sess: '&PHPSESSID=ad7rjsf4k3ikurc3a0mrn7vrgl'
	,sessid: 'ad7rjsf4k3ikurc3a0mrn7vrgl'
	,sessname: 'PHPSESSID'
	,dev: 'false'


	/* old version fix */
	,baseUrl: '//thelisresa.webcamp.fr/'
	,_SESS: '&PHPSESSID=ad7rjsf4k3ikurc3a0mrn7vrgl'

    ,SEARCH_LIST: 'list'
    ,SEARCH_MULTILIST: 'multi-list'
    ,search_page: 'list'

	/* usefuls */
	,$: function(s) {
		return document.getElementById(s);
	}
	,$$: function(s, ss) {
		try {
			var a = (typeof s == 'string') ? $(s).getElementsByTagName(ss) : s.getElementsByTagName(ss);
			var b = [];
			for (var i=0; i<a.length; i++)
				b.push(a[i]);
			return b;
		} catch (e) {
			return false;
		}
	}
	,_: function(s) {
		return document.createElement(s);
	}
	,delegate: function(o, f) {
		var a = new Array();
		for (var i = 2 ; i < arguments.length ; i++) a[i - 2] = arguments[i];
		return function() {
			var aP = [].concat(arguments, a);
			f.apply(o, aP);
		}
	}
	,_debug: function(s) {
        if (ThelisResa.Debugger)
            ThelisResa.Debugger.log(s);
	}
	,_error: function(s) {
        if (ThelisResa.Debugger)
            ThelisResa.Debugger.error(s);
        else if (window.console)
			console.error(s);
	}
	,_warn: function(s) {
        if (ThelisResa.Debugger)
            ThelisResa.Debugger.warn(s);
	}
	,_todo: function(s) {
        if (ThelisResa.Debugger)
            ThelisResa.Debugger.todo(s);
	}

	,empty: function (_var) {
		if (_var === null || _var === false || _var === '' || _var === 0 || _var === '0' || typeof _var === 'undefined')
			return true;
		if (typeof _var == 'object') {
			for (i in _var)
				return false;
			return true;
		}
		return false;
	}

	,trim: function(s) {
		return s.replace(/(^\s)|(\s$)/, '');
	}

	,getPosition: function(o) {
		var x = 0;
		var y = 0;
		while (o != null) {
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		x = isNaN(x) ? 0 : x;
		y = isNaN(y) ? 0 : y;
		return {x: x, y: y};
	}

	,decorate: function (url) {
		try {
			var trackers, linker;
			if (typeof ga !== 'undefined' && typeof ga.getAll === 'function') {
				//if universal analytics is used
				trackers = ga.getAll();
				if (trackers.length) {
					linker = new window.gaplugins.Linker(trackers[0]);
					url = linker.decorate(url);
				}
			} else if (typeof _gat !== 'undefined' && typeof _gat._getTrackers === 'function') {
				//if classic analytics is used
				trackers = _gat._getTrackers();
				if (trackers.length) {
					url = trackers[0]._getLinkerUrl(url);
				}
			} else if (pageTracker && pageTracker._getLinkerUrl) {
				//pageTracker define ?
				url = pageTracker._getLinkerUrl(url);
			}

			if (window.ThelisResa.options.trackingAction) {
				var a = window.ThelisResa.options.trackingAction;
				if (typeof a == 'function') {
					url = a(url);
				} else {
					throw 'Modal::The option trackingAction is not a function';
				}
			}
		} catch (e) {
			window.ThelisResa._error(e);
		}

		return url;
	}
};

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Event
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.Event = function(o, name, func) {
		var name = name;
		var func = func;
		var object = o;

		(function() {
			if (object.addEventListener)
				object.addEventListener(name, func, false);
			else if (object.attachEvent)
				object.attachEvent('on'+name, func);
			else
				object['on'+name] = func;
		})();

		function remove() {
			if (object.detachEvent)
				object.detachEvent(name, func);
			else if (object.removeEventListener)
				object.removeEventListener(name, func, false);
			else
				object['on'+name] = null;
		};

		var that = {
			_typeof: '[Object ThelisResa:Event]'
			,toString: function() { return that._typeof; }

			,remove: function() { remove(); }

			,getName: function() { return name; }
			,getFunc: function() { return func; }
			,getObject: function() { return object; }
		};
		return that;
	};
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Browser
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.Browser = (function() {

		/* private */
		var browser = {};
        var mobile = false;

		/* constructor */
		(function() {
			/* Browser informations */
			var ua = navigator.userAgent.toLowerCase();
			var cn = navigator.appCodeName.toLowerCase();

			browser.msie = /msie/gi.test(ua);
			browser.firefox = /firefox/gi.test(ua) && /mozilla/gi.test(cn);
			browser.opera = /opera/gi.test(ua);
			browser.safari = /^((?!chrome|android|crios).)*safari/gi.test(ua);
			browser.chrome = /chrome/gi.test(ua);
			browser.ipod = /ipod/gi.test(ua);
			browser.ipad = /ipad/gi.test(ua);
			browser.iphone = /iphone/gi.test(ua);
            browser.android = /android/gi.test(ua);

			try {
				if (browser.firefox)
					/firefox[\/\s](\d+\.*\d*)/gi.test(ua);
				else if (browser.msie)
					/msie\s*(\d+\.*\d*)/gi.test(ua);
				else if (browser.opera)
					/opera[\/\s](\d+\.*\d*)/gi.test(ua);

				browser.version = new Number(RegExp.$1);
			} catch (e) {
				browser.version = 'unknown';
			}

            mobile = browser.ipod || browser.ipad || browser.iphone || browser.android;

			// var OS_windows = uA.indexOf('win')>-1;
			// var OS_mac = uA.indexOf('mac')>-1;
			// var OS_linux = uA.indexOf('linux')>-1;
		})();

		function getWindowSize() {
			if (typeof window.innerWidth == 'number') {
				w = window.innerWidth;
				h = window.innerHeight;
			} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}

			return {width: w, height: h};
		};

		function getScrollPosition() {
			var top1 = document.body ? document.body.scrollTop : 0;
			var top2 = document.documentElement ? document.documentElement.scrollTop : 0;
			var top = top1 > top2 ? top1 : top2;

			var left1 = document.body ? document.body.scrollLeft : 0;
			var left2 = document.documentElement ? document.documentElement.scrollLeft : 0;
			var left = left1 > left2 ? left1 : left2;

			return {top: top, left: left };
		};

		/* public */
		var that = {
			_typeof: '[Object ThelisResa:Browser]'
			,toString: function() { return that._typeof; }

			,msie: browser.msie
			,firefox: browser.firefox
			,opera: browser.opera
			,safari: browser.safari
			,chrome: browser.chrome
			,ipod: browser.ipod
			,ipad: browser.ipad
			,iphone: browser.iphone
			,version: browser.version

            ,mobile: mobile

			,getWindowSize: function() { return getWindowSize(); }
			,getScrollPosition: function() { return getScrollPosition(); }
		};

		return that;
	})();
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Waiter
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
    ns.Waiter = function(type, selector, func, timeout) {
        this.DELAY = 100;
        var _type = type;
        var _selector = selector;
        var _func = func;
        var _timeout = timeout ? timeout : 5000;
        var _startTime = new Date().getTime();

        (function() {
            wait();
        })();

        function wait() {
            var currentTime = new Date().getTime();
            var spendTime = currentTime - _startTime;

            if (spendTime > _timeout) {
//                if (_type == ns.Waiter.OPTION)
//                    ns._warn('Cannot find option ' + _selector);
                if (_type == ns.Waiter.OBJECT)
                    ns._warn('Cannot find object ' + _selector);
                else if (_type == ns.Waiter.HTML_ELEMENT)
                    ns._warn('Cannot find element #' + _selector);
                return;
            }

            var found = null;
            if (_type == ns.Waiter.OPTION) {
                try {
                    eval('found = ThelisResa.options.' + _selector);
                } catch (e) {}
            }else if (_type == ns.Waiter.OBJECT) {
                try {
                    eval('found = ' + _selector);
                } catch (e) {}
            } else if (_type == ns.Waiter.HTML_ELEMENT) {
                found = ThelisResa.$(_selector);
            } else {
                return;
            }

            if (null == found) {
                setTimeout(wait, ns.Waiter.DELAY);
            } else {
                setTimeout(function() {
                    func(found);
                }, ns.Waiter.DELAY);
            }
        };

		var that = {
			_typeof: '[Object ThelisResa:Waiter]'
			,toString: function() { return that._typeof; }
		}

		return that;
    }

    ns.Waiter.OPTION = 1;
    ns.Waiter.OBJECT = 2;
    ns.Waiter.HTML_ELEMENT = 3;
})(ThelisResa);

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * iLib
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.iLib = (function() {
		var initialized = false;
        var modules = [];

		/* constructor */
		(function() {
            /** Debugger waiter */
            new ns.Waiter(ns.Waiter.OBJECT, 'ThelisResa.Debugger', function(object) {
                if (location.href.match('iLib.debug')) {
                    object.init();
                } else {
                    new ns.Waiter(ns.Waiter.OPTION, 'debug', function(option) {
                        if (true == option)
                            object.init();
                    });
                }
            });

            /** Payment return (for banks acting in window top) */
            if (location.hash.match('iLib.payment_return')) {
                var options = ns.options || {custom: {}},
                    oldconf = options.custom.animation,
					hash = location.hash.split('.');

                location.hash = '';

                new ns.Waiter(ns.Waiter.OPTION, 'custom.color1', function() {
                    new ns.Waiter(ns.Waiter.OBJECT, 'ThelisResa.Modal', function(object) {
                        ns.options.custom.animation = false;
                        object.show('cart-payment.php?hashCart=' + hash[2] + '&lang=' + hash[3], true);
                        ns.options.custom.animation = oldconf;
                    });
                });
            }

            /* Colors */
            initColors();
		})();

		/* private */
        function initColors() {
            require('Colors', function() { ns.Colors.init(); });

            /* check it */
            checkInterval = setInterval(function() {
                if (ns.Colors && ns.Colors.isTerminated()) {
                    clearInterval(checkInterval);
                    init();
                }
            }, 250);
        };

		function init() {
            if (!initialized) {
                if (ns.options) {
                    /* mobile -> mode blank */
                    if (ns.Browser.mobile ||
                        2015 === +ns.versionTHR ||
                        2017 === +ns.versionTHR ||
                        ns.Browser.safari)
                        ns.options.mode = ns.iLib.MODE_BLANK;

                    /* language */
                    if (!ns.options.language || ns.empty(ns.options.language))
                        ns.options.language = 'fr';

                    /* Button */
                    new ns.Waiter(ns.Waiter.OPTION, 'button', function(option) {
                        new ns.Waiter(ns.Waiter.HTML_ELEMENT, option, function(element) {
                            new ns.Event(ns.$(ns.options.button), 'click', function() {
                                ns.$(ns.options.button).blur();
                                showModal();
                            });
                        });
                    });

                    /* Favorites */
                    new ns.Waiter(ns.Waiter.OPTION, 'favorites.layer', function(option) {
                        new ns.Waiter(ns.Waiter.HTML_ELEMENT, option, function(element) {
                            require('Favorites', function() { ns.Favorites.init(); });
                        });
                    });

                    /* DirectPayment */
                    new ns.Waiter(ns.Waiter.OPTION, 'directPayment.layer', function(option) {
                        new ns.Waiter(ns.Waiter.HTML_ELEMENT, option, function(element) {
                            require('DirectPayment', function() { ns.DirectPayment.init(); });
                        });
                    });

                    /* Accomodations */
                    new ns.Waiter(ns.Waiter.OPTION, 'accommodations.layer', function(option) {
                        new ns.Waiter(ns.Waiter.HTML_ELEMENT, option, function(element) {
                            require('Accommodations', function() { ns.Accommodations.init(); });
                        });
                    });
                }

                initialized = true;
            }
		};

		function showModal() {
			if (!ns.Modal) {
				require('Modal', function() { showModal(); });
				return;
			}

			ns.Modal.show(ns.options);
		};

		function require(module, callback) {
			if (!modules[module]) {
				modules[module] = {
					loaded: false
					,loading: false
					,callbacks: []
				};
			}
			modules[module].callbacks.push(callback);

			if (!modules[module].loaded && !modules[module].loading) {
				modules[module].loading = true;
				ns._debug('iLib::Including module '+module);

				var _callback = function() {
					modules[module].loaded = true;
					modules[module].loading = false;
					for (var i=0; i<modules[module].callbacks.length; i++)
						setTimeout(modules[module].callbacks[i], 500);
				}

				var host = /http:\/\//gi.test(module) ? '' : ns.host;
				var s = ns._('script');
				s.setAttribute('src', host+module+'.js?'+new Date().getTime());
				s.setAttribute('type', 'text/javascript');

				if (ns.Browser.msie && ns.Browser.version < 9) {
					s.onreadystatechange = function() {
						if (s.readyState == 'loaded')
							_callback();
						ns._debug('iLib::'+module+' loaded');
					}
				} else {
					if (_callback && ns.Event)
						new ns.Event(s, 'load', _callback);
				}

				ns.$$(document, 'head')[0].appendChild(s);
			}
		};

		/* public */
		var that = {
			_typeof: '[Object ThelisResa:iLib]'
			,toString: function() { return that._typeof; }

            ,MODE_MODAL: 'modal'
            ,MODE_BLANK: 'blank'

			,require: function(module, callback) { require(module, callback); }
			,call: function(url) { call(url); }

            ,isInitialized: function() { return initialized === true; }
		};

		return that;
	})();
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Debugger
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.Debugger = (function() {
		/* private */
        var CONSOLE_HEIGHT = 226;
		var initialized = 0;
		var sTime = new Date().getTime();
		var o = {};
		var logs = [];

		function init() {
			if (!ns.CSS || !ns.Draggable) {
				ns.iLib.require('CSS', function() { init(); });
				ns.iLib.require('Draggable', function() { init(); });
				return;
			}

            if (initialized < 1) {
                o.body = document.body || document.documentElement;
                if (!o.console) {
                    ns.CSS.set(o.body, 'padding-bottom', CONSOLE_HEIGHT+'px');

                    o.console = ns._('div');
                    ns.CSS.set(o.console, 'z-index', '100000000');
                    ns.CSS.set(o.console, 'position', 'fixed');
                    ns.CSS.set(o.console, 'bottom', '0px');
                    ns.CSS.set(o.console, 'left', '0px');
                    ns.CSS.set(o.console, 'width', '100%');
                    ns.CSS.set(o.console, 'height', CONSOLE_HEIGHT+'px');
                    ns.CSS.set(o.console, 'opacity', '25');
                    ns.CSS.set(o.console, 'background', '#073642');
                    ns.CSS.set(o.console, 'border-top', '4px double white');
                    ns.CSS.set(o.console, 'transition', 'all .6s ease');

                    o.text = ns._('div');
                    ns.CSS.set(o.text, 'z-index', '100000001');
                    ns.CSS.set(o.text, 'position', 'absolute');
                    ns.CSS.set(o.text, 'top', '0px');
                    ns.CSS.set(o.text, 'left', '0px');
                    ns.CSS.set(o.text, 'width', '100%');
                    ns.CSS.set(o.text, 'height', '100%');
                    ns.CSS.set(o.text, 'overflow-y', 'scroll');

                    o.enlarge = ns._('div');
                    ns.CSS.set(o.enlarge, 'z-index', '100000002');
                    ns.CSS.set(o.enlarge, 'position', 'absolute');
                    ns.CSS.set(o.enlarge, 'top', '-20px');
                    ns.CSS.set(o.enlarge, 'left', '50%');
                    ns.CSS.set(o.enlarge, 'width', '46px');
                    ns.CSS.set(o.enlarge, 'height', '16px');
                    ns.CSS.set(o.enlarge, 'margin-left', '-23px');
                    ns.CSS.set(o.enlarge, 'background-color', '#002b36');
                    ns.CSS.set(o.enlarge, 'border', '1px solid #002b36');
                    ns.CSS.set(o.enlarge, 'border-bottom', 'none');
                    ns.CSS.set(o.enlarge, 'border-radius', '10px 10px 0 0');

                    o.enlargeB = ns._('div');
                    ns.CSS.set(o.enlargeB, 'width', '16px');
                    ns.CSS.set(o.enlargeB, 'height', '16px');
                    ns.CSS.set(o.enlargeB, 'margin', 'auto');
                    ns.CSS.set(o.enlargeB, 'cursor', 'pointer');
                    ns.CSS.set(o.enlargeB, 'background-image', 'url(**images/iLib/upDownArrows.png)');
                    ns.CSS.set(o.enlargeB, 'background-position', '-16px 0');
                    ns.CSS.set(o.enlargeB, 'background-repeat', 'no-repeat');

                    o.enlarge.appendChild(o.enlargeB);
                    o.console.appendChild(o.enlarge);
                    o.console.appendChild(o.text);
                    o.body.appendChild(o.console);

                    new ns.Event(o.console, 'mouseover', function() {
                        ns.CSS.set(o.console, 'opacity', '100');
                    });
                    new ns.Event(o.console, 'mouseout', function() {
                        ns.CSS.set(o.console, 'opacity', '25');
                    });
                    new ns.Event(o.enlargeB, 'click', function() {
                        var bottom = parseInt(ns.CSS.get(o.console, 'bottom'));
                        if (!bottom || 0 == bottom)
                        {
                            ns.CSS.set(o.body, 'padding-bottom', '0');
                            ns.CSS.set(o.console, 'bottom', '-' + (o.console.offsetHeight - 8) + 'px');
                            ns.CSS.set(o.enlargeB, 'background-position', '0 0');
                        } else {
                            ns.CSS.set(o.body, 'padding-bottom', CONSOLE_HEIGHT+'px');
                            ns.CSS.set(o.console, 'bottom', '0');
                            ns.CSS.set(o.enlargeB, 'background-position', '-16px 0');
                        }
                    });
                }

                initialized = 1;
                append();
            }
		}

		function dock(top, left) {
			ns.CSS.set(o.console, top?'top':'bottom', '0px');
			ns.CSS.set(o.console, left?'left':'right', '0px');
			ns.CSS.set(o.console, !top?'top':'bottom', 'auto');
			ns.CSS.set(o.console, !left?'left':'right', 'auto');
		};

		function append(s, type) {
			if (s || s === false)
				s = s.toString();

			if (s && type && (initialized < 2 || !ns.CSS))
				logs.push([s, type]);

			if (initialized == 1 && ns.CSS) {
				initialized = 2;
				for (var i=0; i<logs.length; i++)
					append(logs[i][0], logs[i][1]);
			}

			if (initialized == 2 && s) {
				if (!type)
					type = 'log';

				var time = new Date().getTime() - sTime;
				if (time < 1000)
					time = time.toFixed(2) + 'ms';
				else
					time = new Number(time/1000).toFixed(2) + 's';

				try {
					var div = ns._('div');
					ns.CSS.set(div, 'position', 'relative');
					ns.CSS.set(div, 'padding-left', '85px');
					ns.CSS.set(div, 'padding-bottom', '2px');
					ns.CSS.set(div, 'margin-bottom', '2px');
					ns.CSS.set(div, 'border-bottom', '1px solid #3d3d3d');

					var bg, color = null;
					switch (type) {
						case 'log':
                            bg = null;
                            color = '93a1a1';
                            break;
						case 'todo':
                            bg = 'todo.png';
                            color = '268bd2';
                            break;
						case 'warn':
                            bg = 'warn.png';
                            color = 'cb4b16';
                            break;
						case 'error':
                            bg = 'error.png';
                            color = 'dc322f';
                            break;
					}
					if (bg)
						ns.CSS.set(div, 'background', 'url(**images/iLib/'+bg+') top left no-repeat');

					var spanTime = ns._('span');
					spanTime.innerHTML = time;
					ns.CSS.set(spanTime, 'display', 'block');
					ns.CSS.set(spanTime, 'position', 'absolute');
					ns.CSS.set(spanTime, 'top', '0px');
					ns.CSS.set(spanTime, 'left', '22px');
					ns.CSS.set(spanTime, 'width', '60px');
					ns.CSS.set(spanTime, 'color', '#657b83');
					ns.CSS.set(spanTime, 'font-size', '11px');
					ns.CSS.set(spanTime, 'font-family', 'Lucida console');

					var spanText = ns._('span');
					spanText.innerHTML = s.toString();
					ns.CSS.set(spanText, 'float', 'left');
					ns.CSS.set(spanText, 'color', '#'+color);
					ns.CSS.set(spanText, 'font-size', '11px');
					ns.CSS.set(spanText, 'font-family', 'Lucida console');
					ns.CSS.set(spanText, 'padding-left', '5px');
					ns.CSS.set(spanText, 'border-left', '1px solid white');

					var clear = ns._('div');
					ns.CSS.set(clear, 'clear', 'both');

					div.appendChild(spanTime);
					div.appendChild(spanText);
					div.appendChild(clear);

					if (ns.$$(o.text, 'div')[0])
						o.text.insertBefore(div, ns.$$(o.text, 'div')[0]);
					else
						o.text.appendChild(div);
				} catch (e) {
					alert(e);
				}
			}
		};

		function clear() {
			o.console.innerHTML = '';
		};

		/* public */
		var that = {
			_typeof: '[Object ThelisResa:Debugger]'
			,toString: function() { return that._typeof; }

			,init: function() { init(); }

			,log: function(s) { append(s, 'log'); }
			,error: function(s) { append(s, 'error'); }
			,warn: function(s) { append(s, 'warn'); }
			,todo: function(s) { append(s, 'todo'); }

			,clear: function() { clear(); }
		};
		return that;
	})();
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Option
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.Option = function() {
        var TYPE_CAMPING = '1';
        var TYPE_LOCATION = '2';

		var url = '';

		var assoc = {
			'language': 'lang',
			'custom.color1': 'custom-c1',
			'custom.color2': 'custom-c2',
			'search.camping': 'typecamping',
			'search.location': 'typelocation',
			'search.regions': 'regions[]',
			'search.sites': 'sites[]',
			'search.simple': 'simpleSearch',
			'search.month': 'simpleMonth',
			'search.day': 'simpleDay'
		};

		function isInternal(o) {
			return o == 'url'
				|| o == 'mode'
				|| o == '_typeof'
				|| o == 'search.niveau'
				|| o == 'custom.title'
                || o == 'custom.animation'
                || o.match('custom.header');
		};

		function co(b, i) {
			return assoc[b+i] ? assoc[b+i] : i;
		};

		function getUrl() {
			url = 'formulaire.php';
			if (that.search.niveau && that.search.niveau > 1) {
                if (that.search.niveau == 2) {
                    if (ns.SEARCH_MULTILIST === ns.search_page) {
                        url = 'multi-list.php';
                    } else {
                        url = 'list.php';
                    }
                } else {
                    url = 'cart-add.php';
                }
            }
			url += '?';
			urlize(that);
			if (ns.options.css_dev === true)
				url += '&CSS_DEV=1';
            if (ns.options.mode == ns.iLib.MODE_MODAL) {
                url += '&fromWidget=1';
                url += '&mode=modal';
            }
            if (ns.options.language != null) {
                url += '&lang=' + ns.options.language;
            }

			return ns.decorate(url);
		}

		function urlize(o, b) {
			if (!b) b = '';
			for (var i in o) {
				if (typeof o[i] != 'function' && !isInternal(b+i)) {
                    if (Object.prototype.toString.call(o[i]) === '[object Array]') {
                        for (var k=0; k<o[i].length; k++) {
                            url += '&' + i + '[]=' + o[i][k];
                        }
                    } else if (typeof o[i] == 'object') {
						urlize(o[i], b+i+'.');
					} else if (o[i] !== '' && o[i] !== null || o[i] === false) {
						p = co(b,i);
						if (p == 'duration' && that.search.simple===true)
							p = 'simpleDuration';
						if (String(o[i]) == 'true') o[i] = 1;
						if (String(o[i]) == 'false') o[i] = 0;
						if (/\[\]/.test(p)) {
							t = ns.trim(''+o[i]).split(',');
							for (j=0; j<t.length; j++)
								url += '&'+p+'='+encodeURIComponent(t[j]);
						} else {
							url += '&'+p+'='+encodeURIComponent(o[i]);
						}
					}
				}
			}
		};

		var that = {
			_typeof: '[Object ThelisResa:Option]'
			,toString: function() { return that._typeof; }

            ,TYPE_CAMPING: TYPE_CAMPING
            ,TYPE_LOCATION: TYPE_LOCATION

			,url: url
			,camping: ''
			,mode: 'modal'
			,language: null
			,color1: ''
			,color2: ''
            ,custom: {
                color1: null
                ,color2: null
                ,title: null
                ,animation: true
                ,header: {
                    background: null
                    ,color: 'white'
                    ,height: null
                    ,displayTitle: null
                    ,displayCartLink: null
                }
            }
			,search: {
				niveau: 1
				,simple: false
				,begin: ''
				,duration: 7
				,month: ''
				,day: ''
				,camping: false
				,location: true
                ,type: TYPE_LOCATION
				,regions: ''
				,sites: ''
				,idCategory: ''
			}

			,getUrl: function() { return getUrl(); }
		}

		return that;
	};
    ns.Option.TYPE_CAMPING = 1;
    ns.Option.TYPE_LOCATION = 2;
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * General options
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.options = new ThelisResa.Option();

	ns.options.debug = null;
	ns.options.button = null;
    ns.options.mode = ns.iLib.MODE_MODAL;
	ns.options.favorites = {
		layer: null
		,quantity: 3
		,sites: null
        ,type: null
        ,oldSchool: false
		,noStyle: false
	};
	ns.options.directPayment = {
		layer: null
		,noStyle: false
	};
	ns.options.searchEngine = {
		layer: null
		,noStyle: false
	};
	ns.options.accommodations = {
		layer: null
		,idsite: null
		,noStyle: false
		,polaroid: true
	};
    ns.options.custom = {
        color1: null
        ,color2: null
        ,title: null
        ,animation: true
        ,header: {
            background: null
            ,color: 'white'
            ,height: 45
            ,displayTitle: true
            ,displayCartLink: true
        }
    };
	ns.options.trackingAction = null;
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * ThelisResa.SimpleBlock
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */

(function(ns) {
    ns.SimpleBlockWaiter = function(id) {
        var _id = id;
        var _category = null;
        var _month = new Date().getMonth()+1;
        var _duration = null;
        var _day = 'saturday';
        var _site = null;
        var _description = null;
        var _noStyle = false;
        var _emptyCart = false;
        var _moreCriteriaDisplay = false;
        var _otherDurationsButton = false;
        var _events = [];

        var _initialized = false;

        /* constructor */
        (function() {
            /* */
        })();

        function display() {
            if (!ns.iLib.isInitialized()) {
                setTimeout(display, 250);
                return;
            }

            if (!_initialized)
            {
                new ns.Waiter(ns.Waiter.HTML_ELEMENT, _id, function(element) {
                    _initialized = true;
                    display(true);
                });
            }
            else
            {
                try {
                    if (!ns.$(_id))
                        throw 'layer (#'+_id+') does not exists on DOM';

                    if (!_category)
                        throw 'category was not set';

                    var callback = function() {
                        var simple = new ns.SimpleBlockImpl(_id, _category)
                            .setMonth(_month)
                            .setDuration(_duration)
                            .setDay(_day)
                            .setSite(_site)
                            .setDescription(_description)
                            .setNoStyle(_noStyle)
                            .setEmptyCart(_emptyCart)
                            .setMoreCriteriaDisplay(_moreCriteriaDisplay)
                            .setOtherDurationsButton(_otherDurationsButton)
                            .setEvents(_events)
                            .init();
                    };

                    if (!ns.SimpleBlockImpl) {
                        ns.iLib.require('SimpleBlockImpl', callback);
                        return;
                    } else {
                        callback();
                    }
                } catch (e) {
                    ns._error('iLib::SimpBlock '+e);
                    return;
                }
            }
        };

        var that = {
			_typeof: '[Object ThelisResa:SimpleBlockWaiter]'
			,toString: function() { return that._typeof; }

            ,category: function(category) { _category = category; return that; }
            ,month: function(month) { _month = month; return that; }
            ,duration: function(duration) { _duration = duration; return that; }
            ,day: function(day) { _day = day; return that; }
            ,site: function(site) { _site = site; return that; }
            ,description: function(description) { _description = description; return that; }
            ,noStyle: function(noStyle) { _noStyle = noStyle; return that; }
            ,emptyCart: function(emptyCart) { _emptyCart = emptyCart; return that; }
            ,moreCriteriaDisplay: function(moreCriteriaDisplay) { _moreCriteriaDisplay = moreCriteriaDisplay; return that; }
            ,otherDurationsButton: function(otherDurationsButton) { _otherDurationsButton = otherDurationsButton; return that; }
            ,on: function(event, callback) { _events[event] = callback; return that; }

            ,display: function() { display(); }
        };

        return that;
    };
})(ThelisResa);

(function(ns) {
    ns.SimpleBlock = (function() {
        var objects = [];

        /* todo */
        //ns._todo('Ajouter une image + avec le "plus de criteres"');

        function id(_id) {
            var waiter = new ns.SimpleBlockWaiter(_id);
            objects.push(waiter);

            return waiter;
        }

        var that = {
			_typeof: '[Object ThelisResa:SimpleBlock]'
			,toString: function() { return that._typeof; }

            ,id: function(_id) { return id(_id); }
        };

        return that;
    })();
}(ThelisResa));

/* --------------------------------------------------------------------------------------------------------------------------------------------
 * Fix for ThelisResa.Modal.show() old version and mobile versions
 * --------------------------------------------------------------------------------------------------------------------------------------------
 */
(function(ns) {
	ns.Modal = (function() {

		function show(option, fromUrl) {
            if (ns.options.mode == ns.iLib.MODE_BLANK) {
                var url = fromUrl ? option : option.getUrl();

                if (!/\?/.test(url)) {
                    url += '?';
                }
                url += ns.sess;
                if (!/(\?|&)camping=/.test(url)) {
                    url += '&camping='+ns.options.camping;
                }

                url = (url.match(ns.base) ? '' : ns.base) + url;

                window.open(ns.decorate(url));

                return true;
            }

			if (ns.Modal.oldVersion) {
				ns.iLib.require('Modal', function() { ns.Modal.show(option, fromUrl); });
				return;
			}
		};

		/* public */
		var that = {
			_typeof: '[Object ThelisResa:OldModal]'
			,toString: function() { return that._typeof; }

			,oldVersion: true

			,show: function(option, fromUrl) { show(option, fromUrl); }
		};

		return that;
	})();
}(ThelisResa));
