var $j = jQuery.noConflict();

var current_event = 1;

function rotate_image() {
	switch(current_event) {
		case 1:
			$j("#event p.one").css('z-index','1');
			$j("#event p.two").css('z-index','5');
			$j("#event p.two").slideDown("slow", function() { $j("#event p.one").css('display','none'); });
			
			current_event = 2;
		break;
		case 2:
			$j("#event p.two").css('z-index','1');
			$j("#event p.three").css('z-index','5');
			$j("#event p.three").slideDown("slow", function() { $j("#event p.two").css('display','none'); });

			current_event = 3;
		break;
		case 3:
			$j("#event p.three").css('z-index','1');
			$j("#event p.four").css('z-index','5');
			$j("#event p.four").slideDown("slow", function() { $j("#event p.three").css('display','none'); });

			current_event = 4;
		break;
		case 4:
			$j("#event p.four").css('z-index','1');
			$j("#event p.five").css('z-index','5');
			$j("#event p.five").slideDown("slow", function() { $j("#event p.four").css('display','none'); });

			current_event = 5;
		break;
		case 5:
			$j("#event p.five").css('z-index','1');
			$j("#event p.one").css('z-index','5');
			$j("#event p.one").slideDown("slow", function() { $j("#event p.five").css('display','none'); });

			current_event = 1;
		break;
		
		
	};
};


