PosterMaker = {
	queue_wait_time: null,
	show_processing: function() {
		// Get current language from the HTML tag:
		var language = $('html').attr('lang');
		// Set the processing message in the correct language:
		if (language == 'en') {
			var message = 'Processing, this will take a few moments...';
		} else if (language == 'fr') {
			var message = 'Traitement (cela peut prendre quelques minutes)...';
		}
		var box_height = $('#main-content').height()-100+22;
		// Inject a cover box with message and a throbber image into main content area and have it fade in:
		$('#main-content').prepend($('<div>')
			.attr('id','processing-wait-cover')
			.css({'height': box_height+'px'})
			.html(message)
			.append($('<br>'))
			.append($('<img>')
				.attr('src','/theme/images/throbber.gif')
				.attr('alt',message)
				.addClass('throbber')
			).fadeIn('fast')
		);
	},
	init_count_down: function(wait_time) {
		this.queue_wait_time = wait_time;
		// Update the counter in realtime every second:
		setTimeout('PosterMaker.update_wait_time();',1000);
		// Refresh data from the server every 5 seconds
		setTimeout('PosterMaker.update_queue_from_server();',5000);
	},
	update_wait_time: function() {
		this.queue_wait_time -= 1;
		if (this.queue_wait_time >= 0) {
			// Repeat the same logic from the PHP script to turn the number into a pretty time:
			var hours = 0;
			var minutes = 0;
			var seconds = 0;
			if (this.queue_wait_time >= 60*60) {
				hours = Math.floor(this.queue_wait_time/(60*60));
				remaining = this.queue_wait_time-(hours*60*60);
				minutes = Math.floor(remaining/60);
				seconds = remaining-(minutes*60);
			} else if (this.queue_wait_time >= 60) {
				hours = 0;
				minutes = Math.floor(this.queue_wait_time/60);
				seconds = this.queue_wait_time-(minutes*60);
			} else {
				hours = 0;
				minutes = 0;
				seconds = this.queue_wait_time;
			}
			if (hours < 10) {
				hours = '0'+hours;
			}
			if (minutes < 10) {
				minutes = '0'+minutes;
			}
			if (seconds < 10) {
				seconds = '0'+seconds;
			}
			var pretty_time = hours+':'+minutes+':'+seconds;
			// Update the div that displays the time
			$('#waiting-time-counter').html(pretty_time);
			// Continue the count-down
			setTimeout('PosterMaker.update_wait_time();',1000);
		}
	},
	update_queue_from_server: function() {
		$('#updating-throbber').show();
		Ajax.Request('standard',{
			url: '/production_queue',
			dataType: 'json',
			success: function(data) {
				$('#updating-throbber').hide();
				$('#status-column').html(data.wait_time_html+data.status_html);
				PosterMaker.queue_wait_time = data.wait_time;
				if (data.my_queue_count > 0) {
					setTimeout('PosterMaker.update_queue_from_server();',5000);
				}
			}
		});
	}
}

$(document).ready(function() {
	$('#drug-names').cycle({
		random: true
	});
	$('.expandable .expandable-content').hide();
	$('.expandable').addClass('collapsed');
	$('.expandable legend a').click(function() {
		var expandable_content = $(this).parent().next();
		var curr_display = expandable_content.css('display');
		if (curr_display == 'none') {
			$(this).parent().parent().removeClass('collapsed');
			expandable_content.slideDown('fast');
		} else {
			expandable_content.slideUp('fast', function() {
				$(this).parent().addClass('collapsed');
			});
		}
		$(this).blur();
		return false;
	});
	$('input.form-submit, input.form-button').mouseover(function() {
		$(this).addClass('hover');
	});
	$('input.form-submit, input.form-button').mouseout(function() {
		$(this).removeClass('hover');
	});
	$('#form-poster-maker').submit(function() {
		var is_valid = ($('#form-poster-maker-is-valid').val() == 'true');
		if (!is_valid) {
			new Ajax.FormValidator('form-poster-maker');
			return false;
		}
		PosterMaker.show_processing();
		return true;
	});
	$('#form-access-retrieval').submit(function() {
		var is_valid = ($('#form-access-retrieval-is-valid').val() == 'true');
		if (!is_valid) {
			new Ajax.FormValidator('form-access-retrieval');
			return false;
		}
		return true;
	});
	$('.delete-button').click(function() {
		var my_label = $(this).attr('rel');
		if (my_label == '') {
			my_label = 'the selected item';
		} else {
			my_label = 'the item "'+my_label+'"';
		}
		if (!confirm('Are you sure you want to delete '+my_label+'?')) {
			return false;
		}
	});
	$('.queue-remove-button').click(function() {
		var my_label = $(this).attr('rel');
		if (my_label == '') {
			my_label = 'the selected item';
		} else {
			my_label = 'the item "'+my_label+'"';
		}
		if (!confirm('Are you sure you want to remove '+my_label+' from the queue?')) {
			return false;
		}
	});
});
if(typeof sIFR == "function") {
  sIFR.replaceElement("#page-title h1", named({sFlashSrc: "/theme/PFFuelPro.swf", sColor: "#7d0a09", sLinkColor: "#000000", sHoverColor: "#666666", sBgColor: "#FFFFFF", sWmode: "transparent", sFlashVars: "textalign=left"}));
  sIFR.replaceElement("#body-content #poster-text-item h2", named({sFlashSrc: "/theme/PFFuelPro.swf", sColor: "#a60100", sLinkColor: "#000000", sHoverColor: "#666666", sBgColor: "#FFFFFF", sWmode: "transparent", sFlashVars: "textalign=left"}));
  sIFR.replaceElement("#body-content h2", named({sFlashSrc: "/theme/PFFuelPro.swf", sColor: "#ffffff", sLinkColor: "#000000", sHoverColor: "#666666", sBgColor: "#FFFFFF", sWmode: "transparent", sFlashVars: "textalign=left"}));
};

