var content;
var contentId;

$(document).ready(function () {
	
	commentList(1);
	
	$("#like-button").button({
		icons: {
			primary: 'ui-icon-like'
		}
	});
	
	$("#dislike-button").button({
		icons: {
			primary: 'ui-icon-dislike'
		},
		text: false
	});
	
	$("#like-dislike").progressbar({
		value: content.likes * 100 / (parseInt(content.likes) + parseInt(content.dislikes))
	});
	
	$("#share-button").button({
		icons: {
			primary: 'ui-icon-share'
		}
	});
	
	$("#comment-post-button").button();
	
	$("#description-box").live("click", function() {
		$("#detail-box").slideToggle("slow");
	});
	
	$("#like-button").live("click", function() {
		if(loggedIn) {
			like();
		} else {
			$(".control-box:not('#login-box')").slideUp("fast", function() {
				$("#login-box").slideDown("slow");
				$("#register-dialog, #login-dialog").unbind("logged");
				$("#register-dialog, #login-dialog").bind("logged", function() {
					like();
				});
			});
		}
	});
	
	$("#dislike-button").live("click", function() {
		if(loggedIn) {
			dislike();
		} else {
			$(".control-box:not('#login-box')").slideUp("fast", function() {
				$("#login-box").slideDown("slow");
				$("#register-dialog, #login-dialog").unbind("logged");
				$("#register-dialog, #login-dialog").bind("logged", function() {
					dislike();
				});
			});
		}
	});
	
	$("#share-button").live("click", function() {
		$(".control-box:not('#share-box')").slideUp("fast", function() {
			$("#share-box").slideDown("slow");
		});
	});
	
	$("#comment-input").focus(function() {
		if(loggedIn) {
			enableComment();
		} else {
			$(this).attr("readonly", true);
			$(".control-box:not('#login-box')").slideUp("fast", function() {
				$("#login-box").slideDown("slow");
				$("#register-dialog, #login-dialog").unbind("logged");
				$("#register-dialog, #login-dialog").bind("logged", function() {
					enableComment();
				});
			});
		}
	});
	
	$("#comment-post-button").live("click", function() {
		$(this).button("option", "disabled", true);
		var comment = $("#comment-input").val();
		$.ajax({
			cache: true,
			type: "POST",
			url: "../ajax/commentPost.php",
			data: "&post=" + contentId + "&comment=" + comment,
			success: function(html){
				if(html == "") {
					$("#comment-message").html("Comment sent.");
				} else {
					$("#comment-message").html(html);
					$("#comment-post-button").button("option", "disabled", false);
				}
			}
		});
	});
	
	$("#comment-cancel-button").live("click", function() {
		$("#comment-input").attr("rows", "4");
		$("#comment-input").val("");
		$("#comment-bar").hide();
	});
	
	$('div.viral_icon').find("img").fadeTo("slow", 0.5);
	
	$('div.viral_icon').find("img").hover(function() {
		if ($(this).is(':animated')) {
			$(this).stop().fadeTo("fast", 1);
		} else {
			$(this).fadeTo("fast", 1);
		}
		$(this).parent().parent().css("color", "#d91d1d");
	}, function () {
		if ($(this).is(':animated')) {
			$(this).stop().fadeTo("slow", 0.5);
		}
		else {
			$(this).fadeTo("slow", 0.5);
		}
		$(this).parent().parent().css("color", "#2d2d2d");
	});
	
	$("#playlists").live("change", function() {
		setPlaylist($(this).val());
	});
	
	function like() {
		$.ajax({
			cache: true,
			type: "POST",
			url: "../ajax/like.php",
			data: "&id=" + contentId,
			success: function(html){
				if(html == "") {
					$(".control-box:not('#liked-box')").slideUp("fast", function() {
						$("#liked-box").slideDown("slow");
					});
				} else {
					alert(html);
				}
			}
		});
	}
	
	function dislike() {
		$.ajax({
			cache: true,
			type: "POST",
			url: "../ajax/dislike.php",
			data: "&id=" + contentId,
			success: function(html){
				if(html == "") {
					$(".control-box:not('#disliked-box')").slideUp("fast", function() {
						$("#disliked-box").slideDown("slow");
					});
				} else {
					alert(html);
				}
			}
		});
	}
	
	function enableComment() {
		$("#comment-input").attr("rows", "8");
		$("#comment-input").val("");
		$("#comment-input").removeAttr("readonly");
		$("#comment-input").select();
		$("#comment-bar").show();
	}

});

function commentList(page) {
	$.ajax({
		cache: true,
		type: "POST",
		url: "../ajax/commentList.php",
		data: "&post=" + contentId + "&page=" + page,
		success: function(html){
			if(html != "") {
				$("#comment-list").html(html);
			} else {
				alert(html);
			}
		}
	});
}

function setPlaylist(name) {
	$.ajax({
		cache: true,
		type: "POST",
		url: "../ajax/playlist.php",
		data: "&name=" + name,
		success: function(result){
			//alert(result);
			$("#playlist").html(result);
		}
	});
}
