<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  $slideshow_banner_extension = tep_banner_image_extension();

  if (tep_not_null($action)) {
    switch ($action) {
      case 'setflag':
        if ( ($HTTP_GET_VARS['flag'] == '0') || ($HTTP_GET_VARS['flag'] == '1') ) {
          tep_set_slideshow_banner_status($HTTP_GET_VARS['bID'], $HTTP_GET_VARS['flag']);

          $messageStack->add_session(SUCCESS_SLIDER_BANNER_STATUS_UPDATED, 'success');
        } else {
          $messageStack->add_session(ERROR_UNKNOWN_STATUS_FLAG, 'error');
        }

        tep_redirect(tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $HTTP_GET_VARS['bID']));
        break;
      case 'insert':
      case 'update':
        if (isset($HTTP_POST_VARS['slideshow_banners_id'])) $slideshow_banners_id = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_id']);
        $slideshow_banners_title = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_title']);
        $slideshow_banners_url = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_url']);
        $new_slideshow_banners_group = tep_db_prepare_input($HTTP_POST_VARS['new_slideshow_banners_group']);
        $slideshow_banners_group = (empty($new_slideshow_banners_group)) ? tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_group']) : $new_slideshow_banners_group;
        $slideshow_banners_image_local = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_image_local']);
        $slideshow_banners_target = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_target']);
				$slideshow_banners_image_thumb = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_image_thumb']);
        $db_image_location = '';
        $expires_date = tep_db_prepare_input($HTTP_POST_VARS['expires_date']);
        $expires_impressions = tep_db_prepare_input($HTTP_POST_VARS['expires_impressions']);
        $date_scheduled = tep_db_prepare_input($HTTP_POST_VARS['date_scheduled']);
				
				$slideshow_banners_field_1 = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_1']);
				$slideshow_banners_field_2 = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_2']);
				$slideshow_banners_field_3 = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_3']);
				$slideshow_banners_field_4 = tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_4']);

        $banner_error = false;
        if (empty($slideshow_banners_title)) {
          $messageStack->add(ERROR_SLIDER_BANNER_TITLE_REQUIRED, 'error');
          $banner_error = true;
        }

        if (empty($slideshow_banners_group)) {
          $messageStack->add(ERROR_SLIDER_BANNER_GROUP_REQUIRED, 'error');
          $banner_error = true;
        }

        if (empty($slideshow_banners_target)) {
          if (empty($slideshow_banners_image_local)) {
            $slideshow_banners_image = new upload('slideshow_banners_image');
            $slideshow_banners_image->set_destination(DIR_FS_CATALOG_IMAGES . $slideshow_banners_target);
            if ( ($slideshow_banners_image->parse() == false) || ($slideshow_banners_image->save() == false) ) {
              $banner_error = true;
            }
          }
        }

        if ($banner_error == false) {
          $db_image_location = (tep_not_null($slideshow_banners_image_local)) ? $slideshow_banners_image_local : $slideshow_banners_target . $slideshow_banners_image->filename;
          $sql_data_array = array('slideshow_banners_title' => $slideshow_banners_title,
																	'slideshow_banners_url' => $slideshow_banners_url,
                                  'slideshow_banners_image' => $db_image_location,
                                  'slideshow_banners_group' => $slideshow_banners_group,
								//  'slideshow_banners_target' => $slideshow_banners_target,
                                  'expires_date' => 'null',
                                  'expires_impressions' => 0,
                                  'date_scheduled' => 'null');
          if ($action == 'insert') {
            $insert_sql_data = array('date_added' => 'now()',
                                     'status' => '1');

            $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

            tep_db_perform(TABLE_SLIDER_BANNERS, $sql_data_array);

            $slideshow_banners_id = tep_db_insert_id();

            $messageStack->add_session(SUCCESS_SLIDER_BANNER_INSERTED, 'success');
          } elseif ($action == 'update') {
            tep_db_perform(TABLE_SLIDER_BANNERS, $sql_data_array, 'update', "slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");

            $messageStack->add_session(SUCCESS_SLIDER_BANNER_UPDATED, 'success');
          }


					$languages = tep_get_languages();
						for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
							$language_id = $languages[$i]['id'];
							$sql_data_array = array('slideshow_banners_field_1' => tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_1'][$language_id]),
																			'slideshow_banners_field_2' => tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_2'][$language_id]),
																			'slideshow_banners_field_3' => tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_3'][$language_id]),
																			'slideshow_banners_field_4' => tep_db_prepare_input($HTTP_POST_VARS['slideshow_banners_field_4'][$language_id]));
		
						if ($action == 'insert') {
								$insert_sql_data = array('slideshow_banners_id' => $slideshow_banners_id,
																				 'language_id' => $language_id['id']);
		
							$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
						 
								tep_db_perform(TABLE_SLIDER_BANNERS_DESCRIPTION, $sql_data_array);
						//		$messageStack->add_session(SUCCESSR_SLIDER_SLIDER_BANNER_UPDATED, 'success');
						} elseif ($action == 'update') {
								 tep_db_perform(TABLE_SLIDER_BANNERS_DESCRIPTION, $sql_data_array, 'update', "slideshow_banners_id = '" . (int)$slideshow_banners_id . "' and language_id = '" . (int)$languages[$i]['id'] . "'");
						//		$messageStack->add_session(SUCCESSR_SLIDER_SLIDER_BANNER_UPDATED, 'success');
						}
        }

          if (tep_not_null($expires_date)) {
            $expires_date = substr($expires_date, 0, 4) . substr($expires_date, 5, 2) . substr($expires_date, 8, 2);

            tep_db_query("update " . TABLE_SLIDER_BANNERS . " set expires_date = '" . tep_db_input($expires_date) . "', expires_impressions = null where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
          } elseif (tep_not_null($expires_impressions)) {
            tep_db_query("update " . TABLE_SLIDER_BANNERS . " set expires_impressions = '" . tep_db_input($expires_impressions) . "', expires_date = null where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
          }

          if (tep_not_null($date_scheduled)) {
            $date_scheduled = substr($date_scheduled, 0, 4) . substr($date_scheduled, 5, 2) . substr($date_scheduled, 8, 2);

            tep_db_query("update " . TABLE_SLIDER_BANNERS . " set status = '0', date_scheduled = '" . tep_db_input($date_scheduled) . "' where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
          }

          tep_redirect(tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, (isset($HTTP_GET_VARS['page']) ? 'page=' . $HTTP_GET_VARS['page'] . '&' : '') . 'bID=' . $slideshow_banners_id));
        } else {
          $action = 'new';
        }
        break;
      case 'deleteconfirm':
        $slideshow_banners_id = tep_db_prepare_input($HTTP_GET_VARS['bID']);

        if (isset($HTTP_POST_VARS['delete_image']) && ($HTTP_POST_VARS['delete_image'] == 'on')) {
          $banner_query = tep_db_query("select slideshow_banners_image from " . TABLE_SLIDER_BANNERS . " where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
          $banner = tep_db_fetch_array($banner_query);

          if (is_file(DIR_FS_CATALOG_IMAGES . 'slider/'. $banner['slideshow_banners_image'])) {
            if (tep_is_writable(DIR_FS_CATALOG_IMAGES . 'slider/'. $banner['slideshow_banners_image'])) {
              unlink(DIR_FS_CATALOG_IMAGES . 'slider/'. $banner['slideshow_banners_image']);
            } else {
              $messageStack->add_session(ERROR_IMAGE_IS_NOT_WRITEABLE, 'error');
            }
          } else {
            $messageStack->add_session(ERROR_IMAGE_DOES_NOT_EXIST, 'error');
          }
        }

        tep_db_query("delete from " . TABLE_SLIDER_BANNERS . " where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");
        tep_db_query("delete from " . TABLE_SLIDER_SLIDER_BANNERS_HISTORY . " where slideshow_banners_id = '" . (int)$slideshow_banners_id . "'");

        if (function_exists('imagecreate') && tep_not_null($banner_extension)) {
          if (is_file(DIR_WS_IMAGES . 'graphs/slideshow_banner_infobox-' . $slideshow_banners_id . '.' . $banner_extension)) {
            if (tep_is_writable(DIR_WS_IMAGES . 'graphs/slideshow_banner_infobox-' . $slideshow_banners_id . '.' . $banner_extension)) {
              unlink(DIR_WS_IMAGES . 'graphs/slideshow_banner_infobox-' . $slideshow_banners_id . '.' . $banner_extension);
            }
          }

          if (is_file(DIR_WS_IMAGES . 'graphs/slideshow_banner_yearly-' . $slideshow_banners_id . '.' . $banner_extension)) {
            if (tep_is_writable(DIR_WS_IMAGES . 'graphs/slideshow_banner_yearly-' . $slideshow_banners_id . '.' . $banner_extension)) {
              unlink(DIR_WS_IMAGES . 'graphs/slideshow_banner_yearly-' . $slideshow_banners_id . '.' . $banner_extension);
            }
          }

          if (is_file(DIR_WS_IMAGES . 'graphs/slideshow_banner_monthly-' . $slideshow_banners_id . '.' . $banner_extension)) {
            if (tep_is_writable(DIR_WS_IMAGES . 'graphs/slideshow_banner_monthly-' . $slideshow_banners_id . '.' . $banner_extension)) {
              unlink(DIR_WS_IMAGES . 'graphs/slideshow_banner_monthly-' . $slideshow_banners_id . '.' . $banner_extension);
            }
          }

          if (is_file(DIR_WS_IMAGES . 'graphs/slideshow_banner_daily-' . $slideshow_banners_id . '.' . $banner_extension)) {
            if (tep_is_writable(DIR_WS_IMAGES . 'graphs/slideshow_banner_daily-' . $slideshow_banners_id . '.' . $banner_extension)) {
              unlink(DIR_WS_IMAGES . 'graphs/slideshow_banner_daily-' . $slideshow_banners_id . '.' . $banner_extension);
            }
          }
        }

        $messageStack->add_session(SUCCESS_SLIDER_BANNER_REMOVED, 'success');

        tep_redirect(tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page']));
        break;
    }
  }

// check if the graphs directory exists
  $dir_ok = false;
  if (function_exists('imagecreate') && tep_not_null($banner_extension)) {
    if (is_dir(DIR_WS_IMAGES . 'graphs')) {
      if (tep_is_writable(DIR_WS_IMAGES . 'graphs')) {
        $dir_ok = true;
      } else {
        $messageStack->add(ERROR_GRAPHS_DIRECTORY_NOT_WRITEABLE, 'error');
      }
    } else {
      $messageStack->add(ERROR_GRAPHS_DIRECTORY_DOES_NOT_EXIST, 'error');
    }
  }

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

<script type="text/javascript"><!--
function popupImageWindow(url) {
  window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
//--></script>

    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
<?php
  if ($action == 'new') {
    $form_action = 'insert';

    $parameters = array('expires_date' => '',
                        'date_scheduled' => '',
                        'slideshow_banners_title' => '',
                        'slideshow_banners_url' => '',
                        'slideshow_banners_group' => '',
						'slideshow_banners_target' => '',
                        'slideshow_banners_image' => '',
												'slideshow_banners_image_thumb' => '',
                        'expires_impressions' => '',
																	'slideshow_banners_field_1' => '',
																	'slideshow_banners_field_2' => '',
																	'slideshow_banners_field_3' => '',
																	'slideshow_banners_field_4' => '');

    $bInfo = new objectInfo($parameters);

    if (isset($HTTP_GET_VARS['bID'])) {
      $form_action = 'update';
      $bID = tep_db_prepare_input($HTTP_GET_VARS['bID']);

      $banner_query = tep_db_query("select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_url, s.slideshow_banners_image, s.slideshow_banners_image_thumb, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_group, s.status, date_format(s.date_scheduled, '%Y/%m/%d') as date_scheduled, date_format(s.expires_date, '%Y/%m/%d') as expires_date, s.expires_impressions, s.date_status_change from " . TABLE_SLIDER_BANNERS . " s, " . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd where s.slideshow_banners_id = '" . (int)$bID . "'  and s.slideshow_banners_id = sd.slideshow_banners_id and sd.language_id = '" . (int)$languages_id . "'");
      $banner = tep_db_fetch_array($banner_query);

      $bInfo->objectInfo($banner);
    } elseif (tep_not_null($HTTP_POST_VARS)) {
      $bInfo->objectInfo($HTTP_POST_VARS);
    }

    $groups_array = array();
    $groups_query = tep_db_query("select distinct slideshow_banners_group from " . TABLE_SLIDER_BANNERS . " order by slideshow_banners_group");
    while ($groups = tep_db_fetch_array($groups_query)) {
      $groups_array[] = array('id' => $groups['slideshow_banners_group'], 'text' => $groups['slideshow_banners_group']);
    }
?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><?php echo tep_draw_form('new_banner', FILENAME_SLIDER_BANNER_MANAGER, (isset($HTTP_GET_VARS['page']) ? 'page=' . $HTTP_GET_VARS['page'] . '&' : '') . 'action=' . $form_action, 'post', 'enctype="multipart/form-data"'); if ($form_action == 'update') echo tep_draw_hidden_field('slideshow_banners_id', $bID); ?>
        <td>
      <table>
          <tr>
            <td class="main" style="width:16%;"><?php echo TEXT_SLIDER_BANNERS_TITLE; ?></td>
            <td class="main" style="width:84%;"><?php echo tep_draw_input_field('slideshow_banners_title', $bInfo->slideshow_banners_title, '', true); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_SLIDER_BANNERS_URL; ?></td>
            <td class="main"><?php echo tep_draw_input_field('slideshow_banners_url', $bInfo->slideshow_banners_url); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_SLIDER_BANNERS_TARGET; ?></td>
            <td class="main"><?php echo tep_draw_input_field('slideshow_banners_target', $bInfo->slideshow_banners_target).TEXT_SLIDER_BANNERS_TARGET_TEXT; ?></td>
          </tr>          
          <tr>
            <td class="main" valign="top"><?php echo TEXT_SLIDER_BANNERS_GROUP; ?></td>
            <td class="main"><?php echo tep_draw_pull_down_menu('slideshow_banners_group', $groups_array, $bInfo->slideshow_banners_group) /*. TEXT_SLIDER_BANNERS_NEW_GROUP . '<br />' . tep_draw_input_field('new_slideshow_banners_group', '', '', ((sizeof($groups_array) > 0) ? false : true))*/; ?></td>
          </tr>
          <tr>
          	<td class="main" valign="top"><?php echo TEXT_SLIDER_BANNERS_IMAGE; ?></td>
            <td class="slider_banners_image"><?php echo tep_image(DIR_WS_CATALOG_IMAGES . 'slider/'. (isset($bInfo->slideshow_banners_image) ? $bInfo->slideshow_banners_image : ''), '');?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_SLIDER_BANNERS_IMAGE_PARTH;?></td>
            <td class="main"><?php echo /* tep_draw_file_field('slideshow_banners_image') . ' ' . TEXT_SLIDER_BANNERS_IMAGE_LOCAL . '<br />
						' .*/ DIR_FS_CATALOG_IMAGES . 'slider/'. tep_draw_input_field('slideshow_banners_image_local', (isset($bInfo->slideshow_banners_image) ? $bInfo->slideshow_banners_image : ''), ' class="input_90"', true); ?></td>
          </tr>
          <!--<tr>
            <td class="main" valign="top"><?php echo TEXT_SLIDER_BANNERS_THUMBS_PARTH;?></td>
            <td class="main"><?php echo tep_draw_file_field('slideshow_banners_image') . ' ' . TEXT_SLIDER_BANNERS_IMAGE_LOCAL . '<br />
						' . DIR_FS_CATALOG_IMAGES . 'slider/'. tep_draw_input_field('slideshow_banners_image_thumb', (isset($bInfo->slideshow_banners_image_thumb) ? $bInfo->slideshow_banners_image_thumb : ''), ' class="input_90"'); ?></td>
          </tr> -->         
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <!-- <tr>
            <td class="main"><?php echo TEXT_SLIDER_BANNERS_IMAGE_TARGET; ?></td>
            <td class="main"><?php echo DIR_FS_CATALOG_IMAGES . 'slider/'. tep_draw_input_field('slideshow_banners_target'); ?></td>
          </tr> -->
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
        </table>
        
      <div class="group-1">        
				<dl>
        	<dt><?php echo TEXT_SLIDER_BANNERS_NAME;?></dt> 
<?php
$languages = tep_get_languages();
for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
?>        
            <dd class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
						<?php echo tep_draw_input_field('slideshow_banners_field_1[' . $languages[$i]['id'] . ']', 
						tep_get_slideshow_banners_field_1($bInfo->slideshow_banners_id, $languages[$i]['id']), 
						'', 
						false); ?></dd>
<?php
}
?>            
				</dl>                 
			</div>
      
      <div class="group-1">        
				<dl>  
        	<dt><?php echo TEXT_SLIDER_BANNERS_DESC;?></dt> 
<?php
for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
?> 
            <dd class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
						<?php echo tep_draw_textarea_field('slideshow_banners_field_2[' . $languages[$i]['id'] . ']', 'soft', '400', '200', (empty($bInfo->slideshow_banners_id) ? '' : tep_get_slideshow_banners_field_2($bInfo->slideshow_banners_id, $languages[$i]['id'])), ' class="ckeditor"'); ?></dd>
<?php
}
?>            
				</dl>                 
			</div>
      <div class="group-1">        
				<dl>
        	<dt><?php echo TEXT_SLIDER_BANNERS_PRICE;?></dt> 
<?php
$languages = tep_get_languages();
for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
?>  
            <dd class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?><?php echo tep_draw_input_field('slideshow_banners_field_3[' . $languages[$i]['id'] . ']', tep_get_slideshow_banners_field_3($bInfo->slideshow_banners_id, $languages[$i]['id']), '', false); ?></dd>
<?php
}
?>            
				</dl>                 
			</div>
      <div class="group-1">        
				<dl>
        	<dt><?php echo TEXT_SLIDER_BANNERS_BUTTON;?></dt> 
<?php
$languages = tep_get_languages();
for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
?>        
            <dd class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?><?php echo tep_draw_input_field('slideshow_banners_field_4[' . $languages[$i]['id'] . ']', tep_get_slideshow_banners_field_4($bInfo->slideshow_banners_id, $languages[$i]['id']), '', false); ?></dd>
<?php
}
?>            
				</dl>                 
			</div>
<script type="text/javascript">
$(document).ready(function(){	     
	$('.group-12 dl').each(function (group_1) {
		$(this).find('dd.main').first().addClass('first');
		$(this).find('dd.main').last().addClass('last');
	});
});		
</script>
       
       <table>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_SLIDER_BANNERS_SCHEDULED_AT; ?></td>
            <td class="main"><?php echo tep_draw_input_field('date_scheduled', $bInfo->date_scheduled, 'id="date_scheduled"') . ' <small>(YYYY-MM-DD)</small>'; ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td valign="top" class="main"><?php echo TEXT_SLIDER_BANNERS_EXPIRES_ON; ?></td>
            <td class="main"><?php echo tep_draw_input_field('expires_date', $bInfo->expires_date, 'id="expires_date"') . ' <small>(YYYY-MM-DD)</small>' . TEXT_SLIDER_BANNERS_OR_AT . '<br />' . tep_draw_input_field('expires_impressions', $bInfo->expires_impressions, 'maxlength="7" size="7"') . ' ' . TEXT_SLIDER_BANNERS_IMPRESSIONS; ?></td>
          </tr>
        </table>

<script type="text/javascript">
$('#date_scheduled').datepicker({
  dateFormat: 'yy-mm-dd'
});
$('#expires_date').datepicker({
  dateFormat: 'yy-mm-dd'
});
</script>

        </td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main"><?php echo  TEXT_SLIDER_BANNERS_INSERT_NOTE . '<br />' . TEXT_SLIDER_BANNERS_EXPIRCY_NOTE . '<br />' . TEXT_SLIDER_BANNERS_SCHEDULE_NOTE; ?></td>
            <td class="smallText" align="right" valign="top" nowrap><?php echo tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, (isset($HTTP_GET_VARS['page']) ? 'page=' . $HTTP_GET_VARS['page'] . '&' : '') . (isset($HTTP_GET_VARS['bID']) ? 'bID=' . $HTTP_T_VARS['bID'] : ''))); ?></td>
          </tr>
        </table></td>
      </form></tr>
