/**
 * PeopleSet object
 * @param {Object} data
 */
function PeopleSet( data ) {
	if (data.people) {
		this.people = data.people;
	}
	this.ptr = 0;
	this.len = this.people.length;
}
PeopleSet.prototype.get = function( key ) {
	var temp = null;
	var tempPtr = 0;
	switch( key ) {
		case 'empty' :
			if( this.people.length == 0 ) {
				temp = true;
			} else { 
				temp = false;
			}
			break;
		case 'next':
			this.ptr++; 
			if( this.ptr > this.people.length ) {
				this.ptr = 0;
				return null;
			}
			tempPtr = this.ptr - 1;
			temp = this.people[ tempPtr ];
			temp.pid = tempPtr;
			break;
		case 'random':
			tempPtr = Math.floor(Math.random() * this.len);
			if( this.people[ tempPtr ].removed && this.people[ tempPtr ].removed == true ) {
				temp = this.get( 'random' );
			} else {
				temp = this.people[ tempPtr ];
				temp.pid = tempPtr;
			}
			break;
		default: 
			break;
	}
	return temp;
}
PeopleSet.prototype.remove = function( index ) {
	this.people[ index ].removed = true;
}

/**
 * Game object
 * @param {Object} data
 */
function Game( data ) {
	this.people = new PeopleSet( data );
	this.curMatch = null;
	this.choices = 0;
	this.populateSelectPane();
	this.updateMatchPane();
	$('#possible').text( this.choices );
}
Game.prototype.checkMatch = function( chosen ) {
	if( chosen == this.curMatch.pid ) {
		/**
		 * If correct:
		 * -Update status
		 * -Update score
		 */
		this.deactiveChosen( chosen );
		this.updateStatus( 'Correct!' );
		this.showNextButton();
		this.updateMessage( this.curMatch.mesg + '<br />&nbsp;&nbsp;&nbsp;&nbsp;-' + this.curMatch.name );
		this.updateScore( 1 );
		this.people.remove( this.curMatch.pid );
		this.choices--;
	} else {
		/* if incorrect */
		this.updateStatus( 'Incorrect.' );
		this.updateScore( 0 );
	}
}
Game.prototype.updateScore = function( points ) {
	$( '#points' ).text( parseInt( $( '#points' ).text() ) + points );
	$( '#tries' ).text( parseInt( $( '#tries' ).text() ) + 1 );
}
Game.prototype.updateStatus = function( mesg ) {
	$( '#statusPane' ).text( mesg );
}
Game.prototype.showNextButton = function(){
	$( '#nextPane' ).text( 'Next' );
	$( '#nextPane' ).bind( 'click', function() {
		g.nextAction();
	});
}
Game.prototype.nextAction = function() {
	/* if the game is done */
	if ( this.choices <= 0 ) {
		g.updateStatus( '');
		$( '#messagePane' ).hide( 'slow' );
		$( '#messagePane' ).html( '' );
		$('#winPane').html( '<img src="img/MG_xmas_goodbye_sign.png" alt="" />' );
		$( '#winPane' ).animate({
			'top': '120px'
		}, 2000 );
		$( '#nextPane' ).text( '' );
	} else {
		g.updateStatus( ' ' );
		g.updateMatchPane();
	}
}
Game.prototype.updateMessage = function( mesg ) {
	$( '#messagePane' ).show( 'slow' );
	$( '#messagePane' ).html( mesg );
}
Game.prototype.updateMatchPane = function() {
	$( '#messagePane' ).hide( 'slow' );
	$( '#messagePane' ).text( '' );
	$( '#nextPane' ).text( '' );
	var photo = this.people.get( 'random' );
	this.curMatch = photo;
	$( '#matchDrop' ).css( 'background', 'url( img/xmaspics/' + photo.match + ') top left no-repeat' );
	
}
Game.prototype.populateSelectPane = function() {
	var photo = null;
	var html = '';
	while( photo = this.people.get( 'next' ) ) {
		this.choices++;
		html += '<img id="' + photo.pid + '" class="selectable thumbnail" src="img/headshots/' + photo.select + '" alt="" />';
	}
	$( '#selectPane' ).html( html );
	$( '.selectable' ).bind( 'click', function( e ) {
		g.checkMatch( this.id );
	});
}
Game.prototype.deactiveChosen = function( idx ) {
	$( '#' + idx ).fadeTo( 'slow', 0.33 );
	$( '#' + idx ).unbind( 'click' );
	$( '#' + idx ).removeClass( 'selectable' );
}

var g;
$(document).ready( function() {
	$.getJSON( 'respHandler.php', { 'init': '1' },
		function( data ) { g = new Game( data ); 
	});
});

