function WorksObject(selector) {
	var that = {
		e: $(selector),
		list: null,
		pagination: $('#works ul:last-child li'),
		id: function(i) {
			return(that.list.eq(i).attr('id').replace('work-', ''));
		},
		setPage: function(page) {
			if(page === 0) return;
			that.e.css('marginLeft', -960*(page-1));
		}
	};
	that.list = that.e.children('li');
	
	return that;
}
function UrlObject() {
	var that = {
		page: 1,
		work: 0,
		initialize: function() {
			var anchor = self.document.location.hash.match(/page:([0-9]+),work:([0-9]+)/);
			if(anchor) {
				that.page = parseInt(anchor[1], 10);
				that.work = parseInt(anchor[2], 10);
			}
		},
		update: function() {
			self.document.location.hash = 'page:'+that.page+',work:'+that.work;
		},
		setPage: function(id) {
			that.page = id;
			that.update();
		},
		setWork: function(id) {
			that.work = id;
			that.update();
		}
	};
	that.initialize();
	
	return that;
}
function ModalObject(selector) {
	var that = {
		e: $(selector),
		state: -1,
		show: function() {
			that.e.css('display', 'block').animate({'opacity': 1}, 400);
			that.state = 1;
		},
		hide: function() {
			that.e.animate({'opacity': 0}, 400, function() { that.e.css('display', 'none'); });
			that.state = -1;
		},
		load: function(id) {
			if(id === 0) return;
			that.state = 0;
			
			workUrl = $('#work-'+id+' a.image').attr('href')+'?returnModal';
			
			$.get(workUrl, function(data) {
				that.e.html(data);
				var pagination = that.e.find('.gallery ul:first-child');
				var images = that.e.find('.gallery ul:last-child');
				
				pagination.children('li').bind('click', function() {
					var this0 = $(this);
					var children = images.find('li');
					var n = parseInt(this0.html(), 10)-1;
					var move = 0;
					for(var i = 0; i < n; i++) {
						move += children.eq(i).height();
					}
					pagination.children('li.current').removeClass('current');
					this0.addClass('current');
					images.css('marginTop', 0 ? 0 : -1*move);
				});
				
				that.show();
			}, 'html');
		}
	};
	
	return that;
}
function watcher(obj, prop, value, callback) {
	setTimeout(function() {
		if(obj[prop] !== value) {
			watcher(obj, prop, value, callback);
		}
		else {
			callback();
		}
	}, 10);
}

$(document).ready(function() {
	var works = WorksObject('#works ul:first-child');
	var url = UrlObject();
	var modal = ModalObject('#modal');
	var m = Math;
	var worksListLength = (works.list.length)*320-($('#worksList li:nth-child(even)').length*120)+160;
	
	works.list.each(function(i, e) {
		var this0 = $(e);
		var tmpMargin = m.floor(m.random()*31);
		worksListLength -= tmpMargin;
		this0.animate({
			'margin-top': (m.floor(m.random()*2) ? '-=' : '+=') + m.floor(m.random()*41),
			'margin-left': '-=' + tmpMargin
		}, 300);
	});
	
	works.e.find('.image').each(function(i, e) {
		var this0 = $(e);
		this0.bind('click', function(event) {
			if(modal.state < 0) {
				var id = works.id(i);
				if(id !== url.work) {
					loader = this0.find('span').addClass('loading');
					watcher(modal, 'state', 1, function() { loader.removeClass('loading'); });
					modal.load(id);
				}
				else {
					modal.show();
				}
				url.setWork(id);
			}
			event.preventDefault();
		});
	});
	
	$('#modal > span').live('click', function() {
		modal.hide();
		url.setWork(0);
	});
	
	works.pagination.each(function(i, e) {
		var this0 = $(e);
		this0.bind('click', function() {
			works.pagination.filter('.current').removeClass('current');
			this0.addClass('current');
			var page = parseInt(this0.html(), 10);
			works.setPage(page);
			url.setPage(page);
		});
	});
	
	works.pagination.eq(url.page-1).trigger('click');
	modal.load(url.work);
});

