<?php if(!defined('master_script')) die(); ?>

<!-- EXTENSION: widgets -->

<div id="widgets">
	<?php
		$default_left = 400;
		$default_top = 400;
		$widget_default_top = 50;
	?>
	
	<?php foreach($widgets as $widget) if(file_exists($system_data['folder']['widgets'] . $widget . '/index.php')) { ?>
		<?php
			$widget_config_file = $system_data['folder']['settings'] . 'widgets/' . $widget . '/config.php';
			file_exists($widget_config_file) ? include($widget_config_file) : $widget_config = array();
			$widget_position = isset($widget_config['position']) ? explode(',', $widget_config['position']) : array($widget_default_top, $default_left);
			$widget_default_top = $widget_default_top + $default_top;
		?>
		
		<div class="widget" id="<?php echo $widget; ?>" style="position: absolute; top: <?php echo $widget_position[0]; ?>px; left: <?php echo $widget_position[1]; ?>px">
			<div class="handle"></div>
			<?php include($system_data['folder']['widgets'] . $widget . '/index.php'); ?>
		</div>
	<?php } ?>
</div>

<script type="text/javascript">
    var saveWidgetPosition = function(element) {
    	var widgetConfigFile = 'index.php?backend&actions=widget-config.php&name=' + element.id + '&left=' + element.offsetLeft + '&top=' + element.offsetTop;
		var widgetConfig = MoreCSS.createXMLHttpRequest();
		widgetConfig.open('get', noCache(widgetConfigFile), true);
		widgetConfig.send(null);
    };
</script>
