"use strict";

/*globals $, jQuery, window, document */

(function () {
	var wrapper, overlay, thumbs;

	function setMeta(index) {
		var ref = thumbs.parent().parent().eq(index);
		
		wrapper
			.find('.meta')
				.find('h3')
					.text(ref.find('h3').text())
				.end()
				.find('p')
					.text(ref.find('p').text())	
				.end()
			.end();						
	}

	function bindClicks() {
		overlay
			.find('.close')
				.click(function () {
/*					overlay

						.find('.meta')
							.find('h3')
								.text('')
							.end()
							.find('p')
								.text('')	
							.end()
						.end()
*/					
					overlay
						.fadeOut(function () {
							$(this)
								.removeAttr('style')
								.find('.image_canvas')
									.html('')
									.removeAttr('style')
								.end();							
						});	
				});
				
		thumbs.each(function (i) {
			var index = i;
			$(this).click(function () {
				var temp = $(this).attr('src');
				temp = temp.replace(/thumbs/gi, 'big');
				
				overlay.fadeIn(function () {
					overlay
						.find('.meta')
							.find('h3')
								.text('')
							.end()
							.find('p')
								.text('')	
							.end()
						.end()		
						.find('.image_canvas')
							.html($('<img src="' + temp + '" />')
										.bind('load', function () { 
											var img = $(this);
											overlay		
												.css('width', 'auto')					
												.find('.image_canvas').animate({
													width: img.width(),
													height: img.height()
												}, function () {
													overlay.css('width', img.width());
													//setMeta(index);	
													img.fadeIn();											
											});
										}));
				});	
				
				return false;
			});	
		});				
	}

	$(document).ready(function () {
		wrapper = $('.photos_content');
		overlay = wrapper.find('.overlay');
		thumbs = wrapper.find('li a img');
		
		bindClicks();
	});
}());