/*
 * 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 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#rhubForm #btnJoinMeet').click(function (e) {
		e.preventDefault();
		// load the rhub form using ajax
		$.get("blocks/rhub_meeting/data/rhub.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'rhub-overlay',
				containerId: 'rhub-container',
				onOpen: rhub.open,
				onShow: rhub.show,
				onClose: rhub.close
			});
		});
	});

	// preload images
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'img/rhub/' + this;
	});
});

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

		// dynamically determine height
		var h = 280;
		if ($('#rhub-subject').length) {
			h += 26;
		}
		if ($('#rhub-cc').length) {
			h += 22;
		}

		var title = $('#rhub-container .rhub-title').html();
		$('#rhub-container .rhub-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#rhub-container .rhub-content').animate({
						height: h
					}, function () {
						$('#rhub-container .rhub-title').html(title);
						$('#rhub-container form').fadeIn(200, function () {
							$('#rhub-container #rhub-name').focus();

							$('#rhub-container .rhub-cc').click(function () {
								var cc = $('#rhub-container #rhub-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#rhub-container .rhub-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) {
		$('#rhub-container .rhub-send').click(function (e) {
			e.preventDefault();
			// open meeting gateway
			//alert($('#rhub-container form').serialize());
			rhub_url = "http://"+rhub_host+"/as/wapi/goto_downloader?role=attendee&sumbit=submit&"+$('#rhub-container form').serialize();
			window.open(rhub_url);
      $.modal.close();
		});
	},
	close: function (dialog) {
		$('#rhub-container .rhub-message').fadeOut();
		$('#rhub-container .rhub-title').html('Goodbye...');
		$('#rhub-container form').fadeOut(200);
		$('#rhub-container .rhub-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		//Not validate any value
			return true;
	},
	showError: function () {
		$('#rhub-container .rhub-message')
			.html($('<div class="rhub-error">').append(rhub.message))
			.fadeIn(200);
	}
};
