<?php
mt_srand();
$index = mt_rand(1, 9999);
$front_app = __('front_app', true);
$theme = isset($_GET['theme']) ? $_GET['theme'] : $tpl['option_arr']['o_theme'];
?>
<div id="pjWrapperEquipment_<?php echo $theme;?>">
	<div id="erContainer_<?php echo $index; ?>" class="container-fluid pjErContainer"></div>
	<?php
	$front_app = __('front_app', true); 
	?>		
	<div class="modal fade" id="pjErClearTimeModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  	<div class="modal-dialog" role="document">
	    	<div class="modal-content">
	      		<div class="modal-body">
	        		<?php echo $front_app['clear_body'];?>
	      		</div>
	      		<div class="modal-footer">
	        		<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $front_app['btn_cancel'];?></button>
	        		<button type="button" class="btn btn-primary pjErClearTimeConfirm" data-type="items"><?php echo $front_app['btn_ok'];?></button>
	      		</div>
	    	</div>
	  	</div>
	</div>
	<div class="modal fade" id="pjErWrongPeriodModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  	<div class="modal-dialog" role="document">
	    	<div class="modal-content">
	      		<div class="modal-body">
	        		<?php echo $front_app['wrong_period_content'];?>
	      		</div>
	      		<div class="modal-footer">
	        		<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $front_app['btn_ok'];?></button>
	      		</div>
	    	</div>
	  	</div>
	</div>
	<?php
	if (isset($tpl['terms_arr']) && !empty($tpl['terms_arr']))
	{
		?>
		<div class="modal fade pjTbModal" id="pjCpbTermModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  	<div class="modal-dialog">
		    	<div class="modal-content">
		      		<div class="modal-header">
		        		<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?php __('front_btn_close');?></span></button>
		        		<h4 class="modal-title pjTbModalTitle" id="myModalLabel"><?php __('front_terms', false, true); ?></h4>
		      		</div>
				    <div class="modal-body">
				    	<?php echo nl2br(pjSanitize::html(@$tpl['terms_arr']['terms_body'])); ?>
				    </div>
			      	<div class="modal-footer">
			        	<button type="button" class="btn btn-default pjTbBtn pjTbBtnPrimary" data-dismiss="modal"><?php __('gridBtnOk');?></button>
			      	</div>
		    	</div>
		  	</div>
		</div>
		<?php
	} 
	?>
</div><!-- /#pjWrapper -->

<script type="text/javascript">
var pjQ = pjQ || {},
	EquipmentRent_<?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.call(null, 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.call(null, url, "js", callback);
	},
	loadCss = function (url, callback) {
		loadRemote.call(null, url, "css", callback);
	},
	isMSIE = function() {
		var ua = window.navigator.userAgent,
        	msie = ua.indexOf("MSIE ");

        if (msie !== -1) {
            return true;
        }

		return false;
	},
	getSessionId = function () {
		return sessionStorage.getItem("session_id") == null ? "" : sessionStorage.getItem("session_id");
	},
	createSessionId = function () {
		if(getSessionId()=="") {
			sessionStorage.setItem("session_id", "<?php echo session_id(); ?>");
		}
	},
	options = {
		server: "<?php echo PJ_INSTALL_URL; ?>",
		folder: "<?php echo PJ_INSTALL_URL; ?>",
		index: <?php echo $index; ?>,
		locale: <?php echo isset($_GET['locale']) && (int) $_GET['locale'] > 0 ? (int) $_GET['locale'] : 'null'; ?>,
		direction: "<?php echo isset($_GET['dir']) ? $_GET['dir'] : $controller->getDirection(); ?>",
		daysOff: <?php echo pjAppController::jsonEncode($tpl['days_off']); ?>,
		datesOff: <?php echo pjAppController::jsonEncode($tpl['dates_off']); ?>,
		datesOn: <?php echo pjAppController::jsonEncode($tpl['dates_on']); ?>,
		defaultTimes: <?php echo pjAppController::jsonEncode($tpl['default_times']); ?>,
		customTimes: <?php echo pjAppController::jsonEncode($tpl['custom_times']); ?>,
		fields: <?php echo pjAppController::jsonEncode(__('front_app', true)); ?>
	};
	<?php
	$dm = new pjDependencyManager(PJ_INSTALL_PATH, PJ_THIRD_PARTY_PATH);
	$dm->load(PJ_CONFIG_PATH . 'dependencies.php')->resolve();
	?>
	loadScript("<?php echo PJ_INSTALL_URL . $dm->getPath('storage_polyfill'); ?>storagePolyfill.min.js", function () {
		if (isSafari) {
			createSessionId();
			options.session_id = getSessionId();
		}else{
			options.session_id = "";
		}
		loadScript("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_jquery'); ?>pjQuery.min.js", function () {
			loadScript("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_validate'); ?>pjQuery.validate.min.js", function () {
				loadScript("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_jquery_ui'); ?>js/pjQuery-ui.custom.min.js", function () {
					loadScript("<?php echo PJ_INSTALL_URL . $dm->getPath('pj_bootstrap'); ?>pjQuery.bootstrap.min.js", function () {
						loadScript("<?php echo PJ_INSTALL_URL . PJ_JS_PATH; ?>pjEquipmentRent.js", function () {
							EquipmentRent_<?php echo $index; ?> = new EquipmentRent(options);
						});
					});
				});
			});
		});
	});
})();
</script>