var oldTwitterPopup = "";
function showTwitterPopup(job, callback) {
	//check for default tweet
	if (!job.tweet) {
		//backup tweet in case AJAX fails
		var backupTweet = tweet = job.title + " (" + job.location + ") " + job.url + " #jobs #in";
		
		//request default tweet
		$.ajax({
			type: 'POST',
			url: '/job/twitter/default',
			data: { jobId:job.id },
			success:function(data) {
				if ((0 < data.trim().length) && (data.trim().length < 140)) {
					job.tweet = data.trim();
				} else {
					job.tweet = backupTweet;
				}
				showTwitterPopupDialog(job, callback); 
			},
			error:function() { job.tweet = backupTweet; showTwitterPopupDialog(job, callback); }
		});
	} else {
		showTwitterPopupDialog(job, callback);
	}
}

function showTwitterPopupDialog(job, callback) {
	//reset popup html
	$("#twitterPopup #tweetText").val(job.tweet);
	$("#twitterPopup #charCount").html(140 - job.tweet.length);
	$("#charCount").css("color","#cccccc");
	$("#twitterPopup #error").html("");
	
	//show popup
	$("#twitterPopup").dialog({
		buttons: { "Tweet":function() { postTwitterUpdate(callback); }, "Skip": function() { $(this).dialog("close"); callback(); } },
		closeText:"X",
		dialogClass:"twitter",
		draggable:false,
		position:["center", 125],
		width:600,
		resizable:false,
		title:"<img src='/images/logoTwitter_16.png' />Update your Twitter status"
	});
	//save popup html
	oldTwitterPopup = $("#twitterPopup").html();
}
	
function postTwitterUpdate(callback) {
	var form = $('#twitterForm');
	//disable button
	disableSubmit();
	
	//post tweet
	//NOTE: Using the generic ajax function ensures that fbParams are added to form post
	saveFormViaAjax(form, function(response) {
		var data = parseJSON(response);
		if (data.success) {
			//successful tweet, update popup
			$("#twitterPopup").html('<h3 style="margin-bottom:10px;">Job Tweeted Successfully.</h3>');
			$("#twitterPopup").dialog({buttons: {"Done": function() { $(this).dialog("close"); callback(); $("#twitterPopup").html(oldTwitterPopup); }}});
		} else {
			onTwitterError(data.error);
		}
	}, function() {
		onTwitterError("");
	});
}

function onTwitterError(error) {
	//show custom error for duplicate tweet error
	if (error) {
		$("#twitterPopup #error").html("Twitter post failed: "+error);
	} else {
		$("#twitterPopup #error").html("There was a problem posting your tweet. Try again later.");
	}
	
	//unlock button
	enableSubmit();
}

function disableSubmit() {
	$(".ui-dialog-buttonpane button:first-child").attr("disabled","disabled");
	$(".ui-dialog-buttonpane button:first-child").addClass("disabled");
}

function enableSubmit() {
	$(".ui-dialog-buttonpane button:first-child").attr("disabled","");
	$(".ui-dialog-buttonpane button:first-child").removeClass("disabled");
}

function updateCount() {
	//update char count
	$("#charCount").html(140 - $("#tweetText").val().length);
	var count = $("#charCount").html();
	
	//diable form submit
	if (count < 0) {
		disableSubmit();
	} else {
		enableSubmit();
	}
	
	//update color
	if (count < 10) {
		$("#charCount").css("color","#ff0000");
	} else if (count < 20) {
		$("#charCount").css("color","#5C0002");
	} else {
		$("#charCount").css("color","#cccccc");
	}
}