<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/module.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
	  
		<table class="form">
          <tr>
            <td><?php echo $entry_product; ?></td>
            <td><input type="text" name="product" value="" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><div id="special-deal-featured-product" class="scrollbox">
                <?php $class = 'odd'; ?>
                <?php foreach ($products as $product) { ?>
                <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                <div id="special-deal-featured-product<?php echo $product['product_id']; ?>" class="<?php echo $class; ?>"><?php echo $product['name'] . ' ' . $product['special_status']; ?> <img src="view/image/delete.png" alt="" />
                  <input type="hidden" value="<?php echo $product['product_id']; ?>" />
                </div>
                <?php } ?>
              </div>
              <input type="hidden" name="special_deal_featured_product" value="<?php echo $special_deal_featured_product; ?>" /></td>
          </tr>
        </table>
		
        <div class="vtabs">
          <?php $module_row = 1; ?>
          <?php foreach ($modules as $module) { ?>
          <a href="#tab-module-<?php echo $module_row; ?>" id="module-<?php echo $module_row; ?>"><?php echo $tab_module . ' ' . $module_row; ?>&nbsp;<img src="view/image/delete.png" alt="" onclick="$('.vtabs a:first').trigger('click'); $('#module-<?php echo $module_row; ?>').remove(); $('#tab-module-<?php echo $module_row; ?>').remove(); return false;" /></a>
          <?php $module_row++; ?>
          <?php } ?>
          <span id="module-add"><?php echo $button_add_module; ?>&nbsp;<img src="view/image/add.png" alt="" onclick="addModule();" /></span> </div>
        <?php $module_row = 1; ?>
        <?php foreach ($modules as $module) { ?>
        <div id="tab-module-<?php echo $module_row; ?>" class="vtabs-content">
          <table class="form">
            <tr>
			  <td><span class="required">* </span><?php echo $entry_limit; ?></td>
			  <td><input type="text" name="special_deal_module[<?php echo $module_row; ?>][limit]" value="<?php echo $module['limit']; ?>" size="3" />
			  <?php if (isset($error_limit[$module_row])) { ?>
                <span class="error"><?php echo $error_limit[$module_row]; ?></span>
                <?php } ?>
			  </td>
			</tr>
			<tr>
			  <td><span class="required">* </span><?php echo $entry_image; ?></td>
			  <td>
				 <input type="text" name="special_deal_module[<?php echo $module_row; ?>][image_width]" value="<?php echo $module['image_width']; ?>" size="3" />
				 <input type="text" name="special_deal_module[<?php echo $module_row; ?>][image_height]" value="<?php echo $module['image_height']; ?>" size="3" />
				 <?php if (isset($error_image[$module_row])) { ?>
                 <span class="error"><?php echo $error_image[$module_row]; ?></span>
                 <?php } ?>
			  </td>
			</tr>
			<tr>
              <td><?php echo $entry_display_type; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][display_type]">
                  <?php if ($module['display_type']) { ?>
                  <option value="1" selected="selected"><?php echo $text_display_normal; ?></option>
                  <option value="0"><?php echo $text_display_popup; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_display_normal; ?></option>
                  <option value="0" selected="selected"><?php echo $text_display_popup; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
			<tr>
              <td><span class="required">* </span><?php echo $entry_popup_no_times; ?></td>
              <td><input type="text" name="special_deal_module[<?php echo $module_row; ?>][popup_no_times]" value="<?php echo $module['popup_no_times']; ?>" size="3" />
			  <?php if (isset($error_popup_no_times[$module_row])) { ?>
              <span class="error"><?php echo $error_popup_no_times[$module_row]; ?></span>
              <?php } ?>
			  </td>
			</tr>
			<tr>
              <td><?php echo $entry_random_deal; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][random_deal]">
                  <?php if ($module['random_deal']) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
			<tr>
              <td><?php echo $entry_slide_type; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][slide_type]">
                  <?php if ($module['slide_type']) { ?>
                  <option value="1" selected="selected"><?php echo $text_slide_auto; ?></option>
                  <option value="0"><?php echo $text_slide_manual; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_slide_auto; ?></option>
                  <option value="0" selected="selected"><?php echo $text_slide_manual; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
			<tr>
			  <td><span class="required">* </span><?php echo $entry_slide_time; ?></td>
			  <td><input type="text" name="special_deal_module[<?php echo $module_row; ?>][slide_time]" value="<?php echo $module['slide_time']; ?>" size="3" />
			  <?php if (isset($error_slide_time[$module_row])) { ?>
              <span class="error"><?php echo $error_slide_time[$module_row]; ?></span>
              <?php } ?>
			  </td>
			</tr>
			<tr>
              <td><?php echo $entry_buy_now_behaviour; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][buy_now_behaviour]">
                  <?php if ($module['buy_now_behaviour']) { ?>
                  <option value="1" selected="selected"><?php echo $text_goto_product_page; ?></option>
                  <option value="0"><?php echo $text_add_product_to_cart; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_goto_product_page; ?></option>
                  <option value="0" selected="selected"><?php echo $text_add_product_to_cart; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
			<tr>
              <td><?php echo $entry_layout; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][layout_id]">
                  <?php foreach ($layouts as $layout) { ?>
                  <?php if ($layout['layout_id'] == $module['layout_id']) { ?>
                  <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select></td>
            </tr>
            <tr>
              <td><?php echo $entry_position; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][position]">
                  <?php if ($module['position'] == 'content_top') { ?>
                  <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
                  <?php } else { ?>
                  <option value="content_top"><?php echo $text_content_top; ?></option>
                  <?php } ?>
                  <?php if ($module['position'] == 'content_bottom') { ?>
                  <option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
                  <?php } else { ?>
                  <option value="content_bottom"><?php echo $text_content_bottom; ?></option>
                  <?php } ?>
                  <?php if ($module['position'] == 'column_left') { ?>
                  <option value="column_left" selected="selected"><?php echo $text_column_left; ?></option>
                  <?php } else { ?>
                  <option value="column_left"><?php echo $text_column_left; ?></option>
                  <?php } ?>
                  <?php if ($module['position'] == 'column_right') { ?>
                  <option value="column_right" selected="selected"><?php echo $text_column_right; ?></option>
                  <?php } else { ?>
                  <option value="column_right"><?php echo $text_column_right; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
            <tr>
              <td><?php echo $entry_status; ?></td>
              <td><select name="special_deal_module[<?php echo $module_row; ?>][status]">
                  <?php if ($module['status']) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
            </tr>
            <tr>
              <td><?php echo $entry_sort_order; ?></td>
              <td><input type="text" name="special_deal_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /></td>
            </tr>
          </table>
        </div>
        <?php $module_row++; ?>
        <?php } ?>
      </form>
    </div>
  </div>
</div>

<script type="text/javascript"><!--
$('input[name=\'product\']').autocomplete({
	delay: 500,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=module/special_deal/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.name,
						value: item.product_id
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('#special-deal-featured-product' + ui.item.value).remove();
		
		$('#special-deal-featured-product').append('<div id="special-deal-featured-product' + ui.item.value + '">' + ui.item.label + '<img src="view/image/delete.png" alt="" /><input type="hidden" value="' + ui.item.value + '" /></div>');

		$('#special-deal-featured-product div:odd').attr('class', 'odd');
		$('#special-deal-featured-product div:even').attr('class', 'even');
		
		data = $.map($('#special-deal-featured-product input'), function(element){
			return $(element).attr('value');
		});
						
		$('input[name=\'special_deal_featured_product\']').attr('value', data.join());
					
		return false;
	},
	focus: function(event, ui) {
      	return false;
   	}
});

