$(document).ready(function(){
	// Load Cufón & fonts
	$("head").append('<script type="text/javascript" src="/assets/script/cufon.js"></script><script type="text/javascript" src="/assets/script/belwe.font.js"></script>');

	// Front Page
	if($("body.home").length > 0){

	}

	// Single Post
	if($("body.single").length > 0){
		Cufon.replace('h2.entry-title');
		
		$(".post .entry-content p:first").magicDropcap();
		Cufon.replace('.post .entry-content p .dropcap');

		$('.entry-content').magicColumns({
			columnclass: 'column',
			append: '<hr />',
			columns: 2
		});
	}
});



/**
 * Magic Columns ( http://labs.clearspanmedia.com/jquery/magic-columns/)
 * A jQuery plugin for creating columns of text
 * 
 * Version 1.0
 * March 12th, 2009
 *
 * Copyright (c) 2009 Adrian Gonzales Jr. (http://www.clearspanmedia.com)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
 *
 **/ 
/**
 * 
 * @desc Replaces a block of text elements with as-even-as-possible columns
 * @author Adrian Gonzales Jr.
 * @version 1.0
 *
 * @name magicColumns
 * @param columnclass:String CSS class to apply to columns
 * @param prepend:String Element to prepend the columns with
 * @param append:String Element to append the columns with
 * @param exceptions:Array An array of tags that should not count as significant column elements
 * @param col:Int Number of columns
 * @type jQuery
 *
 * @example $('#story').magicColumns();
 * @desc Create text in two columns
 *
 * @example $('#story').magicColumns({append: '<hr />',	columns: 3});
 * @desc Create text in 3 columns and place an HR after to help with clearing
 *
**/
jQuery.fn.magicColumns = function(settings) {
    settings = jQuery.extend({
		columnclass: 'mcolumn',
		prepend: '',
		append: '',
		exceptions: ['h1','h2','h3','h4','h5'],
		columns: 2
    }, settings);

	var instanceCount = 0;

	// Support function for searching one dimensional array
	var findInArray = function(arr,srch){
		var returnVal = false;
		for(var i=0;i<arr.length;i++){
			if(arr[i] == srch){
				returnVal = true;
			}
		}
		return returnVal;
	};

	// Loop through matched items
	this.each(function(){
		var items = jQuery(this).find(" > *"); // Get All Child Items
		var countItems = jQuery(this).find(" > *:not("+settings.exceptions.join(",")+")"); // Get all child items that we want count
		var itemCount = 0; // Keep track of our items
		var colCount = 0; // Keep track of our columns
		var parent = jQuery(this); // Save parent for reference later
		var itemsPerColumn = Math.ceil(countItems.length / settings.columns);

		// Prepend optional element
		jQuery(this).prepend(settings.prepend);

		// Build columns
		for(var i=0;i<settings.columns;i++){
			jQuery(this).append('<div id="'+settings.columnclass+instanceCount+i+'" class="'+settings.columnclass+'"></div>');
		}
		// Set the last column's class as "last"
		jQuery(this).find('div.'+settings.columnclass+':last').addClass("last");

		// Move elements into columns
		items.each(function(i,n) {
			parent.find("#"+settings.columnclass + instanceCount + colCount).append(jQuery(this).clone()); // Clone item into column container
			jQuery(this).remove(); // Remove original item
	
			// If this isn't an exception element, count it towards the column count
			if(!findInArray(settings.exceptions, n.tagName.toLowerCase())){
				if(itemCount == (itemsPerColumn-1)){
					colCount++;
					itemCount = 0;
				} else {
					itemCount++;
				}
			}
		});
	
		// Append optional element
		jQuery(this).append(settings.append);
		
		instanceCount++;
	});
	
	return true;
};


/**
 * Magic Dropcap ( http://labs.clearspanmedia.com/jquery/magic-dropcap/)
 * A jQuery plugin for creating dropcaps
 * 
 * Version 1.0
 * March 12th, 2009
 *
 * Copyright (c) 2009 Adrian Gonzales Jr. (http://www.clearspanmedia.com)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
 *
 **/ 
/**
 * 
 * @desc Replaces a block of text elements with as-even-as-possible columns
 * @author Adrian Gonzales Jr.
 * @version 1.0
 *
 * @name magicColumns
 * @param dropclass:String CSS class to apply to the dropcap
 * @param recurse:Array Array of tags that should be looked into for text if its the first element in the target
 * @type jQuery
 *
 * @example $('#story').magicDropcap();
 * @desc Create simple dropcap
 *
 * @example $('#story').magicDropcap({recurse: ['a']});
 * @desc Create dropcap but only look to create dropcaps in <a> tags
 *
**/
jQuery.fn.magicDropcap = function(settings) {
    settings = jQuery.extend({
		dropclass: 'dropcap',
		recurse: ['a','span','div']
    }, settings);

	// Support function for searching one dimensional array
	var findInArray = function(arr,srch){
		var returnVal = false;
		for(var i=0;i<arr.length;i++){
			if(arr[i] == srch){
				returnVal = true;
			}
		}
		return returnVal;
	};

	// Support Function to test if character is alphanumeric
	var isalphanumeric = function(chr){
		var chrCode = chr.charCodeAt(0);
		if((chrCode>47&&chrCode<58) || (chrCode>64&&chrCode<91) || (chrCode>96&&chrCode<123)){
			return true;
		}
		return false;
	};

	// Loop through matched items
	this.each(function(){
		var paragraphText = jQuery(this).html(); // Store the html of the element
		var firstCharacter = paragraphText.substr(0,1); // Store the first character

		// If the first character is alphanumeric
		if(isalphanumeric(firstCharacter)){
			// Set the inner html as everything but the first letter
			jQuery(this).html(paragraphText.substr(1));
			// Add dropcap span
			jQuery(this).prepend('<span class="'+settings.dropclass+' '+firstCharacter.toLowerCase()+'">'+firstCharacter+'</span>');
		} else {
			// If the first character is an opening bracket
			// then we know this starts with a tag
			if(firstCharacter == "<"){
				// If this tag is one of the ones we want to still drop cap in, do it
				if(findInArray(settings.recurse, jQuery(this).find(":first")[0].tagName.toLowerCase())){
					var firstElement = jQuery(this).find(":first"); // Store the first element
					var elementText = firstElement.html(); // Store the first element's html
					firstCharacter = elementText.substr(0,1); // Store the first character
					// If this first character is alphanumeric
					if(isalphanumeric(firstCharacter)){
						// Set the inner html as everything but the first letter
						firstElement.html(elementText.substr(1));
						// Add dropcap span
						firstElement.prepend('<span class="'+settings.dropclass+' '+firstCharacter.toLowerCase()+'">'+firstCharacter+'</span>');
					}
				}
			}
		}
	});
	
	return true;
};