$(function() {
	if (!$.browser.msie) {
		$('a#screenshots-button').corners('14px bottom');
	}
	
	var screenshots = $('div#screenshots');
//	var height = 364;
	var height = 371;
	$('a#screenshots-button').click(function() {
		$(this).blur();

		if (screenshots.data('isAnimatingUp') || screenshots.data('isAnimatingDown')) {
			return false;
		}
	
		if (screenshots.is(':visible')) {
			screenshots.data('isAnimatingUp', true);
			$('a#screenshots-button').text('show me more');
			screenshots.animate({
				height: 0
			}, 500, function() {
				screenshots.hide();
				screenshots.data('isAnimatingUp', false);
			});
//			screenshots.slideUp(500);				
		} else {
			screenshots.data('isAnimatingDown', true);
			$('a#screenshots-button').text('show me less');
			screenshots.show().animate({
				height: height
			}, 500, function() {
				screenshots.data('isAnimatingDown', false);
			});
//			screenshots.slideDown(500);
		}

		return false;
	});
	
	$('div#screenshots div.preview a').click(function() {
		var anchor = $(this).attr('href');
		var vimeo_id = anchor.substr('http://vimeo.com/'.length);
	
		var url = 'http://vimeo.com/moogaloop.swf?clip_id=';
		url += vimeo_id;
		url += '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1';
	
		var obj = $('<object width="640" height="400""></object>');
		$('<param name="allowfullscreen" value="true" />').appendTo(obj);
		$('<param name="allowscriptaccess" value="always" />').appendTo(obj);
		$('<param name="movie" />').attr('value', url).appendTo(obj);
		$('<embed type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="400" width="640"></embed>').attr('src', url).appendTo(obj);
		
		Shadowbox.open({
			content: url
			,player: 'swf'
			,title: $('img', this).attr('alt')
			
			,height: 400
			,width: 640
		});
		return false;
	});
	
	$('div#news ol li').hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});
	
	$('div#users div.user a').click(function() {
		return true;
		var url = $(this).attr('href') + '?partial=1';
		$.get(url, function(html) {
			Shadowbox.open({
				content: '<div id="users-lightbox">' + html + '</div>'
				,player: 'html'
				,title: $(this).parent().find('a.name').text()
				
				,height: 720
				,width: 960
			});
		});
		return false;
	});
	
	var controller = new MainController();
	controller.render();
	CornersMain.apply();
});

var CornersMain = {
	apply: function() {
		$('div#users div.user').corners('15px');
		$('div#users-start').corners('15px right');
		$('div#users-end').corners('15px left');
	}
};

var MainController = function() {
};

MainController.prototype = {
	_toggleScreenshotsTime: 4000,
	
	_toggleScreenshots: function() {
		var active = $('div#screenshots2 img.active');
		var self = this;
		active.fadeOut(500, function() {
			active.removeClass('active');
			var next = active.next();
			if (active.next().size() == 0) next = $('div#screenshots2 img:first');
			next.addClass('active').hide().fadeIn(500, function() {
				setTimeout(function() {
					self._toggleScreenshots();
				}, self._toggleScreenshotsTime);
			});
		});
	},

	attachEvents: function() {
		this.attachUserEvents();
	},
	
	attachUserEvents: function() {
		var self = this;
		$('div#users div#users-start a, div#users div#users-end a').click(function() {
			var url = $(this).attr('href').replace('main/index', 'eaters/featured');
			$.get(url, function(html) {
				$('div#users').replaceWith(html);
				self.attachUserEvents();
				CornersMain.apply();
			});
			return false;
		});
		setTimeout(function() {
			self._toggleScreenshots();
		}, this._toggleScreenshotsTime);
	},
	
	render: function() {
		this.attachEvents();
	}
};

