$(document).ready(function () {
	$('a.tsandcs').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("/ajax/tsandcs", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'big-overlay',
				containerId: 'big-container',
				onOpen: tsandcs.open,
				onShow: tsandcs.show,
				onClose: tsandcs.close
			});
		});
	});

	// preload images
	var img = ['cancel.png', 'big_bottom.gif', 'big_top.gif', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '/public/images/modal/' + this;
	});
});

var tsandcs = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#big-container .big-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#big-container .big-input').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 380;
		if ($('#big-subject').length) {
			h += 26;
		}
		if ($('#big-cc').length) {
			h += 22;
		}

		var title = $('#big-container .big-title').html();
		$('#big-container .big-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#big-container .big-content').animate({
						height: h
					}, function () {
						$('#big-container .big-title').html(title);
						$('#big-container form').fadeIn(200, function () {
							$('#big-container #big-name').focus();

							$('#big-container .big-cc').click(function () {
								var cc = $('#big-container #big-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#big-container .big-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) {
		$('#big-container .big-send').click(function (e) {
			e.preventDefault();
			// validate form
			if (tsandcs.validate()) {
				$('#big-container .big-message').fadeOut(function () {
					$('#big-container .big-message').removeClass('contact-error').empty();
				});
				$('#big-container .big-title').html('Sending...');
				$('#big-container form').fadeOut(200);
				$('#big-container .big-content').animate({
					height: '80px'
				}, function () {
					$('#big-container .big-loading').fadeIn(200, function () {
						$.ajax({
							url: '/ajax/tsandcs',
							data: $('#big-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								$('#big-container .big-loading').fadeOut(200, function () {
									$('#big-container .big-title').html('Thank you!');
									$('#big-container .big-message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: tsandcs.error
						});
					});
				});
			}
			else {
				if ($('#big-container .big-message:visible').length > 0) {
					var msg = $('#big-container .big-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						tsandcs.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#big-container .contact-message').animate({
						height: '30px'
					}, tsandcs.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		$('#big-container .big-message').fadeOut();
		$('#big-container .big-title').html('Goodbye...');
		$('#big-container form').fadeOut(200);
		$('#big-container .big-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);
	}
};
