/**
 * @author Bryan
 */

/* plugin for autoclearing inputs when the user clicks on them */
$.fn.inputDefaultState = function(options){
	var options = jQuery.extend( {
		text: 'Search'
	},options);

	$(this)
		.val(options.text)
		.focus(function(e){
			if($(this).val() == options.text)
				$(this).val('');
		}).
		blur(function(e){
			if (this.value === '') {
                this.value = options.text;
            }
		});
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

if( typeof( WHITNEY_BLOG ) === 'undefined') {
		WHITNEY_BLOG = {};
	}
	
	WHITNEY_BLOG = {
		setup: function() {
			jQuery(document).ready( function() {
				Cufon.replace('.post h2');
				Cufon.replace('#content h1');
				$('#searchBox').inputDefaultState({text: 'Search Blog'});
				$('#emailBox').inputDefaultState({text: 'Sign up for Keds email updates.'});
				$('#emailBox2').inputDefaultState({text: 'Sign up for Whitney email updates.'});
				//***Start Post Image Pagination
				var postImagePagination = jQuery('.postImagePagination ul.pagination li a');
				var images = [];
				var imagesPriority = [];
				var spinner;
				//preload spinner
				jQuery.preloadImages('/whitney/wp-content/themes/keds_whitney/images/spinner.gif');

				//unhide all the hidden paginations
				jQuery('div.postImagePagination ul.pagination').css('display','block');
				
				jQuery('div.postImagePagination ul.images li img').each(function(){
					var imageObject ={};				
					var originalImageSource = jQuery(this).attr('src');
					var index = jQuery(this).closest('ul.images').children().index(jQuery(this).parent());
					
					imageObject['that'] = this;
					imageObject['originalImageSource'] = originalImageSource;
					
					if (index === 0) {
						imagesPriority.push(imageObject);
					} else {
						
						imageObject['originalImageSource'] = jQuery(this).attr('rel').match(/image:(.*)/)[1];
						images.push(imageObject);
					}
					
				});
				images = imagesPriority.concat(images);
				for(var i in images) {
					var stuff = function(images, i){
						var parent = jQuery(images[i]['that']).parent();
						var img = new Image();
						jQuery(images[i]['that']).after('<div class="loading"></div>').remove();
						jQuery(img).load(function(){
							jQuery(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
							parent.append(this);
							parent.children().eq(0).remove();
							jQuery(this).fadeIn();
						}).attr('src', images[i]['originalImageSource']);
					}(images, i);
				}
					
				//if a .pagination link is clicked, 
				//show the previous li with the same number as the current li inside of the current postImagePag 
				postImagePagination.click( function() {
					//select li children of the grandparent ul of the current clicked a href
					var currentPaginationList = jQuery(this).closest('ul').children();
					//index of current li 
					var index = currentPaginationList.index(jQuery(this).parent());
					//select the ul.images of the current .postImagePagination div
					var currentImageList = jQuery(this).closest('.postImagePagination').find('ul.images li');
					//set all images inactive
					currentImageList.removeClass('active').addClass('inactive');
					//set the image with the same index of the li to active
					currentImageList.eq(index).removeClass('inactive').addClass('active');
					//remove the activeLink class from all a href elements in the current pagination list
					jQuery(this).closest('ul').find('li a').removeClass('activeLink');
					//add activeLink class to the current clicked link
					jQuery(this).addClass('activeLink');
					return false;
				});
				
				
				//***End Post Image Pagination 
			});
		}()
	};
