$(function(){
		
	// $("#email .space").hover(function() {
	// 			$(this).next("em").animate({opacity: "show", top: "-21"}, "slow");
	// 		}, function() {
	// 			$(this).next("em").animate({opacity: "hide", top: "-35"}, "fast");
	// 		});

	$('p.blogtitle').truncate( 40, {
			chars: /\s/,
			trail: [ " ... <a href='#' class='truncate_show'>more</a>", " <a href='#' class='truncate_hide'>less</a>" ]
		});
		
		$('p.blogtext').truncate( 127, {
			chars: /\s/,
			trail: [ " ... <a href='#' class='truncate_show'>more</a>", " <a href='#' class='truncate_hide'>less</a>" ]
		});
		
		$('p.posttitle').truncate( 40, {
			chars: /\s/,
			trail: [ " ... <a href='#' class='truncate_show'>more</a>", " <a href='#' class='truncate_hide'>less</a>" ]
		});
		
		$('p.posttext').truncate( 127, {
			chars: /\s/,
			trail: [ " ... <a href='#' class='truncate_show'>more</a>", " <a href='#' class='truncate_hide'>less</a>" ]
		});

	$('.single_video').find('a.add_link, a.delete_link, a.edit_link').remove();
	
	$('.single_video').draggable({
		helper:  function () { return $(this).clone().appendTo('body').css('zIndex',5).show(); },
		cursor:'move'
	});
	
	$('#add_to_library').droppable({
		drop: function(event, ui) {
			addToLibrary(ui.draggable);
		}
	});
	
	$('.btn_add_to_library').click(function(){
		$video_thumb = $(this).parents('div.video_info').find('div.single_video');
		addToLibrary($video_thumb);
	});
	
	$('#post_id').change(function(){
	  dest_post_id = $(this).val();
	  var url = $(location).attr('href').replace(/\d+$/, dest_post_id);
	  $(location).attr('href',url);
  });
	
  // $('#post_vlog_img').load(function() {
  //   	$(this).center();
  // 	});
  
  $('.more_link').click(function(){
		$commentDiv = $(this).parents('div.comment');
		$commentDiv.find('.comment_cutoff').hide('fast');
		$commentDiv.find('.comment_full').show('slow');
		if(isChangeExpandTime($('div.comment_full'),'none')){
			$('#expand_colapse').html('<img id="colapse" src="/images/less_img.png" alt="Show Less">');
		}
		return false;
	});
	
	$('.less_link').click(function(){
		$commentDiv = $(this).parents('div.comment');
		$commentDiv.find('.comment_full').hide('fast');
		$commentDiv.find('.comment_cutoff').show('slow');
		$link = $('#expand_colapse')
		if ($link.find('img').attr('id')!='expand'){
			$link.html('<img id="expand" src="/images/expand_img.png" alt="Expand All">');
		}
		return false;
	});
	
	$('#expand_colapse').click(function(){
		$link = $(this);
		$img = $link.find('img')
		var imgId = $img.attr('id');
		if (imgId=='expand'){
			$('div.comment_cutoff').hide();
			$('div.comment_full').show();
			$link.html('<img id="colapse" src="/images/less_img.png" alt="Show Less">');
		} else {
			$('div.comment_full').hide();
			$('div.comment_cutoff').show();
			$link.html('<img id="expand" src="/images/expand_img.png" alt="Expand All">');
		}
		return false;
	});
	
	$('#add_comments').click(function(){
		$('#tab_img').hide('fast');
		$('#comments_holder').hide('fast');
		$('#new_comment').show('fast');
		$('#text_comment').focus();
		return false;
	});
	
	$('#btn_cancel_comment').click(function(){
		$('#new_comment').hide('fast');
		$('#tab_img').show('fast');
		$('#comments_holder').show('fast');
		
		return false;
	});
  
});


function addToLibrary(thumb){
	var video_id = $(thumb).find('a.play').attr('href').replace(/.*\//, ''),
    add_video_url = '/videos/add_video_new/:id'.replace(/:id/, video_id),
    $post_videos = $('#post_videos');
    template = $(thumb).clone();
    
	if ($post_videos.find('div#single_video_' + video_id).length) {
		alert('Video already added to your library.');
	} else {
		$.get(add_video_url, function(){
		  $post_videos
		    .find('img.one_slot').before(template).end()
		    .find('.btn_play a.play').remove()
		    .end()
		    .find('div.single_video:last')
		      .attr('id', 'single_video_' + video_id)
		      .wrap('<div class="video"></div>')
		      .css({'cursor':'default'})
		      .find('img.video_img')
		        .css({'cursor':'default'});
		});
	}
}

function isChangeExpandTime(obj, style){
	var isChangeTime = true;
	obj.each(function (i) {
		if ($(this).css('display') == style) {
			isChangeTime = false;
			return false;
		}
	});
	return isChangeTime;
}
