// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
sfHover = function() {
	var elements = $('main').getElementsByClassName("menu_items");
	var sfEls = [];
	for (var i=0; i < elements.length; i++) {
			element = elements[i];
			sfEls += element.getElementsByTagName("LI");
	}
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

popupHover = function() {
	var elements = $('main').getElementsByClassName("linked_image");
	var sfEls = elements;
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" popup_hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" popup_hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", popupHover);

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
		container.observe('change', function(e) {
      var el = e.element();
      if (el.match('.pagination a')) {
      	var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
        el.insert({ after: spinnerImg });
      	new Ajax.Request(el.href, { method: 'get' });
        e.stop();
      } else if (el.match('select.requireSpinner')) {
      	var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
        el.insert({ after: spinnerImg });
      }
    })  	
    container.observe('click', function(e) {
      var el = e.element();
      if (el.match('.pagination a')) {
      	var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
        el.insert({ after: spinnerImg });
      	new Ajax.Request(el.href, { method: 'get' });
        e.stop();
      } else if (el.match('input.requireSpinner') || el.match('.requireSpinner a') || el.match('a.requireSpinner')) {
      	var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
        el.insert({ after: spinnerImg });
      }
    })
  }
})

document.observe("dom:loaded", function() {
    var container = $(document.body);

    if (container) {
     var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
     var clicked = null;

     container.observe('click', function(e) {
      el = e.element();
      inputEl = $(el);
      if (inputEl.match('.disableSpinning')) { return; }

      if (el.match('.notSpinning')) {
       clicked = el;
       clicked.hide().insert({ after: spinnerImg });
      }

      if (el.match('.isSpinning')) {
       $(spinnerImg).remove();
       clicked.show();
      }
    })
  }
})

function startSpinner(e) {
	var spinnerImg = new Element('img', { src: '/images/ajax-loader.gif', 'class': 'isSpinning' });
	spinnerImg.id = "spinner_" + e.id;
	clicked = e;
	clicked.hide().insert({ after: spinnerImg });
}

function stopSpinner(buttonId) {
	spinnerId = "spinner_" + buttonId;
	clicked = $(buttonId);
	clicked.show();
	$(spinnerId).remove();	
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */
        var range = obj.createTextRange();
        range.move("character", pos);
        range.select();
    } else if(obj.selectionStart != null) {
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */
        obj.focus();
        obj.setSelectionRange(pos, pos);
 
		var evt = document.createEvent("KeyboardEvent");
		evt.initKeyEvent(
                 "keypress",        //  in DOMString typeArg,
                  true,             //  in boolean canBubbleArg,
                  true,             //  in boolean cancelableArg,
                  null,             //  in nsIDOMAbstractView viewArg,  Specifies UIEvent.view. This value may be null.
                  false,            //  in boolean ctrlKeyArg,
                  false,            //  in boolean altKeyArg,
                  false,            //  in boolean shiftKeyArg,
                  false,            //  in boolean metaKeyArg,
                   27,               //  in unsigned long keyCodeArg,
                   0);              //  in unsigned long charCodeArg);
		obj.dispatchEvent(evt);
    }
}

function browseForms(element) {
	var evt = document.createEvent("KeyboardEvent");
	evt.initKeyEvent(
               "keypress",        //  in DOMString typeArg,
                true,             //  in boolean canBubbleArg,
                true,             //  in boolean cancelableArg,
                null,             //  in nsIDOMAbstractView viewArg,  Specifies UIEvent.view. This value may be null.
                false,            //  in boolean ctrlKeyArg,
                false,            //  in boolean altKeyArg,
                false,            //  in boolean shiftKeyArg,
                false,            //  in boolean metaKeyArg,
                 32,               //  in unsigned long keyCodeArg,
                 0);              //  in unsigned long charCodeArg);
	obj.dispatchEvent(evt);
}

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) { return false; } 
	else { return true; }
}

function resetDataQueryAction() {
	$('data_query_action').value = 'Select action';
	$('actions_select').innerHTML = '';
}

function selectAllDataQueries() {
	var form = $('data_queries_index');
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 1 });
}

function unSelectAllDataQueries() {
	var form = $('data_queries_index');
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 0 });
}

function resetDataFormSetDataFormsAction() {
	$('data_form_set_data_form_action').value = 'Select action';
	$('actions_select').innerHTML = '';
}

function selectAllDataFormSetDataForms() {
	var form = $('data_form_set_data_forms_index');
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 1 });
}

function unSelectAllDataFormSetDataForms() {
	var form = $('data_form_set_data_forms_index');
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 0 });
}

function selectAll(form_id) {
	var form = $(form_id);
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 1 });
}

function unSelectAll(form_id) {
	var form = $(form_id);
	checkboxes = form.getInputs('checkbox');	
	checkboxes.each(function(e){ e.checked = 0 });
}

function resetCollectiveAction() {
	$('collective_action').value = 'Select action';
	$('actions_select').innerHTML = '';
}

function toggleBlind(header)
{
    if($(header.id+'Body').hasClassName('bodyClose'))
    {
        // open
        new Effect.BlindDown($(header.id+'Body'));
        $(header.id+'Body').addClassName('bodyOpen');
        $(header.id+'Body').removeClassName('bodyClosed');
    }
    else
    {
        // close
        new Effect.BlindUp($(header.id+'Body'));
        $(header.id+'Body').addClassName('bodyClosed');
        $(header.id+'Body').removeClassName('bodyOpen');
    }
}


/* from http://wiki.github.com/madrobby/scriptaculous/effect-appear */
function visibleAppear(id) {
	$(id).setStyle({visibility: 'visible'});
	new Effect.Opacity(
   id, { 
      from: 0.0, 
      to: 1.0,
      duration: 1.0
   }
);	
}

function toggle_show_message_reasons(id) {
	height = $(id).getStyle('height');
	if (height == '100%') {
		$(id).setStyle({ height: '210px'});
	} else {
		$(id).setStyle({ height: '100%'});
	}
}

function toggle_data_query_reasons(id) {
	height = $(id).getStyle('height');
	if (height == '100%') {
		$(id).setStyle({ height: '300px'});
	} else {
		$(id).setStyle({ height: '100%', maxHeight: 'none'});
	}
}

function toggle_display_forms(id) {
	height = $(id).getStyle('height');
	if (height == '100%') {
		$(id).setStyle({ height: '300px'});
	} else {
		$(id).setStyle({ height: '100%'});
	}
}

function toggle_height(id, ht) {
	height = $(id).getStyle('height');
	
	re = new RegExp(/(\d+)px$/);
	h1 = re.exec(height);
	h2 = re.exec(ht);
	set_ht = h1 && h1.length > 1 && h2 && h2.length > 1 ? h2[1] < h1[1] : false;
	if (h1 == null) { set_ht = true; }
	if (height == '100%' || set_ht) {
		$(id).setStyle({ height: ht })
	} else {
		$(id).setStyle({ height: '100%'})
	}
}

function create_hidden_commit_field(form_id, value) {
	var input = document.createElement("input");
	input.setAttribute("type", "hidden");
	input.setAttribute("name", "commit");
	input.setAttribute("value", value);
	document.getElementById(form_id).appendChild(input);
}