// JavaScript Document


function get_comments()
{
	$('#comm-box').fadeOut('slow');
	var vid = $('#video_id').attr('value');
	$.post("index.php?eID=contest_ajax&action=get_comments", { video_id: vid, offset: current_comment_page},
	  function(data){ 
		$('#comm-box').html(data);
		$('#comm-box').fadeIn('slow');
		attach_functions(); 
	  }, 'html'
	);	
}

function get_prev_comments()
{	
	current_comment_page = current_comment_page - 1;
	get_comments();
}

function get_next_comments()
{	
	current_comment_page = current_comment_page + 1;
	get_comments();
}

function attach_functions()
{
	$('#next_comments').bind('click', function(){get_next_comments();});
	$('#prev_comments').bind('click', function(){get_prev_comments();}); 
}

$(document).ready(function() {
	attach_functions();
	
	$('#more a').click(function() {
		var description = $('#description_full').html();
		$('#description_trunc').html(description);		
		$('#more').hide();
	});
	
	$('#show_recommend_box').click(function() {
		$('#recommend-box').slideToggle('slow');
	});
	
	$('#show_comment_box').click(function() {
		$('#comment-box').slideToggle('slow');
	});	
	
	$('#select-video-arrowleft').click(function() {
		$('#small_video_list').hide();
		$('#small-video-spinner').show();
		if (breaker != 0)
		{
			current_page = breaker + 1;
			breaker = 0;
		}
		$.post("index.php?eID=contest_ajax&action=get_videos", { page: current_page - 1, order: current_order},
		  function(data){
		  	$('#small-video-spinner').hide();
			$('#small_video_list').html(data);
			$('#small_video_list').fadeIn('slow');					
			current_page=current_page - 1;
			if (current_page < 0)
			{
				current_page = 1;
			}
		  }, 'html'
		);		
	});
	
	$('#select-video-arrowright').click(function() {
		saved_content = $('#small_video_list').html();
		$('#small_video_list').hide();
		$('#small-video-spinner').show();
		$.post("index.php?eID=contest_ajax&action=get_videos", { page: current_page + 1, order: current_order},
		  function(data){
		  	$('#small-video-spinner').hide();
			$('#small_video_list').html(data);
			$('#small_video_list').fadeIn('slow');
			if (data != '')
			{
				current_page=current_page + 1;
			} else {
				$('#small_video_list').html(saved_content);
			}			
		  }, 'html'
		);		
	});	
	
	$('.rate-stars').click(function() {
		var selected = $(this).attr('name');
		var rating_selected = 0;
		switch (selected)
		{
			case 'vote-1': rating_selected = 1; break;
			case 'vote-2': rating_selected = 2; break;			
			case 'vote-3': rating_selected = 3; break;
			case 'vote-4': rating_selected = 4; break;
			case 'vote-5': rating_selected = 5; break;		
			default: rating_selected = 1;
		}
		
		var vid = $('#video_id').attr('value');
		$('#rate-count').html('Speichere...');
		$('#rate-count').attr('class', '');
		$.post("index.php?eID=contest_ajax&action=vote", { video_id: vid, rating: rating_selected},
		  function(data){
	      	if (data.status == 'error')
			{
				$('#rate-count').attr('class', 'error');
			} else {
				$('#rate-count').attr('class', 'success');
			}
			$('#rate-count').html(data.message);			
		  }, 'json'
		);
	});		
	
    var options_recommend = { 
        target:        '#recommend-response',   // target element(s) to be updated with server response 
		beforeSubmit:  prepareRecommendBox,
        success:       processRecommendJson,  // post-submit callback 
        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
    }; 
    
    var options_comment = { 
        target:        '#comment-response',   // target element(s) to be updated with server response 
		beforeSubmit:  prepareCommentBox,
        success:       processCommentJson,  // post-submit callback 
        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
    };     
 
    // bind form using 'ajaxForm' 
    $('#recommend-form').ajaxForm(options_recommend); 
    $('#comment-form').ajaxForm(options_comment); 
	
	function prepareRecommendBox() {
		$('#recommend-response').removeClass('error');				
		$('#recommend-response').removeClass('success');	
		$('#recommend-response').html('');
		$('#recommend-response').hide();
		$('#recommend-spinner').show();
	}

	function processRecommendJson(data) {
		$('#recommend-spinner').hide();		
		// 'data' is the json object returned from the server 		
		if (data.status == 'error')
		{
			$('#recommend-response').addClass('error');
		} else {
			$('#recommend-response').addClass('success');
			$('#recommend-form')[0].reset();
		}
		$('#recommend-response').html(data.message);		
		$('#recommend-response').fadeIn('slow');
	}

	function prepareCommentBox() {
		$('#comment-response').removeClass('error');			
		$('#comment-response').removeClass('success');
		$('#comment-response').html('');
		$('#comment-response').hide();
		$('#comment-spinner').show();
	}

	function processCommentJson(data) {
		$('#comment-spinner').hide();		
		// 'data' is the json object returned from the server 		
		if (data.status == 'error')
		{
			$('#comment-response').addClass('error');
		} else {
			$('#comment-response').addClass('success');
			$('#comment-form')[0].reset();
			get_comments();
		}
		$('#comment-response').html(data.message);		
		$('#comment-response').fadeIn('slow');
	}	
	
});
