document.domain=document.domain;

// 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;
			}
		});
	}

	// local menu :last-child fix for IE7/8
	jQuery('#localMenu li.active li.current li:last-child,#localMenu li.active li.active li:last-child').addClass('lastChild');

	// 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) {
		// @todo Remove hardcoded text.
		jQuery(this).find('ul li:eq(0)').css({left: '45px', 'z-index': 1}).find('a').append('<p>See 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,
		onComplete: function() {
			var campusDropdown = jQuery('#mailformcampus');
			if (campusDropdown.length) {
			var campusUid = 0;
			var selectedPanel = jQuery('#slider_nav .selected a');
			// If we we're on a page with a campus slider and have loaded a contact form, continue
			if (selectedPanel.length) {
				// Get the ID of the selected tab in the campus slider and remap to match campus dropdown IDs
				campusUid = selectedPanel.attr('href').substr(-1);
			} else {
				name = 'campus'
    				var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(this.href);
    				if (results) { 
        				campusUid = results[1] || 0;
				}
			}

			if (campusUid) {
				switch(campusUid) {
                                        case '1':
                                                campusUid = 1;
                                                break;
                                        case '2':
                                                campusUid = 3;
                                                break;
                                        case '3':
                                                campusUid = 2;
                                                break;
                                        case '4':
                                                campusUid = 4;
                                                break;
                                        case '5':
                                                campusUid = 6;
                                                break;
                                        case '6':
                                                campusUid = 7;
                                }
                                // Set the desired campus id
                                campusDropdown.val(campusUid);
			}	
			}	
		}
	});

	var isMobile = false;
	if (navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/iPad/i) ||
		navigator.userAgent.match(/BlackBerry/i) ||
		navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/IEMobile/i) ||
		navigator.userAgent.match(/Nook/i)) {
		isMobile = true;
	}

	if (isMobile) {
		jQuery("#fancybox-img").live("click", function(e) {
			jQuery.fancybox.next();
		});
	}

	if (!isMobile) {
		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') + '?autoplay=true',
			'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/') + '?autoplay=1',
			'type': 'swf',
			'padding': 0,
			'swf': {
				'wmode': 'transparent',
				'allowfullscreen': 'true'
			}
		});
		return false;
	});

	if (isMobile) { 
		jQuery('a.lightbox-jwplayer').removeClass('lightbox-jwplayer');
	}
	jQuery("a.lightbox-jwplayer").click(function() {
		var videoUrl = jQuery(this).attr("href");
		jQuery.fancybox({
			padding: 0,
			autoDimensions: true,
			content: '<div id="video" style="background-color: #333; width:640px; height:360px; overflow: hidden; margin:0; padding:0"></div>',
			onComplete: function() {
				var baseUrl = '/fileadmin/20CF/skins/skin_christfellowship/extensions/messages/';
				var videoPlayer = jwplayer("video").setup({
					flashplayer: baseUrl + "lib/mediaplayer-5.5-licensed/player.swf",
					file: videoUrl,
					height: 360,
					width: 640,
					skin: baseUrl + "lib/mediaplayer-5.5-licensed/glow.zip",
					autostart: true
				});
			}
		});
		return false;
        });

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

	jQuery(".contactLightbox .tx-cfcontact-pi1 form").live("submit", function() {
		// @todo Remove hardcoded text.
		if (validateForm('mailform','first_name,First Name:,_EMAIL,email,Email:,campus,Campus:,comments,Comments:','','','')) {
			jQuery.fancybox.showActivity();

			jQuery.ajax({
				type : "POST",
				processData : false,
				cache : false,
				url : jQuery(this).attr("action"),
				data : jQuery(this).serialize() + '&formtype_mail=1',
				success: function(data) {
					// @todo Remove hardcoded text.
					jQuery.fancybox("<h3>Your Submission Was a Success!</h3><p>We are so thrilled you decided to send us a question, comment, or idea.  We will do our best to respond to you as soon as possible.</p><p>Thanks again for letting us hear from you.  Have a blessed day.</p>");
				}
			});
		}

		return false;
	});

	jQuery("span.resourceWithoutLink").hover(function() {
			originalText = jQuery(this).text();
			// @todo Remove hardcoded 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() {
				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');
	
	// Accordion Content
	jQuery('.fceAccordionContent').hide();
	jQuery('.fceAccordionHeader').bind('click', function () {
		if (jQuery(this).next('.fceAccordionContent').is(':hidden')) {
			jQuery('.fceAccordionContent').slideUp('slow');
			jQuery('.fceAccordionHeader').removeClass('open');
			jQuery(this).toggleClass('open');
			jQuery(this).next('.fceAccordionContent').slideDown('slow');
		} else {
			jQuery('.fceAccordionContent').slideUp('slow');
			jQuery('.fceAccordionHeader').removeClass('open');
		}
	});

	if (window.location.hash) {
		var element = jQuery(window.location.hash);
		if (element.length) {
			jQuery(element).find('.fceAccordionHeader').addClass('open');
			jQuery(element).find('.fceAccordionContent').slideDown('slow');
		}
	}
	
});

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;}};

