function replace_first ( r, s ) {
	var ns = '';
	for ( var i = 0; i < s.length; i++ ) {
		if ( i == 0 ) {
			ns = r;
		}
		else {
			ns += s[i];
		}
	}

	return ns;
}

$(document).ready(function(){
	$('#list_video .video').live( 'click', function(){
		var $this = $(this).find('a');
		
		$('#modal .content').html( '' );

		if ( !$('#modal').length ) {
			$('body').append( '<div id="modal"><div class="overlay"></div><div class="content"></div></div>' );
		}

		var href = replace_first( ':', $this.attr('href') );

		$.post( URL+'ajax'+href, function( result ) {
			$('#modal').fadeIn('slow', function(){
				$('#modal .content').html( result );
			});
		});

		return false;
	});

	$('#modal .overlay, #modal .close').live( 'click', function(){
		$('#modal').fadeOut('slow');
	});

	$('#list_video .video').live( 'mousemove', function(){
		var $this = $(this).find('a');

		if ( !$('#viewTitle').length ) {
			$('body').append('<div id="viewTitle"></div>');
		}

		$('#viewTitle').html( $this.attr('title') );

		$('#viewTitle').show();
		$('#viewTitle').css({
			'position' : 'absolute',
			'float' : 'left'
		});

		var offset = $this.offset();

		var top = offset.top - $('#viewTitle').height();

		if ( top < 52 ) {
			top += $this.outerHeight( true ) + $('#viewTitle').height();
		}

		var left = ( offset.left + ( $this.width() / 2 ) ) - ( $('#viewTitle').width() / 2 );

		$('#viewTitle').css({
			'top' : top,
			'left' : left
		});
	});

	$('body').live( 'mousemove', function(e){
		if ( e.clientX < 1 && $('#right').width() > 0 ) {
			$('#left').animate({
			    'left' : '0'
			});

			$('#content').animate({
			    'left' : $('#right').width()
			});

			$('#right').animate({
			    'width' : '0',
			    'display' : 'none'
			});
		}
	});

	$('#list_video .video').live( 'mouseleave', function(){
		$('#viewTitle').hide();
	});

	$('ul.tab li a').live( 'click', function(){
		var $this = $(this);
		var tab = $this.attr('data-tab');

		$('ul.tab li').removeClass('current');
		$this.parent().addClass('current');

		$('div.tab').removeClass('current');
		$('div.tab.'+tab).addClass('current');
		
		return false;
	});
});
