/*
 * UPDATED: 12.19.07
 *
 ******************************************** */


(function($){
  $.fn.jNice = function(options){
    var self = this;
    var safari = $.browser.safari; /* We need to check for safari to fix the input:text problem */
    var block_index = 100;
    if (options!=undefined && options['block_index']!=undefined)
        block_index = options['block_index'];
  
    /* each form */
    this.each(function(){
      /***************************
        Buttons
       ***************************/
             /*
      var setButton = function(){
        $(this).replaceWith('<button id="'+ this.id +'" name="'+ this.name +'" type="'+ this.type +'" class="'+ this.className +'"><span><span>'+ $(this).attr('value') +'</span></span>');
      };
      $('input:submit, input:reset', this).each(setButton);
            */
      
      /***************************
        Text Fields 
       ***************************/
             
             /*
      var setText = function(){
        var $input = $(this);
        $input.addClass("jNiceInput").wrap('<div class="jNiceInputWrapper"><div class="jNiceInputInner"><div></div></div></div>');
        var $wrapper = $input.parents('div.jNiceInputWrapper');
        $wrapper.css("width", $(this).width()+10);
        $input.focus(function(){
          $wrapper.addClass("jNiceInputWrapper_hover");
        }).blur(function(){
          $wrapper.removeClass("jNiceInputWrapper_hover");
        });
      };
      $('input:text:visible, input:password', this).each(setText);
      /* If this is safari we need to add an extra class */
            /*
      if (safari){$('.jNiceInputWrapper').each(function(){$(this).addClass('jNiceSafari').find('input').css('width', $(this).width()+11);});}
            */
      
      /***************************
        Check Boxes 
       ***************************/
      $('input:checkbox', this).each(function(){
        //if ($(this).hasClass('jNiceOff')) return false;
        $(this).addClass('jNiceHidden').wrap('<span></span>');
        var $wrapper = $(this).parent();
        $wrapper.prepend('<a href="#" class="jNiceCheckbox"></a>');
        /* Click Handler */
        $(this).siblings('a.jNiceCheckbox').click(function(){
            var $a = $(this);
            var input = $a.siblings('input')[0];
            
            //window.alert('fff-'+input.id);
            //$("#"+input.id).change();
            
            
            if (input.checked===true){
              input.checked = false;
              $a.removeClass('jNiceChecked');
            }
            else {
              input.checked = true;
              $a.addClass('jNiceChecked');
            }
            
            $(input).change();
            
            if (!$(input).hasClass('manual_check')) $(input).addClass('manual_check');
            if (function_exists('set_variants_groups')) {set_variants_groups();sync_jnice_checkboxes();}
            if (function_exists('change_dynamic_form')) change_dynamic_form();
            
            return false;
        });
        /* set the default state */
        if (this.checked){$('a.jNiceCheckbox', $wrapper).addClass('jNiceChecked');}
      });
      
      /***************************
        Radios 
       ***************************/
      $('input:radio', this).each(function(){
        $input = $(this);
        $input.addClass('jNiceHidden').wrap('<span class="jRadioWrapper"></span>');
        var $wrapper = $input.parent();
        $wrapper.prepend('<a href="#" class="jNiceRadio" rel="'+ this.name +'"></a>');
        /* Click Handler */
        $('a.jNiceRadio', $wrapper).click(function(){
            var $a = $(this);
            $a.siblings('input')[0].checked = true;
            
            
            $a.addClass('jNiceChecked');
            /* uncheck all others of same name */
            $('a[rel="'+ $a.attr('rel') +'"]').not($a).each(function(){
              $(this).removeClass('jNiceChecked').siblings('input')[0].checked=false;
            });
            return false;
        });
        /* set the default state */
        if (this.checked){$('a.jNiceRadio', $wrapper).addClass('jNiceChecked');}
      });
  
      
      /***************************
        Selects 
       ***************************/
      $('select', this).each(function(index){
        var $select = $(this);
        /* First thing we do is Wrap it */
        $(this).addClass('jNiceHidden').wrap('<div class="jNiceSelectWrapper"></div>');
        var $wrapper = $(this).parent().css({zIndex: block_index-index});
        /* Now add the html for the select */
        $wrapper.prepend('<div><span style="z-index:1000;overflow: hidden;"></span><a href="#" class="jNiceSelectOpen"></a></div><ul><li style="padding-top:10px; font-size:0; line-height:0; background:#fff;"><b style="border-top:solid 1px #bfbfbf;xwidth:238px; margin:0 0 0 10px;display:block; height:1px; font-size:0; line-height:0;"></b></li></ul>');
        var $ul = $('ul', $wrapper);
        /* Now we add the options */
        $('option', this).each(function(i){
          $ul.append('<li ' + (i===0?'style="display:none;"':'') + ' ><a href="#" ' + (i===0?'class="jNiceIndex0"':'') + ' index="'+ i +'" style=padding-top:10px;>'+ this.text +'</a></li>');
        });
        /* Hide the ul and add click handler to the a */
        var do_change_sel = false;
        $ul.hide().find('a').click(function(){//console.log('aaa1');
            $('a.selected', $wrapper).removeClass('selected');
            $(this).addClass('selected');  
            /* Fire the onchange event */
            //if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) { $select[0].selectedIndex = $(this).attr('index'); $select[0].onchange();}
            $select[0].selectedIndex = $(this).attr('index');
            $('span:eq(0)', $wrapper).html($(this).html());
            $ul.hide();
            //
            //$(this).change();
            //console.log(do_change_sel,'aaa1.9');
            if (do_change_sel) $select.change();//console.log(do_change_sel,'aaa2');
            if (function_exists('change_dynamic_form')) change_dynamic_form();//console.log('aaa3');
            //
            return false;
        });
        /* Set the defalut */
        
        $('a:eq('+ this.selectedIndex +')', $ul).click();
        do_change_sel = true;
      });/* End select each */
      
      /* Apply the click handler to the Open */
      $('a.jNiceSelectOpen', this).click(function(){
                            var $ul = $(this).parent().siblings('ul');
                            if ($ul.css('display')=='none'){hideSelect();} /* Check if box is already open to still allow toggle, but close all other selects */
                              $ul.slideToggle();
                            //var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
                            var offSet = ( ( $('a.selected', $ul)==undefined ? $('a.selected', $ul).offset().top : 0 ) - $ul.offset().top);
                            $ul.animate({scrollTop: offSet});
                            return false;
                        });
    
    }); /* End Form each */
    
    /* Hide all open selects */
    var hideSelect = function(){
      $('.jNiceSelectWrapper ul:visible').hide();
    };
    
    /* Check for an external click */
    var checkExternalClick = function(event) {
      if ($(event.target).parents('.jNiceSelectWrapper').length === 0) { hideSelect(); }
    };

    /* Apply document listener */
    $(document).mousedown(checkExternalClick);
    
      
    /* Add a new handler for the reset action */
    var jReset = function(f){
      var sel;
      $('.jNiceSelectWrapper select', f).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; $('ul', $(this).parent()).each(function(){$('a:eq('+ sel +')', this).click();});});
      $('a.jNiceCheckbox, a.jNiceRadio', f).removeClass('jNiceChecked');
      $('input:checkbox, input:radio', f).each(function(){if(this.checked){$('a', $(this).parent()).addClass('jNiceChecked');}});
    };
    this.bind('reset',function(){var action = function(){jReset(this);}; window.setTimeout(action, 10);});
    
  };/* End the Plugin */

  /* Automatically apply to any forms with class jNice */
  $(function(){$('form.jNice').jNice();  });

})(jQuery);
           

           
           
           
  //ADDON         
