/*
	Copyright (c) JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
*/

(function ($) {

var buttons = {
		mb_button_continue: {
			src: 'mb-button-button.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Continue'
		},
		mb_button_yes: {
			src: 'mb-button-yes.png',
			width: 85,
			height: 25,
			redirect: true,
			alt: 'Yes'
		},
		mb_button_promote: {
			src: 'mb-button-promote.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Promote'
		},
		mb_button_demote: {
			src: 'mb-button-demote.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Demote'
		},
		mb_button_archive: {
			src: 'mb-button-demote.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Archive'
		},
		mb_button_unarchive: {
			src: 'mb-button-unarchive.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Unarchive'
		},
		mb_button_subscribe: {
			src: 'mb-button-subscribe.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Subscribe'
		},
		mb_button_unsubscribe: {
			src: 'mb-button-unsubscribe.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Unsubscribe'
		},
		mb_button_delete: {
			src: 'mb-button-delete.png',
			width: 101,
			height: 25,
			redirect: true,
			alt: 'Delete'
		},
		mb_button_cancel: {
			src: 'mb-button-cancel.png',
			width: 101,
			height: 25,
			redirect: false,
			alt: 'Cancel'
		},
		mb_button_no: {
			src: 'mb-button-no.png',
			width: 85,
			height: 25,
			redirect: false,
			alt: 'No'
		}
	},
	
	structure = [
		'mb-top-left',
		'mb-top-middle',
		'mb-top-right',
		'mb-middle-left',
		'mb-middle-right',
		'mb-bottom-left',
		'mb-bottom-middle',
		'mb-bottom-right',
		'mb-shadow-top',
		'mb-shadow-left',
		'mb-shadow-right',
		'mb-shadow-bottom'
	],
	
	mb = $('<div id="mb" />'),
	mbContent = $('<div id="mb-content" />')
		.appendTo(mb),
	
	_getButton  = function (key, successUrl) {
		var spec = buttons[key];
 		return $('<img />')
				.attr('src', '/img/' + spec.src)
				.attr('width', spec.width)
				.attr('height', spec.height)
				.click(function () {
					window.closeMessageBox();
					if (spec.redirect) {
						window.location.assign(successUrl);
					}
				});
	};
	
// Build the message box.
$.each(structure, function () {
	mb.append('<div id="' + this + '" />');
});

// Global functions. In the context of an anonymous function, this refers
// to window, i.e. the global scope.

this.closeMessageBox = function () {
	if (mb.css('display') == 'block') {
		mb.fadeOut('fast');
	}
};

this.showMessageBox = function (heading, text, form, buttons, successUrl) {
	
	// Centre in the middle of the window
	// TODO: Does jQuery have better abstractions for finding these values?
	
	var windowHeight = window.innerHeight
		? window.innerHeight
		: document.documentElement.clientHeight
			? document.documentElement.clientHeight
			: document.body.clientHeight;
	
	var windowWidth  = window.innerWidth
		? window.innerWidth
		: document.documentElement.clientWidth
			? document.documentElement.clientWidth
			: document.body.clientWidth;
	
	// However, if we are in IE6 then we're using an absolute positioning system
	// to emulate the fixed positioning system in safari\firefox.
	// So use an absolute left value, as width of body element is known
	if ($.browser.msie && $.browser.version == '6.0') {
		windowWidth = 968;
	}
	
	mb.css({
			left: windowWidth  / 2 - 256,
			top:  windowHeight / 2 - 111
		});
	
	mbContent.empty();
	
	if (heading) {
		$('<h2 />')
			.text(heading)
			.appendTo(mbContent);
	}
	
	if (text) {
		$('<p />')
			.text(text)
			.appendTo(mbContent);
	}
	
	if (form) {		
		mbContent.append(form);
	} else {
		var container = $('<div id="button-container" />').appendTo(mbContent);
		$.each(buttons, function () {
			container.prepend(_getButton(this, successUrl));
		});
	}
	
	if (mb.css('display') == 'none') {
		mb.fadeIn('fast');
	}
};
	
// The created message box widget is appended to the main content.
// TODO: Image preloading, although doesn't seem neccesary since the background
// images in CSS files are usually downloaded.
$(function () {
	$('#main .middle').append(mb);
});
	
})(jQuery);
