// HBC College Ministry Javascript Document
$ = jQuery.noConflict();
$ = jQuery;

$(document).ready(function(){	
	$("#nav > li").hover(
		function(){ $("ul:first", this).css({display: 'block'}); },
		function(){ $("ul:first", this).css({display: 'none'}); }
	);
	
	$("#nav > li ul li").hover(
		function(){ $("ul:first", this).css({display: 'block'}); }, 
		function(){ $("ul:first", this).css({display: 'none'}); } 
	);
	
	$("#nav > li ul li ul li").hover(
		function(){ $("ul:first", this).css({display: 'block'}); }, 
		function(){ $("ul:first", this).css({display: 'none'}); } 
	);
	
	$("#nav > li ul li ul li ul li").hover(
		function(){ $("ul:first", this).css({display: 'block'}); }, 
		function(){ $("ul:first", this).css({display: 'none'}); } 
	);
	
	$("#nav li ul").css({display: 'none'});
	
	if($('#banner').length > 0){ //If element exists
		$('#banner').cycle({
			fx : 'fade',
			speed :  4000, 
    		timeout : 4000,
			pause : 1,
			random : true,
			cleartype :  1, // enable cleartype corrections
			cleartypeNoBg: true,
			pager : '#banner-pager',
			pagerAnchorBuilder: function(idx, slide) {
				if (idx == 0) {
						return '<li class="pagination"><a href="#"></a></li>';
				}
				else {
                        return '<li class="pagination"><a href="#"></a></li>';
                }

			}

		});
	}
	
	/* ############### History Manager ############### */
	historyManager = {
		hashVal : '',
		interval : 100,
		init : function(){
			// historyManager.checkHash();
			setInterval('historyManager.checkHash()', this.interval);
		},
		backFunc : function(hash){
			cl(hash);
		},
		registerBackFunc : function(func){
			this.backFunc = func;
		},
		checkHash : function(){
			hash = this.getHash();
			if(hash != this.hashVal){
				// cl('setting hash: ' + hash);
				this.backFunc(hash, 0);
				this.hashVal = hash;
			}
		},
		getHash : function(){
			return document.location.hash.replace('#', '');
		},
		setHash : function(hash){
			if(hash.replace(' ', '').length <= 0){
				cle('Trying to setHash with empty string. String: "' + hash + '"');
				clt('Trying to setHash with empty string. String: "' + hash + '"');
				return false;
			}
			document.location.hash = hash;
			this.hashVal = hash;
			// Delay the logging of Clicky data so that we don't have a lag
			setTimeout("clicky.log('" + document.location.pathname + '#' + hash + "')", 850);
		}
	}
	
	if($('.bio-cycle').length > 0){
		$('.bio-cycle').cycle({ 
		    fx : 'scrollDown',
			easing : 'bounceout', 
			speed :  1200, 
    		timeout : 0,
			random : 0,
			cleartype :  1, // enable cleartype corrections 
			cleartypeNoBg: true,
			updateActivePagerLink: true,
			next : '#bio_next',
			prev : '#bio_previous',
			pager: '#bio-pager',
			before : function(currSlideElement, nextSlideElement, options, forwardFlag){
			
				cl('cycle().before ' + currSlideElement);
			
			
				// This prevents the slide from running if the page loads with a string in the hash
				if($('.bio-cycle').data('first_slide_loaded')){
				
					// If hash is empty and slide is first, don't update hash
					if($(this).prevAll().length == 0 && historyManager.getHash() == '') 
						return $('.bio-cycle').data('first_slide_loaded', true);
					
					historyManager.setHash($(this).attr('class'));
				}
				$('.bio-cycle').data('first_slide_loaded', true)
			
			},
			after : function(){
			},
			pagerAnchorBuilder: function(idx, slide) { 
		        return '#bio-pager li:eq(' + idx + ') a'; 
		    }
		});
	}
	
	historyManager.registerBackFunc(function(hash){
		slider = $('.bio-cycle');
		
		// Make sure the hash is found in our slider:
		if($('.bio-cycle').find('.' + hash).length <= 0)
			return false;
		
		if(hash == '')
			hash = slider.find('li:first').attr('class')
		
		cl('sliding in first slide');
		// 
		// if(slider.data('first_slide_loaded') !== true){
		// 	cl('sliding in first slide');
		// 	prevSpeed = slider.data('cycle.opts').speedOut;
		// 	slider.data('cycle.opts').speedIn = slider.data('cycle.opts').speedOut = 0;
		// }
		
		var currentSlide = $('.' + hash);
		var slideIndex = currentSlide.prevAll().length;
		slider.cycle(slideIndex);
		
		// if(typeof prevSpeed != "undefined")
		// 	slider.data('cycle.opts').speedIn = slider.data('cycle.opts').speedOut = prevSpeed;
			
		slider.data('first_slide_loaded', true);
	});
	historyManager.init();
	
	function cl(msg){
		if(typeof(console) != "undefined"){
			console.log(msg);
		}
	}
	
	function cle(msg){
		if(typeof(console) != "undefined"){
			console.error(msg);
		}
	}
	function clt(msg){
		if(typeof(console) != "undefined"){
			console.trace(msg);
		}
	}

});
	
$(function(){$.fn.scrollToTop=function(){$(this).hide().removeAttr("href");if($(window).scrollTop()!="0"){$(this).fadeIn("fast")}var scrollDiv=$(this);$(window).scroll(function(){if($(window).scrollTop()=="0"){$(scrollDiv).fadeOut("fast")}else{$(scrollDiv).fadeIn("fast")}});$(this).click(function(){$("html, body").animate({scrollTop:0},"slow")})}});
$(function() {
	$("#toTop").scrollToTop();
});