<?php
  } else {
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_BANNERS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_GROUPS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_STATISTICS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_STATUS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
    $slideshow_banners_query_raw = "select s.slideshow_banners_id, s.slideshow_banners_title, s.slideshow_banners_image, s.slideshow_banners_target, sd.slideshow_banners_field_1, sd.slideshow_banners_field_2, sd.slideshow_banners_field_3, sd.slideshow_banners_field_4, s.slideshow_banners_image_thumb, s.slideshow_banners_group, s.status, s.expires_date, s.expires_impressions, s.date_status_change, s.date_scheduled, s.date_added from 
		" . TABLE_SLIDER_BANNERS . " s, 
		" . TABLE_SLIDER_BANNERS_DESCRIPTION . " sd 
		where s.slideshow_banners_id = sd.slideshow_banners_id 
		and sd.language_id = " . (int)$languages_id . " 
		order by s.slideshow_banners_group, s.slideshow_banners_title";
		
    $slideshow_banners_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS_ADMIN, $slideshow_banners_query_raw, $slideshow_banners_query_numrows);
    $slideshow_banners_query = tep_db_query($slideshow_banners_query_raw);
    while ($slideshow_banners = tep_db_fetch_array($slideshow_banners_query)) {
      $info_query = tep_db_query("select sum(slideshow_banners_shown) as slideshow_banners_shown, sum(slideshow_banners_clicked) as slideshow_banners_clicked from " . TABLE_SLIDER_BANNERS_HISTORY . " where slideshow_banners_id = '" . (int)$slideshow_banners['slideshow_banners_id'] . "'");
      $info = tep_db_fetch_array($info_query);

      if ((!isset($HTTP_GET_VARS['bID']) || (isset($HTTP_GET_VARS['bID']) && ($HTTP_GET_VARS['bID'] == $slideshow_banners['slideshow_banners_id']))) && !isset($bInfo) && (substr($action, 0, 3) != 'new')) {
        $bInfo_array = array_merge($slideshow_banners, $info);
        $bInfo = new objectInfo($bInfo_array);
      }

      $slideshow_banners_shown = ($info['slideshow_banners_shown'] != '') ? $info['slideshow_banners_shown'] : '0';
      $slideshow_banners_clicked = ($info['slideshow_banners_clicked'] != '') ? $info['slideshow_banners_clicked'] : '0';

      if (isset($bInfo) && is_object($bInfo) && ($slideshow_banners['slideshow_banners_id'] == $bInfo->slideshow_banners_id)) {
        echo '              <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_SLIDER_BANNER_STATISTICS, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $bInfo->slideshow_banners_id) . '\'">' . "\n";
      } else {
        echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $slideshow_banners['slideshow_banners_id']) . '\'">' . "\n";
      }
