/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2008 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 170 2008-12-04 19:03:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('.login').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("login.php?href="+$(this).attr("href"), function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["40%",],
				overlayId: 'login-overlay',
				containerId: 'login-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});

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

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

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

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

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

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#login-container .contact-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.gif)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
//		$('#login-container .contact-send').click(function (e) {
		$('#login-container #login-send').click(function (e) {															
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#login-container .contact-message').fadeOut(function () {
					$('#login-container .contact-message').removeClass('contact-error').empty();
				});
				$('#login-container .contact-title').html('Sending...');
				$('#login-container form').fadeOut(200);
				$('#login-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#login-container .contact-loading').fadeIn(200, function () {
						$.ajax({
							url: '_validate_schoolnumber.php',
							data: $('#login-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								text = xhr.responseText
								$('#login-container .contact-loading').fadeOut(200, function () {
/*									$('#login-container .contact-title').html( 'Thank You!' );

									$('#login-container .contact-message').html( xhr.responseText ).fadeIn(200);
*/																	
								});
									
								$('#login-container .contact-title').html( '' );
								$('#login-container .contact-message').html( text ).fadeIn(200);
								
								
							},
							error: function (){
								$('#login-container .contact-title').html( 'Error' );
							}
						});

					});
				});
			}
			else {
				if ($('#login-container .contact-message:visible').length > 0) {
					var msg = $('#login-container .contact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						contact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#login-container .contact-message').animate({
						height: '30px'
					}, contact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		$('#login-container .contact-message').fadeOut();
		$('#login-container .contact-title').html('Goodbye...');
		$('#login-container form').fadeOut(200);
		$('#login-container .contact-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 () {
		contact.message = '';
		if (!$('#login-container #school-code').val()) {
			contact.message += 'School Code is required. ';
		}

/*		var email = $('#login-container #login-email').val();
		if (!email) {
			contact.message += 'Email is required. ';
		}
		else {
			if (!contact.validateEmail(email)) {
				contact.message += 'Email is invalid. ';
			}
		}

		if (!$('#login-container #login-message').val()) {
			contact.message += 'Message is required.';
		}

		if (contact.message.length > 0) {
			return false;
		}
*/		
		else {
			return true;
		}
	},
	showError: function () {
		$('#login-container .contact-message')
			.html($('<div class="contact-error">').append(contact.message))
			.fadeIn(200);
	}
};
