// Skin Specific JS goes here. The jQuery library has already been loaded by the core templates. So if you use jQuery, you are ready to go.
jQuery(document).ready(function() {
	// Clear default search text on click.
	if (jQuery('#siteSearchInput').length > 0) {
		defaultSearchValue = jQuery('#siteSearchInput')[0].value;
		jQuery('#siteSearchInput').focus(function() {
			if (jQuery(this)[0].value === defaultSearchValue) {
				jQuery(this)[0].value = '';
			}
		});

		jQuery('#siteSearchInput').blur(function() {
			if (jQuery(this)[0].value === '') {
				jQuery(this)[0].value = defaultSearchValue;
			}
		});
	}

	// Clear homepage newsletter subscribe text on click.
	if (jQuery('#homeNewsletterFormWrap .emailInput').length > 0) {
		defaultEmailValue = jQuery('#homeNewsletterFormWrap .emailInput')[0].value;
		jQuery('#homeNewsletterFormWrap .emailInput').focus(function() {
			if (jQuery(this)[0].value === defaultEmailValue) {
				jQuery(this)[0].value = '';
			}
		});

		jQuery('#homeNewsletterFormWrap .emailInput').blur(function() {
			if (jQuery(this)[0].value === '') {
				jQuery(this)[0].value = defaultEmailValue;
			}
		});
	}
	
	// Clear sermon search text on click.
	if (jQuery('#messages-search #messages-search-field').length > 0) {
		defaultEmailValue = jQuery('#messages-search #messages-search-field')[0].value;
		jQuery('#messages-search #messages-search-field').focus(function() {
			if (jQuery(this)[0].value === defaultEmailValue) {
				jQuery(this)[0].value = '';
			}
		});

		jQuery('#messages-search #messages-search-field').blur(function() {
			if (jQuery(this)[0].value === '') {
				jQuery(this)[0].value = defaultEmailValue;
			}
		});
	}

	// Slide ministry index - up or down
	jQuery('#ministry_tab').toggle(function() {
		jQuery('#ministry_index:hidden').slideDown();
		jQuery('#ministry_tab')[0].blur();
		jQuery(this).addClass('on');
		return false;
	}, function() {
		jQuery('#ministry_index:visible').slideUp();
		jQuery('#ministry_tab')[0].blur();
		jQuery(this).removeClass('on');
		return false;
	});
		
	//Campus Location glider or slider
	jQuery('#slider .csc-textpic').each(function(index, el) {
		jQuery(this).find('ul li:eq(0)').css({left: '45px', 'z-index': 1}).find('a').append('<p>Click for more photos of this campus</p>');
		jQuery(this).find('ul li:eq(1)').css({left: '20px', top: '20px'}).find('img').attr({width: 170, height: 120});
		jQuery(this).find('ul li:eq(2)').css({left: '120px', top: '20px'}).find('img').attr({width: 170, height: 120});
		jQuery(this).find('ul li:gt(2)').each(function() {
			jQuery(this).hide();
			jQuery(this).find('img').removeAttr('src');
		});
	});

	//Campus Location image lightbox
        jQuery('a.lightbox').fancybox({padding: 0});

	jQuery("a.lightbox-iframe").fancybox({
		'width': '75%',
		'height': '75%',
		'type': 'iframe',
		'padding': 0
	});

	jQuery("a.lightbox-vimeo").click(function(){
		jQuery.fancybox({
			'width': 600,
			'height': 397,
			'href': this.href.replace(new RegExp("(vimeo.com)", "i"), 'player.vimeo.com/video'),
			'type': 'iframe',
			'padding': 0
		});
		return false;
	});

	jQuery("a.lightbox-youtube").click(function(){
		jQuery.fancybox({
			'width': 600,
			'height': 437,
			'href': this.href.replace(new RegExp("christfellowship#p\/u\/[0-9]*\/", "i"), 'v/index.html'),
			'type': 'swf',
			'padding': 0,
			'swf': {
				'wmode': 'transparent',
				'allowfullscreen': 'true'
			}
		});
		return false;
	});

	// jQuery("a.lightbox-mediasuite").fancybox({
	//	'width': 520,
	//	'height': 620,
	//	'type': 'iframe',
	//	'padding': 10
	// });

	jQuery("span.resourceWithoutLink").hover(function() {
			originalText = jQuery(this).text();
			jQuery(this).text('Coming Soon');	
		},
		function() {
			jQuery(this).text(originalText);
		}
	);


	//twitter
	jQuery('#wt_twitter_newsticker li').delay(800).fadeIn();

	// countdown
	if (jQuery('#countdown').length > 0) {
		var countdown = jQuery("#countdown").countDown({
			'targetDate': countdownTargetDate,
			'onComplete': function() {
				SA.redirection_mobile ({
					mobile_url: 'gochristfellowship.com/mobile-live-service/',
					tablet_redirection : 'true'
				});

				jQuery(".countdown-active").fadeOut(1000, function() {
					jQuery(".countdown-complete").fadeIn(500);
				});
			}
		});

		if (countdown.data().diffSecs) {
			jQuery(".countdown-active").fadeIn();
		}
	}
	
	//globalMenu
	/*
	jQuery("#globalMenu li").hoverIntent({
		over: function() {
			jQuery(this).addClass('hovered').children('ul').slideToggle(800, 'easeOutBack');
		},
		out: function() {
			jQuery(this).children('ul').stop(true).hide();
			jQuery(this).removeClass('hovered');
		}
	});
	*/

	jQuery("#globalMenu li").hover(
		function() {
			jQuery(this).addClass('hovered').children('ul').show();
		},
		function() {
			jQuery(this).children('ul').hide();
			jQuery(this).removeClass('hovered');
		}
	);
	
	//odd Category Item
	jQuery('.classCategory .classCategoryItem:odd').addClass('oddCategoryItem');
});

