$(document).ready(function(){
	$('#container > ul').tabs();
	$('#divFavorite').hide(); $('#divEmail').hide(); $('#divFlag').hide(); $('#comment_msg').hide();
	var fadetime = 3000;
	var serial = $('#video_serial').val();
	/* var current_rating = $('#vrating').val(); */
	var pageno = 1;
	/* $('#video_rating').rating('http://www.blackbottom.com/video_action.php', {maxvalue:5, increment:.5, curvalue:current_rating, video:serial}); */
	
	$('#txtComment').keydown(function(e){
		if( (e.keyCode == 13) ){
			$('#btnComment').click();
			return false;
		}
	});
	$('#container > ul > li > a:eq(3)').click( function(){
		//setCommentFocus();
	});
	/**
	var updateLinks = function() {
		$('a[@id^="page"]').click( function() {
			var href = $(this).attr('id');
			var pg = href.split( '-' );
			loadCommentBlock( serial, pg[1] );
			return false;
		});
	};
	**/
	var setCommentFocus = function() {
		setTimeout( function() {
			$('#txtComment').focus();
		}, 15);
	};
	/**
	var loadCommentBlock = function( s, p ) {
		$('#comment_block').load( 'comments.php', { v:s, pageno:p } );
		setTimeout( function() {
			// Recommend
			$('a[@id^="recommend"]').click( function() {
				var tag = $(this).attr('id');
				$.ajax({
					'url': 'http://www.blackbottom.com/video_action.php',
					'data': { 'action':'ca', 'subaction': tag, 'video': serial },
					'type': 'POST',
					'async': false,
					'success': function( msg ){
						if( msg != false ){
							$('a#' + tag + ' + span').text( msg );
						} else {
							alert( 'You have already recommended this comment.' );
						}
					}
				});
			});
			// Abuse
			$('a[@id^="abuse"]').click( function() {
				var tag = $(this).attr('id');
				$.ajax({
					'url': 'http://www.blackbottom.com/video_action.php',
					'data': {'action':'ca','subaction': tag,'video': serial },
					'type': 'POST',
					'async': false,
					'success': function( msg ){
						if( msg == true ){
							alert('This comment (#' + tag + ') has been reported. Thank you.');
						} else {
							alert( 'You have already flagged this comment.' );
						}
					}
				});
			});
			//updateLinks();
			setCommentFocus();
		}, 500);
	};
	// Comment tab (#3) clicked...
	$('#container > ul > li > a:eq(3)').one( 'click', function(){
		loadCommentBlock( serial, pageno );
	});
	**/
	
	$('#btnFavorites').click( function() {
		$.ajax({
			'url': 'http://www.blackbottom.com/video_action.php',
			'data': {'action':'favorite', 'subaction':'add', 'video': serial },
			'type': 'POST',
			'async': false
		});
		$(this).attr('disabled','disabled');
		$('#msg_confirm').text('Video has been added to your Favorites.').fadeIn( fadetime ).fadeOut( fadetime );
	});
	$('#btnVideoRecommend').click( function() {
		$.ajax({
			'url': 'http://www.blackbottom.com/video_action.php',
			'data': {'action':'recommend','video': serial },
			'type': 'POST',
			'async': false,
			'success': function( msg ){
				if( msg != false ){
					$('#msg_confirm').text('Video has been recommended!').fadeIn( fadetime ).fadeOut( fadetime );
				} else {
					alert( 'You have already recommended this video.' );
				}
			}
		});
		$(this).attr('disabled','disabled');
	});
	$('#btnShareIt').click( function() {
		$('#divEmail').slideDown('slow');
		$('#btnShareIt').attr('disabled','disabled');
	});
	$('#btnShareCancel').click( function() {
		$('#divEmail').slideUp('slow');
		$('#error_msg').empty();
		$('#btnShareIt').attr('disabled','');
	});
	$('#btnFlagIt').click( function() {
		$('#divFlag').slideDown('slow');
		$('#btnFlagIt').attr('disabled','disabled');
	});
	$('#btnFlagCancel').click( function() {
		$('#divFlag').slideUp('slow');
		$('#btnFlagIt').attr('disabled','');
	});
	$('#btnFlagThisVideo').click( function() {
		var numFlag = $('#selectFlag').val();
		if( numFlag != 0 ){
			$.ajax({
				'url': 'http://www.blackbottom.com/video_action.php',
				'data': {'action':'flag', 'subaction': numFlag, 'video': serial },
				'type': 'POST',
				'async': false,
				'success': function(){
					$('#divFlag').slideUp('slow');
					$('#msg_confirm').text('Video has been Flagged.').fadeIn( fadetime ).fadeOut( fadetime );
				}
			});
		} else {
			alert( 'Please select a reason for the Flagging action.' );
		}
	});
	$('#btnComment').click( function() {
		var c = $('#txtComment').val();
		c = jQuery.trim( c );
		if( (c.length != 0) ){
			$.post( 'http://www.blackbottom.com/video_action.php', {'action':'comment','subaction': c,'video': serial } );
			$('#comment_msg').text('Your comment has been added (will show in 15 minutes).').fadeIn( fadetime ).fadeOut( fadetime );
			$('#txtComment').val('');
			/**
			setTimeout( function() {
				loadCommentBlock( serial, '1' );
			}, 500);
			**/
		} else {
			alert( 'Please enter some comment text!' );
		}
	});
	
	$('textarea[@maxlength]').textLimiter(); /** all textarea's with a maxlength attr will get a textLimiter **/
	
	$('#btnSendMessage').click( function() {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var hasError = false;
	
		var emails = $('#emails').val();
		if( emails == '' ){
			$('#error_msg').text('You must enter an email.');
			hasError = true;
		}
		var email = emails.split( ',' );
		$.each( email, function() {
			if( !emailReg.test( $.trim( this ))){
				$('#error_msg').text('Please enter valid emails.');
				hasError = true;
				return false;
			}
		});
	
		if( hasError == false ){
			$.ajax({
				'url': 'http://www.blackbottom.com/video_action.php',
				'data': {'action':'share','email': emails, 'msg': $('#message').val(), 'video': serial },
				'type': 'POST',
				'async': false,
				'success': function(){
					$('#divEmail').slideUp('slow');
					$('#error_msg').empty();
					$('#msg_confirm').text('Thank you for sharing this video!').fadeIn( fadetime ).fadeOut( fadetime );
				}
			});
		}
		$('#btnShareIt').attr('disabled','');
	});
});
