$(document).ready(function() {
	
	// loaded
	
/**
 * Extend footer div to bottom
 *
 */

	$('#footer').height(function(index, height) {  
		return window.innerHeight - $(this).offset().top;  
	});
	
	// when resize window, we need to re-extend the legal div
	$(window).resize(function() {
		$('#footer').height(function(index, height) {  
			return window.innerHeight - $(this).offset().top;  
		});
	});
	
/**
 * External links in new window
 *
 */

	$('html a:not(.popupwindow)').filter(function(){
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)){

					// Build tracking url
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;

					// Track
					//pageTracker._trackPageview(fixedLink);
				};
			});
		};
	});
	
/**
 * Book excerpts
 *
 */
	
	$("#excerpts .large").hide();		// hide all full size
	$("#excerpt-1").show();	// show first

	// when click on thumb, hide all, and show full
	$(".thumb").click(function() {
		$("#excerpts .large").hide();
		$("#excerpt-"+$(this).attr("id").replace("thumb-","")).show();
		return false;
	});

/**
 * jQuery Validation
 *
 */
	
	$("form#book-appearance").validate({
		rules:
		{
			name:
			{
				required: true,
			},
			organization:
			{
				required: true,
			},
			email:
			{
				required: true,
				email: true
			},
			phone:
			{
				required: true,
			},
			event_date:
			{
				required: true,
			},
			details:
			{
				required: true
			}
		},
		errorLabelContainer: "#error_display",
		wrapper: "p",
		errorClass: "invalid"
	});
	
/**
 * Input masks
 *
 */

	$(".phone").mask("(999) 999-9999");
	$(".date").mask("99/99/9999");
	
/**
 * Search fields
 *
 */
	
	$('input[name=keywords]').searchField();
	
/**
 * Slideshows (bottom of page)
 *
 */
	
	// wrap all items in main div
	var columns = $(".slide").attr("rel");
	var items = $(".slide"),i=0,c=1;
	while( i < items.length ) {
		items.slice( i,i+=parseInt(columns) ).wrapAll('<div class="slide-group" id="slide-group-'+c+'"></div>');
		c++;
	}
	
	// hide all items
	$(".slide-group").hide();
	$("#slide-group-1").show();
	$(".slide-set").attr('rel', '1');
	
	// total number of divs
	var total = $(".slide-group").length;
	
	$(".slide-set .next").click(function() {
		
		// increment slide-set
		var next = parseInt($(".slide-set").attr('rel')) + parseInt(1);
		
		if(next <= total){
		
			// set rel for .slide-set of current count
			$(".slide-set").attr('rel', next);
		
			// hide all again
			$(".slide-group").hide();
			$("#slide-group-"+$(".slide-set").attr('rel')).show();
		}
		return false;
	});
	
	$(".slide-set .prev").click(function() {
		
		// increment slide-set
		var prev = parseInt($(".slide-set").attr('rel')) - parseInt(1);

		if(prev >= 1){
		
			// set rel for .slide-set of current count
			$(".slide-set").attr('rel', prev);
		
			// hide all again
			$(".slide-group").hide();
			$("#slide-group-"+$(".slide-set").attr('rel')).show();
		}
		return false;
	});
	
});
