var lightbox_options = {imageLoading: www_root + 'img/loading.gif'};

$(document).ready(function ()
{
	$('.view_detail').each(function ()
	{
		$(this).item({}).chain();
	});
	
	$('a.project_title').click(toggleProject);
	
	$('a.hide_project').click(hideProject);
	$('a.unhide_project').click(unhideProject);
	
	$('a.report_detail').click(reportDetail);
	
	// $('a.close').click(function ()
	// {
	// 	$(this).parent().fadeOut();
	// 	return false;
	// });
	
	$('form.comment_form').submit(postComment);
	$('a.show_all_comments').click(showComments);
	$('a.delete_comment').live('click', deleteComment);
	$('a.reply').live('click', replyToComment);
	$('a.cancel_reply').live('click', cancelReply);
	
	// View detail
	$('ul.details li').click(showDetail);
	
	$('#actions a.favorite').click(addFavorite);
	$('#actions a.watch, .detail_actions .watch').click(watchNomination);
	$('#actions a.vote, .detail_actions .vote').click(addVote);
});


/**
 * Event Handlers
 */
	function addFavorite(event)
	{
		var url = $(this).attr('href');
	
		$.getJSON(url, function(json)
		{
			show_flash(json.message);
		});
	
		return false;
	}

	function watchNomination(event)
	{
		var url = $(this).attr('href');
	
		$.getJSON(url, function (json)
		{
			show_flash(json.message);
		});
	
		return false;
	}
	
	function addVote()
	{
		var url = $(this).attr('href');
	
		$.getJSON(url, function (json)
		{
			show_flash(json.message);
		});
	
		return false;
	}
	
	
	function postComment(event)
	{
		event.pageX = $(this).offset().left + 100;
		event.pageY = $(this).offset().top + 50;
		var form = $(this);
		
		if (form.find('textarea').val() === '')
		{
			show_flash('Comment cannot be empty', 1000);
			return false;
		}
		
		confirm('submit this comment', event, function()
		{
			var action = form.attr('action');
			$.post(action, form.serialize(), function(json)
			{
				if (json.status == 'ok')
				{
					if (form.find('input.submit_btn').val() == 'Reply to Comment')
					{
						$(json.html).insertBefore(form.parent()).addClass('comment_reply').find('img.comment_image').attr({width: 38, height: 38}).end().show();
						form.clear().parent().remove();
					}
					else
					{
						form.clear();
						$(json.html).insertBefore(form).show();
					}
				}
				
				if (json.message !== '')
				{
					show_flash(json.message);	
				}
				
			}, 'json');
		});

		return false;
	}
	
	function replyToComment()
	{
		$(this).next('form').show().end().hide();
		return false;
	}
	
	function cancelReply()
	{
		var comment_form = $(this).parents('.comment_reply_form');
		comment_form.find('form').clear().hide();
		comment_form.find('a.reply').show();
		
		return false;
	}
	
	function deleteComment(event)
	{
		var comment = $(this);
		
		confirm('delete this comment', event, function ()
		{
			var url = comment.attr('href');
			$.getJSON(url, function (json)
			{
				if (json.status == 'ok')
				{
					comment.parents('.comment').eq(0).fadeOutAndRemove();
				}
				else
				{
					show_flash(json.message);
				}
			});
		});
		
		return false;
	}
	
	function showComments(event)
	{
		var comments = $('#comments .comment').not('.comment_reply');
		comments.fadeIn();
		var count = comments.length;
		$('p.comments_showing em').html('Showing ' + count + ' of ' + count);
		
		return false;
	}
	
	function hideProject(event)
	{
		var href = $(this).attr('href');
		confirm('hide this project', event, function ()
		{
			$.getJSON(href, function (json)
			{
				show_flash(json.message);
			});
		});

		return false;
	}
	
	function unhideProject(event)
	{
		var href = $(this).attr('href');
		confirm('unhide this project', event, function ()
		{
			$.getJSON(href, function (json)
			{
				show_flash(json.message);
			});
		});

		return false;
	}

	function reportDetail(event)
	{
		var item = $(this).parents('.view_detail').item();
		var href = $(this).attr('href') + item.id;
		
		confirm('report this detail as inappropriate', event, function ()
		{
			$.getJSON(href, function (json)
			{
				if (json.response == 'ok')
				{
					show_flash(json.message);
				}
				else
				{
					show_flash(json.message);
				}
			});
		});
		
		return false;
	}
	
	function toggleProject()
	{
		project = $(this).attr('href');
		
		if ($(project).hasClass('selected'))
		{
			hide_project(project);
		}
		else
		{
			show_project(project);
		}
		
		return false;
	}

