var reloadOnClose = false;

document.observe("dom:loaded", function() {

	// Find and script all slideTo anchor links
	if ( $$('a.slideToAnchor') ){
		$$('a.slideToAnchor').each( function(element){
			element.observe("click", function(event){
				Event.stop(event);
				Effect.ScrollTo(Event.element(event),{offset:0,duration:0.4});
			});
		});
	}

	// Script Show Add Comment button
	if ( $('showAddCommentButton') ){
		$('showAddCommentButton').observe("click", function(event){
			Event.stop(event);
			$('post_comment').show();
		});
	}

	// Script Show Add Comment button
	if ( $('showAddPostButton') ){
		$('showAddPostButton').observe("click", function(event){
			Event.stop(event);
			$('post_comment').show();
		});
	}

	// Find all delete links and add confirmation check
	if ( $$('a.deleteButton') ){
		$$('a.deleteButton').each( function(element){
			element.observe("click", function(event){
				var r=confirm("Are you sure you want to delete this post/comment?");
				if (r==true){
					//
				} else {
					Event.stop(event);
				}
			});
		});
	}

	// Check comments form field
	post_comment();

	////////////////////
	// Takes an array of elements and catches them 'onClick' to update the modal window
	////////////////////

	makeModalLinks = function(linksArray){

		linksArray.each( function(currentModalLink){

			currentModalLink.observe('click', function(event){
				new Ajax.Updater(modal.container, currentModalLink.href, {
					method: 'get',
					evalScripts: true,
					onComplete: function(){
						setModalHyperlinksBehaviour(true);
						modal.open('empty_modal');
					}
				});

				Event.stop(event);
			});
	
		});
	}


	/////////////////////
	// Create one modal box for all modal content
	/////////////////////

	var modal = new Control.Modal('empty_modal',{
		fadeDuration: 0.25,
		width: 580,
		className: 'modal_window',
		fade: true,
		afterOpen: function(){
			setModalHyperlinksBehaviour(false);
			setModalHyperlinksBehaviour(true);
		},
		afterClose: function(){
			setModalHyperlinksBehaviour(false);
			modal.remoteContentLoaded=false;

			if ( reloadOnClose ){
				location.reload();
			}
		}
	});

	
	/////////////////////
	// Close the modal and reload the page - can be called from the modal window
	/////////////////////
	
	closeAndReload = function(){
		modal.container.update('');
		modal.container.hide();
		location.reload();
	}


	//////////////////////
	// Find all forms and hyperlinks in the modal window and apply ajax functionality
	//////////////////////

	setModalHyperlinksBehaviour = function(b_setBehaviours){

		// Find all modal windows
		$$('.modal_window').each( function(currentWindow){

			// Find all hyperlinks and catch to use as AJAX Updater
			currentWindow.select('a').each( function(currentElement){

				if ( b_setBehaviours ) {
					profile_form();
					post_comment();
					currentElement.observe('click', function(event){

						if ( currentElement.readAttribute('class') == "modal_close" ){
							if ( reloadOnClose ){
								location.reload();
							}
							Control.Modal.close();

						} else {

							new Ajax.Updater(currentWindow, currentElement.readAttribute('href'), {
								method: 'get',
								evalScripts: true,
								onComplete: function(){
									setModalHyperlinksBehaviour(true);
								}
							});

						}
						Event.stop(event);
					});

				} else {

					currentElement.stopObserving('click');

				}

			});
		});

		// Find all forms and catch to use as AJAX Updater
		$$('.modal_window').each( function(currentWindow){
			$$('form').each( function(currentForm){
			if ($('bookingform')) {
			    if($('repeat_day')) {
				  book_to_pray($RF('bookingform', 'repeat'));
				  book_to_praytoggle();
			    }
			}
				if ( b_setBehaviours ) {
					currentForm.observe('submit', function(event){

						window.scrollTo(0,0);

						var formValues = currentForm.serialize();
						var submitUrl = currentForm.readAttribute('action');

						new Ajax.Updater(currentWindow, submitUrl, {
							evalScripts: true,
							parameters: formValues,
							onComplete: function(){
								setModalHyperlinksBehaviour(true);
							}
						});
						
						Event.stop(event);
					});

					currentForm.select('.modal_close').each( function(currentCloseButton){
						currentCloseButton.observe('click', function(event){
							Control.Modal.close();
						});
					});

				} else {
					currentForm.stopObserving('submit');
				}
	
			});

		});

	}

	// Make prayer wall form submit on any change
	if ( $('prayerwall_nav_form') ) {

		// Hide submit button on prayer wall
		$('prayerwall_submit_button').hide();

		$('prayerwall_nav_form').select('select').each( function(theElement){
			theElement.observe('change', function(){
				$('prayerwall_nav_form').submit();
			});
		});
	}



	// Find all modal links and apply function
	makeModalLinks( $$('a.modal_link') ); 

	// Make login and register link from navmenu into modal links
	makeModalLinks( $$('a.nav_login') );
	makeModalLinks( $$('a.nav_register') );


	function $RF(el, radioGroup) {
	    if($(el).type && $(el).type.toLowerCase() == 'radio') {
		var radioGroup = $(el).name;
		var el = $(el).form;
	    } else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	    }
	
	    var checked = $(el).getInputs('radio', radioGroup).find(
		function(re) {return re.checked;}
	    );
	    return (checked) ? $F(checked) : null;
	}

//	var value = $RF('form_id', 'radio_grp_name');


	function book_to_praytoggle() {
		$$('.repeat').each( function(currentElement){ 
			currentElement.observe('click', function(currentEvent){
			book_to_pray(currentElement.value);
			})
		  });

		$$('.repeat_until').each( function(currentElement){ 
			currentElement.observe('change', function(currentEvent){
			    $('repeat_forever').checked = false;
			})
		  });


	      $('repeat_forever').observe('click', function(event){
		  if($('repeat_forever').checked==true) {	
		      $('repeat_day').selectedIndex = "0"; 
		      $('repeat_month').selectedIndex = "0"; 
		      $('repeat_year').selectedIndex = "0"; 
		  }
	      });
	}

       function book_to_pray(repeat_value) {

	    if(repeat_value=="false") {
		 return $$('.repeat_hidden').each(function(currentElement){ currentElement.hide();});
	    } else {
		 return $$('.repeat_hidden').each(function(currentElement){ currentElement.show();});
	    }

       }

	if ($('post_wall_comment')) {
		$('post_wall_comment').observe('click', function(event){
			Event.stop(event);
			val = 0;

			if($("wallcomment").value.length<3) {
				val=1;
			} 

			if(val==0) {
				$("form_wall_comment").submit();
			}
		});
	}
}); 

function post_comment() {
	// Check comments form field
      if ($('postcomment')) {

	      $('postcomment').observe('submit', function(event){
      
	      myForm = Event.element(event);
	      txtArea = myForm.down('textarea');

	      if ((Form.Element.getValue(txtArea).length < 1)  || (Form.Element.getValue(txtArea).length > 2000) ) { 
		      alert('You need to enter a comment (max 2000 characters)');
		      Form.Element.focus(txtArea);
		      Event.stop(event);}

	      });
      }
}

function textCounter(field,maxlimit) {
      if (field.value.length > maxlimit) // if too long...trim it!
      field.value = field.value.substring(0, maxlimit);
      // otherwise, update 'characters left' counter
      else
 //     cntfield.value = maxlimit - field.value.length;
      $('char_remaining').update(maxlimit - field.value.length);
}



