/**
 * @desc permalink js
 */

var default_url     = '';
var default_comment = '';

var login_url       = '/ajax/login.php';
var submit_url      = '/ajax/submit.php';

var permalink       = null;
var permalink_id    = null;
var thumbs          = null;
var tiny            = null;
var title           = null;
var comment         = null;
var avatar_url      = null;
var profile_url     = null;
var screen_name     = null;

$(document).ready(function(){
	// init permalink_id
	permalink_id = $('#permalink_id').attr( 'value' );
	dlog( 'ready(): permalink_id = ' + permalink_id );

	// clicking Comment => step3
	$('button#comment').click( function() {
		submitComment();
	});

	// disable previous enter key event handler if any
	$(window).unbind( 'keydown' );
	// enter key => submit comment
	$(window).keydown(function( event ) {
		if ( event.keyCode == 13 )
			submitComment();
	});

	// clicking Login
	$('button#login').click( function() {
		submitLogin();
	});

	// clicking Post
	$('button#post').click( function() {
		submitPost();
	});

	$('#comment_text').blur( function() {
		dlog('blur');
		$(this).css( 'background', 'none');
		if ( $(this).val() == '' )
			$(this).val( default_comment );
	});

	$('#comment_text').focus( function() {
		dlog('focus');
		$(this).css( 'background', '#f0f0f0');
		if ( $(this).val() == default_comment )
			$(this).val('');
	});

	$('#comment_text').focus();
	$('#comment_text').keypress( function() {
		var len = $('#comment_text').val().length;
		$('#remaining').html( 140 - len );
		if ( 140 - len < 21 )
			$('#remaining').css( 'color', 'red' );
		else
			$('#remaining').css( 'color', '#ccc' );
	});
});

function dlog( msg )
{
	if ( typeof console == 'object' && typeof console.log == 'function' )
		console.log( msg );
}

function showStep( step )
{
	dlog( 'showStep( ' + step + ' )' );

	switch ( step )
	{
		case 1:
		$('.pane').hide();
		$('#step1').show();
		$('.step-tabs li').css( 'color', '#777' );
		$('#tab1').css( 'color', 'black' );
		$("#progressbar").progressbar( 'option', 'value', 10 );
		$('#url').focus();
		// disable previous enter key event handler if any
		$(window).unbind( 'keydown' );
        // enter key => submit URL
		$('#url').keydown(function( event ) {
			if ( event.keyCode == 13 )
				submitURL();
		});
		break;

		case 2:
		$('.pane').hide();
		$('#step2').show();
		$('.step-tabs li').css( 'color', '#777' );
		$('#tab2').css( 'color', 'black' );
		$('#comment_text').focus();
		// disable previous enter key event handler if any
		$(window).unbind( 'keydown' );
        // enter key => submit comment
		$(window).keydown(function( event ) {
			if ( event.keyCode == 13 )
				submitComment();
		});
		break;

		case 3:
		$('.pane').hide();
		$('#step3').show();
		$('.step-tabs li').css( 'color', '#777' );
		$('#tab3').css( 'color', 'black' );
		$('#user').focus();
		// disable previous enter key event handler if any
		$(window).unbind( 'keydown' );
        // enter key => submit login
		$(window).keydown(function( event ) {
			if ( event.keyCode == 13 )
			{
				if ( $('#login').html() )
					submitLogin();
				else
					submitPost();
			}
		});
		break;
	}
}

function submitComment()
{
	comment = jQuery.trim( $('#comment_text').val() );

	dlog( 'submitting comment: "' + comment + '", permalink_id: ' + permalink_id );

	// check for non-empty comment before submitting to server
	if ( comment )
	{
		var obj = new Object;
		obj.comment      = comment;
		obj.permalink_id = permalink_id;

		$.getJSON( submit_url, obj, function( json ) {
			if ( json.response )
			{
				dlog( 'received: ' + json.response + ', message: ' + json.message );

				if ( json.response == 'ack' )
				{
					// hide characters remaining
					$('#remaining').hide();

					// update prompt
					$('#thought-prompt').hide();
					$('#share-prompt').show();

					// update comment in post_text
					$('#post-wrapper .comment_text').html( comment );
					$('#login-wrapper').show();

					$('#comment-wrapper').hide( "blind", { direction: "vertical", mode: "show" }, 1000 );
					$('#post-wrapper').show( "blind", { direction: "vertical", mode: "show" }, 1000 );

					// disable previous enter key event handler if any
					$(window).unbind( 'keydown' );

					// enter key => submit login
					$(window).keydown(function( event ) {
						if ( event.keyCode == 13 )
						{
							if ( $('#login').html() )
								submitLogin();
							else
								submitPost();
						}
					});
				}
			}
		});
	}
	else
	{
		// display acknowledgement of empty comment => step 3
		/*
		$('#no-comment').show();
		$('#comment-wrapper').hide( "blind", { direction: "vertical" }, 1000, function() {
		});
		*/
	}
}

function submitLogin()
{
	var user = $('#user').val();
	var pass = $('#pass').val();
	var remember = $('#remember').is(':checked');

	dlog( 'login user: ' + user + ', pass: ' + pass + ', remember: ' + remember + ', permalink: ' + permalink );

	var obj = new Object;
	obj.user = user;
	obj.pass = pass;
	obj.remember  = remember;
	obj.permalink_id = permalink_id;

	$.getJSON( login_url, obj, function( json ) {
		if ( json.response )
		{
			dlog( 'received: ' + json.response + ', message: ' + json.message );
			if ( json.response == 'ack' )
			{
				// login success...
				avatar_url  = json.avatar_url;
				profile_url = json.profile_url;
				screen_name = json.screen_name;

				$('#login-required').hide();
				$('#login-failed').hide();
				$('#login-success').show();

				// update avatar
				$('#post-wrapper .tweet .avatar').attr( {
					src:   avatar_url,
					alt:   screen_name,
					title: screen_name
				});

				// update screen_name
				$('#post-wrapper .tweet .nick').attr( {
					href:   profile_url,
					target: '_blank',
					title:  screen_name
				}).html( screen_name );

				submitPost();
			}
			else
			{
				$('#login-required').hide();
				$('#login-success').hide();
				$('#login-failed').show();
			}
		}
	});
}

function submitPost()
{
	var post = comment;
	dlog( 'posting status update for ' + screen_name + ': ' + post );

	var obj = new Object;
	obj.post         = post;
	obj.permalink_id = permalink_id;

	$.getJSON( submit_url, obj, function( json ) {
		if ( json.response )
		{
			dlog( 'received: ' + json.response + ', message: ' + json.message );
			if ( json.response == 'ack' )
			{
				$('#login-required').hide();
				$('#login-failed').hide();
				$('#login-success').hide();
				$('#post-success').show();
				$('#post').hide();

				$('#share-prompt').html( 'Your Twitter status has been updated' );

				setTimeout( function() {
					$('#login-wrapper').hide( "blind", { direction: "vertical" }, 1000 );
				}, 1000 );
			}
		}
	});
}

