/**
 * @author ezdermam
 */

var cid ="301";
var MSG_NO_COMMENT = "Please enter a comment";
var MSG_NO_ID = "No Item ID";
var ERROR_STORY_SUBMISSION = "There were errors in your story submission.";
var ERROR_SERVER_LOGIN = "You must be logged in to enter.";
var MSG_ERROR = "Error";
var ERROR_SERVER_LOGIN_KEY = "membername";
var postDate = Date;

function doCommentPost(form){
	postDate = new Date();
	var data = getComment();
	
	if (isCommentValid(data)) {
		toggleButton('submit', false);
		UPICKDAILY.utils.loader.show();
		// gdc is performing a redirect after success -- convert it to an xml blob and pass by "xml" param...
		data = {cid:data.cid, xml:getGdcXml(data.cid, data)};

	
		try { // Catch browser security exceptions.
			$.ajax({
				type: "GET",
				url: URL_COMMENT_POST,
				data: data,
				dataType:"jsonp",
				success:handleCommentPost,
				error:handleSubmitDataErrors
			});
		} catch(error) {
			handleSubmitPollErrors(error);
		}
	
		toggleButton('btnSaveComment', false);
	}
	return false;
}

function handleCommentPost(json) {
	if(json == null) {
		handleCommentPostErrors(ERROR_STORY_SUBMISSION);
	} else if(json.code == "ok") {
		//JS for reporting
		pageNameReset("/upickdaily/comment-submitted")
		UPICKDAILY.utils.loader.hide();
		$(".commentpost").fadeOut("slow", showThankyou)
	} else if(json.code == "error") {
		UPICKDAILY.utils.loader.hide();
		var errors = "";
		var isLoggedOut = false;

		for(var key in json.errors) {
			//doLog("handleCommentPost: error: "+key+": "+json.errors[key]+" | isLoggedOut: "+isLoggedOut);
			errors += json.errors[key]+"\n";

			if(isLoggedOut) continue;
			isLoggedOut = key == ERROR_SERVER_LOGIN_KEY || ERROR_SERVER_LOGIN == json.errors[key];
		}

		//doLog("handleCommentPost: status: isLoggedOut: "+isLoggedOut);
		if(isLoggedOut) {
			doLog(errors);
			UPICKDAILY.login.doLoginPrompt();
		} else {
			UPICKDAILY.utils.message.show(MSG_ERROR, errors);
		}
	}

	toggleButton('submit', true);
}


function showThankyou(){
	$(".commentpost").empty();
	$(".commentpost").append("<div>Thanks for your comment! You can see it right here once it's approved! </div>");
	$(".commentpost").fadeIn("slow");
}

function handleSubmitDataErrors(request, error, exception) {
	doLog("handleSubmitDataErrors: "+error);
}

function handleCommentPostErrors(error){
	doLog(error);
	UPICKDAILY.utils.loader.hide();
	UPICKDAILY.utils.message.show(MSG_ERROR, error);
	toggleButton('submit', true);
}



function getComment(){
	
	return {
		cid:cid,
		type: "updComment",
		itemid: $("input[name='itemId']").attr("value"),
		comment: $("textarea").val(),
		workflow_stages:$(".commentpost #workflow_stages").attr("value"),
		workflow_name:$(".commentpost #workflow_name").attr("value"),
		status_url:$(".commentpost #status_url").attr("value"),
		suspend_url:$(".commentpost #suspend_url").attr("value"),
		resume_url:$(".commentpost #resume_url").attr("value"),
		publish_url:$(".commentpost #publish_url").attr("value"),
		membername:$(".commentpost #membername").attr("value")
	}
}


function isCommentValid(_data){
	var valid = true;
	var errorMsg = "";
	
	if (_data.comment == "") {
		errorMsg += MSG_NO_COMMENT + "\n";
		valid = false;
	}
	
	if (_data.itemid == "") {
		errorMsg += MSG_NO_ID + "\n";
		valid = false;
	}
	
	if(!valid){
		UPICKDAILY.utils.message.show("Attention", errorMsg);
	}
	
	return valid;
}


/* -- moved to utils.js
function getGdcXml(id, obj) {
	var gdc = "<answers collectionID=\""+id+"\">";
	for(var key in obj) {
		gdc += "<answer tag=\""+ key +"\">"+ obj[key] +"</answer>";
	}
	gdc += "</answers>";
	return gdc;
}
*/


function initCommentPost() {
	doLog("init poll submit");
	toggleButton('submit', true);
}

$(document).ready(initCommentPost);