window.addEvent('resize', function() { 
	fixScrollBars();

});

function init() {
	setFormCheck();
	setupMusicDomm();
	setupSpecialEvents();
	fixScrollBars();
	fixBackground(); 
	setupEvents();


	
}

function setupMusicDomm() {

	if($$('a.music_domm')[0]) {


		      /*
		      The new 'validTags' setting is optional and allows
		      you to specify other HTML elements that curvyCorners
		      can attempt to round.
		
		      The value is comma separated list of html elements
		      in lowercase.
		
		      validTags: ["div", "form"]
		
		      The above example would enable curvyCorners on FORM elements.
		      */
		      settings = {
		          tl: { radius: 10 },
		          tr: { radius: 10 },
		          bl: { radius: 10 },
		          br: { radius: 10 },
		          antiAlias: true,
		          autoPad: false,
		          validTags: ["div", "a"]
		      }
		
		      /*
		      Usage:
		
		      newCornersObj = new curvyCorners(settingsObj, classNameStr);
		      newCornersObj = new curvyCorners(settingsObj, divObj1[, divObj2[, divObj3[, . . . [, divObjN]]]]);
		      */
		      var myBoxObject = new curvyCorners(settings, "music_domm");
		      myBoxObject.applyCornersToAll();


	}





}

function setupSpecialEvents() {
	if($$('.special_event')[0]) {
	      	$$('.special_event').each(function(el,key){
	  		el.addEvent('mouseenter', function(event){
				el.setStyle('opacity',1);
			
			});
	  		el.addEvent('mouseleave', function(event){
				el.setStyle('opacity',.6);
			
			});

		});

	}

	if($$('.special_event2')[0]) {
	      	$$('.special_event2').each(function(el,key){
	  		el.addEvent('mouseenter', function(event){
				el.setStyle('opacity',1);
			
			});
	  		el.addEvent('mouseleave', function(event){
				el.setStyle('opacity',.8);
			
			});

		});

	}

}

function fixSermons() {
	//function sets the sermon's columns to equal height
	$sermons_by_date_height=$('sermons_by_date').getHeight();
	$sermons_by_series_height=$('sermons_by_series').getHeight();

	// alert($sermons_by_date_height);alert($sermons_by_series_height);

	if ($sermons_by_date_height>$sermons_by_series_height) {
		$('sermons_by_series').setStyle('height',$sermons_by_date_height+'px');

	}

	if ($sermons_by_series_height>$sermons_by_date_height) {
		$('sermons_by_date').setStyle('height',$sermons_by_series_height+'px');
	
	}

}

function fixBackground() {
		//repositions background image of body to top of footer
		//footerTop=$('footer').getTop();
		//$('myBody').setStyle('background-position','0px '+footerTop+'px');

		//adjust height of content_wrapper to equal max height of content_left and content_right plus content_bottom
		contentLeftHeight=$('content_left').getHeight();
		contentRightHeight=$('content_right').getHeight();
		//contentBottomHeight=$('content_bottom').getHeight();

		if (contentLeftHeight>=contentRightHeight) {
			//$('content').setStyle('height',contentLeftHeight+contentBottomHeight+'px');
			$('content').setStyle('height',contentLeftHeight+'px');

		} else if (contentLeftHeight<contentRightHeight) {
			//$('content').setStyle('height',contentRightHeight+contentBottomHeight+'px');
			$('content').setStyle('height',contentRightHeight+'px');

		} 
	
}

function fixBackground2() {
		/* fixes content area background */
		mastheadHeight=$('masthead').getHeight();
		footerTop=$('footer').getTop();
		//$('content').setStyle('height',(footerTop-mastheadHeight)+'px');


	//put title and date on same line. The space between both items is made with a &nbsp; in ./admin/events_toggle.php
	$$('.subpage_text div.events_title').setStyle('display','inline');
	$$('.subpage_text div.events_date').setStyle('display','inline');

	
}

function fixScrollBars() {
	//function fixes scrollbar bug in IE
	//check to see if browser is IE
	if (Browser.Engine.trident) {
		container_height=$('myBody').getStyle('height');
		container_height=container_height.substr(0,container_height.length-2);
	
		container_width=$('myBody').getStyle('width');
		container_width=container_width.substr(0,container_width.length-2);
		//alert(container_height);
	
		if (window.innerHeight) {
			theHeight=window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			theHeight=document.documentElement.clientHeight;
		} else if (document.body) {
			theHeight=document.body.clientHeight;
		}
	
	
		if (window.innerWidth) {
			theWidth=window.innerWidth;
		} else if (document.documentElement && document.documentElement.clientWidth) {
			theWidth=document.documentElement.clientWidth;
		} else if (document.body) {
			theWidth=document.body.clientWidth;
		}
	
		if (theHeight>container_height) {
			$('myHtml').setStyle('overflow-y','hidden');
	
		} else {
			$('myHtml').setStyle('overflow-y','auto');
	
		}
	
		//980 is 10 pixels greater than the width of the content_container class, which will not change in width
		if (theWidth>container_width || theWidth>980) {
			$('myHtml').setStyle('overflow-x','hidden');
	
		} else {
			$('myHtml').setStyle('overflow-x','auto');
	
		}

	}

}

