$(document).ready(function() {

    $('#like-on-facebook').click(function(){
        $(this).showFBbox();
    });
    $('#share-on-twitter').click(function(){
        $(this).showTwit();
    });
    $('#btn_email4media').click(function(){
        $(this).showE4M();
    });
    $('#box-reset').click(function(){
        $(this).resetBox();
    });

    $('#tweetthis').live('keyup', function() {
        $(this).updateRemaining('tweetlabel');
    });

    $('#tweetthis').live('keydown', function(e) {
        var remaining_chars = $(this).updateRemaining('tweetlabel');

        if(remaining_chars<1 && e.key != 'delete' && e.key != 'backspace'){
            e.stop();
        }
    });

    $(this).updateRemaining('tweetlabel');

	$('#tweetlink').live('click', function() {
		$('#tweetlinkspc').html('Thank You!');
    });

});

jQuery.fn.updateRemaining = function(labelid){
    var id='#'+labelid;
    var max_chars = 140;
    var current_value = '';
    if($('#tweetthis').length) current_value = $('#tweetthis').val();
    var current_length = current_value.length;
    var remaining_chars = max_chars-current_length;
    $(id).html('Remaining: ' + remaining_chars);

    if(remaining_chars<=13){
        $(id).css({'color':'#5fcdee'});
    } else {
        $(id).css({'color':'#808080'});
    }
    return remaining_chars;
}

jQuery.fn.showFBbox = function(){
    if($('#twit-box').is(':visible')){
        $('#twit-box').fadeOut(0);
        $('#share-on-twitter').fadeIn(500);
    }
    $(this).fadeOut(0);
    $('#fb-root').fadeIn(500);
    $('#box-reset').fadeIn(500);
}
jQuery.fn.showTwit = function(){
    if($('#fb-root').is(':visible')){
        $('#fb-root').fadeOut(0);
        $('#like-on-facebook').fadeIn(500);
    }
    $(this).fadeOut(0);
    $('#twit-box').fadeIn(500);
    $('#box-reset').fadeIn(500);
}
jQuery.fn.showE4M = function(){
    $(this).fadeOut(0);
    $('#e4m').fadeIn(500);
}
jQuery.fn.resetBox = function(){
    if($('#fb-root').is(':visible')) $('#fb-root').fadeOut(0);
    if($('#twit-box').is(':visible')) $('#twit-box').fadeOut(0);
    if($('#box-reset').is(':visible')) $('#box-reset').fadeOut(0);
    $('#like-on-facebook, #share-on-twitter').fadeIn(500);
}
$(function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
        drop: function( event, ui ) {
            $( this ).addClass( "ui-state-highlight" );
            $('#tsFrame77752').attr('src','http://cdn.topspin.net/api/v2/widget/player/77752');
            $('#streaming-player').fadeIn(500);
        }
    });
function periodical() {
  $('#draggable').effect('shake', { times:3 }, 200);
	}
	$(document).ready(function() {
	  $('#draggable').hide().css('display','').fadeIn(600);
	  var shake = setInterval(periodical, 5000);
	  
	  /* updated: click clear */
	  $('#draggable').click(function() {
		clearInterval(shake);
	  })  
	});        
});
