<?php

function kippis_get_font_select($id_name,$value,$id_target = '')
{
  $opts = '<option value=""></option>';
  for ($i = 5; $i <= 60; $i++) {
    $val = $i . 'px';
    $selected = $val == $value ? ' selected' : '';
    $opts .= '<option value="' . $val . '"' . $selected . '>' . $i . 'px</option>';
  }
  if (!empty($id_target)) $onchange = 'onchange="jQuery(' . "'#" . $id_target . "').css('font-size'," . 'jQuery(this).val());"';
  else                    $onchange = '';
  return '<select ' . $onchange . ' name="' . $id_name . '" id="' . $id_name . '">' . $opts . '</select>';
}

function kippis_get_radius_select($id_name,$value)
{
  $opts = '<option value=""></option>';
  for ($i = 1; $i <= 25; $i++) {
    $val = $i . 'px';
    $selected = $val == $value ? ' selected' : '';
    $opts .= '<option value="' . $val . '"' . $selected . '>' . $i . 'px</option>';
  }
  return '<select name="' . $id_name . '" id="' . $id_name . '">' . $opts . '</select>';
}

function kippis_get_text_align_select($id_name,$value)
{
  $opts = '<option value=""></option>';
  for ($i = 0; $i < 4; $i++) {
    switch ($i) {
      case 0: $val = 'left';    break;
      case 1: $val = 'right';   break;
      case 2: $val = 'center';  break;
      case 3: $val = 'justify'; break;
    }
    $selected = $val == $value ? ' selected' : '';
    $opts .= '<option value="' . $val . '"' . $selected . '>' . $val . '</option>';
  }
  return '<select name="' . $id_name . '" id="' . $id_name . '">' . $opts . '</select>';
}

$arrOptions = get_option('kippis');
$kippis_has_data = !empty($arrOptions);

$option_std = array();
foreach ($options as $option) {
  if (isset($option['std'])) $option_std[$option['id']] = $option['std'];
}

