function disableSelectors(_type){
  $('input[name=castor_type]:radio').attr('disabled', true);
  if (_type != 'castor'){
    $('select[name=castor_categories_id]').attr('disabled', true);
  }
  var _select_value = '';
  $.each(_page.search_options, function(key, value){
      if (value != undefined){
        _select_value = $('select[name=castor_attributes_' + key + '] option:selected').val();
        if (_select_value > 0){
          $('select[name=castor_attributes_' + key + ']').attr('disabled', true);
        }
      }
  })
  $('#search_button').attr('disabled', true);
}

function enableSelectors(){
  $('input[name=castor_type]:radio').attr('disabled', false);
  $('select[name=castor_categories_id]').attr('disabled', false);
  $('select[name=castor_categories_id]').removeClass('expanded-width');
  var _select_value = '';
  $.each(_page.search_options, function(key, value){
      if (value != undefined){
        _select_value = $('select[name=castor_attributes_' + key + '] option:selected').val();
        if (_select_value > 0){
          $('select[name=castor_attributes_' + key + ']').attr('disabled', false);
          $('select[name=castor_attributes_' + key + ']').removeClass('expanded-width');
        }
      }
  })
  $('#search_button').attr('disabled', false);
}


function createPage(){
  this.products_id = 0;
  this.search_options = new Array();


  this.selectIEFix = function(){
    $('select.ie-drop-down-fix').bind('mousedown', function(){
      if(document.all) $(this).addClass('expanded-width');
    });
    $('select.ie-drop-down-fix').bind('blur change', function(){
      $(this).removeClass('expanded-width');
    });
  }
  this.searchUpdate = function(_type){
    var _data = 'categories_id=' + $('select[name=castor_categories_id] option:selected').val() + '&attributes=';
    var _rel_data = '';
    $.each(this.search_options, function(key, value){
      if (value != undefined){
        var _select_value = $('select[name=castor_attributes_' + key + '] option:selected').val();
        var _select_text = $('select[name=castor_attributes_' + key + '] option:selected').text();
        var _select_rel = $('select[name=castor_attributes_' + key + ']').attr('rel');
        if (_select_value == '0' || _type == '' || _type == 'castor'){
          $('#castor_attributes_' + key + '_wrapper').html('<div align="center"><img border="0" src="images/ajax_loader_black.gif"></div>');
        }
        if (_select_value != undefined){
          _data += '{' + key + '}' + _select_value;
        }
        if (_select_rel != undefined){
          _rel_data += '{' + key + '}' + _select_text;
        }
      }
    });

    _action_type = _type;

    disableSelectors(_type);

    if (_type != ''){
      $('#castor_categories_id_wrapper').html('<div align="center"><img border="0" src="images/ajax_loader_black.gif"></div>');
    }
    _data += '&castor_type=' + $('input[name=castor_type]:checked').val();

    var _req = new createRequest('ajax/ajax_get.php');
    _req._action = 'search_refresh_products';
    _req.sendRequest('action=search_refresh_products&' + _data + '&attributes_rel=' + _rel_data + '&key=' + _type);
  }
  this.checkAttributesRelation = function(){
    $.each(this.search_options, function(key, value){
      if (value != undefined){
        if (value['related_option'] != undefined){
          var _selected_option_value = $('select[name=castor_attributes_' + key + '] option:selected').val();
          $.each($('select[name=castor_attributes_' + value['related_option'] + '] option'), function(key1, value1){
            value1.disabled = false;
          });
          if (_page.search_options[key]['options'][_selected_option_value] != undefined){
            next_value = false;
            $.each($('select[name=castor_attributes_' + value['related_option'] + '] option'), function(key1, value1){
              if (_page.search_options[key]['options'][_selected_option_value][value1.value] != undefined ){
                value1.disabled = true;
                if (value1.selected){
                  update = 'true';
                  next_value = true;
                }
              }
            });
            if (next_value){
              $.each($('select[name=castor_attributes_' + value['related_option'] + '] option'), function(key1, value1){
                if (!value1.disabled){
                  value1.selected = true;
                }
              });
            }
          }
        }
      }
    });
  }
  this.refreshProducts = function(categories_id, key){
    var _selected_option_value = $('select[name=special_options_' + key + '] option:selected').val();
    $('#products_data').html('<img border="0" src="images/ajax_loader_black.gif">');
    $('#products_data').addClass('table_cel_right_top2');
    var _req = new createRequest('ajax/ajax_get.php');
    _req._action = 'refresh_products';
    _req.sendRequest('action=refresh_products&categories_id=' + categories_id + '&selected_option=' + key + '&selected_option_value=' + _selected_option_value);
  }
  this.selectUpdatePrice = function(key){
    var _selected_option_value = $('select[name=id[' + key + ']] option:selected').val();
    _options[key] = _selected_option_value;
    var send_options = '';
    $.each(_options, function(key1, value1){
      if (value1 != undefined && value1 != 0){
        send_options += key1 + '}' + value1 + '^_^';
      }
    });
    $('#products_price').html('<img border="0" src="images/ajax_loader_line.gif">');
    var _req = new createRequest('ajax/ajax_get.php');
    _req._action = 'refresh_price';
    _req.sendRequest('action=refresh_price&products_id=' + this.products_id + '&send_options=' + send_options);
  }
  this.checkRelation = function(key, update){
    var _selected_option_value = $('select[name=id[' + key + ']] option:selected').val();
    if (_special_relationship[key] != undefined){
      $.each($('select[name=id[' + _special_relationship[key]['related_option'] + ']] option'), function(key1, value1){
        value1.disabled = false;
      });
      if (_special_relationship[key]['options'][_selected_option_value] != undefined){
        next_value = false;
        $.each($('select[name=id[' + _special_relationship[key]['related_option'] + ']] option'), function(key1, value1){
          if (_special_relationship[key]['options'][_selected_option_value][value1.value] != undefined){
            value1.disabled = true;
            if (value1.selected){
              update = 'true';
              next_value = true;
            }
          }
        });
        if (next_value){
          $.each($('select[name=id[' + _special_relationship[key]['related_option'] + ']] option'), function(key1, value1){
            if (!value1.disabled){
              value1.selected = true;
            }
          });
          $.each(_relationship[_special_relationship[key]['related_option']], function(key1, value1){
            if (value1 != undefined){
              $('select[name=id[' + key1 + ']]').val('0');
              if (value1 != _selected_option_value){
                $('select[name=id[' + key1 + ']]').attr('disabled', true);
                $('select[name=id[' + key1 + ']]').addClass('drop_menu_3_gray');
                $('#container_wrapper_dropdown_' + key1).addClass('drop_menu_3_gray');
              }
              else{
                $('select[name=id[' + key1 + ']]').attr('disabled', false);
                $('select[name=id[' + key1 + ']]').removeClass('drop_menu_3_gray');
                $('#container_wrapper_dropdown_' + key1).removeClass('drop_menu_3_gray');
              }
              _options[key1] = 0;
            }
          });
        }
      }
    }
//    $.each($('select[name=id[' + key + ']] option'), function(key1, value1){
//      alert(key1 + "\n" +  value1.value + "\n" + value1.text);
//    })
//    alert(_selected_option_value);
//    alert(_relationship[key]);
    $.each(_relationship[key], function(key1, value1){
      if (value1 != undefined){
        $('select[name=id[' + key1 + ']]').val('0');
        if (value1 != _selected_option_value){
          $('select[name=id[' + key1 + ']]').attr('disabled', true);
          $('select[name=id[' + key1 + ']]').addClass('drop_menu_3_gray');
          $('#container_wrapper_dropdown_' + key1).addClass('drop_menu_3_gray');
        }
        else{
          $('select[name=id[' + key1 + ']]').attr('disabled', false);
          $('select[name=id[' + key1 + ']]').removeClass('drop_menu_3_gray');
          $('#container_wrapper_dropdown_' + key1).removeClass('drop_menu_3_gray');
        }
        _options[key1] = 0;
      }
    });
    if (update == 'true'){
      if (_timeout != ''){
        clearTimeout(_timeout);
      }
      $('#products_price').html('<img border="0" src="images/ajax_loader_line.gif">');
      _timeout = setTimeout("_page.selectUpdatePrice(" + key + ")", 2000);
    }
  }
}