function reparseSelect(id) {//window.alert('-'+id); 
  var container = document.getElementById(''+id+'_container');
  if (container==null) return;
  //window.alert("#"+id+'_container');
  var select = $("#"+id); //document.getElementById(''+id);
    //window.alert(document.getElementById(''+id));
    if (document.getElementById(''+id+'_selectcheckbox_container')!=null) return;
  var selDiv = $('#'+id).parent('.jNiceSelectWrapper');
  selDiv = selDiv[0];
  //console.log(selDiv[0]);
  //window.alert(selDiv[0].innerHTML);
  var $ul = $('ul', selDiv);
  $ul.empty();
  //console.log($ul);
  //window.alert($ul);
  $('option', select).each(function(i){
    //window.alert('test'+ this.text);
    //console.log($ul);
    $ul.append('<li ' + (i===0?'style="display:none;"':'') + '><a href="#"  ' + (i===0?'class="jNiceIndex0"':'') + '  index="'+ i +'">'+ this.text +'</a></li>');
  });
  
  //var selecetedA;
  
  var $wrapper = $(selDiv);
  var $select = $(select);
        $ul.hide().find('a').click(function(){
            $('a.selected', $wrapper).removeClass('selected');
            $(this).addClass('selected');
            /* Fire the onchange event */
            if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) { $select[0].selectedIndex = $(this).attr('index'); $select[0].onchange(); }
            $select[0].selectedIndex = $(this).attr('index');
            $('span:eq(0)', $wrapper).html($(this).html());
            $ul.hide();
            return false;
        });
        /* Set the defalut */
        $('a:eq('+ '0' +')', $ul).click();//TODO this.selectedIndex
        
  //parentControlValueId = 
        
    //console.log($("option", select).get(selectedA).attr('value'));    
}


