var Expressiv = {

	thumbsLoaded: [],
	zoomsLoaded: [],

	init: function() {
		this.imageLoader();
		this.project();
		this.projects();
		this.contact();
		this.imageFancyBox();
	},

	imageFancyBox: function() {
		$('a.fancybox').fancybox({
			overlayColor: '#000',
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			autoScale: false
		});
	},

	imageLoader: function() {
		var $thumbs = $('a.thumb img');

		$thumbs.each(function() {
			var src = $(this).attr('src');

			if ($.inArray(src, Expressiv.thumbsLoaded) == -1) {
				Expressiv.thumbsLoaded.push(src);

				$(this).hide();
				$(this).attr('src', '');

				var img = new Image();
				var $parent = $(this).parent();

				$(this).remove();

				$(img).load(function() {
					$(this).hide();
					$parent.append(this);
					$(this).fadeIn();
				})
				.attr('src', src);
			}
		});
	},

	project: function(a) {
		var $project = $('#content.project');

		if ($project.length > 0) {
			var $photo  = $project.find('.photo');
			var $zoom   = $photo.find('img');
			var $thumbs = $project.find('a.thumb');

			if (a == undefined || a == false) {
				$zoom.hide();
				this.projectLoadZoom($zoom.attr('src'), $photo);
			}

			this.handlePagination();

			if (a == undefined || a == false) {
				$zoom.remove();
			}

			$thumbs.click(function() {
				if ($photo.find('img').attr('src') != $(this).attr('href') || $.inArray(src, Expressiv.zoomsLoaded) == -1) {
					Expressiv.projectLoadZoom($(this).attr('href'), $photo);
				}

				return false;
			});
		}
	},

	handlePagination: function() {
		var $pages = $('#content.project a.page');

		if ($pages.length > 0) {
			var $thumbs = $('#content.project .sidebar ul');

			this.fixPagination($pages);

			$pages.live('click', function() {
				$.ajax({
					url: $(this).attr('href'),
					dataType: 'html',
					success: function(x) {
						$thumbs.html(x);
						Expressiv.imageLoader();
						Expressiv.project(true);
					}
				});

				return false;
			});
		}
	},

	fixPagination: function(p) {
		p.each(function() {
			$(this).attr('href', $(this).attr('href') + '&action=thumbs');
		});
	},

	projectLoadZoom: function(src, el) {
		Expressiv.zoomsLoaded.push(src);

		el.addClass('loading');
		el.find('a.fancybox').fadeOut().remove();

		var img = new Image();

		$(img).load(function() {
			var $i = $(this);

			$i.hide();
			el.append(this);
			el.animate({height: $i.height() + 'px'}, 500, function() {
				$(this).removeClass('loading');
				$i.fadeIn();
				el.find('img').wrap('<a href="' + src.replace('639x900', '1024x768') + '" class="fancybox"></a>');
				Expressiv.imageFancyBox();
			});
		})
		.attr('src', src);
	},

	projects: function() {
		var $projects = $('#content.projects');

		if ($projects.length > 0) {
			var positions = [
				[1, 1],   [161, 1],   [321, 1],   [481, 1],   [641, 1],   [801, 1],
				          [161, 161], [321, 161], [481, 161], [641, 161],
				[1, 321], [161, 321], [321, 321], [481, 321], [641, 321], [801, 321]
			];

			$projects.find('li').each(function() {
				var position = Math.floor(Math.random() * positions.length);
				var xy       = positions[position];


				$(this).css('top', xy[1] + 'px');
				$(this).css('left', xy[0] + 'px');

				if (position >= 0 && position < positions.length) {
					positions.splice(position, 1);
				}
				else {
					positions.splice(position - 1, 1);
				}
			});

			$projects.find('li').hover(
				// mouse in
				function() {
					$(this).find('a').append(
						'<span class="tooltip">' +
							'<span class="title">' + $(this).data('title') + '</span>' +
							'<span class="location">' + $(this).data('location') + '</span>'+
						'</span>');
				},
				// mouse out
				function() {
					var li = $(this);

					if ($.browser.msie && parseInt($.browser.version, 10) < 9) {
						li.find('.tooltip').remove();
					}
					else {
						li.find('.tooltip').fadeOut('fast', function() {
							$(this).remove();
						});
					}
				}
			);
		}
	},

	contact: function() {
		var $contact = $('#content.contact');

		if ($contact.length > 0) {
			var $form = $contact.find('form');

			$form.find('li input, li textarea').focus(function() {
				$(this).parent().find('.error').fadeOut();
			});
		}
	}

};

$(document).ready(function() {
	Expressiv.init();
});

