// document ready
var scrollDirection = 1;
var videoPlaceholderHtml = null;

$(function() {
	
	// Blinking Fields
	$('input[type="text"], textarea')
		.focus(function() { if(this.title==this.value) { this.value = ''; } })
		.blur(function(){ if(this.value=='') {this.value = this.title; } });
		
	// show watch interface
	$('#head-dd .cols .watch-btn').click(function(){
		var href = $(this).attr('href');
		// $('#head-dd .cols').slideUp();
		$(href).fadeIn();
		return false;
	});
	
	$('.row label').each(function(){
		var length = $(this).text().length;
		if(length > 40){
			$(this).css('width','auto');
		};
	});
	
	// toggle head dropdown
	$('.open-head-dd').click(function(){
		$(this).toggleClass('close-head-dd');
		$('#head-dd').stop(true,true).slideToggle();
		return false;
	});
	
	// hover effect
	$('#head-dd .watch-btn, #head-dd .more, #head-dd input[type="submit"]').hover(function(){
		$(this).parents('.col').find('h2').toggleClass('orange');
	});
	
	$('.header-dd .cols .more.inactive').click(function(){
		return false;
	});
	
	$('a[href*="store.crossroadchristianchurch.org"]').attr('target','_blank');
	$('a[href*="bookstore.aspx"], a[href*="page13154227.aspx"]').attr('href','http://store.crossroadchristianchurch.org/').attr('target','_blank');
	$('a[href*="page1314416.aspx"]').attr('href','http://store.crossroadchristianchurch.org/Events/').attr('target','_blank');
	
	// Main navigation dropdown wrap
	(function() {
		var nav = $('#dd-nav');
		var dds = nav.find('.dd');
		
		nav.find('> ul > li > a').wrapInner('<span />');
		
		dds.wrap('<div class="dd-wrap" />');
		dds.wrapInner('<div class="m" />');
		dds.prepend('<div class="t"><span class="arrow"><span></span></span></div>');
		dds.append('<div class="b"></div>');
	})();
	
	// navigation dropdown
	// $('#dd-nav > ul > li').hover(function(e){
	// 	$(this).find('.dd').stop(true,true).fadeToggle();
	// });
	
	// colorbox
	if($('.gallery a').length){
		$('.gallery a:not(.editButton)').fancybox({
			'width':'auto'
		});
	};
	
	// sliders
	$('#connect-slider #slider-01 ul').jcarousel({
		auto: 0,
		scroll: 1,
		visible: 1,
		start: 1,
		wrap: 'circular',
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: connectCarousel
	});
	
	$('#connect-slider #slider-02 ul').jcarousel({
		auto: 0,
		scroll: 1,
		visible: 1,
		start: 2,
		wrap: 'circular',
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: connectCarousel
	});
	
	$('#connect-slider #slider-03 ul').jcarousel({
		auto: 0,
		scroll: 1,
		visible: 1,
		start: 3,
		wrap: 'circular',
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: connectCarousel
	});
	
	// hover effect
	$('#connect-slider .slider ul li a').hover(function(){
		$('.ico', this).animate({ opacity: 1 },{queue: false});
	},function(){
		$('.ico', this).animate({ opacity: 0 },{queue: false});
	})
	
	$('#connect-slider .slide-area, #slider3d-wrap').mouseenter(function(){
		$('#connect-slider .arrow, #slider3d-wrap .arrow').stop().animate({ opacity: 1 },{queue: false});
	}).mouseleave(function(){
		$('#connect-slider .arrow, #slider3d-wrap .arrow').stop().animate({ opacity: 0 },{queue: false});
	})
	
	// expand & collapse area
	$('#connect-slider .trigger').click(function(){
		if( !$(this).hasClass('opened') ){
			$(this).text('close');
			var fromTop = $('#connect-slider').offset().top;
			//console.log(fromTop);
			$('html, body').animate({scrollTop:fromTop}, 'slow');
			$('.slide-area').animate({
				'height': '428px',
				'padding-top': '23px',
				'padding-bottom': '30px'
			},{
				queue: false,
				easing: 'linear'
			});
		}else{
			$(this).text('open');
			$('.slide-area').animate({
				'height': '0',
				'padding-top': '0',
				'padding-bottom': '0'
			},{
				easing: 'linear'
			});
		}
		$(this).toggleClass('opened');
		return false; 
	});
	
	// 3D Slider
	(function() {
		var slider = $('#slider3d');
		var ul = slider.find('> ul');
		var lis = ul.find('> li');
		var currentItem = 1;
		var direction;
		var prev = slider.find('.arrow-prev');
		var next = slider.find('.arrow-next');
		
		var sliderDots = $('.slider-dots .left a');
		sliderDots.click(function() {
			slideTo($(this).index()+1, true);
			return false;
		}).eq(0).addClass('active');

		var sliderWidth = lis.outerWidth(true) * lis.length;
		slider.css({ width: sliderWidth, marginLeft: parseInt( ($(window).width() - 3*lis.outerWidth(true)) / 2) + 'px' });
		ul.css({ width: sliderWidth + 2*lis.outerWidth(true), height: slider.height() });
		
		slider.data('animated', false);
		
		function slideTo(index, goInstantly) {
			sliderDots.removeClass('active').eq(index-1).addClass('active');
			slider.data('animated', true);
			
			if (index == currentItem) return;
			if (!goInstantly) {
				direction = index - currentItem;
				if (direction < -1) direction = 1;
				if (direction > 1) direction = -1;
				
				if (direction < 0) {
					ul.find('> li:last').clone(false, false).prependTo(ul);
					ul.find('> li .moveable').each(function() {
						var currentLeft = parseInt($(this).css('left'));
						$(this).css('left', (currentLeft + ( direction * $(this).closest('div[class|="slide"]').outerWidth(true)) ) + 'px');
					});
				} else {
					ul.find('> li:first').clone(false, false).appendTo(ul);
				}
			} else {
				direction = index - currentItem;
				if (direction < 0) {
					for (var i = 0; i < Math.abs(direction); i++) {
						ul.find('> li').eq(lis.length-1).clone(false, false).prependTo(ul);	
					};
					ul.find('> li .moveable').each(function() {
						var currentLeft = parseInt($(this).css('left'));
						$(this).css('left', (currentLeft + ( direction * $(this).closest('div[class|="slide"]').outerWidth(true)) ) + 'px');
					});
				} else {
					for (var i = 0; i < direction; i++) {
						ul.find('> li').eq(i).clone(false, false).appendTo(ul);
					};
				}
			};
			
			var moveableItems = ul.find('> li .moveable');
			var itemsAnimated = 0;
			
			Before3DSlide();

			moveableItems.stop().each(function(itemIndex) {
				easing = 'jswing';
				duration = 1500;
				
				if ($(this).hasClass('speed-1')) {
					easing = 'easeInOutCubic';
					duration = 2000;
				} else if ($(this).hasClass('speed-2')) {
					easing = 'easeInOutCubic';
					duration = 1500;
				} else if ($(this).hasClass('speed-3')) {
					easing = 'jswing';
					duration = 1500;
				} else if ($(this).hasClass('speed-4')) {
					easing = 'jswing';
					duration = 1000;
				} else if ($(this).hasClass('speed-5')) {
					easing = 'easeInOutCubic';
					duration = 1000;
				} else if ($(this).hasClass('speed-7')) {
					easing = 'easeInOutCubic';
					duration = 500;
				}
				
				var currentLeft = parseInt($(this).css('left'));
				
				$(this).animate({ left: (currentLeft - ( direction * $(this).closest('div[class|="slide"]').outerWidth(true)) ) + 'px'}, duration, easing, function() {
					if (++itemsAnimated == moveableItems.length) finalizeSlide();
				});
			});
			
			function finalizeSlide() {
				if (!goInstantly) {
					if (direction < 0) {
						ul.find('> li:last').remove();
					} else {
						ul.find('> li:first').remove();
						ul.find('> li .moveable').each(function() {
							var currentLeft = parseInt($(this).css('left'));
							$(this).css('left', (currentLeft + ( direction * $(this).closest('div[class|="slide"]').outerWidth(true)) ) + 'px');
						});
					}	
				} else {
					if (direction < 0) {
						for (var i = 0; i < Math.abs(direction); i++) {
							ul.find('> li:last').remove();	
						};
					} else {
						//ul.find('> li:last').remove();
						for (var i = 0; i < direction; i++) {
							ul.find('> li:first').remove();
						};
						ul.find('> li .moveable').each(function() {
							var currentLeft = parseInt($(this).css('left'));
							$(this).css('left', (currentLeft + ( direction * $(this).closest('div[class|="slide"]').outerWidth(true)) ) + 'px');
						});
					}
				}
				
				
				currentItem = index;
				slider.data('animated', false);

			}
		}
		
		function slidePrev() {
			var prevIndex = (currentItem - 1 > 0) ? currentItem - 1 : lis.length;
			slideTo(prevIndex, false);
		}
		
		function slideNext() {
			var nextIndex = (currentItem + 1 <= lis.length) ? currentItem + 1 : 1;
			slideTo(nextIndex, false);
		}
		
		prev.click(function() {
			if (!slider.data('animated')) slidePrev();
			return false;
		});
		
		next.click(function() {
			if (!slider.data('animated')) slideNext();
			return false;
		});

		function Before3DSlide() {
			$('#slider3d .slide-1 .video').html(videoPlaceholderHtml);
		};
	})();
	
	// video-player module iframe fix URL
	$('.video-player iframe').each(function() {
		var iframeURL = getYouTubeIframeURL($(this).attr('src'));
		if (iframeURL != '') $(this).attr('src', iframeURL);
	});
	
	// YouTube Video URL 2 iframe Embed URL
	(function() {
		
		$('#slider3d .slide-1 .video .play a').live('click', function() {
			var link = $(this);
			
			var url = link.attr('href');
			var iframeURL = getYouTubeIframeURL(url, true);
			if (iframeURL != '') link.attr('href', iframeURL);

			var videoContainer = $(this).closest('div.video');
			videoContainer.html('<iframe src="' + iframeURL + '" width="' + videoContainer.width() + '" height="' + videoContainer.height() + '" frameborder="0" /><a href="#" class="video-stop">x</a>');
			return false;
		});

		videoPlaceholderHtml = $('#slider3d .slide-1 .video').html();
		$('#slider3d .slide-1 .video .video-stop').live('click', function() {
			$('#slider3d .slide-1 .video').html(videoPlaceholderHtml);
			return false;
		});	
		
	})();

	function getYouTubeIframeURL(pageURL, autoplay) {
		var iframeURL = '';
		var autoplay = (typeof(autoplay) != 'undefined' && autoplay);
		
		var videoID = pageURL.match(/watch\?v=([\w\d-]+)/);
		if (typeof(videoID[1]) != 'undefined') {
			iframeURL = 'http://www.youtube.com/embed/' + videoID[1] + '?wmode=transparent&amp;rel=0' + ( autoplay ? '&amp;autoplay=1' : '');
		}
		
		return iframeURL;
	}
	
	
	var hldw = parseInt($('.slider-dots .left').outerWidth(true)) + 18;
	$('.slider-dots .holder').css('width', hldw);

	//Calendar
	$('.event-calendar-table tr:eq(0)').addClass('heading-months').next().addClass('heading-days');
	$('.event-calendar .heading-months a:first').prepend('&laquo;&nbsp;');
	$('.event-calendar .heading-months a:last').append('&nbsp;&raquo;');
	$('.event-calendar .heading-days td:eq(0)').addClass('sunday');
	$('.event-calendar-table tr:nth-child(n+3)').addClass('calendar-cnt');
	$('.event-calendar-table tr:nth-child(2n+4)').addClass('even');
	$('.calendar-cnt td').wrapInner('<div class="cell-cnt" />');
	$('.calendar-cnt .cell-cnt').each(function() {
		$(this).find('a').length > 0 ? $(this).find('a').eq(0).addClass('day') : $(this).wrapInner('<span class="day" />');
	});
	$('.calendar-cnt .cell-cnt > a').addClass('day');
	
	$('.cell-cnt .event-box').hover(
		function() {
			$(this).parents('.cell-cnt').eq(0).css('z-index', '1000');
		},
		function() {
			$(this).parents('.cell-cnt').eq(0).css('z-index', '0');
		}
	);
	
	$('a[href$="give-online.aspx"]').live('click', function(){
		window.open('https://thermalstreamsecureserver.com/cross/thermal/give-online/give-online.php','Give','width=700,height=400,resizable=yes,scrollbars=yes');
		return false;
	});
	
});

$(window).resize(function(){
	var pos = (parseInt($(window).width()) - 3057)/2;
	 $('#slider3d').css({
 		'width': '3057px',
 		'margin-left': pos
 	});
}).trigger('resize');


function connectCarousel(carousel){
	$('#connect-slider .arrow-prev').click(function(){
		carousel.prev();
		return false;
	})
	$('#connect-slider .arrow-next').click(function(){
		carousel.next();
		return false;
	})
}
