
/**
 * JSON Object for Webcam capture
 * It contains current uploaded media id
 * Error message
 */
var WebcamCapture = {
		mediaId : "",
		captureError : ""
		
};

/**
 * JSON Object for maintaining tweet count
 */
var CurrentTweet = {
		page : 1,
		start : 0
};

/**
 * Method for getting mediaId as a respose from webcam capture component.
 * @param mediaId
 * @return
 */
function mediaIdCallback(mediaId)
{
	//Assign media Id to Webcam Capture Object for further use
	WebcamCapture.mediaId = mediaId;
		
	
	//Call to webcamCapureUploaded() method for further changes
		webcamCapureUploaded();
		
}


/**
 * Method for getting Capture error
 * @param msg
 * @return
 */
function captureError(msg)
{
	//Assign error message to Webcam Capture Object for further use
	WebcamCapture.captureError = msg;
		
	//Call to webcamCapureUploaded() method for further changes
	//webcamCapureUploaded();
	
	
	$('#WebcamArea').hide();
	$('#WebCamMsg').hide();
	$('#FileAndWebcam').slideDown();
	
	// get default value of text
	var defaultText = $('#text').val();
	
	//get index of @ in text area
	var indexforAt = defaultText.indexOf("@");
	
	//if text contains @ in the text area it means its reply option so dnt change text to default text otherwise chage it.
	if(indexforAt == -1)
	{	
		$('#TextValue').html('');
     	  
		var html = '<textarea id="text" name="text" class="text_twit" title="Watch my tweet at #tvider" ></textarea>';
     	$('#TextValue').html(html);
	}
	
 	$('#text').inputTextToggle();
 	
	//$('#text').val("");
	$('#tweetPost').show();
	$('#tweetWebcamPosting').replaceWith('<span id="tweetWebcamPost" name="tweetWebcamPost" class="update">Update</span>');
	$('#tweetWebcamPost').hide()
    
	/**
	 * Assign event on tweetPost button for posting tweet
	 */
	$('#text').keyup(
			function()
			{
				updateCharsNum('#text',110,'#CharLimit');
			}
		);
	
	/**
	 * Assign event on tweetPost button for posting tweet
	 */
	$('#tweetPost').one("click",
			function(event)
			{
				loginAndPostTweet('#tweetPost');
				event.stopImmediatePropagation();
			}
		);
	
	alert(msg);
}


/**
 * Method for getting webcam capture status
 */
function webcamCapureUploaded(){
	//$('#FileAndWebcam').slideDown();
	$('#WebcamArea').slideUp();
	$('#WebCam').html("");
	$('#WebCamMsg').slideDown();
}

/**
 * Method for login user from tweet form
 */
function loginAndPostTweet(ele)
{
	var username = $('#headerUsername').val();
	var password = $('#headerPassword').val();
	
	
	
	if($.cookie('Username') == null)
	{
		alert('Please login to update your status.');
		
	}
	else
	{
		postTweet();
	}
}

/**
 * Method for checking textarea char length
 */
function updateCharsNum(o, maxlen, state) {
	
	if (!maxlen) {
		maxlen = 110;
	}
	var message = $("#text").val();
	var message_len = message.length;
	//
	var left = maxlen - message_len;
	$(state).html(left + " characters left");
	var disabled = false;
	if (message_len > maxlen) {
		$(state).css({"color":"#ff0000"});
		disabled = true;
	} else {
		$(state).css({"color":"#494949"});
	}
	
	
		if (disabled) {
			$("#tweetPost").addClass("update_disabled");
			$("#tweetPost").unbind('click');

		} else {
			$("#tweetPost").removeClass("update_disabled");
			$('#tweetPost').click(
					function(event)
					{
						loginAndPostTweet('#tweetPost');
						event.stopImmediatePropagation();
					}
				);
		}
	
}

var Htmltags				= ["<a","<img","<embed","<center","<html","<head","<body"];
var angleBrackets			= "<" ;
/**
 * Method for posting tweet
 */
function postTweet()
{
	var fileValue = $("#file").val();
	
	var tweetText	=	$("#text").val();
	
	
	
	if(tweetText.length > 0 && tweetText !="Watch my tweet at #twiteract")
	{	
		/* Check for the html tags in the comment text ...*/
		for(var i=0; i<= Htmltags.length ; i++)
		{
			tweetText = tweetText.toLowerCase();
			var index = tweetText.indexOf(Htmltags[i]);
			/* if theres html tag */
			if(index != -1)
			{
				alert("Caught You!!Our super intelligent app has caught you red handed putting html code in the text.Remove it dude!");
				

				return false;
			}
		}

		var angularTagsIndex = tweetText.indexOf(angleBrackets);
		if(angularTagsIndex != -1)
		{
			alert("Please Remove the Special Charachters! The App does not like them!");
			
			return false;
		}

	}		
	
	if(fileValue != "" || fileValue.length > 0)
	{		
		
		$('#tweetPost').replaceWith('<img src="'+staticHost+'/images/wait16trans.gif" /><b>updating status</b>');
		
		$('#TweetForm').submit();
		
		
		setTimeout('queryStatus()', 10);
	}
	else
	{
		alert("Please select Media for tweet .");
		return;
	}
}
