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

//Test is IE to disable modal popup (bug with IE on z-index)
var isModal=false;
if(typeof(isIE) == "undefined"){
//if(typeof(isIE) == undefined){  // TODO check this
	isModal=true;
}else{
	isModal=false;
}

//Roll src on all elements with this class on condition
function rollover_if(should_roll, class_id, over_src, out_src, is_mouseover){
	if(should_roll){
		var e = document.getElementsByClassName(class_id);
		if(is_mouseover){
				//e.src=over_src;
				for (var i=0; i<e.length; i++) { 
          e[i].src=over_src;
        }
		}else{
				//e.src=out_src;
				for (var i=0; i<e.length; i++) { 
          e[i].src=out_src;
        }
		}
	}	
	return true;
}

// Set focus to first field
function f_setfocus(aForm){
	if( aForm.elements[0]!=null) {
		var i;
		var max = aForm.length;
		for( i = 0; i < max; i++ ) {
			if( aForm.elements[i].type != "hidden" &&
				!aForm.elements[i].disabled &&
				!aForm.elements[i].readOnly ) {
				$(aForm.elements[i].id).focus();
				//aForm.elements[i].focus();
				break;
			}
		}
	}
}

// Retrieve last form with a defined class
function last_form(){
	c = document.forms
	if(c.length == 0){
		return null;
	}else if(c.length == 1){
		return c[0];
	}else{
		i=0;
		while( (i<c.length) && (c[i].className!=undefined) ){
			i++;
		}
		return document.forms[i-1];
	}
}

//Set focus to last form if defined
function autofocus(){
	var form = last_form();
	if(form != null){
		f_setfocus(form);
	}
}

//Will enable or disable every form fields received
//There is an optionnal parameter that clear field value on disable
function toggleEnable(enable, fields, clearOnDisable){
	if(enable){
		for(var i=0; i<fields.length; i++){
			if($(fields[i])){
				$(fields[i]).enable();
			}
		}
	}else{
		for(var i=0; i<fields.length; i++){
			if($(fields[i])){
				if(clearOnDisable){
					$(fields[i]).clear();
				}
				$(fields[i]).disable();
			}
		}
	}
}

// If a form exist focus the first one and prevent unsaved form
Event.observe(window, 'load', function() {
	if(document.forms[0]){
		//var fp = new FormProtector( document.forms[0] );
		//fp.setMessage('');
		autofocus();
	}
});

//Event.observe(window, 'beforeunload', function() {	
		//alert('beforeunload !');	
//});

//Convert will_paginate to ajax call (see lowpro.js)
Event.addBehavior.reassignAfterAjax = true;
Event.addBehavior({
  'div.pagination a' : Remote.Link
})