/** Devnote: if you have more "from->tos" you'll need to create another rule, foreach one. **/

//Validation.add('validate-AddBasicEvent-event_price_from', 'The amount you entered \'from\' is bigger then the \'to\' field, please try again.', {
//		lessThenField : 'AddBasicEvent[event_price_to]'
//});

Validation.add('validate-AddBasicEvent-event_price_to', 'The amount you entered \'to\' is bigger then the \'from\' field, please try again.', {
		greaterThenOrEqualToField : 'AddBasicEvent[event_price_from]'
});


//yrmg validate (this should be in a global yrmg javascript file for front/back
Validation.add('validate-time-range-yrmg', 'Please check the time range, ending time is set lower then beginning time', function (v, elm){
	//really lazy method for getting select list values.
	var bH    = $('event_single_timebegin_hour').getValue();
	var bM    = $('event_single_timebegin_min').getValue();
	var bAMPM = $('event_single_timebegin_ampm').getValue();
	var dH    = $('event_single_duration_hour').getValue();
	var dM    = $('event_single_duration_min').getValue();
	
	var bValue = bH*1000 + ((bM/60)*1000 ) + ((bAMPM == 'pm') ? 12000 : 0);
	var eValue = bValue + dH*1000 + ((dM/60)*1000 );
	 
	//no ending time set, good.
	if(((dH.length == 0) && (dM.length == 0))){
		return true;
	} else {
		//one time was set for ending, bad.
		if(((dH.length == 0) || (dM.length == 0))){
			return false;
		} else {
			//range check
			if(bValue > eValue){
				return false;
			} else {
				return true;
			}
		}
	}
});

FormHelper = Class.create();
Object.extend(Object.extend(FormHelper.prototype, Abstract.prototype), {
	initialize: function(formID, options){
	    this.form        = $(formID);
	    if(typeof(this.form) == 'undefined'){
	    	throw("Form is undefined, please validate the formID");
	    	return false;
	    }
	    this.elements    = this.getValidElements();
	    this.options     = Object.extend({ offsetTop : 0, offsetLeft : 20 }, options || {});
			this.titleWindow = this.makeTitleWindow();
			this.styleTitleWindow();
			
			this.events = {
	      click: this.click.bind(this)
	    };
	    this.addObservers();
	    
	    if(typeof(this.elements[0]) != 'undefined'){
	    	var position = Position.cumulativeOffset(this.elements[0]);
	    	var elementWidth = this.elements[0].getStyle('width').replace('px', '');
	    	$(this.titleWindow).setStyle({
	    		'top'  : position[1]+ this.options.offsetTop,
	    		'left' : position[0]+ this.options.offsetLeft + parseInt(elementWidth)
	    	});
	    	$(this.titleWindow).down(0).innerHTML = this.elements[0].title;
	    	this.elements[0].focus();
	    	new Effect.ScrollTo(this.elements[0], {offset : -40, duration : 0.3});
	    }
	    
	    this.index = 0;
	    
			/*
			next = $(this.titleWindow.getElementsBySelector('div.helpNext'));
			next.onclick = this.eventOnClick;
			
			previous = $(this.titleWindow.getElementsBySelector('div.helpPrevious'));
			previous.onmouseover = this.eventOnClick;
			//previous.onmouseover();
			*/
	  },
		  
  getValidElements: function() {
    var elements = Form.getElements(this.form);
    
    return elements.reject((function(formElement){
    	if(formElement.tagName.toLowerCase() == 'input' && formElement.type.toLowerCase() == 'hidden') return true;
    	if(!this.notVisible(formElement)) return true;
    	if(formElement.title == '') return true;
    	return false;
    }).bind(this));

  },
  
  makeTitleWindow: function(){
  	if(typeof($(this.titleWindow)) == 'undefined'){
			titleWindow = this.form.id+'_'+this.makeRandom();
  		new Insertion.Bottom(document.body,
  			 '<div id="'+titleWindow+'" class="FormHelper_TitleWindow">'
	  		+'  <div class="content">text</div>'
	  		+'  <div>'
	  		+'    <div class="controls" style="float:left;width:85px;">'
	  		+'      <a href="javascript:void(0);">Previous</a>'
	  		+'    </div>'
	  		+'    <div class="controls" style="text-align:right;float:right;width:85px;">'
	  		+'      <a href="javascript:void(0);">Next</a>'
	  		+'    </div>'
	  		+'  <div>'
	  		+'</div>'
  		);
  	}
  	return titleWindow;
  },
  
  styleTitleWindow: function(){
  	$(this.titleWindow).setStyle({
  		'position'         : 'absolute',
  		'border'           : '1px solid black',
  		'padding'          : '3px',
  		'background-color' : 'white',
  		'width'            : '180px'
  	});
  },
  
  makeRandom: function(){
  	return Math.floor(Math.random()*1000000);
  },
  	  
  addObservers: function() {
    var controls = $(this.titleWindow).getElementsBySelector('div.controls a');
    //alert(controls)
    controls.invoke('observe', 'click', this.events.click);
  },	

  click: function(event) {
    var anchor = Event.findElement(event, 'a');
    
    if(anchor.text.toLowerCase() == 'next'){
    	if(this.index+1 > this.elements.length-1){
    		this.index=0;
    	} else {
    		this.index++;
    	}
    } else if(anchor.text.toLowerCase() == 'previous'){
    	if(this.index-1 < 0){
    		this.index=this.elements.length-1;
    	} else {
    		this.index--;
    	}
    }
    
    if(typeof(this.elements[this.index]) != 'undefined'){
    	var position = Position.cumulativeOffset(this.elements[this.index]);
    	var elementWidth = this.elements[this.index].getStyle('width').replace('px', '');
    	$(this.titleWindow).setStyle({
    		'top'  : position[1]+ this.options.offsetTop,
    		'left' : position[0]+ this.options.offsetLeft + parseInt(elementWidth)
    	});
    	$(this.titleWindow).down(0).innerHTML = this.elements[this.index].title;
    	this.elements[this.index].focus();
    	new Effect.ScrollTo(this.elements[this.index], {offset : -40, duration : 0.3});
    }
    
    Event.stop(event);
  },
		
  next: function(){
  },
	
  previous: function(){
  },
  
  notVisible: function(element) {
    //return $(element).style.display == 'none';
    element = $(element);
    while (document !== element) {
      if ('none' == element.style.display)
        return false;
      element = element.parentNode;
    }
    return true;
   }

});

