window.show_default = function() {
	reset_listing();
	$('#info_pane > *').hide();
	$('#top10').show();
};

//(function() {
	// Current position in the array.
	var thumbnail_position = 0;
	
	// Responsibilities
	//   - Manage order of candidates, including
	//     the navigation of the "current" candidate
	var EntryNavigator = function() {
		this.showing = null;
		this.thumbs = undefined;

		var entry_nav = this; // for closure
	}

	EntryNavigator.prototype = {
		show : function(entry_id) {
			//
		},
		hide : function() {
	
		},
		next : function() {
	
		},
		previous : function() {
	
		}
	};

	var reset_listing = function() {
		$(".entry_detail_a").css("background-color","");
		$("#badge_pane").html("");
	};

	var get_index = function( id ) {
		var arr = $('.entry_detail_a').get();
		for( var x = 0; x < arr.length; x++ ) {
			if( id == $( arr[x] ).attr("jtvdctn") ) {
				return x;
			}
		}
		return -1;
	};

	var correct_index = function( idx_in ) {
		if( idx_in < 0 ) return 49;
		if( idx_in > 49 ) return 0;
		return idx_in;
	};
	
	// Bind the navigator to the controls
	var show_details = function( idx_in ) {
		reset_listing();

		if( typeof( idx_in ) == 'object' ) {
			thumbnail_index = $(".entry_detail_a").index( this );
		}
		else {
			thumbnail_index = idx_in;	
		}
		
		var elem = $(".entry_detail_a").get( thumbnail_index );
		var id = $( elem ).attr("jtvdctn");

		// outline
		$( elem ).css("background-color","#664A51");
	
		// add the top ten badge if they are in the top ten
		var found = $("a[jtvdc='" + id + "']").length;
		if( found > 0 ) {
			$("#badge_pane").html("<img src='/images/vv/top_ten_badge.gif'/>");
		}
		else {
			$("#badge_pane").html("");
		}
		
		var url = $( $(".entry_detail_a").get( thumbnail_index ) ).attr("href");
		
		$('#details_display').hide();
		$('.entry_details').css("background","#fff url(images/wait.gif) center no-repeat");
	
		$('#details_display').load( url + " .candidate_detail", null, function() {
			$('#info_pane > *').hide();

			// Set the vote button.
			$("#details_buttons .vote_button").click( show_vote_dialog );
			has_voted();
			
			// Set the next, previous and bio buttons.
			$("#details_buttons .prev_button").click( show_prev_entry );
			$("#details_buttons .next_button").click( show_next_entry );
			$(".entrant_bio_link a:first").click( show_bio );

			$('.entry_details').css("background","#fff").show();	
			$('#details_display').show();
		});
		
		return false;
	};

	var set_form = function() {
		// Show errors if they exist.
		var regexp = new RegExp("none","gim");
		var error_elem = $("#vote_content .error_message").length;

		if( error_elem > 0 ) {
			if( $("#vote_content .error_message").html().match(/none/gi) == null ) {
				$(".error_message").show();
			}
		}

		$(".modalClose").bind('click',function() {
			$.modal.close();
			$("#vote_content").html("");
			$("#vote_content_hidden");
			return false;
		});

		$("#vote_form").ajaxForm({target: '#vote_content',
			success: function(rt,dt) {
				$(".vote_form_container").css("left", "60%").css("top","10px");
				set_form();
			}
		});
/*
		$("#vote_form").ajaxForm({
			target: '#vote_content_hidden',
			success: function(rt,dt) {
				// $.modal.close();
				$("#vote_content").html( "<div class='vote_form_container'>" + $("#vote_content_hidden .vote_form_container").html() + "</div>" );
				$(".vote_form_container").css("left","60%").css("top","10px");
				show_modal("#vote_content",415,390);
				set_form();
			}
		});
*/
	};
	
	var show_vote_dialog = function() {
		$("#vote_content").load( $(this).attr("href") + " .vote_form_container",null,function(rt,ts,xhr) {
			show_modal("#vote_content",415,390);
			set_form();
		});

		return false;
	};
	
	var show_next_entry = function() {
		var new_index = correct_index( thumbnail_index + 1 );
		show_details( new_index );
		return false;
	};
	
	var show_prev_entry = function() {
		var new_index = correct_index( thumbnail_index - 1 );
		show_details( new_index );
		return false;
	};

	var show_bio = function( e ) {
		$("#bio_content").load( $(this).attr("href") + " .bio_detail",null,function() {
			$('#bio_content').modal();
		});
		
		return false;
	};

	var dialog = null;

	var show_modal = function( div, width, height ) {
		// calc the center position of the close button.
		var b_top = height - 28;
		var b_left = ( width / 2 ) - 24;

		// this could probably be accomplished in a different way.
		$( div ).modal( { 
			containerCss: { "width": width, "height": height }, 
			onShow: function(dlg) {
				dialog = dlg;
				$("#modalContainer a.modalCloseImg").css("top",b_top);
				$("#modalContainer a.modalCloseImg").css("left",b_left);		
			},
			onClose: function(dlg) {
				$("#vote_content").html("");
				$("#vote_content_hidden").html("");
				$.modal.close();
			}
		} );			
	};

	var flag_vote = function() {
		var now = new Date();
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);
		var d = now.getDate();
		d++;		
		now.setDate( d );

		document.cookie = "jtvdc=1;expires="+now.toGMTString();
		has_voted();
	};

	var check_vote = function() {
		var c_name = 'jtvdc';

		if( document.cookie.length > 0 ) {
			c_start = document.cookie.indexOf( c_name );
			if( c_start != -1 ) { 
				c_start = c_start + c_name.length + 1; 
				c_end = document.cookie.indexOf( ";",c_start );
				if( c_end==-1 ) c_end = document.cookie.length;
				return unescape( document.cookie.substring( c_start,c_end ) );
			} 
		}

		return "";
	};

	var has_voted = function() {
		var val = check_vote();
		if( val == '1' ) {
			$('.vote_button img:first').attr('src','/images/vv/thanks_for_voting_button.gif');
			$('.vote_button').attr('href','javascript:void(0);');
			$('.vote_button').bind('click', ignore_click );
			$('.vote_button img:first').bind('click', ignore_click );
		}
	};
	
	var ignore_click = function() {
		return false;
	}

        $('#winners a').click( function() {
		var newWin = window.open( $(this).attr("href"), 'wpu', "resizable,toolbar=no,location=no,scrollbars=no,width=416,height=350" );
                newWin.focus();
        	return false;
	});
  
	$("a[jtvdc]").click( function() {
		show_details( get_index( $(this).attr("jtvdc") ) );
		return false;
	});

	$(".entry_detail_a").click(show_details);
//})();
