<?php
/**
 * @package Genesis
 * @copyright Copyright 2012 - 2015 Sammycorp
 * @author Sammycorp
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */

function genesis_center_column_class(){
  global $flag_disable_left, $flag_disable_right;

  $classes = array();

  $column = 12;

  if ( !isset( $flag_disable_left ) || !$flag_disable_left ) {
    $column -= 3;
    $classes[] = 'col-md-push-3';
  }

  if ( !isset( $flag_disable_right ) || !$flag_disable_right ) {
    $column -= 3;
  }

  $classes[] = 'col-md-' . $column; 

  return implode( ' ', $classes );
}

function genesis_layout_class(){
  global $flag_disable_left, $flag_disable_right;

  $classes = array();

  if ( !isset( $flag_disable_left ) || !$flag_disable_left ) {
    $classes[] = 'left-enabled';
  }

  if ( !isset( $flag_disable_right ) || !$flag_disable_right ) {
    $classes[] = 'right-enabled';
  }

  return implode( ' ', $classes );
}

function genesis_get_additional_images( $product_id ){
  global $db;

  $sql = "select p.products_image from " . TABLE_PRODUCTS . " p  where products_id='" . (int)$product_id . "'";
  $look_up = $db->Execute($sql);
  $products_image = $look_up->fields['products_image'];

  if ($products_image != '') {
    $images = array();
    $image_info = pathinfo( $products_image );
    // prepare image name
    $products_image_extension = $image_info['extension'];
    $products_image_base = $image_info['filename'];

    // if in a subdirectory
    if ( $image_info['dirname'] != '' && $image_info['dirname'] != '.' ) {
      $products_image_directory = DIR_WS_IMAGES . $image_info['dirname'] . '/';
    }else{
      $products_image_directory = DIR_WS_IMAGES;
    }

    $images_array = glob( $products_image_directory . $products_image_base . '{*.jpg,*.JPG,*.png,*.PNG}', GLOB_BRACE );
    $images_array = array_diff( $images_array, array( $products_image_directory . $products_image ) );
    $images = array_merge( $images, $images_array );

    return $images;
  }else{
    return false;
  }
}