function startAutoPlay() {
  return setInterval(function() {
    jQuery("#slider .csc-textpic ul").roundabout_animateToNextChild();
  }, 3000);
}

// Preload menu-related images.
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$(['fileadmin/20CF/skins/skin_christfellowship/css/images/menu-bg.png',
   'fileadmin/20CF/skins/skin_christfellowship/css/images/menu-hover-left.png',
   'fileadmin/20CF/skins/skin_christfellowship/css/images/menu-hover-right.png',
   'fileadmin/20CF/skins/skin_christfellowship/css/images/menu-dropdown-bg.png']).preload();

/*
* JS Redirection Mobile
*
* Developed by
* Sebastiano Armeli-Battana (@sebarmeli) - http://www.sebastianoarmelibattana.com
* Dual licensed under the MIT or GPL Version 3 licenses.
* @version 0.8.6
*/
if(!window.SA){window.SA={};}SA.redirection_mobile=function(j){var c=function(t){var s=new Date();s.setTime(s.getTime()+t);return s;};var m=function(x){if(!x){return;}var s=document.location.search,y=s&&s.substring(1).split("&"),u=0,w=y.length;for(;u<w;u++){var t=y[u],v=t&&t.substring(0,t.indexOf("="));if(v===x){return t.substring(t.indexOf("=")+1,t.length);}}};var a=navigator.userAgent.toLowerCase(),o="false",e="true",r=j||{},n=r.redirection_paramName||"mobile_redirect",p=r.mobile_prefix||"m",k=r.mobile_url,d=r.mobile_scheme?r.mobile_scheme+":":document.location.protocol,l=document.location.host,f=m(n),g=k||(p+"."+(!!l.match(/^www\./i)?l.substring(4):l)),h=r.cookie_hours||1,b=!!(a.match(/(iPhone|iPod|blackberry|android 0.5|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii)/i));if(document.referrer.indexOf(g)>=0||f===o){if(window.sessionStorage){window.sessionStorage.setItem(n,o);}else{document.cookie=n+"="+o+";expires="+c(3600*1000*h).toUTCString();}}var q=(window.sessionStorage)?(window.sessionStorage.getItem(n)===o):false,i=document.cookie?(document.cookie.indexOf(n)>=0):false;if(!!(a.match(/(iPad|SCH-I800|xoom|kindle)/i))){b=(r.tablet_redirection===e)?true:false;}if(b&&!(i||q)){if(r.beforeredirection_callback){if(!r.beforeredirection_callback.call(this)){return;}}document.location.href=d+"//"+g;}};

