var objLastClicked;

var currentUrl = document.URL.toLowerCase();

// break out of iframes...
if ( currentUrl.indexOf('/admin/') == -1 && top.location!= self.location) {
	top.location = self.location.href
}

jQuery(document).ready(function() {

	jQuery('#cLifeApps .Banner a').eq(0).click( function() { // weather report
		jQuery('#modal_box').removeClass('prayer-requests');
		show_modal( jQuery(this), jQuery(this).attr('href'), 665, 475 );
		return false;
	} );
	jQuery('#cLifeApps .Banner a').eq(1).click( function() { // traffic report
		jQuery('#modal_box').removeClass('prayer-requests');
		show_modal( jQuery(this), jQuery(this).attr('href'), 665, 475 );
		return false;
	} );
	jQuery('#cLifeApps .Banner a').eq(2).click( function() { // community calendar
		jQuery('#modal_box').removeClass('prayer-requests');
		show_modal( jQuery(this), jQuery(this).attr('href'), 665, 500 );
		return false;
	} );
	jQuery('#cLifeApps .Banner a').eq(3).click( function() { // prayer requests
		jQuery('#modal_box').addClass('prayer-requests');
		show_modal( jQuery(this), jQuery(this).attr('href'), 950, 500 );
		return false;
	} );
	jQuery('#cLifeApps .Banner a').eq(4).click( function() { // weekly devotion
		jQuery('#modal_box').removeClass('prayer-requests');
		show_modal( jQuery(this), jQuery(this).attr('href'), 950, 500 );
		return false;
	} );

	jQuery('#cT .Banner a').click( function() { // live radio link
		window.open( jQuery(this).attr('href'),'listenconsole','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=200,top=50,left=50','alwaysRasied=yes');
		return false;
	} );


	if ( jQuery('#sHomeBanners').length > 0 ) {
		add_banner_shuffle( jQuery('#sHomeBanners'), 0, 0, false, true );
	}

	if ( jQuery('#cBusinessUnderwriters').length > 0 ) {
		add_banner_shuffle( jQuery('#cBusinessUnderwriters'), 0, 0, false, false );
	}

	if ( jQuery('#cSupporters').length > 0 ) {
		add_banner_shuffle( jQuery('#cSupporters'), 0, 0, false, false );
	}
	
	$('#modal_shadow, #modal_close').click( function() {
		hide_modal();
	});	
	
	$('.home #cPC > div').eq(0).append('<div class="clear"></div>')
	
	add_hover_to_life_apps();

	add_read_article_buttons_to_homepage_rss();
	
});

function show_modal( objClickSource, IFrameURL, Width, Height ) {
	
	objLastClicked = objClickSource;
	
	jQuery('#modal_box').stop(true, true);
	jQuery('#modal_shadow').stop(true, true);

	jQuery('#modal_shadow').width( jQuery('body').width() );
	jQuery('#modal_shadow').height( jQuery('body').height() );

	jQuery('#modal_shadow').fadeTo(200, .5, function() {

		var ClickSourceOffset =  objClickSource.offset();
		
		var targetHeight = Height;
		if (jQuery('.contegro_ie7, .contegro_ie8').length>=1) {
			targetHeight+=30;
		}

		var targetTop = (jQuery(window).height()/2) - (targetHeight/2) - 23
		if (targetTop<20) {
			targetTop = 20;
		}
		
		if ( jQuery('.contegro_chrome').length>0 ) {
			console.log( targetTop + targetHeight );			
			console.log( jQuery(window).height() );			
		}
		
		if (targetTop + targetHeight > jQuery(window).height() ) {
			Height = jQuery(window).height()-targetTop-40;
			targetHeight = Height;
			if (jQuery('.contegro_ie7, .contegro_ie8').length>=1) {
				Height-=30;
			}
		}		

		if (Width > jQuery(window).width() ) {
			Width = jQuery(window).width()-40;
		}

		
		jQuery('#modal_box').css({
				 'width': objClickSource.outerWidth(),
				 'height': objClickSource.outerHeight(),
				 'top': ClickSourceOffset.top,
				 'left': (jQuery('body').width()/2-600) + ClickSourceOffset.left,
				 'display': 'block',
				 'opacity': 0
		});
		jQuery('#modal_box iframe').css('display', 'none');
		
		jQuery('#modal_box').addClass('animating');

		jQuery('#modal_box').animate({
			opacity: 1,
			width: [Width, 'easeOutBack'],
			height: [targetHeight, 'easeOutBack'],
			left: [(jQuery('body').width()/2) - (Width/2) - 15, 'easeOutBack'],
			top: [targetTop, 'easeOutBack']
		}, 500, function() {
			jQuery('#modal_box').removeClass('animating');
			jQuery('#modal_box iframe').css({
				width: 	Width,
				height: Height
			});
			jQuery('#modal_box #modal_header').show();
			
			jQuery('#modal_box iframe').css('display', 'block');
			jQuery('#modal_box iframe').attr('src', IFrameURL);
		});

	});

}