function groupsClass(){
  this.groups_id = '';
  this.used_options = new Array();
  this.used_options_values = new Array();
  this.current_option = 0;

  this.getOptions = function(){
    $('#available_options').html('<img border="0" src="../images/ajax_loader_line.gif">');
    var _req = new createRequest('ajax/ajax_get.php');
    _req._action = 'available_options';
    _req.sendRequest('action=available_options&groups_id=' + this.groups_id + '&used_options=' + this.used_options);
  }
  this.getOptionsValues = function(){
    var _selected_options = $('select[name=available_options] option:selected').val();
    $('#available_options_values').html('<img border="0" src="../images/ajax_loader_line.gif">');
    this.current_option = _selected_options;
    var _req = new createRequest('ajax/ajax_get.php');
    _req._action = 'available_options_values';
    _req.sendRequest('action=available_options_values&groups_id=' + this.groups_id + '&selected_options=' + _selected_options + '&used_options_values=' + this.used_options_values[_selected_options]);
  }
  this.confirmSelectedOptions = function(){
    var _selected_option = $('select[name=available_options] option:selected').val();
    this.used_options[_selected_option] = _selected_option;
    if (this.used_options_values[_selected_option] == undefined){
      this.used_options_values[_selected_option] = new Array();
    }
    var _options_content = '';
    var false_all = false;
    $('select[name=available_options_values] option:selected').each(function(){
      if ($(this).val() != '0'){
        false_all = false;
        if ($('#option_' + _selected_option + '_0')){
          $('#option_' + _selected_option + '_0').remove();
        }
        if ($('#option_' + _selected_option + '_' + $(this).val()).html() == null ){
          _options_content += '<div id="option_' + _selected_option + '_' + $(this).val() + '"><input type="hidden" id="hidden_value_' + _selected_option + '_' + $(this).val() + '" value="' + $(this).text() + '"><input type="hidden" name="selected_options_values[' + _selected_option + '][]" value="' + $(this).val() + '"><a href="#" onclick="javascript: groups.deleteOptionValue(\'' + _selected_option + '_' + $(this).val() + '\')"><img border="0" src="images/icons/cross.gif"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + $(this).text() + '</div>';
          groups.used_options_values[_selected_option][$(this).val()] = $(this).val();
          $(this).remove();
        }
      }
      else{
        false_all = true;
      }
    });
    if (_options_content == '' && !false_all){
      _options_content += '<div id="option_' + _selected_option + '_0"><input type="hidden" name="selected_options_values[' + _selected_option + '][]" value="all">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all options</div>';
    }
    var _content = '';
    if ($('#option_' + _selected_option).html() != null){
      _content += _options_content;
      $(_content).appendTo('#option_' + _selected_option);
    }
    else{
      _content += '<div class="selected-option float-left" id="option_' + _selected_option + '"><input type="hidden" name="selected_options[]" value="' + _selected_option + '">';
      _content += '<div><a href="#" onclick="javascript: groups.deleteOption(' + _selected_option + ')"><img border="0" src="images/icons/cross.gif">&nbsp;&nbsp;' + $('select[name=available_options] option:selected').text() + '</a></div><div style="font-size: 1px; height: 10px;">&nbsp;</div>';
      _content += _options_content;
      _content += '</div>';
      $(_content).prependTo('#selected_options');
    }
//    this.getOptionsValues();
  }
  this.deleteOption = function(_selector){
    if ($('#option_' + _selector).html() != null){
      $('#option_' + _selector).remove();
      this.used_options[_selector] = null;
      this.used_options_values[_selector] = new Array();
      this.getOptions();
    }
  }
  this.deleteOptionValue = function(_selector){
    if ($('#option_' + _selector).html() != null){
      var _temp = _selector.split('_');
      if ($('select[name=available_options_values]').html() != null && this.current_option == _temp[0]){
        $('select[name=available_options_values]').append($('<option></option>').attr('value', _temp[1]).text($('#hidden_value_' + _selector).val()));
      }
      $('#option_' + _selector).remove();
      this.used_options_values[_temp[0]][_temp[1]] = null;
//      this.getOptionsValues();
    }
  }
}

