var windowWidth = 0;
var menuWidth = 0;
var c = 0;
var minC = 0;

var boxWidth = 300;
var boxHeight = 200;
var gutter = 5;

var textBoxHeight = 0;
var newTextBoxHeight = 0;
var paddingTop = 0;
var paddingBottom = 0;
var padding = 0;

var thumbLandscape = [300, 200];
var thumbPortrait = [300, 405];

jQuery(document).ready(function($) {
	
var shrinkImage = function()
{
	//TARGET THE THUMBNAIL SIZE
	if($(this).hasClass("landscape")) {
		targetW = thumbLandscape[0];
		targetH = thumbLandscape[1];
		animateImage($(this).find('img'), targetW, targetH);	
	} else if($(this).hasClass("portrait")) {
		targetW = thumbPortrait[0];
		targetH = thumbPortrait[1];
		animateImage($(this).find('img'), targetW, targetH);
	}
	$(this).unbind('click', shrinkImage);
	$(this).bind('click', expandImage);
	return false;
}

var expandImage = function()
{
	//TARGET THE ORIGINAL SIZE
	targetW = document.getElementById($(this).find('img').attr("id")).naturalWidth;
	targetH = document.getElementById($(this).find('img').attr("id")).naturalHeight;
	
	animateImage($(this).find('img'), targetW, targetH);
	
	$(this).unbind('click', expandImage);
	$(this).bind('click', shrinkImage);
	
	return false;
}

function animateImage(element, w, h)
{
	element.animate({
		width: w + "px",
		height: h + "px"
	}, 500 );
}

var loadImage = function(event)
{

	//FIND OUT WHICH IMAGE TO LOAD, ADD LOADING GIF
	var aTag = $(this);
	var mainDiv = $(this).parent();
	var imageSrc = mainDiv.find('a').attr("href");
	if(imageSrc){
	var imageID = mainDiv.find('img').attr("id");
	mainDiv.find('.loader').addClass("loading");

	//CREATE AND LOAD IMAGE
	var img = new Image();
    $(img).load(function () {
	
		//REMOVE THE LOADING
		mainDiv.find('.loader').removeClass("loading");
		mainDiv.find('img').remove();
        mainDiv.append(img);
		mainDiv.find('img').attr("id", imageID);
		
		//FIND OUT HOW BIG THE IMAGE AND ORIENTATION
		targetW = $(img).width();
		targetH = $(img).height();
		
		if(targetW  >= targetH ) {
			mainDiv.addClass("landscape");
			$(img).css('width', thumbLandscape[0] + 'px');
			$(img).css('height', thumbLandscape[1] + 'px');
			if(targetW >= 605 && targetH >= 405) { mainDiv.addClass("resize"); }
		} else {
			mainDiv.addClass("portrait");
			$(img).css('width', thumbPortrait[0] + 'px');
			$(img).css('height', thumbPortrait[1] + 'px');
			if(targetW >= 605 && targetH >= 815) { mainDiv.addClass("resize"); }
		}

		/* Animate it to large */
		animateImage($(img), targetW, targetH);
		
		aTag.unbind('click', loadImage);
		mainDiv.bind('click', shrinkImage);	
		
    }).error(function () {
		//IMAGE HAS NOT LOADED
    }).attr('src', imageSrc);

	} else {
		aTag.unbind('click', loadImage);
	}

	return false;
}

// Begin Expand Function
$(".entry-image a").bind('click', loadImage);

	// Trigger Infinite Scroll
	$('#nav-below').mouseover(function() {
		$(document).trigger('retrieve.infscr');
	});
	
	////////////////////////////////////
	
	// Begin Align Icons
	function alignToGrid() {
		// For navicons
		windowWidth = $(window).width();
		// How many columns does it takes	
		c = (windowWidth + gutter)/(boxWidth + gutter);
		// Round to the grid
		c = Math.floor(c);
		// Set the minimum
		minC = 1;
		if(c <= minC) {
			menuWidth = minC*(boxWidth+gutter)-gutter;
		} else {
			menuWidth = (c-1)*(boxWidth+gutter)-gutter;
		}

		$('#menu').css('width', menuWidth + 'px');
	}
	
	alignToGrid();
	$('#menu').css('display', 'block');

	$(window).resize(function(){
		alignToGrid();
	});
	// End Align Icons
	
	////////////////////////////////////
	
	// Correct font size for twitter timestamp
	$('ul#twitter_update_list').find('li a').css('font-size', 100 + '%');
	
	// For each post thumbnail
	$('.insidebox, .blackbox').each(function(){
		$(this).css('opacity', 0);
		$(this).css('display', 'block');
	});

	$('.postbox').hover(function(){
		$(this).children('.insidebox').stop().fadeTo(200, 1);
		$(this).children('.blackbox').stop().fadeTo(200, 0.75);
	},function(){
		$(this).children('.insidebox, .blackbox').stop().fadeTo(200, 0);
	});
	
	// For each image caption
	$('.entry-image-caption').each(function(){
		$(this).css('opacity', 0);
		$(this).css('display', 'block');
	});
	
	$('.entry-image').hover(function(){
		$(this).children('.entry-image-caption').stop().fadeTo(200, 0.75);
	},function(){
		$(this).children('.entry-image-caption').stop().fadeTo(200, 0);
	});
	
	////////////////////////////////////
	
	// Begin Search Bar
	$('input[type="text"]').addClass("idleField");
	$('input[type="text"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	    if ($.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	// End Search Bar
	
	////////////////////////////////////
	
	// Begin Text Box
	$.each($('.entry-content-single'), function() {
		textBoxHeight = $(this).height();	
		paddingTop = parseInt($(this).css("padding-top"));
		paddingBottom = parseInt($(this).css("padding-bottom"));
		padding = paddingTop + paddingBottom;
		
		if( textBoxHeight >= boxHeight - padding) {
			var i = 1;
			while( textBoxHeight >= ((boxHeight + gutter) * (i - 1))  - gutter - padding ) {
				newTextBoxHeight = ((i * (boxHeight + gutter)) - gutter - padding);
				i++;
			}
		} else {
			newTextBoxHeight = boxHeight - padding;
		}
			
		$(this).css("height", newTextBoxHeight + "px");
	})
	// End Text Box
		
});
