
/** Rate a video. */
function thumbsUp(video_id) {

	$.getJSON("/ratings/add/"+video_id, function(data) {

		if(data.status) {

			var ratings;
			if(data.new_num_ratings == 1) ratings = "1 person likes this video";
			else if(data.new_num_ratings > 1) ratings = data.new_num_ratings+" people like this video";
			$("#ratings").html(ratings);

			setRated();

		} else {

			// error happened
			// alert(data.msg)
		}
	});
}



/** Set "add rating" button to already-rated state. */
function setRated() {

	$("#btnAddRating").attr("disabled", true);
	$("#btnAddRating").html("Thank you!");
}


/** Add video as a favourite. */
function addFav(video_id) {

	$.getJSON("/favourites/add/"+video_id, function(data) {

		if(data.status) {
			$("#fav").text("Remove from favourites");
			$("#fav").unbind();
			$("#fav").bind("click", function() { removeFav(video_id); });
			$("#fav").effect("highlight", 1000);
		}
	});
}


/** Remove video from favourites. */
function removeFav(video_id) {

	$.getJSON("/favourites/remove/"+video_id, function(data) {

		if(data.status) {
			$("#fav").text("Add to favourites");
			$("#fav").unbind();
			$("#fav").bind("click", function() { addFav(video_id); });
			$("#fav").effect("highlight", 1000);
		}
	});
}


/** Take user to login page then come back here once they're logged in. */
function loginThenReturn() {

	top.location.href = "/login?go="+top.location.href;
}


/** Email video. */
function sendEmail(frm) {

	var inputs = [];
	$(":input", frm).each(function() { inputs.push(this.name + "=" + escape(this.value))});

	$("#send_email_status").removeClass();
	$("#send_email_status").html("Sending <img src='/img/clock-grey-16.gif' style='vertical-align: middle;' />");
	$("#send_email_status").show();

	$.postJSON(frm.action, inputs.join("&"), function(data) {

		if(data.status) {
			$(":input", frm).each(function() {
				if(this.type != "submit") this.value = "";
			});
			$("#send_email_status").removeClass();
			$("#send_email_status").addClass("ok");
			$("#send_email_status").text("Message sent!");
			$("#send_email_status").effect("highlight", null, 1500);
			setTimeout(function() { $("#send_email_status").fadeOut(); }, 3000);
		} else {
			$("#send_email_status").removeClass();
			$("#send_email_status").addClass("err");
			$("#send_email_status").text(data.msg);
		}
	});

	return false;
}


/** Print shopping list. */
function printShoppingList(video_id, video_sid) {

	var url = "/videos/shopping/"+video_id+"/"+video_sid;
	openWin(url, "shopping-list", 400, 460, 1);
}


/** Open a popup window. */
function openWin(win_url, win_name, win_width, win_height, win_scroll) {

	win_attrib='toolbar=no,location=no,directories=no,menubar=no,resizable,scrollbars=' + win_scroll + ',width=' + win_width + ',height=' + win_height + ',';
	win_left=(screen.width-win_width)/2;
	win_top = ((screen.height-win_height)/2)-50;
	if (navigator.appName.indexOf("Microsoft")>=0) {
		win_attrib+='left=' + win_left + ',top=' + win_top;
	} else {
		win_attrib+='screenX=' + win_left + ',screenY=' + win_top;
	}
	var newWin = window.open(win_url,win_name,win_attrib);
	newWin.focus();
}


// Check the "post to facebook" checkbox by default in the Intense Debates comments section:
function modifyCommentsPostToFacebook(attempt) {

	if($("#IDNewThreadFBShare-fb").length > 0) {

		$("#IDNewThreadFBShare-fb").attr("checked", true);

	} else if(attempt < 20) {

		setTimeout(function() { modifyCommentsPostToFacebook(attempt + 1); }, 500);
	}
}


// Change the order of the Intense Debates login buttons:
function modifyCommentsLogins(attempt) {

	if($("#IDCNavList li:first").length > 0) {

		// Move IntenseDebates login to end of list:
		var li = $("#IDCNavList li:first").remove();
		$("#IDCNavList").append(li);

		// Move Wordpress login to end of list:
		var li = $("#IDCNavList li:first").remove();
		$("#IDCNavList").append(li);

	} else if(attempt < 20) {

		setTimeout(function() { modifyCommentsLogins(attempt + 1); }, 500);
	}
}

// Intense Debates hook function for when comments are posted. This hook needs to
// be registered via some javascript on the Intense Debates website:
// http://intensedebate.com/pluginEditor/205954
function sendCommentNotifications(info) {

	var comment_div_id = "#IDComment" + info.comment_id;

	if($(comment_div_id).length < 1) return false;

	var user = info.comment_nonuser_name
	var comment = $(comment_div_id).find("div.idc-c-t-inner").html().trim();

	var url = "/videos/sendCommentNotifications/";
	$.postJSON(url, { vid_id: vid_id, vid_sid: vid_sid, user: user, comment: comment }, function(data) {

		//console.log(data);
	});
}


$(document).ready(function() {

	// Change some of the Intense Debates comments stuff around, once it's loaded:
	setTimeout("modifyCommentsPostToFacebook(1)", 1000);
	setTimeout("modifyCommentsLogins(1)", 1000);
});




/* Old comments code, before switching to Intense Debates

// Add a comment for a video.
function postComment(video_id, username) {

	//$("#add_comment").effect("blind");
	$("#add_comment").hide();

	var comment = $("#comment").val();
	var url = "/comments/add/";
	$.postJSON(url, { comment: comment, video_id: video_id }, function(data) {

		if(data.status) {
			showComments(video_id, data.comments, data.limit, data.page, data.last_page, true);
			$("#comment").val("");
		}
	});
}


// Load comments for this video.
function loadComments(video_id, limit, page) {

	var url = "/comments/getComments/"+video_id+"/limit:"+limit+"/page:"+page;
	$.getJSON(url, function(data) {

		if(data.status) {

			showComments(video_id, data.comments, data.limit, data.page, data.last_page, false);
		}
	});
}


// Display supplied comments.
function showComments(video_id, comments, limit, page, last_page, highlight) {

	if(comments.length > 0) {

		var html = "";
		for(var i=0; i<comments.length; i++) {
			var c = comments[i];
			html += "<div class='comment'";
			if(i==0) html += " id='new_comment'";
			html += ">";
			html += "<h3><a href='/"+c.User.username+"' title="+c.User.username+"&#039;s kitchen'>"+c.User.username+"</a></h3>\n";
			html += "<p>"+c.Comment.date_ago+"</p>\n";
			html += "<p>"+c.Comment.comment+"</p>";
			html += "</div>";
		}

		if(page > 1) {
			html += "<a href='javascript: void(null);' onclick='loadComments("+video_id+", "+limit+", "+(parseInt(page)-1)+");'>prev</a> ";
		}
		if(!last_page) {
			html += "<a href='javascript: void(null);' onclick='loadComments("+video_id+", "+limit+", "+(parseInt(page)+1)+");'>next</a> ";
		}

		$("#comments").html(html);

		if(highlight) $('#new_comment').effect("highlight", null, 1500);

	} else {

		$("#comments").text("There are no comments for this video yet.");
	}
}

*/


