// onload function
$(document).ready(function() {
	loadLastUserStatuses(g_userName);
});

// loads the user statuses
window.loadLastUserStatuses = function(username) {
	$.getJSON("http://search.twitter.com/search.json?q=from%3A"+encodeURIComponent(username)+"&rpp=3&page=1&callback=?",
		function(data){
			if (data.results.length > 0) {
				$.each(data.results, function(i,result) {
						result.text = result.text.replace(new RegExp("http://([a-zA-Z0-9_./#-\?%]+)", "g"), "<a target=\"_blank\" href='http://$1'>http://$1</a>");
						result.text = result.text.replace(new RegExp("#([a-zA-Z0-9_./]+)", "g"), "<a target=\"_blank\" href='http://search.twitter.com/search?q=%23$1'>#$1</a>");
						result.text = result.text.replace(new RegExp("@([a-zA-Z0-9_./]+)", "g"), "<a target=\"_blank\" href='http://twitter.com/$1'>@$1</a>");
						
						$('#lasttweets').append('<div class="tweet">"'+result.text+'"</div>');
				});
			} else {
				$('#lasttweets').append('<div class="tweet">No tweets by this user</div>');
			}
		}
	);
};

// switch sorted data
window.onClickSort = function(id,element) {
	$('.user-videos').hide();
	$('#user-videos-'+id).show();

	$(element).parent().children().removeClass('active');
	$(element).addClass('active');
};

//When the user clicks on the icon to change the video status"
window.onClickChangePublishedStatus = function(videoBid, element) {
	openLoginIfNeeded('sign-in',function (success,data) {
		if (success) {
			var published = $(element).children('.public-img').is(':visible');
			
			// call set published service
			ajaxCall({
				type: 'GET',
				url: g_updatePublishedStatusUrl,
				cache: false,
				data: {
					videoBid: videoBid,
					published: !published
				},
				onSuccess: function(data){
					if ($(element).children('.private-img').is(':visible')) {
						$(element).children('.private-img').hide();
						$(element).children('.public-img').show();
					} else {
						$(element).children('.private-img').show();
						$(element).children('.public-img').hide();
					}
				},
				onError: function (code, error) {
					
				}
			 });
		}
	});
};

//When the user clicks on "follow me on twitter"
window.onClickFollowMe = function(userBid) {
	openTwitterLoginIfNeeded(function (success,data) {
		if (success) {
			// call follow me service
			ajaxCall({
				type: 'GET',
				url: g_followUserUrl,
				cache: false,
				data: {
					userBid: userBid
				},
				onSuccess: function(msg){
				},
				onError: function (code, error) {
					
				}
			 });
		}
	});
};

//When the user clicks on "Delete"
window.onClickDeleteVideo = function(videoBid) {
	// save the id
	$('#deleteVideoBid').val(videoBid);
	
	// show the confirmation popup
	showPopup('confirm-delete-popup');
};

window.onClickCancelDeleteVideo = function() {
	hidePopup('confirm-delete-popup');
}

window.onClickConfirmDeleteVideo = function() {
	openLoginIfNeeded('sign-in',function (success,data) {
		if (success) {
			var videoBid = $('#deleteVideoBid').val();
			
			// call delete video service
			ajaxCall({
				type: 'GET',
				url: g_deleteVideoUrl,
				cache: false,
				data: {
					videoBid: videoBid
				},
				onSuccess: function(msg){
					// hide the popup
					hidePopup('confirm-delete-popup');
					
					// delete video from page
					$('.video-bid-'+videoBid).animate({
					    height: 'hide',
					    opacity: 'hide'
					}, 'slow');
					
					// decrement the number of videos
					$('.total-videos').each(function (index) {
						var val = parseInt($(this).html()) - 1;
						$(this).html(''+val);
						
						if (val != 1) $('.recording-s').show();
						else $('.recording-s').hide();
					});
				},
				onError: function (code, error) {
					
				}
			 });
		}
	});
};
