/*
 * SimpleModal Save Search Form
 *
 * based on 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
 *
 *
 * NOTES:
 *
 * 		requires jquery.selectboxes.js
 */

var saveSearch = {
	message: null,
	
	open: function (dialog) {
		// input field font size
		if ($.browser.safari) {
			$('#save-container .save-input').css({
				'font-size': '.9em'
			});
		}

		var title = $('#save-container .save-title').html();

		$('#save-container .save-title').html('Loading...');

		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#save-container .save-content').animate({
						height: 260
					}, function () {
						$('#save-container .save-title').html(title);
						$('#save-container form').fadeIn(200, function () {
							$('#save-container #save-name').focus();

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#save-container .save-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")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	
	close: function (dialog) {
		$('#save-container .save-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 () {
		saveSearch.message = '';
		if (!$('#save-container #save-title').val()) {
			saveSearch.message += 'Title is required. ';
		}

		if (saveSearch.message.length > 0) {
			saveSearch.showFormError(saveSearch.message);
			return false;
		}
		else {
			return true;
		}
	},

	showFormError: function (message) {
		if ($('#save-container .save-message:visible').length > 0) {
			var msg = $('#save-container .save-message div');
			msg.fadeOut(200, function () {
				msg.empty();
			});
		}

		$('#save-container .save-message').html($('<div class="save-error">').append(message)).fadeIn(200);
	},
	
	show: function (dialog) {
		$('#save-container .save-send').click( function (e) {
			e.preventDefault();
			
			if (saveSearch.validate()) {
				$('#save-container .save-message').fadeOut(function () {
					$('#save-container .save-message').removeClass('save-error').empty();
				});
				$('#save-container .save-title').html('Saving...');
				$('#save-container form').fadeOut(200);
				
				$('#save-container .save-content').animate({
					height: '80px'
				}, function () {
					$('#save-container .save-loading').fadeIn(200, function () {
						$.ajax({
							url: 'SaveSearch.aspx',
							data: $('#save-container form').serialize() + '&action=save',
							type: 'post',
							cache: false,
							dataType: 'json',
							success: function (data,textStatus) {
								// display saved title/id in saved-search list
								if(data.result==="OK") {
									saveSearch.displaySave(data);
								} else {
									alert(data.message);
									$.modal.close();
								}
							},
							error: saveSearch.error
						});
					});
				});
			}
		});
	},


	displaySave: function(data) {
		var sel=$('#USID');

		// get rid of 'nothing available'
		if(sel.containsOption('0')) {
			sel.removeOption('0');
		}

		sel.addOption(data.optionValue,data.optionDisplay);
		sel.selectOptions(data.optionValue);

		$('#show-saved-search').show().focus();

		$('#save-container .save-loading').fadeOut(200, function () {
			$('#save-container .save-title').html('Saved');
			$('#save-container .save-title').fadeOut(200,function () {
				$.modal.close();
			});
		});
	}
};
