function TRedactor()
{
  this.ActivWizy=0;
  this.JQWizys=new Array();
  this.Wizys=new Array();
  this.SaveStr='';
  this.SaveCommand='';
}

TRedactor.prototype.Create=function()
{
  this.WizyWindow_OutlineActive="2px groove #f2cb80";
  this.WizyWindow_OutlineNoActiv="none";
  this.WizyWindow_OutlineAdvanced="2px dotted #f2cb80";
  this.WizyWindow_Outline=this.WizyWindow_OutlineNoActiv;
  //alert($("[id='redact_div/site/content']").length);
  //alert('ok');
  //this.Wizys=$("div[id^='redact_div/']").each(function(){alert(this.id);});
  this.JQWizys=$("div[id^='redact_div/']");
  var ar=$.makeArray(this.JQWizys);
  //alert(this.Wizys.length);
  
  $('#redactor_button_activate_wizys').click(function(){Redactor.Activate();});  
  $('#redactor_button_deactivate_wizys').click(function(){Redactor.Deactivate();});
  $('#redactor_button_outline_advanced').click(function(){
  if(this.checked){Redactor.WizyWindow_Outline=Redactor.WizyWindow_OutlineAdvanced;}
  else{Redactor.WizyWindow_Outline=Redactor.WizyWindow_OutlineNoActiv;}
  Redactor.Activate();
  });
  
  $('#redactor_button_add_bold').click(function(){document.execCommand('bold');Redactor.onChange();});
  $('#redactor_button_add_italic').click(function(){document.execCommand('italic');Redactor.onChange();});
  $('#redactor_button_add_underline').click(function(){document.execCommand('underline');Redactor.onChange();});


  /*
  $('#redactor_button_add_p_h1').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_p_h1').click(function(){document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p','h1');});

  $('#redactor_button_add_p_h2').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_p_h2').click(function(){document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p','h2');});

  $('#redactor_button_add_p').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_p').click(function(){document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p','');});
  
  $('#redactor_button_add_p_red').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_p_red').click(function(){document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p','red');});
  
  $('#redactor_button_add_p_data').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_p_data').click(function(){document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p','data');});
*/
  
  $('.-redactor_button_add_p').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('.-redactor_button_add_p').click(function(){
  aaa=explode('_',this.id);
  document.execCommand('formatblock', false, '<h4>');Redactor.FormatBlock('h4','p',aaa[4]);
  });
  
  
  
  
  //$('#redactor_button_add_img').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_img_submit').click(function(){//Redactor.Wizys[Redactor.ActivWizy].Div.focus();
  //window.getSelection().setPosition(3);
  document.execCommand('inserthtml', false,'<img src="'+$('#redactor_button_add_img_url')[0].value+'">');
  });

  $('#redactor_button_add_galimg_submit').click(function(){//Redactor.Wizys[Redactor.ActivWizy].Div.focus();
  //window.getSelection().setPosition(3);
  dsa='<div class="div_gal_java" contentedittable="false"><table class="div_gal_java_table"><tr style="height:100%;"><td class="div_gal_td_vmiddle"><table class="div_gal_java_table_table"><tr style="height:7px;"><td class="div_gal_fon_ul"><img src="/i.gif"></td><td class="div_gal_fon_u"><img src="/i.gif"></td><td class="div_gal_fon_ur"><img src="/i.gif"></td></tr><tr><td class="div_gal_fon_l"><img src="/i.gif"></td><td><div class="div_gal_java_subdiv"><img src="/pic/minigal/zoom.png"></div><a class="darkbox" href="'+$('#redactor_button_add_galimg_url2')[0].value+'"><img src="'+$('#redactor_button_add_galimg_url1')[0].value+'"></a></td><td class="div_gal_fon_r"><img src="/i.gif"></td></tr><tr style="height:15px;"><td class="div_gal_fon_dl"><img src="/i.gif"></td><td class="div_gal_fon_d"><img src="/i.gif"></td><td class="div_gal_fon_dr"><img src="/i.gif"></td></tr></table></td></tr></table></div> ';
  //document.execCommand('inserthtml', false,'<a class="darkbox" href="'+$('#redactor_button_add_galimg_url2')[0].value+'"><img src="'+$('#redactor_button_add_galimg_url1')[0].value+'"></a>');
  document.execCommand('inserthtml', false,dsa);
  Redactor.onChange();
  });
  
  $('#redactor_button_add_link_submit').click(function(){//Redactor.Wizys[Redactor.ActivWizy].Div.focus();
  //window.getSelection().setPosition(3);
  document.execCommand('CreateLink', false,$('#redactor_button_add_link_url')[0].value);
  Redactor.onChange();
  });
  
  
  $('#redactor_button_add_html_cat').mousedown(function(){return false}).each(function(){this.onselectstart=function(){return false;}});
  $('#redactor_button_add_html_cat').click(function(){//Redactor.Wizys[Redactor.ActivWizy].Div.focus();
  //window.getSelection().setPosition(3);
  document.execCommand('inserthtml', false, '<div style="width:180px;float:left;margin:15px;display:table;"><div style="width:160px;height:160px;overflow:hidden;border:10px solid #cccaca;" id="cat_img"><a href="#"><img src="/pic/nopic.gif"></a></div><p style="text-align:center;margin-left:0px;margin-right:0px;margin-top:5px;"><a href="#">Ссылка</a></p></div>');
  Redactor.onChange();
  });
  
  
  $('#save').click(function(){//alert('12');
  this.disabled=true;
  this.value='Сохранение...';
  Redactor.SaveStr='';
  Redactor.SaveCommand='';
  
  wizytosave=$('div.-redact_wizy_normal');
  wizynew=$('div.object_div_added div.-redact_wizy_not_real');
  //wizyx=
  $('div.-redact_wizy_not_real').each(function(){
  if($(this).parents('div.object_div').size()==0){
  //alert('yes');
  wizytosave=wizytosave.add($(this));
  }
  });
  wizytosave=wizytosave.add(wizynew);
  
  //wizynew.parents('div.object_div_contenner').
  wizyadded=$('div.object_div_added');
  wizydelete=$('div.object_div_delete');
  
  wizyadded.each(function(){
  parent=$(this).parents('div.object_div_contenner:first');
  id=parent.attr('id');
  m=explode('&',id);
  Redactor.SaveCommand+='add*|*'+this.id+'*|*'+m[2]+'*}*';
  });
  
  wizydelete.each(function(){
  parent=$(this).parents('div.object_div_contenner:last');
  id=parent.attr('id');
  m=explode('&',id);
  Redactor.SaveCommand+='del*|*'+this.id+'*|*'+m[2]+'*}*';
  });
  
  wizytosave.each(function(){
  Redactor.SaveStr+=this.id+'*|*'+this.innerHTML+'*}*';
  });
  
  //alert(Redactor.SaveCommand);
  
  
  $.ajax({
   type: "POST",
   url: "/ajax/module/redact/save.php",
   data: "data="+Redactor.SaveStr+"&command="+Redactor.SaveCommand,
   success: function(msg){
     //alert( "Data Saved: " + msg );
	 $('#save')[0].value='Сохранено';
	 //alert(msg);
   },
   error:function(){
     alert('Не удалось сохранить. Попробуйте позже.');
     $('#save')[0].value='Сохранить';
	 $('#save')[0].disabled=false;
   }
 });
  
  
  //alert(Redactor.SaveStr);
  //this.value='Сохраненно';
  });
 
 
  
  for(var key in ar)
  {
    //alert(ar[key].id);
	this.Wizys[ar[key].id]=new TWizy(ar[key].id);
  }
  //alert(this.Wizys[0]);
  this.Activate();
  $('#save')[0].disabled=true;
}

TRedactor.prototype.Activate=function()
{
  //this.Wizys[this.ActivWizy].onChange();
  for(var a in this.Wizys)this.Wizys[a].Activate();
  this.JQWizys.parents('a').click(function(){return false;});
  this.JQWizys.parents('a').css('-webkit-user-drag','none');
  //this.JQWizys.parents('a').css('-webkit-user-select','none');
  //this.JQWizys.css('-webkit-user-select','true');
  //this.JQWizys.parents('a').attr('href','');
}

TRedactor.prototype.Deactivate=function()
{
  for(var a in this.Wizys)this.Wizys[a].Deactivate();
  this.JQWizys.parents('a').unbind();
  this.JQWizys.parents('a').css('-webkit-user-drag','');
}

TRedactor.prototype.onChange=function()
{
  $('#save')[0].disabled=false;
  this.Wizys[this.ActivWizy].onChange();
}

TRedactor.prototype.FormatBlock=function(t1,t2,t2class)
{//alert('1');
  this.Wizys[this.ActivWizy].FormatBlock(t1,t2,t2class);
}
