// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggleExpand(id) {
	childId = 'children-' + id;
	controlId = 'control-' + id;
	if ($(childId).style.display == 'none') {
		$(childId).style.display = 'block';
		$(controlId).innerHTML = '-';
	} else {
		$(childId).style.display = 'none';
		$(controlId).innerHTML = '+';
	}
}


function updateFileTypes(el) {
  // hide all lower levels
  var spl = el.className.split("_");
  var lvl = parseInt(spl[1]);

  for (i=lvl+1;i<10;i++) {
    var targs = document.getElementsByClassName("file-type-select_"+i);
    for (j=0;j<targs.length;j++) {
      $(targs[j]).style.display = "none";
    }
  }
  
  // show the current child if exists
  if ($("menu_"+el.value)) {
    $("menu_"+el.value).style.display = "";
  }
}

function validateFileTypes(start_id, valid_fields) {
  if (valid_fields == "") {
    valid_fields = new Array();
  }
  
  var msg = "Please assign this asset to a valid category";
  if ($(start_id).value == "") {
    alert(msg);
    return false;
  } else {
    val = $(start_id).value;
    valid_fields[valid_fields.length] = val;
    if ($("menu_"+val)) {
      return validateFileTypes("menu_"+val,valid_fields);
    } else {
      return buildFileTypeValues(valid_fields);
    }
  }

}

function buildFileTypeValues(valid_fields) {
  var theform = $('file_type_form');
  for (i=0;i<valid_fields.length;i++) {
    
    var nf = document.createElement("input");
    nf.setAttribute("type", "hidden");
    nf.setAttribute("name", "file_type[ancestors]["+i+"]");
    nf.setAttribute("value",valid_fields[i]);
    theform.appendChild(nf);
    
  }
  return true;
  
}


function beforeReorderDim(id) {
  $(id).style.opacity = "0.4";
}

function prepareUpload() {
  var arrayPageSize = getPageSize();
  $('busy-uploading').style.height = arrayPageSize[1] + "px";
  
  $('busy-uploading').style.display = '';
  $('busy-uploading-status').style.display = '';

}



// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}