/**
 * Resizes floated image containers to the size of the image
 */
$.fn.imageWidth = function(threshold) {
	return this.each(function() {
		// Threshold is the maximum width an image plus its border and padding can be in relation to its parent container
		var threshold = (threshold) ? threshold : 2/3;
		
		// Determine the width of the image along with borders and padding
		var $image = $('img', $(this));
		var imageWidth = $image.width();
		var paddingLeft = parseInt($image.css('padding-left').replace('px', ''), 10);
		var paddingRight = parseInt($image.css('padding-right').replace('px', ''), 10);
		var borderLeft = parseInt($image.css('border-left-width').replace('px', ''), 10);
		var borderRight = parseInt($image.css('border-right-width').replace('px', ''), 10);
		
		// Calculate total edge (padding and border) width and the total width (edge and image)
		var edgeWidth = paddingLeft + paddingRight + borderLeft + borderRight;
		var totalWidth = imageWidth + edgeWidth;
		
		// Find parent and determine width
		var $parent = $(this).parent();
		var parentWidth = $parent.width();
		
		// If the image is greater then the threshold times the parent width resize the image and the container width
		// Otherwise set the image left's div to the size of the image plus the edge
		if ((threshold * parentWidth) <= totalWidth) {
			var revisedWidth = parentWidth * threshold;
			var revisedImageWidth = revisedWidth - edgeWidth;
			
			$image.width(revisedImageWidth);
			$(this).width(parseInt(revisedWidth, 10));
		} else {
			$(this).width(totalWidth);
		};
	});		
};

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
$.fn.pullQuote = function() {
	return this.each(function() {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '&hellip; ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' &hellip;';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});		
};

/**
 * Clears a text form element when it has the style 'clear-default'
 */
$.fn.clickClear = function() {
	return this.each(function() {
		this.defaultValue = $(this).val();
		$(this).click(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).focus(function() {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val(this.defaultValue);
			};
		});
		
		$('form').submit(function(event) {
			if ($(this).val() == this.defaultValue) {
				$(this).val('');
			};
		});
	});	
};

var markupPrep = function() {
	var listPrep = function() {
		$('> li:last', 'ul, ol').addClass('last');
	};
	
	var	tablePrep = function() {
		$('table tr:odd').addClass('alt');
		$('table td:last, table th:last').addClass('last');
	};
	
	listPrep();
	tablePrep();
};

// Loads in Modernizr script which checks for CSS3 capabilities
$('body').ready(function() {
	$.getScript('/js/modernizr.min.js');
});

var leadersMentioned = function(){	
	$("div.leaders-mentioned li a").mouseover(function(){
		var title = this.title;
		var top = $(this).position().top;
		var left = $(this).position().left;
		var topOffset = 34;
		var leftOffset = 40;
		
		$("div.head-rollover." + title)
			.css('display', 'block')
			.css("top",(top - topOffset - $("div.head-rollover." + title).height()) + "px")
			.css("left",(left - leftOffset) + "px");								 				
    }).mouseout(function() {
		$("div.head-rollover").css('display','none');
    });	

};

var showPage = function(page1) {
	$('#comic-pages div').hide();
	$('#comic-pages div.page-' + page1).show();
	$('.nav li').removeClass('active');
	$('li.link-' + page1).addClass('active');
	$('#comic-pages div').removeClass('marker');
	$('#comic-pages div.page-' + page1).addClass('marker');
	pagingStatus();
}

var nextPage = function() {
	if ($('#comic-pages div.marker').next().size() > 0) {
		$('.nav li.active').removeClass('active').next().addClass('active');
		$('#comic-pages div').hide();
		$('#comic-pages div.marker').next().show();
		$('#comic-pages div.marker').removeClass('marker').next().addClass('marker');
		
		pagingStatus();
	}
}

var prevPage = function() {
	if ($('#comic-pages div.marker').prev().size() > 0) {
		$('.nav li.active').removeClass('active').prev().addClass('active');
		$('#comic-pages div').hide();
		$('#comic-pages div.marker').prev().show();
		$('#comic-pages div.marker').removeClass('marker').prev().addClass('marker');
		
		pagingStatus();
	}
}

var pagingStatus = function() {
	if ($('#comic-pages div.marker').prev().size() == 0) {
		$('a.prev').addClass('inactive');
		$('a.next').removeClass('inactive');
	} else if ($('#comic-pages div.marker').next().size() == 0) {
		$('a.next').addClass('inactive');
		$('a.prev').removeClass('inactive');
	} else {
		$('a.prev').removeClass('inactive');
		$('a.next').removeClass('inactive');
	}
}

$(document).ready(function() {
	$('input.clear-default').clickClear();
	$('div.image-left, div.image-right').imageWidth();
	$('span.pullquote-left, span.pullquote-right').pullQuote();
	
	leadersMentioned();
	markupPrep();
	
	$('ul.leader-photos a').lightBox();
	
	$('.world-leaders #keywords, .world-leaders-profile #keywords').autocomplete('/ajax/leader-search.php').result(function(event, item) {
		window.location.href = "/leaders/profile/" + item[1];
	});
	
	$('ul#carousel').innerFade({
		speed: 1000,
		timeout: 5000,
		indexContainer: '#carousel-index'
	});
	
});