
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};

// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());


function nextBanner(banner_index) {
	$(".huge_banner").stop(true, true);
	if (window_focus) {
		if (!banner_index && banner_index != 0) {
			var next_banner = $(".huge_banner:visible").next();
		} else {
			var next_banner = $(".huge_banner:eq(" + banner_index + ")");
		}
		
		var current_banner = $(".huge_banner:visible");
		
		if (!next_banner.length) {
			next_banner = $(".huge_banner:first");
		}
		
		$(".huge_banner:visible .description").fadeOut('slow');
		$(".huge_banner:visible").fadeOut('slow');	
		$("#huge_banner_navigation .dot").css('background-color', '#fff');
		$("#huge_banner_navigation .dot:eq(" + ($(next_banner).index()-1) + ")").css('background-color', '#323692');
		$(next_banner).fadeIn('slow', function() {
			$(".description", next_banner).animate({
				height: 'toggle'
			}, 300);
		});
	}
}

function fontSize(level) {
	if (level == 1) {
		$('body').css('font-size', '12px');
	} else if (!level || level == 2) {
		$('body').css('font-size', '14px');
	} else {
		$('body').css('font-size', '18px');
	}
}

huge_banner_timer = null;
window_focus = true;

$(document).ready(function() {
	// Are we in focus? This resolves issues with Chrome and possibly Firefox where setInterval gets jacked up when not in focus. Love me some Chrome but seriously Google, what?
	$(window).focus(function() {
		window_focus = true;
	}).blur(function() {
		window_focus = false;
	});

	// Banner loader
	$(window).load(function() {
		$(".huge_banner_loader").fadeOut('fast', function() {
			$(".huge_banner_loader").remove();
		});
	});
	
	// If we have a huge banner, start it rotating but pause if it's hovered over
	if ($(".huge_banner").length) {
		if (!huge_banner_timer) {
			huge_banner_timer = setInterval(nextBanner, 10000);
		}
		
		$(".huge_banner").hover(function() {
			clearInterval(huge_banner_timer);
			huge_banner_timer = null;
		}, function() {
			if (!huge_banner_timer) {
				huge_banner_timer = setInterval(nextBanner, 10000);
			}
		});
	}
	
	// Add navigation dots to the huge banner
	for (i = 0; i < $(".huge_banner").length; i++) {
		$("#huge_banner_navigation").append("<div class='dot'></div>");
	}
	
	// Set first navigation dot
	$(".huge_banner:visible .description").css('display', 'block');
	$("#huge_banner_navigation .dot:first").css('background-color', '#323692');
	
	// Change banner if navigation dot is clicked
	$("#huge_banner_navigation .dot").live('click', function() {
		clearInterval(huge_banner_timer);
		huge_banner_timer = null;
		nextBanner($(this).index());
	});
	
	// Change banner on mousewheel
	$(".huge_banner").mousewheel(function(event, delta, deltaX, deltaY) {
		var o = '';
		if (delta > 0) {
			if (($(".huge_banner:visible").index()-2) < 0) {
				nextBanner($(".huge_banner:last").index()-1);
			} else {
				nextBanner($(".huge_banner:visible").index()-2);
			}
		} else if (delta < 0) {
			nextBanner();
		}
		
		return false;
	});
	
	// Set the accessible main navigation to slide down when clicked
	$(".accessible_main_navigation li a").click(function() {
		if ($(window).width() <= 719) {
			if ($("ul.sub-menu", $(this).parent()).length) {
				$("ul.sub-menu", $(this).parent()).toggle('fast');
				return false;
			}
		}
	});
	
	// Fancybox it up
	$(".iframe").fancybox({
		'overlayOpacity'	: 0.8,
		'overlayColor'		: '#000',
		'width'				: '75%',
		'height'				: '75%',
		'autoScale'		: true,
		'transitionIn'		: 'elastic',
		'transitionOut'	: 'fade',
		'type'					: 'iframe'
	});
	
	$(".wp-caption a").fancybox({
		'overlayOpacity'	: 0.8,
		'overlayColor'		: '#000',
		'transitionIn'		: 'elastic',
		'transitionOut'	: 'fade'
	});
	
	// Map reader
	if ($(".zoomable_map").length) {
		zoom_increment = 0.5;
		current_zoom = 1;
		
		$(".zoomable_map img").load(function() {
			// Start map image at 100%
			original_width = $(".zoomable_map img").width();
			$(".zoomable_map img").css('width', '100%');
			
			// Zoom Image
			$(".zoomable_map").click(function(e) {
				var top = e.pageY - $(".zoomable_map").offset().top;
				var left = e.pageX - $(".zoomable_map").offset().left;
				current_zoom += zoom_increment;
				if (current_zoom > (zoom_increment*4)) {
					current_zoom = 1;
					$(".zoomable_map img").css('left', '0');
					$(".zoomable_map img").css('top', '0');
				}
				
				$(".zoomable_map img").css('width', (current_zoom*$(".zoomable_map").width()) + 'px');
			});
			
			$(".zoomable_map img").draggable();
		});
	}
	
	// Make sure video's are resizing responsively
	$(".responsive_video").css('height', ($(".responsive_video").width()*0.77) + 'px');
	$(window).resize(function() {
		$(".responsive_video").css('height', ($(".responsive_video").width()*0.77) + 'px');
	});
});

// Mousewheel extension
(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener ) {
            for ( var i=types.length; i; ) {
                this.addEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = handler;
        }
    },
    
    teardown: function() {
        if ( this.removeEventListener ) {
            for ( var i=types.length; i; ) {
                this.removeEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = null;
        }
    }
};

$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});


function handler(event) {
    var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
    event = $.event.fix(orgEvent);
    event.type = "mousewheel";
    
    // Old school scrollwheel delta
    if ( event.wheelDelta ) { delta = event.wheelDelta/120; }
    if ( event.detail     ) { delta = -event.detail/3; }
    
    // New school multidimensional scroll (touchpads) deltas
    deltaY = delta;
    
    // Gecko
    if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
        deltaY = 0;
        deltaX = -1*delta;
    }
    
    // Webkit
    if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
    if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
    
    // Add event and delta to the front of the arguments
    args.unshift(event, delta, deltaX, deltaY);
    
    return $.event.handle.apply(this, args);
}

})(jQuery);
