﻿$( document ).ready( function() {
    $( '#movie-tabs-menu li' ).hover(
        function() { $(this).addClass( 'over' ) },
        function() { $(this).removeClass( 'over' ) }
    );
} );

$( document ).ready( setupCommentPaging );

function setupCommentPaging() {
	$( '#comment-paging a' ).click( loadComments );
	$( '#movie-tabs-menu li.defaultCommentPage').click(loadComments );
	setLinkDisplay();
}
function loadComments( event ) {
	event.preventDefault();
	
	var command = $(this).attr( 'class' );
	
	if ( command == 'first' ) { currentPage = 0; }
	if ( command == 'defaultCommentPage' ) { currentPage = 0; }
	if ( command == 'previous' ) { currentPage--; }
	if ( command == 'next' ) { currentPage++; }
	if ( command == 'last' ) { currentPage = totalCommentPages; }
	setLinkDisplay();
	
	// don't duplicate the load if we're at a boundary
	if ( currentPage < 0 ) { currentPage = 0; return; }
	if ( currentPage > totalCommentPages ) { currentPage = totalCommentPages; return; }
	
	var holder = $( '#divUserReviewList' );
	if (currentPage == 0) $( '#load-comments' ).css( 'display', 'block' );
	
	holder.load( '/data/userreviewlist.aspx?movieid=' + movieid + '&p=' + currentPage, '', commentsLoaded() );
}
function commentsLoaded() {
    $( '#comment-paging' ).css( 'display', 'none' );
    if (currentPage == 0) setTimeout("showComments()", 750);
    else showComments();
}

function showComments() {
	$( '#load-comments' ).css( 'display', 'none' );
	$( '#comment-paging' ).css( 'display', 'block' );
}
function setLinkDisplay() {
	// hide items
	var backDisplay = ( currentPage <= 0 ) ? '#999' : '#c00';
	var forwardDisplay = ( currentPage >= totalCommentPages ) ? '#999' : '#c00';
	var showPagers = totalCommentPages > 1;
	
	if (!showPagers) {
        $( '#toppager' ).css( 'display', 'none' );
        $( '#bottompager' ).css( 'display', 'none' );
	    return;    
	}
	
	$( '#comment-paging a.first' ).css( 'color', backDisplay );
	$( '#comment-paging a.previous' ).css( 'color', backDisplay );
	$( '#comment-paging a.next' ).css( 'color', forwardDisplay );
	$( '#comment-paging a.last' ).css( 'color', forwardDisplay );
	
	if ( currentPage <= 0 ) {
	    $( '#comment-paging a.first' ).css( 'text-decoration', 'none' );
	    $( '#comment-paging a.previous' ).css( 'text-decoration', 'none' );
	    $( '#comment-paging a.first' ).css( 'cursor', 'default' );
	    $( '#comment-paging a.previous' ).css( 'cursor', 'default' );
	}
	else {
	    $( '#comment-paging a.first' ).css( 'text-decoration', 'underline' );
	    $( '#comment-paging a.previous' ).css( 'text-decoration', 'underline' );
	    $( '#comment-paging a.first' ).css( 'cursor', '' );
	    $( '#comment-paging a.previous' ).css( 'cursor', '' );	
	}
	
	if ( currentPage >= totalCommentPages ) {
	    $( '#comment-paging a.next' ).css( 'text-decoration', 'none' );
	    $( '#comment-paging a.last' ).css( 'text-decoration', 'none' );
	    $( '#comment-paging a.next' ).css( 'cursor', 'default' );
	    $( '#comment-paging a.last' ).css( 'cursor', 'default' );
	}
	else {
	    $( '#comment-paging a.next' ).css( 'text-decoration', 'underline' );
	    $( '#comment-paging a.last' ).css( 'text-decoration', 'underline' );
	    $( '#comment-paging a.next' ).css( 'cursor', '' );
	    $( '#comment-paging a.last' ).css( 'cursor', '' );	
	}	
	
	
	var userPageNumber = (parseInt(currentPage) + 1);
	var userTotalPages = (parseInt(totalCommentPages) + 1);
	var pagerText = "Page " + userPageNumber + " of " + userTotalPages;
	$( '#showPaging' ).text(pagerText);
	$( '#showPaging2' ).text(pagerText);
}

function loadInitialComments() {
	var command = $(this).attr( 'class' );
	var holder = $( '#divUserReviewList' );
	holder.load( '/data/userreviewlist.aspx?movieid=' + movieid + '&p=0', '', commentsLoaded() );
}