function hide_modal() {
	var LastClickedOffset =  objLastClicked.offset();
	
	jQuery('#modal_box').addClass('animating');
	jQuery('#modal_box iframe').attr('src', '/blank.htm');
	jQuery('#modal_box iframe').css('display', 'none');
	jQuery('#modal_box #modal_header').hide();
	jQuery('#modal_box').animate({
		 'width': [objLastClicked.outerWidth(), 'easeInBack'],
		 'height': [objLastClicked.outerHeight(), 'easeInBack'],
		 'top': [LastClickedOffset.top, 'easeInBack'],
		 'left': [(jQuery('body').width()/2-600) + LastClickedOffset.left, 'easeInBack']
	}, 500, function() {
		jQuery('#modal_box').hide();
		jQuery('#modal_box').removeClass('animating');
		jQuery('#modal_shadow').fadeOut(200);
	});
}


function add_hover_to_life_apps() {

	jQuery('#cLifeApps .Banner img').each( function() {
		jQuery(this).attr('non_hover_src', jQuery(this).attr('src'));
		jQuery(this).attr('hover_src', jQuery(this).attr('src').replace('.gif', '_hover.gif'));
		
		$('<img/>')[0].src = jQuery(this).attr('hover_src');
	});

	jQuery('#cLifeApps .Banner img').hover( function() {
		jQuery(this).attr('src', jQuery(this).attr('hover_src'));
	}, function() {
		jQuery(this).attr('src', jQuery(this).attr('non_hover_src'));
	});
}


function add_read_article_buttons_to_homepage_rss() {
	
	jQuery('.home .RSSReader .ListItemDate').each( function() {
		
		var articleLink = jQuery(this).siblings('.ListTitle').eq(0).children('a').eq(0).attr('href');
		
		jQuery(this).append('<a href="' + articleLink + '" target="_blank">Read Article</a>');
	
	});
	
}


