var activePanel = "share_panel";
var activeTab = "share";

var cmtPanel = "cmts_panel";
var cmtTab = "cmts";

function loadComments(cid) {
	$.get("/rpc.php?act=loadComments&content_id=" + cid, function(data) {
		$("#cmts_body").html(data);
	});
}


$(document).ready(function() {
	var max_width = 525; 	//Sets the max width, in pixels, for every image

	$("img.contentimg").each(function(){
		var width = $(this).width();
		var height = $(this).height();

		if (width > max_width) {
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
			
			$(this).height(new_height).width(new_width);
		}
	}
	);
	
	$('.taglink').click(function() {
		$("#addTagForm").toggle();
//		$("#catvoterhide").toggle();
				
		return false;
	});
	
	$("#s2fLink").click(function() {
		$("#shareMore").hide();
		$("#s2f").show();
		$("#shareMoreLink").text("more...");
		
		return false;
	});
	
	
	$("#s2fForm").ajaxForm({
		dataType: 'script'
	});
	
	$("#shareMoreLink").click(function() {
		$("#s2f").hide();
		
		if ($("#shareMore").css("display") == "none") {
			$(this).text("hide...");
		} else {
			$(this).text("more...");
		}
		
		$("#shareMore").toggle();

		return false;
	});
	
	$(".shareCancel").click(function() {
		$("#shareMore").hide();
		$("#s2f").hide();
		
		return false;	
	});
	
	$("#addCommentForm").ajaxForm({
		dataType: 'script'
	});
	
	$(".cmtCancel").click(function() {
		$("#addCommentForm").resetForm();
		$("#addCommentFeedBack").html('');
		$("#commentFeedBack").html('');
		
		$("#" + cmtTab).attr("class", "tab");
		$("#cmts").attr("class", "active");
			
		$("#" + cmtPanel).hide();
		$("#cmts_panel").show();
		
		cmtPanel = "cmts_panel";
		cmtTab = "cmts";
		
		return false;
	});
	
	$("#viewTabs ul:first li").each(function() {
		var isActive = false;
		
		if ($(this).attr("class") != "info") {
			if ($(this).attr("class") != "active") {
				$(this).attr("class", "tab");
			} else {
				isActive = true;
				activePanel = $(this).find("a").attr("class") + "_panel";
				activeTab = $(this).find("a").attr("class");
			}

			$(this).find("a").click(function() {
				$(this).blur();
				var s = $(this).attr("class");
				var panelId = s + "_panel";
			
				$("#" + activeTab).attr("class", "tab");
				$("#" + s).attr("class", "active");
			
				$("#" + activePanel).hide();
				$("#" + panelId).show();
				//setTimeout('$("#' + panelId + '").fadeIn("slow");', 10);
			
				activePanel = panelId;
				activeTab = s;
						
				return false;
			}).each(function() {
				var panelId = $(this).attr("class") + "_panel";
			
				if (panelId != activePanel) {
					$("#" + panelId).attr("class", "hiddenPanel")
				}
			});
		}
	});
	
	$("#cmtTabs ul:first").attr("class", "tabs");
	$("#cmtTabs ul:first li").each(function() {
		var isActive = false;
		
		if ($(this).attr("class") != "active") {
			$(this).attr("class", "tab");
		} else {
			isActive = true;
			cmtPanel = $(this).find("a").attr("class") + "_panel";
			cmtTab = $(this).find("a").attr("class");
		}

		$(this).find("a").click(function() {
			$(this).blur();
			var s = $(this).attr("class");
			var panelId = s + "_panel";
			
			$("#" + cmtTab).attr("class", "tab");
			$("#" + s).attr("class", "active");
			
			$("#" + cmtPanel).hide();
			$("#" + panelId).show();
			
			cmtPanel = panelId;
			cmtTab = s;
						
			return false;
		}).each(function() {
			var panelId = $(this).attr("class") + "_panel";
			
			if (panelId != cmtPanel) {
				$("#" + panelId).attr("class", "hiddenPanel")
			}
		});
	});

	$("#addTagForm").ajaxForm({
		dataType: 'script'
	});
	
	initPage();
});