/************************************************/
/*	Senzafine: the Yahzee Skellington Blog			*/
/*	jQuery Functionality File							*/
/*	Written by Yahzee Skellington						*/
/*																*/
/*	Code by Senzafine										*/
/*	Copyright (C) 2011 SenzafineMedia				*/
/************************************************/

// Load functions after the page is loaded
jQuery(document).ready(function() {

	// Animate the logo, navigation, and sharing links
	jQuery('h1, nav a, ul#social-links a, aside#meta li, aside.meta li').css({opacity: 0.5});
	jQuery('h1, nav a, ul#social-links a, aside#meta li, aside.meta li').hover( function(){
		jQuery(this).animate({opacity: 1});
	}, function() {
		jQuery(this).animate({opacity: 0.5});
	});

	// Animate the article links
	jQuery('h3').hover( function(){
		jQuery(this).animate({color: 'rgb(255, 255, 225)'});
	}, function() {
		jQuery(this).animate({color: 'rgb(250, 250, 250)'});
	});

	// Animate the submit button
	jQuery('input#submit').hover( function(){
		jQuery(this).animate({opacity: 1, backgroundPosition: '0px -40px'});
	}, function() {
		jQuery(this).animate({opacity: 0.5, backgroundPosition: '0px 0px'});
	});

	jQuery('input.text, textarea.text').each(function() {
		var defaultValue = this.value;
		jQuery(this).css({opacity: '0.5'}, 'slow');
		jQuery(this).focus(function() {
			if(this.value == defaultValue) {
				this.value = '';
			}
			jQuery(this).animate({opacity: '1'}, 'slow');
		});
		jQuery(this).blur(function() {
			if(this.value == '') {
				this.value = defaultValue;
			}
			jQuery(this).animate({opacity: '0.5'}, 'slow');
		});
	});

	// Call the lightbox function
	lightbox();

});

// Setup the Senzafine Lightbox function
function lightbox() {
	// Get all links with the class 'thumb'
	var links = $('a[class^=thumb]');
	// Declare the overlay window for the lighbox
	var overlay = $(jQuery('<div id="overlay" style="display: none"></div>'));
	// Declare the div holding the requested link
	var container = $(jQuery('<div id="lightbox" style="display: none"></div>'));
	// Declare the close button
	var close = $(jQuery('<a href="#close" class="close">&times; Close</a>'));
	// Declare the target div
	var target = $(jQuery('<div class="target"></div>'));

	// Append the overlay section and container div to the body
	$('body').append(overlay).append(container);
	// Append the close button and target div to the container div
	container.append(close).append(target);

	// Show the container div, calculating first the size of the window to display the content in the middle
	container.show().animate({
		'top': Math.round((($(window).height()>window.innerHeight?window.innerHeight: $(window).height())-container.outerHeight())/2)+'px',
		'left': Math.round(($(window).width()-container.outerWidth())/2)+'px',
		'margin-top': 0,
		'margin-left': 0
	}).hide();

	// Add an action to the close button
	close.click(function(c) {
		c.preventDefault();
		overlay.add(container).fadeOut('slow', 'easeOutSine');
	});

	// Add an action to the overlay div to function the same as the close button
	overlay.click(function(c) {
		c.preventDefault();
		overlay.add(container).fadeOut('slow', 'easeOutSine');
	});

	// Add a click function to each link variable
	links.each(function(index) {
		var link = $(this);
		link.click(function(c) {
			c.preventDefault();
			open(link.attr('href'));
			links.filter('.selected').removeClass('selected');
			link.addClass('selected');
		});
	});

	// Check whether the container div is visible to show it or hide it
	var open = function(url) {
		if (container.is(':visible')) {
			target.children().fadeOut('normal',function() {
				target.children().remove();
				loadImage(url);
			});
		} else {
			target.children().remove();
			overlay.add(container).fadeIn('normal',function() {
				loadImage(url);
			});
		}
	}

	// Load the requested link into the container div
	var loadImage = function(url) {
		if (container.is('.loading')) {
			return;
		}

		container.addClass('loading');

		var img = new Image();

		img.onload=function() {
			img.style.display = 'none';
			var maxWidth = ($(window).width()-parseInt(container.css('padding-left'),10)-parseInt(container.css('padding-right'),10))-100;
			var maxHeight = (($(window).height()>window.innerHeight?window.innerHeight: $(window).height())-parseInt(container.css('padding-top'),10)-parseInt(container.css('padding-bottom'),10))-100;
			if (img.width>maxWidth||img.height>maxHeight) {
				var ratio = img.width/img.height;
				if (img.height>=maxHeight) {
					img.height = maxHeight;
					img.width = maxHeight*ratio;
				} else {
					img.width = maxWidth;
					img.height = maxWidth*ratio;
				}
			}
			container.animate({
				'width': img.width,
				'height': img.height,
				'top': Math.round((($(window).height()>window.innerHeight?window.innerHeight: $(window).height())-img.height-parseInt(container.css('padding-top'),10)-parseInt(container.css('padding-bottom'),10))/2)+'px',
				'left': Math.round(($(window).width()-img.width-parseInt(container.css('padding-left'),10)-parseInt(container.css('padding-right'),10))/2)+'px'
				}, 'slow', function() {
				target.append(img);
				$(img).fadeIn('slow', function() {
					container.removeClass('loading');
				});
			}
		)}
		img.src=url;
	}
}