/**
 * Helper functions
 */
	// Params - project: #project_id
	function show_project(project, detail_id, scroll)
	{
		scroll = (scroll === undefined) ? true : scroll;
	
		if ($(project).hasClass('selected'))
		{
			hide_project(project);
			return;
		}
	
		hide_projects();
	
		$(project).addClass('selected');
	
		$('ul.details li.selected').removeClass('selected');
	
		if (detail_id === undefined || detail_id === null)
		{
			detail = $('.details .detail', project).slice(0, 1).addClass('selected').attr('id');
			detail_id = detail.substr(7);
		}
	
		$('.view_detail', project).show();
		$('.view_detail .detail_content', project).animate({height: '414px', opacity: 'show'}, {duration: 1000, queue: false, easing: 'easeOutQuad', complete: function()
		{
			load_detail(project, detail_id, 1000);
			if (scroll === true)
			{
				$.scrollTo(project, 1500, {offset: {top: -10}, easing: 'easeOutCubic', onAfter: function(){
					$('.project_overview', project).show();
				}});
			}
			else
			{
				$('.project_overview', project).show();
			}
		}});
	}

	// Params - project: #project_id
	function hide_project(project)
	{
		$(project).removeClass('selected');
		$('ul.details li.selected', project).removeClass('selected');
		$('.project_overview', project).hide();
		$('.view_detail .detail_content', project).children().hide().html('');
		$('.view_detail .title, .view_detail .description, .view_detail .detail_actions', project).hide();
		$('.view_detail .detail_content', project).animate({height: 0, opacity: 'hide'}, {duration: 1000, queue: false, easing: 'easeOutQuad'});
	}

	function hide_projects()
	{
		$('.project.selected').each(function(){
			hide_project(this);
		});
	}

	// Detail click event
	function showDetail(event)
	{
		if (!$(this).hasClass('selected'))
		{
			project = '#' + $(this).parents('.project').attr('id');
		
			// Fade out current detail
			$('.view_detail .detail_content', project).children().animate({opacity: 'hide'}, {duration: 500, queue: false, easing: 'easeOutQuad'});

			$('ul.details li.selected').removeClass('selected');
			$(this).addClass('selected');
			detail_id = $(this).attr('id').substr(7);
		
			if (!$(project).hasClass('selected'))
			{
				show_project(project, detail_id);
			}
			else
			{
				load_detail(project, detail_id, 0);
			}
		}
	}

	// Load detail from database into current project
	// Params - project: #project_id, detail_id: id, delay: time in ms
	function load_detail(project, detail_id, delay)
	{	
		// Get JSON object of detail
		$.getJSON(www_root + 'details/get/' + detail_id, function (detail)
		{
			// Swap out detail info
			if (detail.type == 'image')
			{
				img = new Image();
				img.onload = function ()
				{
					var margin_top = (414 - img.height) / 2;
					var html = '<a href="' + www_root + detail.data + '" title="' + detail.title + '" style="display:none">';
					html += '<img src="' + detail.large + '" alt="' + detail.title + '" width="' + img.width + '" height="' + img.height + '" style="margin-top:' + margin_top + 'px;" /></a>';

					$('.view_detail .detail_content', project).html(html);
					$('.view_detail .detail_content a', project).lightbox();

					fadein_detail(project, detail, delay);
				};
			
				img.src = detail.large;
			}
			else if (detail.type == 'video')
			{
				if (detail.info == 'youtube')
				{
					$('.view_detail .detail_content', project).html(embed_youtube(detail.data));
				}
				else
				{
					flashvars = {file: detail.data, type: 'video', width: '633', height: '414'};
					params = {wmode: 'transparent', allowscriptaccess: 'always'};
					$('.view_detail .detail_content', project).html('<div class="detail_wrapper" style="display:none"><div id="video_player"></div></div>');
					swfobject.embedSWF(www_root + 'swf/player.swf', 'video_player', '633', '414', '9.0.0', false, flashvars, params);
				}
			
				fadein_detail(project, detail, delay);
			}
			else if (detail.type == 'audio')
			{
				flashvars = {file: detail.data, type: 'audio', width: '633', height: '414'};
				params = {wmode: 'transparent', allowscriptaccess: 'always'};
				$('.view_detail .detail_content', project).html('<div class="detail_wrapper" style="display:none"><div id="audio_player"></div></div>');
				swfobject.embedSWF(www_root + 'swf/player.swf', 'audio_player', '633', '414', '9.0.0', false, flashvars, params);
			
				fadein_detail(project, detail, delay);
			}
			else if (detail.type == 'text')
			{
				if (detail.path == 1)
				{
					$('.view_detail .detail_content', project).html('<a href="' + detail.data + '" style="display:none"><img src="' + detail.large + '"  width="633" height="414" alt="' + detail.title + '" /></a>');
				}
				else
				{
					$('.view_detail .detail_content', project).html('<div class="text_detail"><pre>' + detail.data + '</pre></div>');
				}
			
				fadein_detail(project, detail, delay);
			}
		});
	}

	function fadein_detail(project, detail, delay)
	{
		$('input.detail_id').val(detail.id);
	
		// Fade in detail
		setTimeout(function ()
		{
			$('.view_detail', project).item(detail);
			$('.view_detail .title, .view_detail .description, .view_detail .detail_actions', project).show();
			$('.view_detail .detail_content', project).animate({opacity: 'show'}, {duration: 1000, queue: false, easing: 'easeOutQuad'});
			$('.view_detail .detail_content', project).children().animate({opacity: 'show'}, {duration: 1000, queue: false, easing: 'easeOutQuad'});
		}, delay);
	}