<?php
mt_srand();
$index = mt_rand(1, 9999);
$theme = isset($_GET['theme']) ? $_GET['theme'] : $tpl['option_arr']['o_theme'];
?>
<div id="cmContainer_<?php echo $index; ?>" class="wrapper"></div>
<script type="text/javascript">
var pjQ = pjQ || {},
	CallbackWidget_<?php echo $index; ?>;
(function () {
	"use strict";
	var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor),

	loadCssHack = function(url, callback){
		var link = document.createElement('link');
		link.type = 'text/css';
		link.rel = 'stylesheet';
		link.href = url;

		document.getElementsByTagName('head')[0].appendChild(link);

		var img = document.createElement('img');
		img.onerror = function(){
			if (callback && typeof callback === "function") {
				callback();
			}
		};
		img.src = url;
	},
	loadRemote = function(url, type, callback) {
		if (type === "css" && isSafari) {
			loadCssHack(url, callback);
			return;
		}
		var _element, _type, _attr, scr, s, element;
		
		switch (type) {
		case 'css':
			_element = "link";
			_type = "text/css";
			_attr = "href";
			break;
		case 'js':
			_element = "script";
			_type = "text/javascript";
			_attr = "src";
			break;
		}
		
		scr = document.getElementsByTagName(_element);
		s = scr[scr.length - 1];
		element = document.createElement(_element);
		element.type = _type;
		if (type == "css") {
			element.rel = "stylesheet";
		}
		if (element.readyState) {
			element.onreadystatechange = function () {
				if (element.readyState == "loaded" || element.readyState == "complete") {
					element.onreadystatechange = null;
					if (callback && typeof callback === "function") {
						callback();
					}
				}
			};
		} else {
			element.onload = function () {
				if (callback && typeof callback === "function") {
					callback();
				}
			};
		}
		element[_attr] = url;
		s.parentNode.insertBefore(element, s.nextSibling);
	},
	loadScript = function (url, callback) {
		loadRemote(url, "js", callback);
	},
	loadCss = function (url, callback) {
		loadRemote(url, "css", callback);
	},
	loadScriptOrContinue = function (url, check, callback) {
		var result;
		if (typeof check === "function") {
			result = check.call(null);
		} else {
			result = check;
		}
		if (result) {
			loadScript(url, callback);
		} else if (typeof callback === "function") {
			callback.call(null);
		}
	},
	options = {
		server: "<?php echo PJ_INSTALL_URL; ?>",
		folder: "<?php echo PJ_INSTALL_URL; ?>",
		theme: "<?php echo $theme;?>",
		index: <?php echo $index; ?>
	};
	<?php
	$dm = new pjDependencyManager(PJ_INSTALL_PATH, PJ_THIRD_PARTY_PATH);
	$dm->load(PJ_CONFIG_PATH . 'dependencies.php')->resolve();
	?>
	loadScriptOrContinue("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_jquery'); ?>pjQuery.min.js", function () {
		if (typeof window.pjQ.$ === "undefined") {
			return true;
		}
		return false;
	}, function () {
		loadScriptOrContinue("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_jquery_ui'); ?>js/pjQuery-ui.custom.min.js", function () {
			if (typeof window.pjQ.$.ui === "undefined" || typeof window.pjQ.$.ui.datepicker === "undefined") {
				return true;
			}
			return false;
		}, function () {
			loadScriptOrContinue("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_datetimepicker'); ?>pjQuery-ui-timepicker-addon.js", function () {
				if (typeof window.pjQ.$.ui.timepicker === "undefined") {
					return true;
				}
				return false;
			}, function () {
				loadScriptOrContinue("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_validate'); ?>pjQuery.validate.min.js", function () {
					if (typeof window.pjQ.$.fn.validate === "undefined") {
						return true;
					}
					return false;
				}, function () {
					loadScriptOrContinue("<?php echo PJ_INSTALL_URL . PJ_JS_PATH; ?>pjCallbackWidget.js", function () {
						if (typeof window.CallbackWidget === "undefined") {
							return true;
						}
						return false;
					}, function () {
						CallbackWidget_<?php echo $index; ?> = new CallbackWidget(options);
					});
				});
			});
		});
	});
})();
</script>