/** 
 * Travel Auctions jQuery extension.
 */
(function($) {
	
	/**
	 * Create Travel Auctions jQuery extension.
	 */
	if( !$.fn.ta ) {
		$.fn.ta = function( name, options ) {
			if( this.ta[name] )
				return this.ta[name].call( this, options );
			return this;
		}
	}
	
	/**
	 * Creates a countdown timer for all selected elements.
	 */
	$.fn.ta.countdown = function( options ) {
		
		// Get local references
		var t = this;
		var d = $.fn.ta.diff;
		
		// Update the message for each item
		var update = function() {
			var loop = false;
			t.each( function() {
				
				// If this element does not have a date object then create one from the class name
				if( !this.oDate ) {
					this.oDate = this.className.split( " " ).pop();
					if( this.oDate.length > 10 ) {
						this.oDate = this.oDate.replace( "__", ", " ).replace( /-/g, ":" ).replace( /_/g, " " ) + "+0000";
						this.oDate = new Date( this.oDate );
					} else {
						this.oDate = "closed";
					}
				}
				
				// If there is no end date, skip it
				if( this.oDate == "closed" )
					return true;
				
				// Break up the time difference
				var date = new Date();
				var seconds = Math.floor( ( this.oDate.getTime() - date.getTime() + d ) / 1000 );
				var minutes = Math.floor( seconds / 60 );
				var hours = Math.floor( minutes / 60 );
				var days = Math.floor( hours / 24 );
				
				// Update the countdown
				var countdown = true;
				var text = "Ends: ";
				
				// If the auction has closed
				if( seconds <= 0 ) {
					text = "Bidding has closed";
					countdown = false;
					$(this).parent().addClass( "closed" ).find( ".bid" ).remove();
					
				// Otherwise update the countdown text
				} else if( days > 0 ) {
					text += ( days == 1 ? "Tomorrow" : ( days + " days" ) );
				} else if( hours > 3 ) {
					text += hours + " hours";
				} else if( hours >= 1 ) {
					minutes = minutes - ( hours * 60 );
					text +=
						hours + " hour" + ( hours == 1 ? "" : "s" ) + " and " +
						minutes + " minute" + ( minutes == 1 ? "" : "s" );
				} else {
					seconds = seconds - ( minutes * 60 );
					minutes = minutes - ( hours * 60 );
					text +=
						( hours == 0 ? "" : ( hours + " hour" + ( hours == 1 ? "" : "s" ) + ( minutes > 4 ? " and " : "" ) ) ) +
						( minutes == 0 ? "" : ( minutes + " minute" + ( minutes == 1 ? "" : "s" ) + ( minutes > 4 ? "" : " and " ) ) ) +
						( minutes > 4 ? "" : ( seconds + " second" + ( seconds == 1 ? "" : "s" ) ) );
				}
				
				// Update the text
				$(this).text( text );
				
				// Indicate if another loop needs to be made
				loop = ( loop || countdown );
				
			} );
			
			// If an update should take place again
			if( loop )
				setTimeout( function() { update(); }, 1000 );
			
		}
		
		// Update the messages now
		update();
		
		return t;
	};
	
	/**
	 * Setup the standard options.
	 */
	$.fn.ta.diff = 0;
	
	/**
	 * Initialises the countdown differential.
	 */
	$.fn.ta.init = function( options ) {
		if( options.server ) {
			var oServerDate = new Date( options.server );
			var oCurrentDate = new Date();
			$.fn.ta.diff = ( oCurrentDate.getTime() - oServerDate.getTime() );
		}
	};
	
})(jQuery);