function add_banner_shuffle( containerObject, topPosition, leftPosition, ignoreFirstBanner, showControls ) {
	
	containerObject.fadeDelay = 5000;
	containerObject.fadeSpeed = 500;

	containerObject.ignoreFirstBanner = ignoreFirstBanner || false;
	containerObject.topPosition = topPosition || 0;
	containerObject.leftPosition = leftPosition || 0;
	containerObject.showControls = showControls || false;

	containerObject.banners = new Array();
	containerObject.currentBannerIndex = 0;
	containerObject.containerMinHeight = 0;
	
	containerObject.timeout = 0;
	containerObject.animating = false;
	
	containerObject.initialise = function() {
		containerObject.banners = containerObject.find('.Banner:has(img)');

		if(ignoreFirstBanner)
			containerObject.banners = containerObject.banners.slice(1);
		
		if (containerObject.banners.length > 1) {
			
			containerObject.banners.each(function( index ) {
				jQuery( containerObject.banners.get(index) ).find('img').load( function() {
					
					var containerMinHeight = parseInt( jQuery(this).parent().parent().parent().css('min-height') );
					var thisHeight = this.height + parseInt( jQuery(this).parent().parent().css('margin-top') ) + parseInt( jQuery(this).parent().parent().css('padding-top') ) + parseInt( jQuery(this).parent().parent().css('margin-bottom') ) + parseInt( jQuery(this).parent().parent().css('padding-bottom') );

					if (containerMinHeight<thisHeight) {
						jQuery(this).parent().parent().parent().css({
							'min-height': thisHeight
						});
					}
				});
			});			
			containerObject.banners.parent().css({
				'min-height': containerObject.containerMinHeight
			});

			containerObject.banners.css({
				'position': 'relative'
			});

			containerObject.banners.css({
				'position': 'absolute',
				'top': containerObject.topPosition,
				'left': containerObject.leftPosition,
				'display': 'none'
			});
			
			if (containerObject.showControls) {
			
				var bannerControls = '<div class="banner-controls"><div class="banner-controls-previous"></div>';
				bannerControls = bannerControls + '<div class="banner-controls-counter"><ul>'
				bannerControls = bannerControls + '<li class="on"></li>';
				for (i=1;i<containerObject.banners.length;i++){
					bannerControls = bannerControls + '<li></li>';
				}
				bannerControls = bannerControls + '</ul></div>';
				bannerControls = bannerControls + '<div class="banner-controls-next"></div></div>';
				
				containerObject.find('> div').eq(0).append(bannerControls);
				
				containerObject.find('.banner-controls-previous').click( function() {
					containerObject.fadeToPreviousBanner();
				});
				containerObject.find('.banner-controls-next').click( function() {
					containerObject.fadeToNextBanner();
				});

				containerObject.currentBannerIndex = 0;
			
			} else {
				containerObject.currentBannerIndex = Math.floor(Math.random() * (containerObject.banners.length));
			}
			
			jQuery( containerObject.banners.get( containerObject.currentBannerIndex ) ).show();
			
			setTimeout( function() {
				containerObject.fadeToNextBanner();
			}, containerObject.fadeDelay);
		}
		
	}

	containerObject.fadeToRandomBanner = function() {
		containerObject.fadeToParticularBanner( containerObject.getRandomBannerIndex() );
	};

	containerObject.fadeToNextBanner = function() {
		containerObject.fadeToParticularBanner( containerObject.getNextBannerIndex() );
	};
	
	containerObject.fadeToPreviousBanner = function() {
		containerObject.fadeToParticularBanner( containerObject.getPreviousBannerIndex() );
	};	
	
	containerObject.fadeToParticularBanner = function(nextBannerIndex) {
		
		if (!containerObject.animating) {
		
			clearTimeout(containerObject.timeout);
			
			currentBanner = jQuery( containerObject.banners.get( containerObject.currentBannerIndex ) )
			containerObject.currentBannerIndex = nextBannerIndex;		
			nextBanner = jQuery( containerObject.banners.get( containerObject.currentBannerIndex ) )
	
			containerObject.animating = true;

			containerObject.find('.banner-controls-counter li.on').removeClass('on');
			
			currentBanner.stop(true, true).fadeOut( containerObject.fadeSpeed );
			nextBanner.stop(true, true).fadeIn( containerObject.fadeSpeed, function() {
				
				containerObject.animating = false;
				
				containerObject.find('.banner-controls-counter li').eq( containerObject.currentBannerIndex ).addClass('on');
				
				containerObject.timeout = setTimeout( function() {
					if (containerObject.showControls) {
						containerObject.fadeToNextBanner();
					} else {
						containerObject.fadeToRandomBanner();
					}
				}, containerObject.fadeDelay);			
			});				
		
		}
			
	};	

	containerObject.getRandomBannerIndex = function() {
		tempNewIndex = Math.floor(Math.random() * (containerObject.banners.length));
		if (tempNewIndex==containerObject.currentBannerIndex) {
			tempNewIndex = containerObject.getRandomBannerIndex();
		}
		return tempNewIndex;
	}
	
	containerObject.getNextBannerIndex = function() {
		tempNewIndex = containerObject.currentBannerIndex + 1;
		if (tempNewIndex==containerObject.banners.length) {
			tempNewIndex = 0;
		}
		return tempNewIndex;
	}	
	
	containerObject.getPreviousBannerIndex = function() {
		tempNewIndex = containerObject.currentBannerIndex - 1;
		if (tempNewIndex<0) {
			tempNewIndex = containerObject.banners.length-1;
		}
		return tempNewIndex;
	}		
	
	var windowLocation = window.location + '';
	if (windowLocation.toLowerCase().indexOf('/admin') == -1 ) {
		containerObject.initialise();
	}
	
}
