/**
 *
 * jQuery Random plugin
 *
 * @author   Michel Belleville <michel.belleville@gmail.com>
 * @version  1.1.0
 * @requires jQuery v1.3.2 or later
 * @license  GPLv3 [http://www.gnu.org/licenses/gpl.html]
 * 
 * @description Picks element(s) at random in a selection.
 * @param integer num (optional) number of elements to pick
 * 
 * Use :
 * $('#whatever .you .like').random();
 * $('.you_can_event select .more_than_one').random(10);
 * 
 */
(function($) {
  jQuery.fn.random = function(num) {
    num = parseInt(num);
    if (num > this.length) return this.pushStack(this);
    if (! num || num < 1) num = 1;
    var to_take = new Array();
    this.each(function(i) { to_take.push(i); });
    var to_keep = new Array();
    var invert = num > (this.length / 2);
    if (invert) num = this.length - num;
    for (; num > 0; num--) {
      for (var i = parseInt(Math.random() * to_take.length); i > 0; i--)
        to_take.push(to_take.shift());
      to_keep.push(to_take.shift());
    }
    if (invert) to_keep = to_take;
    return this.filter(function(i) { return $.inArray(i, to_keep) != -1; });
  };
}) (jQuery);

function onBefore() {
	$(this).closest('ul').andSelf().css({'position': 'static', 'margin': '0 auto', 'width': '100%'});
}

function onAfter() {	
	$(this).find('img').effect('puff', {mode:'show'}, 1000, function() {
		$(this).animate({opacity:1}, 3800).effect('puff', {mode:'hide'}, 300);	
	});
}

function cycleBanner() {
  	// Banner cycling.
  	$('#banner-cycle').cycle({ 
	    fx:     'scrollHorz',
	    speed: 400,
	    timeout: 5500,
	    before: onBefore,
	    after: onAfter,
	    easing: 'easeInQuad'
	});
}

$(document).ready(function() {
	
	//$('#nav-connect .subnav-left dt').next('dd').andSelf().random(4).hide();
	
	// Delay banner until first image load.
	if($('#banner').length > 0) {
		var isrc = $('#preload img:first').attr('src');
		$('#preloader').attr('src', isrc).load(function() {
			cycleBanner();
		});
	}
  	
  	// Main navigation.
  	$('ul#nav li').hover(function() {
  		var $this = $(this);
  		$(this).addClass('hover').find('div.subnav').show();
  	},
  	function() {
  		$(this).removeClass('hover')  		
  		.find('div.subnav').stop(true, true).fadeOut(100);
  	}); 
	
	// Big target.
	$("ul.sidelist li a, ul.widelist li a").bigTarget({
	    hoverClass: 'bigtarget'
	});
	
	$('div.subnav-guide a').bigTarget({
		hoverClass: 'subnav-guide-hover',
		clickZone : 'div:eq(0)'
	});
	
	// Righthalf image overlays
	$('.train .righthalf .content-inner img, .learn .righthalf .content-inner img, .ministry .righthalf .content-inner img').wrap('<div class="image-wrap" />').after('<div class="image-overlay" />');
	
	// Ministry carousel
	$('#homepage-ministries').jCarouselLite({
        btnNext: ".list-next",
        btnPrev: ".list-prev",
        vertical: true,
        visible: 3,
        scroll: 1,
        speed: 500,
        easing: 'easeInOutQuad',
        circular: true
    });
    
    // School tooltips
    $('.schools-box-block').each(function() {
    	$(this).qtip({
    		name: 'tooltip-active',
			content: $('<div class="tooltip-contents" />').html( $(this).children('dd.school-info').contents() ),
			show: {
				delay: 60,
				solo: true,
				when: {
					event: 'mouseover'
				}				
			},		
			hide: {
				delay: 1000,
				fixed: true,
				when: {
					event: 'mouseout'
				}
			},
			position: {
			     corner: {
			        target: 'topMiddle',
			    	tooltip: 'bottomMiddle'
				},			
				adjust: {
					y: -5
				}
			},
			style: { 
				background: '#464646',
				opacity: 0.95,
	      		width: 150,
				padding: 5,
				border: {
				   width: 5,
				   radius: 7,
				   color: '#47e4e3'
				},
				name: 'light',
				tip: {
					color: '#47e4e3',
					corner: 'bottomMiddle'
				}
	   		}
    	});
	});
		
	$('.blog .entry p').has('img').addClass('no-columns');
  	
});