function setupEvents() {

	scrollerTiming=100; //higher is slower

	//collapse events_description & alter events_divider  
	$$('#upcoming_events_scroller div.events_description').setStyle('display','none');
	$$('#upcoming_events_scroller div.events_divider').setStyle('visibility','hidden');
	$$('#upcoming_events_scroller div.events_divider').setStyle('height','10px');
	$$('#upcoming_events_scroller div.events_divider').setStyle('margin-bottom','0px');

	//put title and date on same line. The space between both items is made with a &nbsp; in ./admin/events_toggle.php
	$$('#upcoming_events_scroller div.events_title').setStyle('display','inline');
	$$('#upcoming_events_scroller div.events_date').setStyle('display','inline');

	containerHeight=$('upcoming_events_container').getHeight();
	$('upcoming_events_scroller').setStyle('top',containerHeight+'px');

	delta=0;
	range=$('upcoming_events_scroller').getHeight()+containerHeight+delta;

	origTop=$('upcoming_events_scroller').getStyle('top');
	origTop=origTop.substr(0,origTop.length-2);
	minY=origTop-range;
	// alert("range="+range+" minY="+minY+" origTop="+origTop+" containerHeight="+containerHeight);

	speed=1;
	dir=1; //reverse for movement in opposite direction, set to zero to stop


	$('myBody').addEvent('mouseenter', function(){
		dir=1;
		$('scroller2').setStyle('background-position','-212px 0px'); //fix button background due to rollover of event scroller text fires a rollout event for the button

	});

	$('upcoming_events_container').addEvent('mouseenter', function(){
		dir=0; 
		$('scroller2').setStyle('background-position','0px 0px');

	});

	$('upcoming_events_container').addEvent('mouseleave', function(){
		dir=0;
		$('scroller2').setStyle('background-position','-212px 0px');

	});

	$('scroller').addEvent('mouseenter', function(){
		dir=0;
		$('scroller2').setStyle('background-position','0px 0px');

	});

	$('scroller').addEvent('mouseleave', function(){
		dir=1;
		$('scroller2').setStyle('background-position','-212px 0px');

	});

	/* fix onclick event bug in FF */
	$('scroller').addEvent('click', function(){
		document.location.href='upcoming_events.php?nav_right=5&p=19';

	});
	$('upcoming_events_container').addEvent('click', function(){
		document.location.href='upcoming_events.php?nav_right=5&p=19';

	});

	scrollInterval = window.setInterval("moveScroller(minY,origTop,speed,dir)", scrollerTiming);

}

function moveScroller(minY,origTop,speed,dir) {
	currTop=$('upcoming_events_scroller').getStyle('top');
	currTop=currTop.substr(0,currTop.length-2);

	if (currTop>minY) {
		$('upcoming_events_scroller').setStyle('top',(currTop-speed*dir)+'px');

	} else {
		$('upcoming_events_scroller').setStyle('top',origTop+'px');

	}

}


function isChanged(el) {
	if(el.value==el.original_value) {
		el.errors.push("Please enter a value different than the default.");
		return false;

	} else {
		return true;

	}

// example formcheck validation- ['required','%isChanged'] 


}


function setFormCheck() {

	if($('prayer_form')) {
		/* 
		//this ensures that in Firefox, which saves form values after even after a successful submit, that the form is not displayed to allow resending of the form on page refresh
		$$('#formfirst').each(function(el,key){
			if (el.value!="Name:") {
				$('theprayerform').setStyle('display','none');
	
			} 
	
		});
	
		*/
	
		// when someone clicks on any form element the form results disappears
		$('theprayerform').addEvent('click', function() {
			$('formresults').innerHTML='';
			$('formresults').setStyle('display','none');
	
		});
	
	
		//give each form element its value for formcheck to use the isChanged function to test against
		$$('#theprayerform input','#theprayerform textarea').each(function(el,key){
			el.original_value=el.value;
	
		});
	
	
		var myCheck = new FormCheck('theprayerform', {
			alerts: {
				email: "Please enter a valid email: <br /><span style=\"font-style:italic;\">E.g. yourname@domain.com</span>",
				differs: "Please enter a different value."
	
			},
			tipsClass : 'tips_box',
			display : {
				scrollToFirst : true,
				addClassErrorToField : 1,
				tipsOffsetX : -60,
				tipsOffsetY : 5,
				scrollToFirst : true,
				closeTipsButton : 1
				
	
			},
			tipsClass: 'fc-tbx',			
			errorClass: 'fc-error',			
			fieldErrorClass: 'fc-field-error',
			submitByAjax : true,				
			ajaxResponseDiv : 'formresults',
			onAjaxRequest : form_ajaxreq,	 
			onAjaxSuccess : form_ajaxsuccess,	 
			onAjaxFailure : form_ajaxfail
	
	
	  	
		})
	
	}

}

function form_ajaxreq() {
	//reset formresults scroll position
	var myFx = new Fx.Scroll('formresults', {
		offset: {
        	'x': 0,
        	'y': 0
    	}}).toTop();


	//remove form, loader image, and note, display formresults
	$('theprayerform').setStyle('display','none');

	//display loader
	$('formloader').setStyle('display','block');



}

function form_ajaxsuccess() {

	/*
	//remove loader
	$('formloader').setStyle('display','none');

	//show results
	$('formresults').setStyle('display','block');

	var myTrans = new Fx.Transition(Fx.Transitions.Bounce);
	var fxresults = new Fx.Morph($('formresults'), {delay: 0, duration: 1800, wait:true, transition: myTrans.easeOut});
	
	fxresults.start({
		'width': '700px',
		'height': '200px'
		
	});
	*/
	
	window.location = window.location.href;
	

}

function form_ajaxfail() {
	$('formresults').innerHTML='Form data transmission has failed.<br />Please send e-mail to the <a class="content_link" href="mailto:jlevin@portal2web.biz">Webmaster</a>.';

}