foreach ($options as $option) {
  if (($option['type'] == 'open') || ($option['type'] == 'close') || ($option['type'] == 'displayonly')) {
    // Nothing here, move on.
  }
  else {
    if (isset($arrOptions[$option['id']])) {
      if ($kippis_has_data) $real_value = $arrOptions[$option['id']];
      else                  $real_value = $option_std[$option['id']]; //$option['std'];
    }
    else {
      //$real_value = $option['std'];
      $real_value = $option_std[$option['id']];  // New items.
    }
  }

  if (isset($option['class'])) $fields_html .= '<span class="' . $option['class'] . '">';

  switch ($option['type']) {

    case 'open':
      $tab_id = str_replace(' ', '-', strtolower($option['title']));  // Generate the id which will be used as the id of the section (for the tabs system purposes).
      $menubar_html .= '<li><a href="#kippis_options-'.$tab_id.'">'.$option['title'].'</a></li>';  // Add the new link in the menubar for this section.
      $fields_html .= '<div class="kippis_options-section" id="kippis_options-'.$tab_id.'">';         // Open the new section.
      break;

    case 'close':
      $fields_html .= '</div>';  // Close the current section
      break;

    case 'displayonly':
      switch ($option['data'][0]) {
        case 'colors':
          $field_id = (isset($option['id']) ? ' id="' . $option['id'] . '"' : '');
          $the_style = '';
          $id_bg = $option['data'][1];
          $id_fg = $option['data'][2];
          if ($kippis_has_data && (isset($arrOptions[$id_bg]))) {
            $val_bg = $arrOptions[$id_bg];
            $val_fg = $arrOptions[$id_fg];
          }
          else {
            $val_bg = $option_std[$id_bg];
            $val_fg = $option_std[$id_fg];
          }
          if (!empty($val_bg)) $the_style .= 'background-color:' . $val_bg . ';';
          if (!empty($val_fg)) $the_style .= 'color:'            . $val_fg . ';';
          if (isset($option['data'][3])) {
            $id_font   = $option['data'][3];
            $id_size   = $option['data'][3] . '_size';
            $id_bold   = $option['data'][3] . '_bold';
            $id_italic = $option['data'][3] . '_italic';
            if ($kippis_has_data && (isset($arrOptions[$id_font]))) {
              $val_font   = $arrOptions[$id_font  ];
              $val_size   = $arrOptions[$id_size  ];
              $val_bold   = $arrOptions[$id_bold  ];
              $val_italic = $arrOptions[$id_italic];
            }
            else {
              $val_font   = $option_std[$id_font  ];
              $val_size   = $option_std[$id_size  ];
              $val_bold   = $option_std[$id_bold  ];
              $val_italic = $option_std[$id_italic];
            }
            if (!empty($val_font  )                         ) $the_style .= 'font-family:'      . $val_font . ';';
            if (!empty($val_size  )                         ) $the_style .= 'font-size:'        . $val_size . ';';
            if (!empty($val_bold  ) && ($val_bold   == 'on')) $the_style .= 'font-weight:bold;';
            if (!empty($val_italic) && ($val_italic == 'on')) $the_style .= 'font-style:italic;';
          }
          if (!empty($the_style)) $the_style = ' style="' . $the_style . '"';
          $fields_html .= '<div class="kippis_options-field">'
                       . (isset($option['title']) ? '<div class="kippis_options-displayonly-title">' . $option['title'] . '</div>' : '')
                       . '<div class="kippis_displayonly_colors_font">'
                       . '<input readonly' . $the_style . ' type="text"' . $field_id . ' value="' . $option['desc'] . '" />'
                       . '</div>'
                       . (isset($option['note' ]) ? '<div class="kippis_options-note">'             . $option['note'  ] . '</div>' : '')
                       . '</div>';
          break;
        case 'info':
          $fields_html .= '<div class="kippis_options-field">'
                       . (isset($option['title']) ? '<div class="kippis_options-displayonly-title">' . $option['title'] . '</div>' : '')
                       . (isset($option['desc' ]) ? '<div class="kippis_options-description">'       . $option['desc' ] . '</div>' : '')
                       . $option['data'][1]
                       . '</div>';
          break;
        case 'delimiter':
          $fields_html .= '<div class="kippis_options-field">'
          . ($option['data'][1] == 'top' ? '<div class="kippis_options-displayonly-delimiter"></div>' : '')
          . (isset($option['title']) ? '<div class="kippis_options-displayonly-title">' . $option['title'] . '</div>' : '')
          . (isset($option['desc' ]) ? '<div class="kippis_options-description">'       . $option['desc' ] . '</div>' : '')
          . ($option['data'][1] == 'bottom' ? '<div class="kippis_options-displayonly-delimiter"></div>' : '')
          . '</div>';
          break;
        case 'form':
          $fields_html .= '<div class="kippis_options-field">'
                        . (isset($option['title']) ? '<div class="kippis_options-displayonly-title">' . $option['title'] . '</div>' : '')
                        . (isset($option['desc' ]) ? '<div class="kippis_options-description">'       . $option['desc' ] . '</div>' : '')
                        . $option['data'][1]
                        . '</div>';
          break;
        case 'webservice':
          if (isset($option['if_cbx']) && ($arrOptions[$option['if_cbx']] == 'on')) {
            if ((isset($option['data'][1])) && (!empty($option['data'][1]))) {
              $news = wp_remote_fopen($option['data'][1]);
              if (empty($news)) $fields_html .= '<div class="kippis_options-field"><p><i>' . __('Unavailable','kippis') . '</i></p></div>';
              else              $fields_html .= '<div class="kippis_options-field">' . $news . '</div>';
            }
          }
          else $fields_html .= '<div class="kippis_options-field"><p><i>' . __('Disabled','kippis') . '</i></p></div>';
          break;
      }
      break;  // case 'displayonly':

    case 'text':
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= '<label for="' . $option['id'] . '">' . $option['title'] . '</label>';
      if (isset($option['desc' ])) $fields_html .= '<div class="kippis_options-description">' . $option['desc'] . '</div>';
      if (isset($option['note' ])) $fields_html .= '<div class="kippis_options-note">'        . $option['note'] . '</div>';
      $fields_html .= '<input type="text" name="' . $option['id'] . '" id="' . $option['id'] . '" value="' . $real_value . '" />';
      $fields_html .= '</div>';
      break;

    case 'number':
      $fields_html .= '<div class="kippis_options-field">'
                   .  '<input class="kippis_number_field" type="text" name="' . $option['id'] . '" id="' . $option['id'] . '" value="' . $real_value . '" />'
                   .  '&nbsp; <label for="' . $option['id'] . '">' . $option['title'] . '</label>'
                   . (isset($option['desc']) ? ' <div class="kippis_options-description">' . $option['desc'] . '</div>' : '')
                   . (isset($option['note']) ? ' <div class="kippis_options-note">'        . $option['note'] . '</div>' : '')
                   .  '</div>';
      break;

    case 'checkbox':
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= '<label for="' . $option['id'] . '">'   . $option['title'] . '</label>';
      if (isset($option['note' ])) $fields_html .= '<div class="kippis_options-note">'        . $option['note' ] . '</div>';
      if ($real_value == 'on') $checked = 'checked';
      else                     $checked = '';
      $fields_html .= '<p><input type="checkbox" name="' . $option['id'] . '" id="' . $option['id'] . '" ' . $checked . '>'
                   .  (isset($option['desc' ]) ? '&nbsp; <span class="kippis_options-description">' . $option['desc' ] . '</span>' : '')
                   .  '</p>'
                   .  '</div>';
      break;

    case 'radio':
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= '<label for="' . $option['id'] . '">'   . $option['title'] . '</label>';
      if (isset($option['desc' ])) $fields_html .= '<div class="kippis_options-description">' . $option['desc' ] . '</div>';
      if (isset($option['note' ])) $fields_html .= '<div class="kippis_options-note">'        . $option['note' ] . '</div>';
      foreach($option['data'] as $ix => $val){
        if ($val == $real_value) $checked = 'checked';
        else                     $checked = '';
        $fields_html .= '<p><input type="radio" name="' . $option['id'] . '" id="' . $val . '" value="' . $val . '" ' . $checked . ' />'
                     .  '&nbsp; <span class="kippis_options-description">' . $ix
                     .  '</p>';
      }
      $fields_html .= '</div>';
      break;

    case 'color':
      $fields_html .= '<div class="kippis_options-field">';
      $fields_html .= '<table><tr><td class="kippis_options-color1">';
      if (isset($option['desc'])) $fields_html .= '<div class="kippis_options-description">' . $option['desc'] . '</div>';
      if (isset($option['note'])) $fields_html .= '<div class="kippis_options-note">'        . $option['note'] . '</div>';
      if ((isset($option['data'][0])) && (isset($option['data'][0]))) {
        $onchange = ' onblur="'
        . 'jQuery(' . "'" . '#' . $option['data'][0] . "'" . ').css(' . "'" . $option['data'][1] . "',jQuery(this).val());"
        . '"';
      }
      else $onchange = '';
      $fields_html .= '<input class="kippis_color_field" type="text" name="' . $option['id'] . '" id="' . $option['id'] . '" value="' . $real_value . '"' . $onchange . ' />'
                    . '&nbsp; <label for="' . $option['id'] . '">' . $option['title'] . '</label>'
                    . '</td><td class="kippis_options-color2">'
                    . '<div id="kippis_cp_' . $option['id'] . '" class="kippis_color_picker" rel="' . $option['id'] . '"></div>'
                    . '</td></tr></table>'
                    . '</div>';
      break;

    case 'image':
      $fields_html .= '<div class="kippis_options-field">';
      $fields_html .= '<label for="' . $option['id'] . '">' . $option['title'] . '</label>';
      if (isset($option['desc'])){
        $fields_html .= '<div class="kippis_options-description">';
        $fields_html .= $option['desc'];
        $fields_html .= '</div>';
      }
      if (isset($option['note'])) $fields_html .= '<div class="kippis_options-note">' . $option['note'] . '</div>';
      if (!empty($real_value['url'])) {
        if (!empty($real_value['width']) && (!empty($real_value['height'])) && ($real_value['width'] == '16') && ($real_value['height'] == '16')) {
          $fields_html .= '<img width="16" height="16" src="' . $real_value['url'] . '">';
        }
        else {
          $style = 'background-image:url(' . get_template_directory_uri() . '/pix/transparency.jpg);background-repeat:repeat;';
          $fields_html .= '<img src="' . $real_value['url'] . '" class="kippis_image_preview" style="' . $style . '">';
        }
      }
      if (!empty($real_value['width']) && (!empty($real_value['height']))) {
        $fields_html .= '<p class="kippis_image_size">' . __('Size:','kippis') . ' ' . $real_value['width'] . 'x' . $real_value['height'] . '</p>';
      }
      if (!empty($real_value['file'])) {
        $fields_html .= '<input type="text" readonly value="' . $real_value['url'] . '" class="kippis_image_filename" />';
      }
      $fields_html .= '<input class="kippis_image_upload" type="file" name="' . $option['id'] . '" id="' . $option['id'] . '" value="" />';
      $fields_html .= '</div>';
      break;

    case 'textarea':
      $style = ' style="width:100%;';
      if (isset($option['data'][0])) {
        if ($option['data'][0] == 'small') $style .= 'height:4em;';
      }
      $style .= '"';
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= '<label for="' . $option['id'] . '">'   . $option['title'] . '</label>';
      if (isset($option['desc' ])) $fields_html .= '<div class="kippis_options-description">' . $option['desc' ] . '</div>';
      if (isset($option['note' ])) $fields_html .= '<div class="kippis_options-note">'        . $option['note' ] . '</div>';
      $fields_html .= '<textarea name="'.$option['id'].'" id="'.$option['id'].'"' . $style . '>'.$real_value.'</textarea>';
      $fields_html .= '</div>';
      break;

    case 'font':
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= '<label for="' . $option['id'] . '">' . $option['title'] . '</label> &nbsp; ';
      if (isset($option['desc' ])) $fields_html .= '<div class="kippis_options-description">' . $option['desc'] . '</div>';
      if (isset($option['note' ])) $fields_html .= '<div class="kippis_options-note">'        . $option['note'] . '</div>';
      $id_size   = $option['id'] . '_size';
      $id_bold   = $option['id'] . '_bold';
      $id_italic = $option['id'] . '_italic';
      if ($kippis_has_data && (isset($arrOptions[$id_size]))) {
        $val_size   = isset($arrOptions[$id_size  ]) ? $arrOptions[$id_size  ] : '';
        $val_bold   = isset($arrOptions[$id_bold  ]) ? $arrOptions[$id_bold  ] : '';
        $val_italic = isset($arrOptions[$id_italic]) ? $arrOptions[$id_italic] : '';
      }
      else {
        $val_size   = $option_std[$id_size  ];
        $val_bold   = $option_std[$id_bold  ];
        $val_italic = $option_std[$id_italic];
      }
      $checked_bold   = $val_bold   == 'on' ? 'checked' : '';
      $checked_italic = $val_italic == 'on' ? 'checked' : '';
      if (isset($option['data'])) {
        $onchange_bold   = ' onchange="kippis_checkbox_css(' . "'" . $id_bold   . "','" . $option['data'] . "','font-weight','bold'  ,'normal'" . ');"';
        $onchange_italic = ' onchange="kippis_checkbox_css(' . "'" . $id_italic . "','" . $option['data'] . "','font-style' ,'italic','normal'" . ');"';
      }
      else {
        $onchange_bold   = '';
        $onchange_italic = '';
      }
      $fields_html .= kippis_get_font_select($id_size,$val_size,(isset($option['data']) ? $option['data'] : ''))
                   .  '<input' . $onchange_bold   . ' style="margin-left:1.2em;" type="checkbox" name="' . $id_bold . '" id="' . $id_bold . '" ' . $checked_bold . '>'
                   .  ' <span class="kippis_options-description">' . __('Bold','kippis') . '</span>'
                   .  '<input' . $onchange_italic . ' style="margin-left:1.2em;" type="checkbox" name="' . $id_italic . '" id="' . $id_italic . '" ' . $checked_italic . '>'
                   .  ' <span class="kippis_options-description">' . __('Italic','kippis') . '</span>'
                   . '<input style="margin-left:1.2em;" type="text" readonly class="font-picker" name="' . $option['id'] . '" id="' . $option['id'] . '" value="' . $real_value . '" />'
                   . '</div>';
      break;

    case 'radius':
      $fields_html .= '<div class="kippis_options-field">';
      $fields_html .= kippis_get_radius_select($option['id'],$real_value);
      if (isset($option['title'])) $fields_html .= ' <label for="' . $option['id'] . '">' . $option['title'] . '</label>';
      $fields_html .= '</div>';
      break;

    case 'text-align':
      $fields_html .= '<div class="kippis_options-field">';
      if (isset($option['title'])) $fields_html .= ' <label for="' . $option['id'] . '">' . $option['title'] . '</label> ';
      $fields_html .= kippis_get_text_align_select($option['id'],$real_value);
      $fields_html .= '</div>';
      break;

  }
  if (isset($option['class'])) $fields_html .= '</span>';
}