//////
function fncCommon_ToggleList(obj){
	var elm = $(obj);//extened
	if(elm.next('ul') != undefined){
		new Effect.toggle(elm.next('ul'), 'blind', {
			afterFinish: function(effect_object){
		  	var element = $(effect_object.element);
		  	var src  = elm.readAttribute('src');
		  	elm.src  = elm.readAttribute('_src');
		  	elm.setAttribute('_src', src);
			}
		});
	}
}

function fncCommon_ToggleAllLists(element){
	element = $(element);
	element.immediateDescendants()
	var elements = element.immediateDescendants().collect(function(li){return $(li.down('img'))});
	elements.each(function(img){
  	var src  = img.readAttribute('src');
  	img.src  = img.readAttribute('_src');
  	img.setAttribute('_src', src);
  	img.next().toggle();
	});
  element.immediateDescendants().each(function(li){
		li.down('ul').immediateDescendants().each(function(list){
			if(list.down('ul').getStyle('display') == "none"){
				list.down('ul').show();
			} else {
				list.down('ul').hide();
			}
		});
	});
}

function fncCommon_Children_Click(obj){
	var elm = $(obj);
	if(elm.up('li').down('ul').show() != undefined){
		$A(elm.up('li').down('ul').immediateDescendants()).each(function(li){li.down('input').checked = elm.checked});
	}
}

function fncCommon_ChildrenSub_Click(obj){
	var result;
	var elm = $(obj);
	if(elm.up('ul') != undefined){
		$A(elm.up('ul').immediateDescendants()).each(function(li){
			if(li.down('input').checked == true)
				result = true; 
		});
		
		//break out of function
		if(result){
			elm.up('ul').up('li').down('input').checked = true;
		} else {
			elm.up('ul').up('li').down('input').checked = false;
		}
		
		return true;
	}
}
//////

function scaleImage(img, maxSize, mode) {
	if(mode){
		var ratio  =  img.width / img.height;
		img.height = maxSize;
		img.width  = ratio * img.height;
	} else {
		var ratio   = img.height / img.width;
		img.width   = maxSize;
		img.height  = ratio * img.width;
	}
}


