// make sure jquery doesn't steal $ namespace from other libraries
jQuery.noConflict();

/**************************************
» Jonas Raoni Soares Silva
» http://www.joninhas.ath.cx
**************************************/
String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
};

// THE CORNER CLOCK
jQuery(document).ready(function() {
	
	// set a timer function
	jQuery('#clock').everyTime(1000,function() {
		var currentTime = new Date();
		var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		var month = ''+months[currentTime.getMonth()];
		var day = ''+currentTime.getDate();
		var hour = ''+(currentTime.getHours() % 12 ? currentTime.getHours() % 12 : 12);
		var minute = ''+currentTime.getMinutes();
		minute = (minute.length == 1 ? '0'+minute : minute);
		var ampm = (currentTime.getHours() < 12 ? 'am' : 'pm');
		//var ampm = currentTime.getSeconds();
		jQuery(this).find(".month").html(month);
		jQuery(this).find(".day").html(day);
		jQuery(this).find(".hour").html(hour);
		jQuery(this).find(".minute").html(minute);
		jQuery(this).find(".ampm").html(ampm);
		var colon_vis = jQuery(this).find(".colon").css("visibility");
		if (colon_vis == "visible") {
			jQuery(this).find(".colon").css({visibility:'hidden'});
		} else {
			jQuery(this).find(".colon").css({visibility:'visible'});
		}
	});
	
});

// THE HOMEPAGE MESSAGE
jQuery(document).ready(function() {
	
	// hide all
	jQuery('#messages .message').css({opacity:'0'});
	jQuery('#messages .message:first').addClass('current').fadeTo('slow',0.99,function() {
		jQuery('#messages').everyTime(6000,function() {
			var a = jQuery(this).find('.current');
			var b = (jQuery(a).is(':last-child') ? jQuery(a).siblings(':first') : jQuery(a).next());
			a.removeClass('current');
			b.addClass('current');
			crossFade(a,b);
		})
	});
	
});

// THE PAGE TABLE OF CONTENTS
jQuery(document).ready(function() {
	
	var page_toc, toc_elements;
	
	page_toc = jQuery('#pagetoc ul').empty();
	
	// find and copy all the headers to transform
	toc_elements = jQuery(':header', jQuery('.page .entry-content'));
	
	// loop through the new elements, transorming each
	toc_elements.each(function(){
		
		var header_element, toc_element;
		
		header_element = jQuery(this);
		toc_element = header_element.find('a').clone();
		
		// move the id to the link href attribute
		toc_element.attr('href', '#'+toc_element.attr('id')).removeAttr('id').removeAttr('name');
		
		// change the anchor text
		toc_element.text(header_element.text());
		
		// wrap in 'li', indent
		toc_element = toc_element.wrap('<li></li>').parent();

		// add indentation
		if (jQuery(header_element).is('h4'))
			jQuery(toc_element).addClass('indent');
			
		// append new element to the list
		page_toc.append(toc_element);
		
	});

});

// POST GALLERIES + LIGHTBOX
jQuery(document).ready(function() {
	jQuery('.gallery-icon a').lightbox();
});
	

// SUPPORTING FUNCTIONS
function crossFade(a, b) {
	// fade out
	jQuery(a).fadeTo(2000,0.00);
	// fade in
	jQuery(b).fadeTo(2000,0.99);
}

