	
	
	$(document).ready(function(){
		i = 1;
		
		user_rated = parseInt( user_rated );
		
		if ( user_rated > 0 ) {
			tmp = 1;
			$('img[rel="vote_img"]').each(function() {
				if ( tmp <= user_rated  ){
					$(this).attr('src', www_root + 'public/images/star_white_full.png' );
					tmp++;
				}
			});
		} else {
			// vote images
			$('img[rel="vote_img"]').each(function() {
				$(this).attr('id', i );
				i++;
				// on mouse over
				$(this).mouseover(function(){
					id = $(this).attr('id');
					for ( j = 0 ; j <= id ; j++ ) {
							$('img[id="' + j + '"]').attr('src', www_root + 'public/images/star_white_full.png' );
					}
				});
				// on mouse out
				$(this).mouseout(function(){
					id = $(this).attr('id');
					for ( j = 0 ; j <= id ; j++ ) {
							$('img[id="' + j + '"]').attr('src', www_root + 'public/images/star_white_empty.png' );
					}
				});
				
				// on click, save the vote
				$(this).click(function(){
					no_history_server_post( { m : 'vote_video' , n : $(this).attr('id') , id : video_id } , user_voted );
				});
			});
		}
		
		$('#bbutyes').click(function(){ $('#bbut_frm').submit(); return false;});
		$('#bbutno').click(function(){ $('#bbut').show(); $('#buy_confirm').hide();	return false;});
		$('#bbut').click(function(){ $(this).hide(); $('#buy_confirm').show('fast');return false;});
		
                $('#build_uploaded_videos').prepend( $('#build_uploaded_videos #simple_pagination').clone() );
        $('#build_recorded_videos').prepend( $('#build_recorded_videos #simple_pagination').clone() );
        
        // new comment
        $('#add_new_comment').click(function() {
            v = $('#new_comment').val();
            if ( $('#new_comment').val() == '' )
                display_error( 'Please write a comment.' );
            else if ( v.length > 500 )
                display_error( 'Comment length must have MAXIMUM 500 chars.' );
            else{
                $('#comments_loader').show();
                no_history_server_post( { m : 'add_video_comment', comment : $('#new_comment').val() , id : video_id } , add_video_comment );
                $('#comments_loader').hide();
            }
            return false;
        });
        
        $('#new_comment').keyup(function(){
            v = $(this).val(); x = v.length; 
            if ( x < 1 ) x = '0';
            $('#no_chars').html(x);
        });
        
        if ( typeof(page) != 'undefined' ) build_box_content( page , 'build_video_comments');
        if ( typeof(u_page) != 'undefined' && u_page > 1 ) build_box_content( u_page , 'build_uploaded_videos');
        if ( typeof(r_page) != 'undefined' && r_page > 1 ) build_box_content( r_page , 'build_recorded_videos');

	});
	
	// the user has voted. change the page .. .
	function user_voted( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			// do html changes
			$('div[class="img_show_rate"]').empty().append( data['server']['images'] );
			$('#img_your_rate').empty().append('Thank you for your vote.');
		}
	}
	
	    // adds a comment to the comment section of the video, and refreshes the comments
    function add_video_comment( data )
    {
        if ( data['err'] )
            display_error( data['err'] );
        else {
            build_video_comments( 1 );
            $('#new_comment').val('');
        }
    }

    function build_video_comments( lpage){ build_box_content( lpage , 'build_video_comments' ) }
    function build_uploaded_videos( lpage){ build_box_content( lpage , 'build_uploaded_videos' ) }
    function build_recorded_videos( lpage){ build_box_content( lpage , 'build_recorded_videos' ) }
    
    function build_box_content( lpage , type )
    {
        page = lpage;
        $('#' + type).empty().append( loader() );
        
        no_history_server_post( { m : type , page : lpage , video_id : video_id } , function( data ) {
            if ( data['err'] ) {
                display_error( data['err'] );
            }else if ( data['server'] && data['server'][type] ){
                $('#' + type ).empty().append( data['server']['pagination'] );
                $('#' + type ).append( data['server'][type] ).append( data['server']['pagination'] );
            }
            
            if ( typeof do_footer == 'function' )
                do_footer();
        } );
    }
    
    function delete_comment(id)
    {
        if ( confirm('Are you sure you want to delete this comment ? ') ) {
            no_history_server_post( { m:'delete_video_comment', id:id },function() {
                build_video_comments( page );
            });
        }
        return false;
    }
