// JavaScript Document

$(document).ready(function(){   

	$('.favoriteButton').bind("click", showFavoritePopup);

	
			
	/* Share content by users */

	$('.shareButton').bind("click", showSharingForm);

		$("#frmShareContent").validate({
				rules: {
					txtName: "required",
					txtTo: {
						required:true,
						email:true
					},
					txtMessage: "required",
					txtShareCode: "required"
				},
				messages: {
					txtName: "Please enter name.",
					txtTo: {
						required: "Please enter email.",
						email: "Please enter valid email."
					},
					txtMessage: "Please enter message.",
					txtShareCode: "Please type the characters you see in the picture above"
				}
			});
			
		$("#shareButton").click(function() {


		$("#frmShareContent").valid();

			
			var name  = $("#txtName").val();
			var to  = $("#txtTo").val();
			var message  = $("#txtMessage").val();
			var shareContentId  = $("#shareContentId").val();
			var URL  = $("#URL").val();
			var resName = $("#resName").val();
			var secCode = $("#txtShareCode").val();
						
			if ($("#frmShareContent").valid()){
			
				$("#shareResultMessage").show();

				$("#shareResultMessage").html("<img  src='/images/loading.gif'/>");
/*				alert(name);
				alert(to);
				alert(message);
				alert(shareContentId);
				alert(URL);
				alert(resName);
				alert(secCode);
*/				$.get("/controller/share_content.php", { securityCode: secCode, name: name, to: to, message: message, URL: URL,shareContentId:shareContentId,resName:resName}, function(data) {
				//alert(data);
				 
			 		$("#shareResultMessage").html(data);

				
			 });
			
				img = document.getElementById('shareImgCaptcha'); 
				//Change the image
				img.src = "/lib/classes/CaptchaSecurityImage.php?" + Math.random();

				$("#txtShareCode").attr("value","");

				return false;
			} else {
				return false;
			}
			
		});


	$('.editButton').bind("click", redirectOnEditPage);
	$('.deleteButton').bind("click", showDeleteDialog);


});



/*Show sharing form*/

function showSharingForm(evt) {
 
//	var linkValue = evt.target.getAttribute('id');
	var linkValue = $(this).attr("id");
//	var classValue = evt.target.getAttribute('class');

	$("label").css("display","none");

	$("#shareResultMessage").hide();

	$("#shareContentId").attr("value",linkValue);
		
	$("#txtName").attr("value","");
	$("#txtTo").attr("value","");
	$("#txtMessage").attr("value","");
	$("#txtShareCode").attr("value","");


	img = document.getElementById('shareImgCaptcha'); 
	//Change the image
	img.src = "/lib/classes/CaptchaSecurityImage.php?" + Math.random();

	$('#shareDiv').modal({
		persist:true,
		onOpen: function (dialog) {
			dialog.overlay.fadeIn(1000);
			dialog.container.fadeIn(1000);
			dialog.data.fadeIn(1000);
		},
		onClose: function (dialog) {
			dialog.data.fadeOut(1000);
			dialog.container.fadeOut(1000);
			dialog.overlay.fadeOut(1000);
			$.modal.close();
		},
		containerCss: {
			width: 600,
			height: 450,
			backgroundColor: '#fff',
			border: '3px solid #ccc'
		  }

	});


	
}

/* Redirect on edit page of content */

function redirectOnEditPage(evt) {
 
	var contentId = $(this).attr("id");
	editPageURL = editPageURL+contentId;
	
	//alert(editPageURL);
	
	window.location=editPageURL;
	
}

/* Show delete Dialog */

var contentId = $(this).attr("id");

function showDeleteDialog(evt) {
 
	
	var message  = "";
	
	if (LCRFormattedResName == "questions and answers")
		LCRFormattedResName = "question and answer";
	
	if (resType == "favorite") 
	 	message  = "Do you want to delete this " + LCRFormattedResName + " from favorites?";
	else
	 	message  = "Do you want to delete this " + LCRFormattedResName + "?";
	
	
	$.prompt(message,{
		  buttons:{Yes:true,No:false},
		  callback: mycallbackfunc,
		  prefix:'cleanblue'
	}); 

	contentId = $(this).attr("id");

}

function mycallbackfunc(v,m,f){
   
   if ( v == true ) {
		var targetURL = "/controller/delete_content.php?contentId="+contentId+"&pageURL="+pageURL+"&resName="+resName+"&resType="+resType;
	
/*	alert(contentId);
	alert(pageURL);
	alert(resName);
*/	
//	alert(targetURL);

	window.location=targetURL;
	
   } 
  

}

/* Show dialog for favorite conent */

function showFavoritePopup(evt) {


	var contentId = $(this).attr("id");
	
	
	if (topicId!="") {
		$.get("/controller/favorite.php", { contentId: contentId, resId: resId, techId: techId, topicId: topicId, resName:resName}, function(data) {
	 
			$.prompt(data,{prefix:'impromptu'}).children('#impromptu').corner();

	 	});
	} else {
		$.get("/controller/favorite.php", { contentId: contentId, resId: resId, techId: techId, resName:resName}, function(data) {
	 
			$.prompt(data,{prefix:'impromptu'}).children('#impromptu').corner();

	 	});
		
	}
	

}

