	
	
	$(document).ready(function() {
		i = 1;
		
		// user rated
		if ( typeof(user_rated) == 'undefined' ) 
			user_rated = 0;
		
		if ( user_rated > 0 ) {
			user_rated = parseInt( user_rated );
			
			tmp = 1;
			$('a[rel="vote_img"]').each(function() {
				if ( tmp <= user_rated  ){
					$(this).attr('class', 'starF' );
					tmp++;
				}
				$(this).click(function(){return false;});
			});
		}else{
			// vote images
			$('a[rel="vote_img"]').each(function() {
				$(this).attr('id', i );
				i++;
				
				// on mouse over
				$(this).mouseover(function(){
					id = $(this).attr('id');
					for ( j = 1 ; j <= id ; j++ ) {
						$('a[id="' + j + '"]').attr('class', 'starF' );
					}
				});
				// on mouse out
				$(this).mouseout(function(){
					id = $(this).attr('id');
					for ( j = 1 ; j <= id ; j++ ) {
						$('a[id="' + j + '"]').attr('class', 'starE' );
					}
				});
				
				// on click, save the vote
				$(this).click(function(){
					no_history_server_post( { m : 'vote_show' , n : $(this).attr('id') , id : show_id } , user_voted );
					return false;
				});
			});
		}
		
		$('#build_uploaded_videos').prepend( $('#build_uploaded_videos #simple_pagination').clone() );
		$('#build_recorded_videos').prepend( $('#build_recorded_videos #simple_pagination').clone() );
		
		$('#flag_reason_trigger').click(function(){
			$('#flag_reason').toggle();
			return false;
		});
		
		$('#flag_send').click(function(){
			no_history_server_post( { m : 'flag_show', id : $('[name=flag_reason]').val(), show_id:show_id }, user_flaged );
		});
		
		$('#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;});
		
		// 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_show_comment', comment : $('#new_comment').val() , id : show_id } , add_show_comment );
				$('#comments_loader').hide();
			}
			return false;
		});
		
		// follow
		$('#follow').click(function(){
			no_history_server_post( { m : 'follow', action : 'follow', id : show_id }, follow_callback );
			return false;
		});
		$('#stop_follow').click(function(){
			no_history_server_post( { m : 'follow', action : 'stop_follow', id : show_id }, stop_follow_callback );
			return false;
		});
		
		$('#new_comment').keyup(function(){
			v = $(this).val(); x = v.length; 
			if ( x < 1 ) x = '0';
			$('#no_chars').html(x);
		});
		
	});
	
	// follow
	function follow_callback( data ) {
		if ( data['err'] ){
			display_error( data['err'] );
		}else{
			$('#follow_div').hide();
			$('#stop_follow_div').show();
		}
	}
	function stop_follow_callback( data ) {
		if ( data['err'] ){
			display_error( data['err'] );
		}else{
			$('#follow_div').show();
			$('#stop_follow_div').hide();
		}
	}
	
	// the user has flaged a show. change the page .. .
	function user_flaged( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			// do html changes
			//$('#flag_reason_trigger').remove();
			$('#flag_reason').empty().append('Thank you for your interest.');
		}
	}
	
	// the user has voted. change the page .. .
	function user_voted( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			// do html changes
			$('div.img_show_rate_d').empty().append( data['server']['images'] );
			$('#img_your_rate').empty().append('Thank you for your vote.');
		}
	}
	
	
	
	$(window).load(function() {
		if ( typeof(page) != 'undefined' ) build_box_content( page , 'build_show_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');
	});
	
	// build comments section
	function build_box_content( lpage , type )
	{
		page = lpage;
		
		$('#' + type).empty().append( loader() );
		
		no_history_server_post( { m : type , page : lpage , show_id : show_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();
		} );
	}
	
	//keep compatibility with pagination
	function build_show_comments( lpage){ build_box_content( lpage , 'build_show_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' ) }

	
	// adds a comment to the comment section of the show, and refreshes the comments
	function add_show_comment( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			build_box_content( 1, 'build_show_comments' );
			$('#new_comment').val('');
		}
	}
	
	function delete_comment(id)
	{
		if ( confirm('Are you sure you want to delete this comment ? ') ) {
			no_history_server_post( { m:'delete_show_comment', id:id },function() {
				build_box_content( page, 'build_show_comments' );
			});
		}
		return false;
	}
	
	function showJustStarted(){
		$('#schdule_annonce').next().hide();
		$('#schdule_annonce').hide();
	}

	