?>
                <td class="dataTableContent"><?php echo '<a href="javascript:popupImageWindow(\'' . FILENAME_POPUP_IMAGE . '?banner=' . $slideshow_banners['slideshow_banners_id'] . '\')">' . tep_image(DIR_WS_IMAGES . 'icon_popup.gif', 'View Banner') . '</a>&nbsp;' . $slideshow_banners['slideshow_banners_title']; ?></td>
                <td class="dataTableContent" align="right"><?php echo $slideshow_banners['slideshow_banners_group']; ?></td>
                <td class="dataTableContent" align="right"><?php echo $slideshow_banners_shown . ' / ' . $slideshow_banners_clicked; ?></td>
                <td class="dataTableContent" align="right">
<?php
      if ($slideshow_banners['status'] == '1') {
        echo tep_image(DIR_WS_IMAGES . 'icon_status_green.gif', 'Active', 10, 10) . '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $slideshow_banners['slideshow_banners_id'] . '&action=setflag&flag=0') . '">' . tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif', 'Set Inactive', 10, 10) . '</a>';
      } else {
        echo '<a href="' . tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $slideshow_banners['slideshow_banners_id'] . '&action=setflag&flag=1') . '">' . tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif', 'Set Active', 10, 10) . '</a>&nbsp;&nbsp;' . tep_image(DIR_WS_IMAGES . 'icon_status_red.gif', 'Inactive', 10, 10);
      }