$('#special-deal-featured-product div img').live('click', function() {
	$(this).parent().remove();
	
	$('#special-deal-featured-product div:odd').attr('class', 'odd');
	$('#special-deal-featured-product div:even').attr('class', 'even');

	data = $.map($('#special-deal-featured-product input'), function(element){
		return $(element).attr('value');
	});
					
	$('input[name=\'special_deal_featured_product\']').attr('value', data.join());	
});
//--></script> 
<script type="text/javascript"><!--
var module_row = <?php echo $module_row; ?>;

function addModule() {	
	html  = '<div id="tab-module-' + module_row + '" class="vtabs-content">';
	html += '  <table class="form">';
	html += '    <tr>';
	html += '      <td><span class="required">* </span><?php echo $entry_limit; ?></td>';
	html += '      <td><input type="text" name="special_deal_module[' + module_row + '][limit]" value="5" size="3" /></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><span class="required">* </span><?php echo $entry_image; ?></td>';
	html += '      <td><input type="text" name="special_deal_module[' + module_row + '][image_width]" value="200" size="3" /> x <input type="text" name="special_deal_module[' + module_row + '][image_height]" value="200" size="3" /></td>';
	html += '    </tr>';
	html += '    </tr>';
	html += '      <td><?php echo $entry_display_type; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][display_type]">';
	html += '        <option value="1"><?php echo $text_display_normal; ?></option>';
	html += '        <option value="0" selected="selected"><?php echo $text_display_popup; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><span class="required">* </span><?php echo $entry_popup_no_times; ?></td>';
	html += '      <td><input type="text" name="special_deal_module[' + module_row + '][popup_no_times]" value="1" size="3" /></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><?php echo $entry_random_deal; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][random_deal]">';
	html += '        <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
	html += '        <option value="0"><?php echo $text_disabled; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    </tr>';
	html += '      <td><?php echo $entry_slide_type; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][slide_type]">';
	html += '        <option value="1" selected="selected"><?php echo $text_slide_auto; ?></option>';
	html += '        <option value="0"><?php echo $text_slide_manual; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><span class="required">* </span><?php echo $entry_slide_time; ?></td>';
	html += '      <td><input type="text" name="special_deal_module[' + module_row + '][slide_time]" value="8" size="3" /></td>';
	html += '    </tr>';
	html += '      <td><?php echo $entry_buy_now_behaviour; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][buy_now_behaviour]">';
	html += '        <option value="1" selected="selected"><?php echo $text_goto_product_page; ?></option>';
	html += '        <option value="0"><?php echo $text_add_product_to_cart; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><?php echo $entry_layout; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][layout_id]">';
	<?php foreach ($layouts as $layout) { ?>
	html += '           <option value="<?php echo $layout['layout_id']; ?>"><?php echo addslashes($layout['name']); ?></option>';
	<?php } ?>
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><?php echo $entry_position; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][position]">';
	html += '        <option value="content_top"><?php echo $text_content_top; ?></option>';
	html += '        <option value="content_bottom"><?php echo $text_content_bottom; ?></option>';
	html += '        <option value="column_left"><?php echo $text_column_left; ?></option>';
	html += '        <option value="column_right"><?php echo $text_column_right; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><?php echo $entry_status; ?></td>';
	html += '      <td><select name="special_deal_module[' + module_row + '][status]">';
	html += '        <option value="1"><?php echo $text_enabled; ?></option>';
	html += '        <option value="0"><?php echo $text_disabled; ?></option>';
	html += '      </select></td>';
	html += '    </tr>';
	html += '    <tr>';
	html += '      <td><?php echo $entry_sort_order; ?></td>';
	html += '      <td><input type="text" name="special_deal_module[' + module_row + '][sort_order]" value="" size="3" /></td>';
	html += '    </tr>';
	html += '  </table>'; 
	html += '</div>';
	
	$('#form').append(html);
	
	$('#module-add').before('<a href="#tab-module-' + module_row + '" id="module-' + module_row + '"><?php echo $tab_module; ?> ' + module_row + '&nbsp;<img src="view/image/delete.png" alt="" onclick="$(\'.vtabs a:first\').trigger(\'click\'); $(\'#module-' + module_row + '\').remove(); $(\'#tab-module-' + module_row + '\').remove(); return false;" /></a>');
	
	$('.vtabs a').tabs();
	
	$('#module-' + module_row).trigger('click');
	
	module_row++;
}
//--></script> 
<script type="text/javascript"><!--
$('.vtabs a').tabs();
//--></script> 
<?php echo $footer; ?>