$j(document).ready(function() {
	

//Add class "event number" to the main images
	var event_num;
	event_num = 1;
	$j("#event p").each(function() {
		switch(event_num) {
			case 1:
				$j(this).addClass("one");
				break;
			case 2:
				$j(this).addClass("two");
				break;
			case 3:
				$j(this).addClass("three");
				break;
			case 4:
				$j(this).addClass("four");
				break;
			case 5:
				$j(this).addClass("five");
				break;
		}
		event_num++;
	});
	
//Hide all paragraphs except top
$j("#event p").css({"display":"none", "z-index":"1"})
$j("#event p.one").css({"display":"block", "z-index":"5"})
	

//Add class "event number" to the thumbnails
	event_num = 1;
	$j("#event ul li").each(function() {
		switch(event_num) {
			case 1:
				$j(this).addClass("one");
				break;
			case 2:
				$j(this).addClass("two");
				break;
			case 3:
				$j(this).addClass("three");
				break;
			case 4:
				$j(this).addClass("four");
				break;
			case 5:
				$j(this).addClass("five");
				break;
		}
		event_num++;
	});

		

	//Wrap div's around WP post
	$j("#event p").wrapAll('<div id="event_image"></div>');
	$j("#event ul").wrap('<div id="event_thumbs"></div>');
	//add class to last ul item
	$j("#event ul li:last").addClass("last");
	
	
//Auto rotate images
	var rotate_interval;
	rotate_interval = setInterval('rotate_image()', 12000);

	

//add onclick event to the thumbnails

	$j("#event ul li").click(function() {
		
		clearInterval(rotate_interval);
		rotate_interval = setInterval('rotate_image()', 12000);
		
		if(current_event == 1) {
			hide_event = "#event p.one"
		} else if (current_event == 2) {
			hide_event = "#event p.two"
		} else if (current_event == 3) {
			hide_event = "#event p.three"
		} else if (current_event == 4) {
			hide_event = "#event p.four"
		} else if (current_event == 5) {
			hide_event = "#event p.five"
		}
		
		
		
		if($j(this).is('.one')) {
			
			$j(hide_event).css('z-index','1');
			$j("#event p.one").css('z-index','5');
			$j("#event p.one").slideDown("slow", function() { $j(hide_event).css('display','none'); });

	        current_event = 1;
	    } if($j(this).is('.two')) {
			$j(hide_event).css('z-index','1');
			$j("#event p.two").css('z-index','5');
			$j("#event p.two").slideDown("slow", function() { $j(hide_event).css('display','none'); });
	        current_event = 2;
	    } if($j(this).is('.three')) {
			$j(hide_event).css('z-index','1');
			$j("#event p.three").css('z-index','5');
			$j("#event p.three").slideDown("slow", function() { $j(hide_event).css('display','none'); });
	        current_event = 3;
	    } if($j(this).is('.four')) {
			$j(hide_event).css('z-index','1');
			$j("#event p.four").css('z-index','5');
			$j("#event p.four").slideDown("slow", function() { $j(hide_event).css('display','none'); });
	        current_event = 4;
	    } if($j(this).is('.five')) {
			$j(hide_event).css('z-index','1');
			$j("#event p.five").css('z-index','5');
			$j("#event p.five").slideDown("slow", function() { $j(hide_event).css('display','none'); });
	        current_event = 5;
	    }
	});
	

	
	
//Create onclick event for Times and Locations
	var tal_closed = true;
	$j("#times_button").click(function() {
		if(tal_closed) {
			$j("#times_wrapper").animate({
				height: "300px"
			}, 500);
			tal_closed = false;
		} else {
			$j("#times_wrapper").animate({
				height: "49px"
			}, 500);
			tal_closed = true;
		};
	});
	
//popout link from "Sundays at Grace" when hovered
	$j("#sundays_links ul li a").hover(function() {
		var get_span = $j(this).children("span").css('display');

		if(get_span == "none"){
			$j(this).animate({paddingRight: '15px'}, 200);
		}
	}, function() {
		$j(this).animate({paddingRight: "0px"}, 200);
	});
	
	
//Hide Sundays at Grace info and show first
	$j("div.sundays_posts").hide();
	$j("div#worship").show();
	$j("span.after_link:first").css({"display":"inline", "padding":"0 0 0 15px"});
	
//When Sundays at Grace links are clicked, show content
	$j("#sundays_links ul li a").click(function(event) {
		event.preventDefault();
		$j("#sundays_links ul li a").css("padding" , "0px");
		$j("#sundays_links ul li a span").css({"display":"none", "padding":"0 0 0 0px"});
		$j(this).css("padding" , "0px");
		$j(this).children("span").css({"display":"inline", "padding":"0 0 0 15px"});

		var sundays_item = $j(this).attr("href");
		
		var dot_location = sundays_item.lastIndexOf('.');
		sundays_item = sundays_item.substring(0, dot_location);
		
		var get_div = "div#" + sundays_item;

		$j("div.sundays_posts").hide();
		$j(get_div).show();
		
		return false;
		
		
	});
	
//Cycle through events
	
	event_num = 0;
	$j("div.event_wrapper").each(function() {
		event_num++;
	});

	if(event_num > 5) {
		var cur_pos = 0;
		//var max_pos = (event_num/5);
		//max_pos = Math.floor(max_pos);
		
		
		$j("a.prev").css("display", "none");
		
		
		$j("a.next").click(function(){
			 $j("div.event_wrapper").animate({"top": "-=420px"}, "slow");
			 cur_pos++;
			 //if(cur_pos == 1) {
				 $j("a.prev").css("display", "inline");
			//}
			
			//if( cur_pos == max_pos ) {
				$j("a.next").css("display", "none");
			//}
			return false;
		});
		
		$j("a.prev").click(function(){
			  $j("div.event_wrapper").animate({"top": "+=420px"}, "slow");
			  cur_pos--;
			  //if( cur_pos == max_pos-1 ) {
				  $j("a.next").css("display", "inline");
			  //}
			  
			  //if( cur_pos == 0 ) {
				$j("a.prev").css("display", "none");
			//}
			return false;
				  
		});

	
	
}


//Get first of the Main Links

$j('#main_links ul li:first').addClass("selected");




// Clear main content
	$j("div.the_content").hide();
// Show events
	$j("#events_wrapper").show();
// When Main content link is selected, show it	
	$j("#main_links ul li a").click(function(event) {
		
		
		
		var content_item = $j(this).attr("href");
		
		if((content_item.indexOf("facebook_wrapper") > -1) || (content_item.indexOf("teaching_wrapper") > -1) || (content_item.indexOf("events_wrapper") > -1)) {
			event.preventDefault();
			$j("#main_links ul li").removeClass("selected");
			$j(this).parent().addClass("selected");
			
			var dot_location = content_item.lastIndexOf('.');
			content_item = content_item.substring(7, dot_location);
			
			var get_div = "div#" + content_item;
			
			$j("div.the_content").hide();
			$j(get_div).show();
			if(content_item == "facebook_wrapper") {
				$j("#main_content_wrapper").addClass("facebook_content_wrapper");
			} else {
				$j("#main_content_wrapper").removeClass("facebook_content_wrapper");
			}
			
			return false;
		}
		
	});
	

	var link_href;
	$j("a").each(function() {
		link_href = $j(this).attr("href");
		
		if(	(link_href.indexOf("fellowshipone")) > -1 ){//Open all links directed at FellowshipOne in a window of a set size.
			
			$j(this).click(function(event) {
				event.preventDefault();
				goto = $j(this).attr("href");
				window.open(goto,'fellowshipone','scrollbars=yes,width=600,height=600,left=100,top=100');
				return false;
			});
		
		} else if( (link_href.indexOf("roomrequest")) > -1 ){ //Also do this for the Room Request form
		
			$j(this).click(function(event) {
				event.preventDefault();
				goto = $j(this).attr("href");
				window.open(goto,'roomrequest','scrollbars=yes,width=800,height=600,left=100,top=100');
				return false;
			});
		
		} else if ( (link_href.indexOf("link_header")) > -1 ){ //Select Link Headers, make bold, disable link
			$j(this).parent().addClass("link_header");
			
			
			$j(this).click(function(event) {
				event.preventDefault();
				return false;
			});
			
		}
		
		
	});
	
	//Keyword search field name...
	
	$j(".search_box").focus(function() {
		
		if(this.value == this.defaultValue) {
			this.value = '';
		}
		
		if(this.value != this.defaultValue) {
			this.select();
		}
	});
	
	$j(".search_box").blur(function() {
		
		if ($j.trim(this.value) == '') {
			
						this.value = (this.defaultValue ? this.defaultValue : '');
			
		}
		
	});
	
	
	//Get the contact form fields and erase them when someone clicks or focuses on them.
	
	$j("div.wpcf7 input").addClass("contactFields idle");
	$j("div.wpcf7 textarea").addClass("contactFields idle");
	$j("div.wpcf7 input.button").removeClass("contactFields idle");
	$j(".contactFields").focus(function() {
		
		$j(this).removeClass("idle").addClass("active");
		
		if(this.value == this.defaultValue) {
			this.value = '';
		}
		
		if(this.value != this.defaultValue) {
			this.select();
		}
	});
	
	$j(".contactFields").blur(function() {
		
		$j(this).removeClass("active").addClass("idle");
		
		if ($j.trim(this.value) == '') {
			
			this.value = (this.defaultValue ? this.defaultValue : '');
			
		}
		
	});

	


}); //Init funciton



