// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL, winName, features);
}

function NewNicEditor(target) {
  bkLib.onDomLoaded(function() {
      new nicEditor({width : '100%', buttonList : ['bold','italic','underline','left','center','right','ol','ul','fontSize','indent','outdent','image','link','unlink','forecolor', 'bgcolor', 'removeformat', 'xhtml']}).panelInstance(target);
  })
}

function per_page(n) {
    uri = parseUri(location.href);
    uri.queryKey['page'] = 1;
    uri.queryKey['per_page'] = n;
    location.href = uri.href();
}

function order(by, direction) {
    uri = parseUri(location.href);
    uri.queryKey['order'] = by;
    uri.queryKey['page'] = 1;
    uri.queryKey['order_direction'] = direction;
    location.href = uri.href();
}

function estate_to_update_filter(day) {
    uri = parseUri(location.href);
    uri.queryKey['page'] = 1;
    uri.queryKey['to_update'] = day;
    location.href = uri.href();
}

function estate_ids_csv() {
    ids = collect_estate_ids();
    return ids.join(',');
}

function user_ids_csv() {
    ids = collect_user_ids();
    return ids.join(',');
}

function checkbox_toggle(css_rule, checked) {
    if (checked) {
	$$(css_rule).each(function(i) { i.checked = true; });
    } else {
	$$(css_rule).each(function(i) { i.checked = false; });
    }
}

function collect_estate_ids () {
    return $$('.estate_ids').collect(function(i) { if (i.checked) {return i.value;} }).uniq().select(function(n) {return n != null});
}

function collect_user_ids() {
    return $$('.user_ids').collect(function(i) { if (i.checked) {return i.value;} }).select(function(n) {return n != null});
}

function confirm_delete(form) {
    if (confirm('このデータを削除しますか？')) {
	if (confirm('削除したデータは復元できません')) {
	    form.submit();
	}
    }
}


function change_layout_form(token, layout) {
    $$('.' + token).each(function(i) {
	class_names = i.className.split(/\s/);
	if (class_names.include(layout)) {
	    i.show();
	} else {
	    i.hide();
	}
    });
}

function tokusenate(tokusenate, untokusenate, checked, estate_id, tokusen_id) {
    var url = (checked) ? tokusenate : untokusenate;
    new Ajax.Request(url,
		     {parameters:
		      "estate_id=" + estate_id + "&tokusen_id=" + tokusen_id
		     });
}

function toggle_rentou(checked) {
    if (checked) {
	$$('.rentou').each(function(i){i.innerHTML = '専有'});
    } else {
	$$('.rentou').each(function(i){i.innerHTML = '建物'});
    }
}