Event.observe(window, 'load', init);

function init() {
  rTLines();
  Nifty("div#nifty_block","big");
  Nifty("div.announcement","medium");
  Nifty("div.download_info","medium");
  initCButs('.table .fn');
  initFButs('.table .folder .prv_c, .table .folder .prv_o');
}
function resetObs(){
  $$('.table .f .n').each(function(text_area){
    text_area.stopObserving('click', null);
    text_area.setAttribute('title', '');
  });
}
function initCButs(cssRule){
  $$(cssRule).each(function(cel){
    if(cel.down('.del', 0) != null){
      cel.observe('mouseover', function(){
        if(this.down('form') == null && this.up('tr', 0).hasClassName('hover')==false) this.down('.del', 0).show();
      });
      cel.observe('mouseout', function(){
        if(this.down('form') == null) this.down('.del', 0).hide();
      });
    }
    if(cel.down('.ren', 0) != null){
      cel.observe('mouseover', function(){
        if(this.down('form') == null && this.up('tr', 0).hasClassName('hover')==false) this.down('.ren', 0).show();
      });
      cel.observe('mouseout', function(){
        if(this.down('form') == null) this.down('.ren', 0).hide();
      });
    }
  });
  
  $$('.table .ren').each(function(ren_but){
    ren_but.observe('click', function(){
      this.up(1).down('.del').hide();
    });
  });
  
  resetObs();
}
function initFButs(cssRule){
  $$(cssRule).each(function(fo_but){
    fo_but.observe('click', function(){
      var this_tr = this.up(1);
      var this_id = this_tr.id.split('_').last();
      if(fo_but.hasClassName('prv_c')){
        fo_but.removeClassName("prv_c");
        fo_but.addClassName("prv_o");
        this_tr.insert({ after: '<tr class="sub tmp_'+this_id+'"><td colspan="6"><img src="/images/spinner.gif" alt="Загрузка..." width="16" height="16" /></td></tr>'});
        var sub_tr = this_tr.next('tr', 0);
        new Ajax.Request(this_tr.down('a', 0).readAttribute('href')+'?only_items=1', {asynchronous:true, evalScripts:true, method:'get',
          onSuccess: function(response) {
            sub_tr.replace(response.responseText);
            initCButs('.table .tmp_'+this_id+' .fn');
            initFButs('.table .tmp_'+this_id+' .prv_c, .table .tmp_'+this_id+' .prv_o');
            rTLines();
          } });
      }else{
        DelFChildsItemsInDOM(this_id);
        fo_but.removeClassName("prv_o");
        fo_but.addClassName("prv_c");
      }
      rTLines();
    });
  });
}

function DelFChildsItemsInDOM(folder_id){
  var all_of = GetOpenedFolders(folder_id);
  var cssR = '.table .tmp_' + all_of.join(', .table .tmp_');
  $$(cssR).invoke('remove');
}

function MovedFileInDOM(file, to_folder){
  if(to_folder==null || to_folder.down('.fn div.prv_c', 0) != null){
    file.remove();
  }else{
    var mF = file.remove();
    to_folder.insert({ after: mF});
    var pf_id = 0;
    var a_classes = $w(to_folder.className);
    for (var i=0;i<a_classes.length;i++){
      if(a_classes[i].indexOf('sub_') == 0){
        pf_id = parseInt(a_classes[i].split('_').last());
        break;
      }
    }
    $w(mF.className).forEach( function(c_name){
      if(c_name.indexOf('sub_') == 0) mF.removeClassName(c_name);
    });
    mF.addClassName('sub_'+(pf_id+1));
    $w(mF.className).forEach( function(c_name){
      if(c_name.indexOf('tmp_') == 0) mF.removeClassName(c_name);
    });
    mF.addClassName('tmp_'+to_folder.id.split('_').last());
    mF.show();
    mF.down(1).next(0).show();
  }
  rTLines();
}

function GetOpenedFolders(childs_in){
  var open_folders = new Array();
  open_folders.push(childs_in);
  $$('.table .tmp_'+childs_in+' .prv_o').each(function(folder_prv){
    var f_id = folder_prv.up('tr', 0).id.split('_').last();
    open_folders.push(f_id);
    open_folders = open_folders.concat(GetOpenedFolders(f_id));
  });
  return open_folders;
}

function rTLines(){
  var j = 0;
  $$('*.table tbody tr').each(function(tog, i){
    tog.removeClassName("zebracolor");
    tog.removeClassName("i");
    if(tog.visible()){
      if(j%2==0) {
        tog.addClassName("zebracolor");
        tog.addClassName("i");
      }
    }else{
      j--;
    }
    j++;
  });
}

var win = null;
function openPreview(id_file, title) {
	if(win==null) win = new Window('dialog1', {className: "yaba",  width:400, height:300, zIndex: 100, resizable: false, draggable:false, showEffect: Element.show, hideEffect: Element.hide, maximizable: false, minimizable: false });
	if(!win.isVisible()){
		win.setTitle(title||'&nbsp;');
		win.setAjaxContent("/ajax/files/" + id_file + "/preview", {method: 'get'}, true, true);
		win.showCenter();
	}
	return false;
}
function clouseWin() {
	if(win!=null) win.close();
}


function dosms(f){
	n = f.selectedIndex;
	if(n){
		if (f.options[n].getAttribute('smsnot') == 'not') {
			$('sms').style.display = "none";
		} else {
			$('sms').style.display = "block";
			$('sms_prefix').innerHTML = f.options[n].getAttribute('smsprefix');
			$('sms_number').innerHTML = f.options[n].getAttribute('smsnumber');
			$('sms_price').innerHTML = f.options[n].getAttribute('smsprice');
			$('sms_currency').innerHTML = f.options[n].getAttribute('smscurrency');
			$('sms_vat').innerHTML = f.options[n].getAttribute('smsvat') == '1' ? "включая НДС" : "не включая НДС"
			
			$('sms_prefix_eng').innerHTML = f.options[n].getAttribute('smsprefix');
			$('sms_number_eng').innerHTML = f.options[n].getAttribute('smsnumber');
			$('sms_price_eng').innerHTML = f.options[n].getAttribute('smsprice');
			$('sms_currency_eng').innerHTML = f.options[n].getAttribute('smscurrency');
			$('sms_vat_eng').innerHTML = f.options[n].getAttribute('smsvat') == '1' ? "with VAT" : "without VAT"
		}
	}
}