var _timeout = ''
var _action = '';
var _action_type = '';
function createRequest(url){
    this._action = '';
    this.url = url;

    this.sendRequest = function(data){
        _action = this._action;
        $.ajax({
            type: 'POST',
            url: this.url,
            data: data,
            success: this._success,
            dataType: 'html'
        });
    }
    this._success = function(data){
        switch (_action){
          case 'refresh_products':
            $('#products_data').removeClass('table_cel_right_top2');
            $('#products_data').html(data);
          break;
          case 'refresh_price':
            $('#products_price').html(data);
          break;
          case 'search_refresh_products':
            if (data.indexOf('@#$%') > 0 && data.indexOf('_^_') > 0){
              data = data.split('@#$%');
              data = data[1].split('_^_');
              var _index = 0;
              var temp = '';
              while (_index < data.length){
                if (data[_index] != ''){
                  temp = data[_index].split('^_^');
                  if (temp.length == 2){
                    if (isNaN(temp[0])){
                      $('#' + temp[0]).html(temp[1]);
                    }
                    else if ($('#castor_attributes_' + temp[0] + '_wrapper')){
                      $('#castor_attributes_' + temp[0] + '_wrapper').html(temp[1]);
                    }
                  }
                }
                ++_index;
              }
            }
            enableSelectors();
            _page.checkAttributesRelation();
            _page.selectIEFix();
          break;
          case 'available_options':
            $('#available_options').html(data);
            $('#available_options_values').html('');
          break;
          case 'available_options_values':
            $('#available_options_values').html(data);
          break;
          default:
            alert('add action!');
          break;
      }
    }
    this._failure = function(){
        alert('File could not be found by Ajax!');
    }
}

