(function($){
	$(document).ready(function () {
		// about us popup
		$('#aboutus,#aboutus2').click(function (e) {
			e.preventDefault();
			// load the contact form using ajax
			$.get("http://www.peaceinislam.com/wp-content/plugins/aboutus-contractus/aboutus.php", function(data){
				// create a modal dialog with the data
				$(data).modal({
					closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
					position: ["7%",],
					overlayId: 'popup-overlay',
					containerId: 'popup-container',
					onOpen: contact.open,
					onShow: contact.show,
					onClose: contact.close
				});
			});
		});
		
		// contact us popup
		$('#contactus').click(function (e) {
			e.preventDefault();
			// load the contact form using ajax
			$.get("http://www.peaceinislam.com/wp-content/plugins/aboutus-contractus/contactus.php", function(data){
				// create a modal dialog with the data
				$(data).modal({
					closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
					position: ["7%",],
					overlayId: 'popup-overlay',
					containerId: 'popup-container',
					onOpen: contact.open,
					onShow: contact.show,
					onClose: contact.close
				});
			});
		});		
		
		// preload images
		/** TODO: generates unneccessary re-directs, b4 we get around this, commented out
		var img = ['cancel1.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
		$(img).each(function () {
			var i = new Image();
			i.src = 'images/registration/' + this;
		});
		*/
	});
	
	var contact = {
		message: null,
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#popup-container .contact-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#popup-container .contact-input').css({
					'font-size': '.9em'
				});
			}
	
			// dynamically determine height
			var h = 425;
			if ($('#contact-subject').length) {
				h += 26;
			}
			if ($('#contact-cc').length) {
				h += 22;
			}
	
			var title = $('#popup-container .contact-title').html();
			$('#popup-container .contact-title').html('&#2482;&#2507;&#2465;&#2495;&#2434;...');
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#popup-container .contact-content').animate({
							height: h
						}, function () {
									$('#popup-container .contact-title').html(title);
									$('#popup-container #popup-text').fadeIn(200);
									$('#popup-container form').fadeIn(200, function () {
											$('#popup-container #contact-name').focus();
											$('#popup-container .contact-cc').click(function () {
													var cc = $('#popup-container #contact-cc');
													cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
											});
				
											// fix png's for IE 6
											if ($.browser.msie && $.browser.version < 7) {
													$('#popup-container .contact-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) {
		},
		close: function (dialog) {
			$('#popup-container .contact-message').fadeOut();
			$('#popup-container .contact-title').html('&#2476;&#2495;&#2470;&#2494;&#2527;...');
			$('#popup-container form').fadeOut(200);
			$('#popup-container #popup-text').fadeOut(200);
			$('#popup-container .contact-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		}
	};
})(jQuery);