function genesis_get_product_action( $data, $how_many = 0 ){
  $output = array();
  if( PRODUCT_LIST_PRICE_BUY_NOW != '0' ) {
    if ( zen_has_product_attributes( $data['products_id'] ) ) {
      $output['button'] = sprintf( '<a class="add-to-cart button" href="%s">%s</a>', 
                  zen_href_link( zen_get_info_page( $data['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ?  zen_get_generated_category_path_rev( $_GET['filter_id'] ) : ( $_GET['cPath'] > 0 ? $_GET['cPath'] : zen_get_generated_category_path_rev( $data['master_categories_id'] ) ) ) . '&products_id=' . $data['products_id']), 
                  'Select Options' 
                );
    } else {
      if (PRODUCT_LISTING_MULTIPLE_ADD_TO_CART != 0) {
        if ( $data['products_qty_box_status'] != 0 &&
             zen_get_products_allow_add_to_cart( $data['products_id']) != 'N' &&
             $data['product_is_call'] == 0 &&
            ($data['products_quantity'] > 0 || SHOW_PRODUCTS_SOLD_OUT_IMAGE == 0) ) {
            $how_many++;
        }

        if ( $data['products_qty_box_status'] == 0 ) {
          $output['button'] = sprintf( '<a class="add-to-cart button" href="%s">%s</a>', zen_href_link( $_GET['main_page'], zen_get_all_get_params( array('action') ) . 'action=buy_now&products_id=' . $data['products_id']), BUTTON_IN_CART_ALT );
        } else {
          $output['button'] = '<input type="number" name="products_id[' . $data['products_id'] . ']" placeholder="0" min="0">';
        }
      } else {
        if (PRODUCT_LIST_PRICE_BUY_NOW == '2' && $data['products_qty_box_status'] != 0) {
          $output['button'] = zen_draw_form( 'cart_quantity', zen_href_link($_GET['main_page'], zen_get_all_get_params(array('action')) . 'action=add_product&products_id=' . $data['products_id']), 'post', 'enctype="multipart/form-data"') . '<input type="number" name="cart_quantity" value="' . (zen_get_buy_now_qty($data['products_id'])) . '" min="0">' . zen_draw_hidden_field('products_id', $data['products_id']) . '<button type="submit" class="button add-to-cart">'.BUTTON_IN_CART_ALT.'</button></form>';
        } else {
          $output['button'] = sprintf( '<a class="add-to-cart button" href="%s">%s</a>', zen_href_link( $_GET['main_page'], zen_get_all_get_params( array('action') ) . 'action=buy_now&products_id=' . $data['products_id']), BUTTON_IN_CART_ALT );
        }
      }
    }
  }else{
    $output['button'] = '';
  }

  $output['how_many'] = $how_many;

  return $output;
}

function genesis_get_product_category( $product_id, $round = true ){
  return zen_get_categories_name( zen_get_products_category_id( $product_id ) );
}

function genesis_get_product_rating($pid = '', $round = true) {
  global $db;
  $rating_query = $db->Execute("SELECT avg(reviews_rating) as avg 
                               from " . TABLE_REVIEWS . " r
                               where products_id='" . (int)$pid . "' limit 1");
  if ($rating_query->EOF) {
    return 0;
  } else {
    if( $round ){
      return round($rating_query->fields['avg'], 2);
    }else{
      return $rating_query->fields['avg'];
    }
    
  }
}

function genesis_has_subcategories($category_id) {
  global $db;
  $child_category_query = "select count(*) as count
                           from " . TABLE_CATEGORIES . "
                           where categories_status = 1 and 
                           parent_id = '" . (int)$category_id . "'";

  $child_category = $db->Execute($child_category_query);

  if ($child_category->fields['count'] > 0) {
    return true;
  } else {
    return false;
  }
}

function genesis_get_max_product_price($cid = ''){
  global $db;

  $max_query = $db->Execute("SELECT max(products_price_sorter) as max FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c WHERE p.products_id = p2c.products_id AND p2c.categories_id = '". (int)$cid ."' AND p.products_status = '1'");
  $max = round($max_query->fields['max'], 0);

  return $max;
}

function genesis_get_product_rating_count($pid = '') {
  global $db;
  $rating_query = $db->Execute("SELECT count(reviews_rating) as count 
                               from " . TABLE_REVIEWS . " r
                               where products_id='" . (int)$pid . "' and status = '1' limit 1");
  if ($rating_query->EOF) {
    return 0;
  } else {
    return $rating_query->fields['count'];
  }
}

function genesis_get_rating_html( $rating = null ) {
  $rating_html = '';
  $mod = 0;

  if ( ! is_numeric( $rating ) ) {
    $rating = 0;
  }
  $rating = round( $rating * 2, 0 ) / 2;

  if( $rating > 0 ){

    switch ( $rating ) {
      case 0:
        $rating_html .= '';
        $mod = 0;
        break;
      case 0.5:
        $rating_html .= '<span class="star on"><i class="fa fa-star-half-o"></i></span>';
        $mod = 4;
        break;
      case 1:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $mod = 4;
        break;
      case 1.5:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star-half-o"></i></span>';
        $mod = 3;
        break;
      case 2:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $mod = 3;
        break;
      case 2.5:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star-half-o"></i></span>';
        $mod = 2;
        break;
      case 3:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $mod = 2;
        break;
      case 3.5:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star-half-o"></i></span>';
        $mod = 1;
        break;
      case 4:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $mod = 1;
        break;
      case 4.5:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star-half-o"></i></span>';
        $mod = 0;
        break;
      case 5:
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $rating_html .= '<span class="star on"><i class="fa fa-star"></i></span>';
        $mod = 0;
        break;

      default:
        break;
    }

    if( $mod > 0){
      for ($i=0;$i<$mod;$i++){
        $rating_html .= '<span class="star off"><i class="fa fa-star"></i></span>';
      }
    }

  }

  return $rating_html;

}

function genesis_footer_script(){
  global $template, $current_page_base, $this_is_home_page;

  $scripts = array();

  if($this_is_home_page){
    $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/jquery.flexslider-min.js';
  }

  if($current_page_base == 'product_info'){
    $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/photoswipe.min.js';
    $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/photoswipe-ui-default.min.js';
    $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/jquery.zoom.min.js';
  }

  $directory_array = $template->get_template_part($template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer'), '/^jscript_/', '.js');
  while(list ($key, $value) = each($directory_array)) {
    $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/' . $value;
  }

  $scripts[] = $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript/footer') . '/genesis.js';

  return $scripts;
}

function genesis_footer(){
  $scripts = genesis_footer_script();

  foreach ($scripts as $script) {
    printf( '<script src="%s"></script>' . "\n", $script );
  }
}

function genesis_large_imageinfo( $image = '' ){
  $info = getimagesize( DIR_FS_CATALOG . $image );
  return $info;
}

function genesis_get_reviews_detail($pid = '', $offset = 0, $limit = 3){
  global $db;

  $sql = "SELECT r.reviews_id, r.products_id, r.customers_name, r.customers_id, r.reviews_rating, r.date_added, rd.reviews_text FROM " . TABLE_REVIEWS . " r, " . TABLE_REVIEWS_DESCRIPTION . " rd  
        where r.products_id = '" . (int)$pid . "' 
        and r.reviews_id = rd.reviews_id 
        and rd.languages_id = '" . (int)$_SESSION['languages_id'] . "'
        and r.status = '1' LIMIT " . $offset . ", " . $limit;

  $reviews_detail = $db->Execute($sql);

  return $reviews_detail;
}

function genesis_get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $atts = array() ) {
  $url = '//www.gravatar.com/avatar/';
  $url .= md5( strtolower( trim( $email ) ) );
  $url .= sprintf( '?s=%s&d=%s&r=%s', $s, $d, $r );
  $url = '<img class="img-responsive" src="' . $url . '"';
  foreach ( $atts as $key => $val )
      $url .= ' ' . $key . '="' . $val . '"';
  $url .= ' >';
  return $url;
}

function genesis_get_customer_email($customer_id) {
  global $db;
  $sql = "SELECT customers_email_address FROM " . TABLE_CUSTOMERS . "
    where customers_id  = '" . (int)$customer_id . "'";

  $email_query = $db->Execute($sql);
  if ($email_query->RecordCount() > 0) {
    $email = $email_query->fields['customers_email_address'];
  }
  return $email;
}

function genesis_get_currency($code = ''){
  global $db;
  $code = zen_db_prepare_input($code);

  if($code == ''){
    $currency_sql = "select *
                    from " . TABLE_CURRENCIES;
  }else{
    $currency_sql = "select *
                    from " . TABLE_CURRENCIES . "
                    where code = '" . $code . "' LIMIT 1";
  }

    $currency = $db->Execute($currency_sql);
    return $currency->fields;
}

function genesis_get_default_currency(){
  global $db;

  $currency_sql = "select *
                    from " . TABLE_CURRENCIES . "
                    where value = 1";
  $currency = $db->Execute($currency_sql);
  return $currency->fields;
}

function genesis_get_language_code($id = '') {
  global $db;

  $check_language= $db->Execute("select code from " . TABLE_LANGUAGES . " where languages_id = '" . (int)$id . "'");
  return $check_language->fields['code'];
}

function genesis_get_language_name($id = '') {
  global $db;

  $check_language= $db->Execute("select name from " . TABLE_LANGUAGES . " where languages_id = '" . (int)$id . "'");
  return $check_language->fields['name'];
}

function genesis_get_banners($name = ''){
  global $db;

  $banners = array();

  $banner_query = "select banners_id, banners_title, banners_image, banners_html_text, banners_open_new_windows, banners_url
                   from " . TABLE_BANNERS . "
                   where status = 1
                   and banners_group = '" . $name . "' 
                   order by banners_sort_order";

  $banner = $db->Execute($banner_query);

  if ($banner->RecordCount() > 0) {
    while (!$banner->EOF) {
      $banners[] = array(
        'title' => $banner->fields['banners_title'], 
        'url'   => ( $banner->fields['banners_url'] != '' ? zen_href_link(FILENAME_REDIRECT, 'action=banner&goto=' . $banner->fields['banners_id']) : '' ), 
        'image' => $banner->fields['banners_image'], 
        'html'  => $banner->fields['banners_html_text'], 
        'onw'   => $banner->fields['banners_open_new_windows']
      );
      $banner->MoveNext();
    }
    zen_update_banner_display_count($banner->fields['banners_id']);
    return $banners;
  }else{
    return false;
  }
}

function genesis_get_theme_options(){
  global $db;

  $options_sql = $db->Execute("select * from " . TABLE_GENESIS . " where id='1'");
  $options = json_decode($options_sql->fields['options_value'], TRUE);

  return $options;
}

function genesis_dynamic_style( $options = '' ){
  ob_start(); ?>
<style>

<?php if( zen_not_null($options['use_background_image']) ) : ?>
	<?php if( zen_not_null( $options['site_body_image_url'] ) ) : ?>
	body { background-image: url(<?php echo $options['site_body_image_url']; ?>); }
	<?php endif; ?>
<?php else : ?>
	<?php if( zen_not_null( $options['site_body_color'] ) ) : ?>
	body { background-image: none; background-color: <?php echo $options['site_body_color']; ?> }
	<?php endif; ?>
<?php endif; ?>

  .important, .amount.forward, .totalBox.forward, .button.button-inverse, button:hover, input[type="button"]:hover, input[type="reset"]:hover, input[type="submit"]:hover,
  .button:hover, button:focus:hover, input[type="button"]:focus:hover, input[type="reset"]:focus:hover, input[type="submit"]:focus:hover, .button:focus:hover, .cart-qty-control a:hover, 
  .messageStackWarning, .cart-header .cart-content span, #aux-menu .cart-header .cart-content a:hover h4, #aux-menu .cart-header .cart-action .button.button-inverse, .breadcrumb a:hover, 
  .products .product-category, .navSplitPagesLinks a:hover, .prev-next a:hover, .review-write:before, .review-write a, #productDateAdded, #productInfoLink, .review-by, .social-footer ul li a:hover,
  .links-footer ul li a, .links-footer ul li + li:before, .select2-container--default .select2-results__option[aria-selected="true"], .widget .menu a:hover, .widget .menu .expand-btn:hover, #main-menu .expand-btn:after, 
  #main-menu .expand-btn:hover, #aux-menu ul li a:hover, .main-navigation > .menu > li:hover > a, .main-navigation > .menu > li > a:hover, .products .product-name a:hover, .category-name a:hover, .products .product-price, 
  #main-menu-btn:hover, .review-write a:hover, #productPrices, .tabs-nav li a:hover, .tabs-nav li a.active, .mailchimp-footer .button:hover, .mailchimp-footer .button:hover, .mailchimp-footer .button:focus:hover, 
  .mailchimp-footer .button:focus, .mailchimp-footer .button:active, .footer-module-content .menu a:hover { color: <?php echo $options['site_color']; ?>; }

  @media (max-width: 991px) { #main-menu a:hover { color: <?php echo $options['site_color']; ?>; } }

  button, input[type="button"], input[type="reset"], input[type="submit"], .button, .button.button-inverse:hover, .button.button-inverse:focus:hover, button:focus,
  input[type="button"]:focus, input[type="reset"]:focus, input[type="submit"]:focus, .button:focus, button:active, input[type="button"]:active, input[type="reset"]:active, input[type="submit"]:active, .button:active, 
  .button.add-to-cart:hover, .main-navigation .menu ul li a:hover, #aux-menu .cart-header .cart-action .button.button-inverse:hover, .product-share a, 
  .product-share a:hover, #back-top, .select2-container--default .select2-results__option--highlighted[aria-selected], .cartAttribsList ul li, .top-navigation, .owl-prev:hover, .owl-next:hover, 
  .cdslider:hover .flex-direction-nav .flex-prev:hover, .cdslider:hover .flex-direction-nav .flex-next:hover, .flex-control-paging li a.flex-active, .flex-control-paging li a:hover, .mailchimp-footer { background-color: <?php echo $options['site_color']; ?>; }
  
  button, input[type="button"], input[type="reset"], input[type="submit"], .button, .button.add-to-cart:hover, input[type="text"]:focus, input[type="password"]:focus, input[type="date"]:focus,
  input[type="datetime"]:focus, input[type="email"]:focus, input[type="number"]:focus, input[type="search"]:focus, input[type="tel"]:focus, input[type="time"]:focus, input[type="url"]:focus, textarea:focus, 
  .messageStackWarning, .main-navigation .menu ul, button:focus, input[type="button"]:focus, input[type="reset"]:focus, input[type="submit"]:focus, .button:focus, button:active, input[type="button"]:active,
  input[type="reset"]:active, input[type="submit"]:active, .button:active, .owl-prev:hover, .owl-next:hover, .button.button-inverse:hover, .button.button-inverse:focus:hover, 
  #cartAdd input[name="cart_quantity"]:focus, .tabs-nav li a:hover, .tabs-nav li a.active { border-color: <?php echo $options['site_color']; ?>; }

  /*! PhotoSwipe Override */
  .pswp,.pswp__bg,.pswp__scroll-wrap{width:100%;height:100%}.pswp,.pswp__item,.pswp__scroll-wrap{overflow:hidden;top:0;position:absolute;left:0}.pswp,.pswp__bg,.pswp__container,.pswp__img--placeholder,.pswp__zoom-wrap{-webkit-backface-visibility:hidden}.pswp,.pswp__bg,.pswp__error-msg,.pswp__img,.pswp__item,.pswp__scroll-wrap,.pswp__zoom-wrap{position:absolute}.pswp{display:none;-ms-touch-action:none;touch-action:none;z-index:9999;-webkit-text-size-adjust:100%;outline:0}.pswp *{-webkit-box-sizing:border-box;box-sizing:border-box}.pswp img{max-width:none}.pswp--animate_opacity{opacity:.001;will-change:opacity;-webkit-transition:opacity 333ms cubic-bezier(.4,0,.22,1);transition:opacity 333ms cubic-bezier(.4,0,.22,1)}.pswp--open{display:block}.pswp--zoom-allowed .pswp__img{cursor:-webkit-zoom-in;cursor:-moz-zoom-in;cursor:zoom-in}.pswp--zoomed-in .pswp__img{cursor:-webkit-grab;cursor:-moz-grab;cursor:grab}.pswp--dragging .pswp__img{cursor:-webkit-grabbing;cursor:-moz-grabbing;cursor:grabbing}.pswp__container,.pswp__img{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none}.pswp__zoom-wrap{width:100%;-webkit-transform-origin:left top;-ms-transform-origin:left top;transform-origin:left top;-webkit-transition:-webkit-transform 333ms cubic-bezier(.4,0,.22,1);transition:transform 333ms cubic-bezier(.4,0,.22,1)}.pswp__bg{left:0;top:0;background:#000;opacity:0;will-change:opacity;-webkit-transition:opacity 333ms cubic-bezier(.4,0,.22,1);transition:opacity 333ms cubic-bezier(.4,0,.22,1)}.pswp--animated-in .pswp__bg,.pswp--animated-in .pswp__zoom-wrap{-webkit-transition:none;transition:none}.pswp__container,.pswp__zoom-wrap{-ms-touch-action:none;touch-action:none;position:absolute;left:0;right:0;top:0;bottom:0;will-change:transform}.pswp__item{right:0;bottom:0}.pswp__img{width:auto;height:auto;top:0;left:0}.pswp__img--placeholder--blank{background:#222}.pswp--ie .pswp__img{width:100%!important;height:auto!important;left:0;top:0}.pswp__error-msg{left:0;top:50%;width:100%;text-align:center;font-size:14px;line-height:16px;margin-top:-8px;color:#CCC}.pswp__error-msg a{color:#CCC;text-decoration:underline}
</style>
<?php return ob_get_clean();
}