?></td>
                <td class="dataTableContent" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_SLIDER_BANNER_STATISTICS, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $slideshow_banners['slideshow_banners_id']) . '">' . tep_image(DIR_WS_ICONS . 'statistics.gif', ICON_STATISTICS) . '</a>&nbsp;'; if (isset($bInfo) && is_object($bInfo) && ($slideshow_banners['slideshow_banners_id'] == $bInfo->slideshow_banners_id)) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $slideshow_banners['slideshow_banners_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
    }
?>
              <tr>
                <td colspan="5"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $slideshow_banners_split->display_count($slideshow_banners_query_numrows, MAX_DISPLAY_SEARCH_RESULTS_ADMIN, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_BANNERS); ?></td>
                    <td class="smallText" align="right"><?php echo $slideshow_banners_split->display_links($slideshow_banners_query_numrows, MAX_DISPLAY_SEARCH_RESULTS_ADMIN, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?></td>
                  </tr>
                  <tr>
                    <td class="smallText" align="right" colspan="2"><?php echo tep_draw_button(IMAGE_NEW_BANNER, 'plus', tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'action=new')); ?></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
<?php
  $heading = array();
  $contents = array();
  switch ($action) {
    case 'delete':
      $heading[] = array('text' => '<strong>' . $bInfo->slideshow_banners_title . '</strong>');

      $contents = array('form' => tep_draw_form('banners', FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $bInfo->slideshow_banners_id . '&action=deleteconfirm'));
      $contents[] = array('text' => TEXT_INFO_DELETE_INTRO);
      $contents[] = array('text' => '<br /><strong>' . $bInfo->slideshow_banners_title . '</strong>');
      if ($bInfo->slideshow_banners_image) $contents[] = array('text' => '<br />' . tep_draw_checkbox_field('delete_image', 'on', true) . ' ' . TEXT_INFO_DELETE_IMAGE);
      $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_DELETE, 'trash', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $HTTP_GET_VARS['bID'])));
      break;
    default:
      if (is_object($bInfo)) {
        $heading[] = array('text' => '<strong>' . $bInfo->slideshow_banners_title . '</strong>');

        $contents[] = array('align' => 'center', 'text' => tep_draw_button(IMAGE_EDIT, 'document', tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $bInfo->slideshow_banners_id . '&action=new')) . tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_SLIDER_BANNER_MANAGER, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $bInfo->slideshow_banners_id . '&action=delete')) . tep_draw_button(IMAGE_DETAILS, 'info', tep_href_link(FILENAME_SLIDER_BANNER_STATISTICS, 'page=' . $HTTP_GET_VARS['page'] . '&bID=' . $bInfo->slideshow_banners_id)));
        $contents[] = array('text' => '<br />' . TEXT_SLIDER_BANNERS_DATE_ADDED . ' ' . tep_date_short($bInfo->date_added));

        if ( (function_exists('imagecreate')) && ($dir_ok) && ($banner_extension) ) {
          $banner_id = $bInfo->slideshow_banners_id;
          $days = '3';
          include(DIR_WS_INCLUDES . 'graphs/slideshow_banner_infobox.php');
          $contents[] = array('align' => 'center', 'text' => '<br />' . tep_image(DIR_WS_IMAGES . 'graphs/banner_infobox-' . $banner_id . '.' . $banner_extension));
        } else {
          include(DIR_WS_FUNCTIONS . 'html_slider_graphs.php');
          $contents[] = array('align' => 'center', 'text' => '<br />' . tep_slideshow_banner_graph_infoBox($bInfo->slideshow_banners_id, '3'));
        }

        $contents[] = array('text' => tep_image(DIR_WS_IMAGES . 'graph_hbar_blue.gif', 'Blue', '5', '5') . ' ' . TEXT_SLIDER_BANNERS_BANNER_VIEWS . '<br />' . tep_image(DIR_WS_IMAGES . 'graph_hbar_red.gif', 'Red', '5', '5') . ' ' . TEXT_SLIDER_BANNERS_BANNER_CLICKS);

        if ($bInfo->date_scheduled) $contents[] = array('text' => '<br />' . sprintf(TEXT_SLIDER_BANNERS_SCHEDULED_AT_DATE, tep_date_short($bInfo->date_scheduled)));

        if ($bInfo->expires_date) {
          $contents[] = array('text' => '<br />' . sprintf(TEXT_SLIDER_BANNERS_EXPIRES_AT_DATE, tep_date_short($bInfo->expires_date)));
        } elseif ($bInfo->expires_impressions) {
          $contents[] = array('text' => '<br />' . sprintf(TEXT_SLIDER_BANNERS_EXPIRES_AT_IMPRESSIONS, $bInfo->expires_impressions));
        }

        if ($bInfo->date_status_change) $contents[] = array('text' => '<br />' . sprintf(TEXT_SLIDER_BANNERS_STATUS_CHANGE, tep_date_short($bInfo->date_status_change)));
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="25%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
        </table></td>
      </tr>
<?php
  }
?>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
