// This file is automatically included by javascript_include_tag :defaults
// 
// (function(){
// 
// 	/*Use Object Detection to detect IE6*/
// 	var  m = document.uniqueID /*IE*/
// 	&& document.compatMode  /*>=IE6*/
// 	&& !window.XMLHttpRequest /*<=IE6*/
// 	&& document.execCommand ;
// 	
// 	try{
// 		if(!!m){
// 			m("BackgroundImageCache", false, true) /* = IE6 only */ 
// 		}
// 		
// 	}catch(oh){};
// })();


// Load testimonials after page is loaded completely
function loadTestimonials(variety, page, type) {
  var baseUrl = "/varieties/async_testimonials" ;
  var pars = "variety_id=" + variety + "&page=" + page + "&type=" + type;

  new Ajax.Updater(type + "-testimonials", baseUrl, {
            method: "get",
            parameters: pars
  });
}

//used by status form to reset status input field when blank
function populateStatusForm(id, msg){
    element = document.getElementById(id);
    if(element.value == ""){
	element.value = msg;
    }
}

function setPlaceholderText(el){
	if (el.value == '') el.value = el.defaultValue;
	else if (el.value == el.defaultValue) el.value = '';
}

function setHolder(id) {
	element = document.getElementById(id);
	if((element).value == (element).defaultValue) {
		(element).value = '';
	}
}

function placeholderText(id, msg){
    element = document.getElementById(id);
    if(element.value == ""){
	element.value = msg;
    }
}


//START SUSPECT FOR DELETION - PREVIOUSLY USED BY STATUS FORM,
//UNKNOWN OTHER USES?

function setCaretPosition (oField, iCaretPos) {
	if (document.selection) {
		oField.focus ();
		var oSel = document.selection.createRange ();
		oSel.moveStart ('character', -oField.value.length);
		oSel.moveStart ('character', iCaretPos);
		oSel.moveEnd ('character', 0);
		oSel.select ();
	}
	else if (oField.selectionStart || oField.selectionStart == '0') {
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus ();
	}
}

function setStatusPlaceholder(element, msg, del, len) {
	if(document.getElementById(element).value == msg){
		document.getElementById(element).value = del;
		$('post_btn').show();
	} else if(document.getElementById(element).value == del) {
		document.getElementById(element).value = msg;
		$('post_btn').hide();
	}
}
//END SUSPECT FOR DELETION

function appendToSearch(text_field_id, string){
	if ($(text_field_id).value.match(/^.*\ssearch/i)) {
		$(text_field_id).value = string;
	}
	else {
		$(text_field_id).value += " " + string;
	}
}


function setOwnership(element, msg) {
	document.getElementById(element).value = msg;
}

function swapForPasswordField(field){
  field.className = "hidden";
  document.getElementById("password").className = "";
  document.getElementById("password").focus();
}
function swapForTextField(field){
  //we only want to preform this swap if the user hasn't entered a password
  if (field.value == ""){
    field.className = "hidden";
    document.getElementById("password_placeholder").className = "";
  }
}
function clearText(field){
  if (field.defaultValue == field.value) field.value = '';
  else if (field.value == '') field.value = field.defaultValue;
}

tagArray = [];

//This is expecting a div of class "tag" and the tag itself
function swapTag(tagDiv, tagInputField, tagString){

  if(tagDiv.up('#unselected-tags') != undefined){
    tagDiv.remove();
    $('selected-tags').insert(tagDiv);
    tagArray.push(tagString);

  }else{
    tagDiv.remove();
    $('unselected-tags').insert(tagDiv);
    tagArray = tagArray.without(tagString);
  }
  tagInputField.value = tagArray.join(",");
  return false;
}
// -------------------------
// Bar Chart
// -------------------------

var options = {
  colors: ["#1B583D"],
  yaxis: {
    min: 0,
    max: 9,
    tickFormatter: function(x){return 10-x;},
    tickSize: 1,
    tickDecimals: 0
  },
  xaxis: {
    min: 0.5,
    max: 7.5,

  ticks:[ [1,"Seed Appearance"],
	  [2,"Seed Vigor"],
	  [3,"Stand Establishment"],
	  [4,"Storm Hardiness"],
	  [5,"Picking Ease/Clean"],
	  [6,"Dry Condition Tolerance"],
	  [7,"Will<br\> Plant Again"]]
  },
  grid:{
    drawXAxis: false
  },
  bars: {
    show: true
  }
}
function plot(data){
	 new Proto.Chart($('barchart'), data, options);
}

// -------------------------
// Bar Chart
// -------------------------

// var options = {
// 	colors: ["#1B583D"],
//  	yaxis: {
// 		min: 0,
// 		tickSize: 1,
// 		tickDecimals: 0
// 	},
// 	xaxis: {
//  		min: 0,
//  		max: 10,
//  		tickSize: 1,
// 		tickDecimals: 0
//  	},
//  	bars: {
//  		show: true
//  	}
// }

// -------------------------
// Multiple File Upload
// -------------------------
function MultiSelector(list_target, max) {
  this.list_target = list_target;
  this.count = 0;
  this.id = 0;
  if( max ){
    this.max = max;
  }
  else {
    this.max = -1;
  };
  this.addElement = function( element ){

    if( element.tagName == 'INPUT' && element.type == 'file' ){
      element.name = 'images[image_' + (this.id++) + ']';
      element.multi_selector = this;
      element.onchange = function(){
	var new_element = document.createElement( 'input' );
	new_element.type = 'file';
	this.parentNode.insertBefore( new_element, this );
	this.multi_selector.addElement( new_element );
	this.multi_selector.addListRow( this );
	this.style.position = 'absolute';
	this.style.left = '-1000px';
      };
      if( this.max != -1 && this.count >= this.max ){
	element.disabled = true;
      };
      this.count++;
      this.current_element = element;
    }
    else {
      alert( 'Error: not a file input element' );
    };};
  this.addListRow = function( element ){
    var new_row = document.createElement('li');
    var new_row_button = document.createElement( 'a' );
    new_row_button.title = 'Remove This Image';
    new_row_button.href = '#';
    new_row_button.innerHTML = '<span class=\'photos-remove\'>(remove)</span>';
    new_row.element = element;
    new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );
				       this.parentNode.parentNode.removeChild( this.parentNode );
				       this.parentNode.element.multi_selector.count--;
				       this.parentNode.element.multi_selector.current_element.disabled = false;
				       return false;};
    new_row.innerHTML = element.value.split('/')[element.value.split('/').length - 1];
    new_row.appendChild( new_row_button );
    this.list_target.appendChild( new_row );
  };
}


// -------------------------
// Functions for handling default text on login forms - the login forms
// include that on the home page and the guest_login sidebar.  Note,
// the default text of input fields in these forms are background images
// NOT text - this is done to get around password fields not handling
// default text like regular text fields
// -------------------------

function clearBg(field){
      if (field.defaultValue == field.value){
        field.value = field.defaultValue;
        field.className = 'sidebar-field';
      }
    }

    function clearOrSetEmailBg(field){
      if (field.defaultValue == field.value){
        field.value = '';
        field.className = 'inactive-email-text';
      }else if (field.value == ''){
        field.value = field.defaultValue;
        field.className = 'inactive-email-text';
      }
    }

    function clearOrSetPasswordBg(field){
      if (field.defaultValue == field.value){
        field.value = '';
        field.className = 'inactive-password-text';
      }else if (field.value == ''){
        field.value = field.defaultValue;
        field.className = 'inactive-password-text';
      }
    }

function showElement(id){
  Element.show(id);
}

function hideElement(id){
  Element.hide(id);
}