/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */

var broadcast = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#message-container .message-button').css({
				'padding-bottom': '2px'
			});
		}

		var title = $('#message-container .message-title').html();
		$('#message-container .message-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#message-container .message-content').animate({
						height: 280
					}, function () {
						$('#message-container .message-title').html(title);
						$('#message-container form').fadeIn(200, function () {
							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#message-container .message-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#message-container .message-send').click(function (e) {
			e.preventDefault();
			$('#message-container .message-loading').fadeIn(200, function () {
				$.ajax({
					url: 'broadcast.php',
					data: $('#message-container form').serialize() + '&action=ok',
					type: 'post',
					cache: false,
					dataType: 'json',
					success: function (data) {
						
						if (data.last == true) {
							dialog.data.fadeOut(200, function () {
								dialog.container.fadeOut(200, function () {
									dialog.overlay.fadeOut(200, function () {
										$.modal.close();
									});
								});
							});
						} else { 
							$('#message-container .message-loading').fadeOut(200, function () {
								$('#message-container .message-title').html(data.broadcast_title);
								$('#message-container .message-body').html(data.broadcast_body);
								$('#message-container #message_id').attr('value', data.broadcast_id);
								$('#message-container #user_id').attr('value', data.user_id);
							});	
						}
					}
				});
			});
		});
	},
	close: function (dialog) {
		$('#message-container form').fadeOut(200);
		$('#message-container .message-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	}
};