function reparseCheckboxes(container) {

//window.alert($('#'+container).$('input:checkbox'));

      //$('#'+container+' > input:checkbox').
      $('#'+container).find('input:checkbox').each(function(){
        $(this).addClass('jNiceHidden').wrap('<span></span>');
        var $wrapper = $(this).parent();
        $wrapper.prepend('<a href="#" class="jNiceCheckbox"></a>');
        /* Click Handler */
        $(this).siblings('a.jNiceCheckbox').click(function(){
            var $a = $(this);
            var input = $a.siblings('input')[0];
            if (input.checked===true){
              input.checked = false;
              $a.removeClass('jNiceChecked');
            }
            else {
              input.checked = true;
              $a.addClass('jNiceChecked');
            }
            
            if (!$(input).hasClass('manual_check')) $(input).addClass('manual_check');
            if (function_exists('set_variants_groups')) {set_variants_groups();sync_jnice_checkboxes();}
            
            return false;
        });
        /* set the default state */
        if (this.checked){$('a.jNiceCheckbox', $wrapper).addClass('jNiceChecked');}
      });
  
}


function sync_jnice_checkboxes() {
  $('a.jNiceCheckbox').each(function(){
    var $a = $(this);
    var input = $a.siblings('input')[0];
    if (input.checked===true){
      $a.addClass('jNiceChecked');
    } else {
      $a.removeClass('jNiceChecked');
    }
  });
}


function function_exists (function_name) {
    if (typeof function_name == 'string'){
        return (typeof this.window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}


function set_opposite_checkboxes(floor_key1, floor_key2) {
  var obj1 = $('#'+floor_key1);
  var obj2 = $('#'+floor_key2);
  //window.alert('fff'+obj1.val()+obj1.attr('checked'));

    if (obj1.attr('checked')) {//window.alert('qqq');!==true
      obj2.attr('checked', false);
      obj2.parent().find('a').removeClass('jNiceChecked');
    }

}

function get_ssite_kprop(ssite_type) {
  var ssite_types = {'2056':1, '2057':100, '2058':10000};
  var ssite_type_val = $("#"+ssite_type).val();
  var kprop;
  if (!(ssite_type_val>0)) kprop = 1;
  else kprop = ssite_types[ssite_type_val];
  return kprop;
}

function change_ssite_view(ssite_type, ssite, ssite_hidden) {
  var ssite_hidden_val = $("#"+ssite_hidden).val();
  if (ssite_hidden_val !== '')
    $("#"+ssite).val( ssite_hidden_val / get_ssite_kprop(ssite_type) );
}

function change_ssite_value(ssite_type, ssite, ssite_hidden) {
  $("#"+ssite_hidden).val( $("#"+ssite).val() * get_ssite_kprop(ssite_type) );
}

/*$(document).ready(function(){
$("input[type='checkbox']").change( function() {set_variants_groups();});
});*/
