/**
 * Peoples Church of Montreal Javascript
 * It's all about Jesus!!!
 */

/**
 * Cufon Replacements
 */
//Cufon.replace('h1, h2, h3, h4, h5, h6, th, .posttitle', { fontFamily: 'Myriad Pro' });


//Rotating Banner stuff
var timeout = 5000;		//The timer value in milliseconds
var fadetime = 500;
var currentimage;	
var nextimage;
var clock;

function fadeToImage(imageindex){
	
	if (imageindex != currentimage){
		currentimage = imageindex;
		
		jQuery('.bannerimage:visible').fadeOut(fadetime);
		
		//fade in the image
		jQuery('#bannerimage' + imageindex).fadeIn(fadetime);
	}
	
}

function highlightPlug(id){
	
	if (id != currentimage){		
		jQuery('.bannerdimmer[id!=bannerplug' + id + ']').animate({opacity: 0.5}, 100);	//Dim out other plugs
		//jQuery('#bannerplug' + id).find('.bannerdimmer').animate({opacity: 0.0}, 500);	//Fade in current plug	//Doesn't work in webkit brosers
		jQuery('div:first', '#bannerplug' + id).animate({opacity: 0.0}, 500);
	}
	
}

function startRotate(){
	clock = setInterval('rotateImage()', timeout);
}

function stopRotate(){
	clearInterval(clock);
}


function rotateImage(){
	//Fade in next banner

	nextimage = parseInt(currentimage) + 1;
	if (nextimage > 4) {
		nextimage = 1;
	}

	highlightPlug(nextimage);
	fadeToImage(nextimage);
}


/*
Master jquery controller	
*/

jQuery(document).ready(function(){

	//Set the banner dimmer
	//jQuery('.bannerdimmer').fadeTo("slow", 0.5);
	jQuery('.bannerdimmer').animate({opacity: 0.5}, 0);

	//Start the banner rotation
	highlightPlug(1);
	fadeToImage(1);
	startRotate();
	
	//Plugs hover
	/*
	jQuery('.plug').hover(
		function(event){
			jQuery(this).animate({backgroundColor: '#E5E5E5'}, 200);
		}, function(event){
			jQuery(this).animate({backgroundColor: '#F5F5F5'}, 200);
		}
	); //plugs hover
	*/
	
	//Bannerplug hover
	jQuery('.bannerplug').hover(
		function(event){
			var id = jQuery(this).attr('id').substring(10);
						
			//Highlight current
			highlightPlug(id);
						
			//alert(id);
			stopRotate();
			fadeToImage(id);
			
		}, function(event){
			startRotate();
		}
	);
	
	//Banner hover
	jQuery('.bannerimage').hover(
		function(event){
			stopRotate();
		}, function(event){
			startRotate();
	});
	
	//Navbar hover
	jQuery('.toplevel').hover(
		function(event){
			//jQuery(this).find('.secondlevelcontainer').fadeIn(500);		//Doesn't work in webkit browsers
			jQuery('ul:first', this).slideDown(150);
		}, function(event){
			jQuery('ul:first', this).slideUp(150);
			//jQuery(this).find('.secondlevelcontainer').fadeOut(500);
		}
	);	//navbar hover
	
	jQuery('.secondlevel').hover(
		function(event){
			jQuery('ul:first', this).slideDown(150);
			//jQuery(this).find('.thirdlevelcontainer').fadeIn(500);
		}, function(event){
			jQuery('ul:first', this).slideUp(150);
			//jQuery(this).find('.thirdlevelcontainer').fadeOut(500);		//Doesn't work in webkit browsers
		}
	);
	
});


