/*
 * 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 $
 *
 */

var actionURL;

$(document).ready(function () {
							
	$('a.LogInPrompt').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get(config.themePath+"login_form.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'LogPopOverlay',
				containerId: 'LogPopContainer',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
		
		actionURL = $(this).attr('href');
		
		
		if(!strstr(actionURL, '?'))
		{
			actionURL = actionURL+'?login=1';
		}
		else
		{
			actionURL = actionURL+'&login=1';
		}
		
	});

	// 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 = config.themePath+'_images_gen/login_prompt/' + this;
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#LogPopContainer .LogPopButton').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#LogPopContainer .LogPopInput').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 250;

		var title = $('#LogPopContainer .LogPopTitle').html();
		$('#LogPopContainer .LogPopTitle').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#LogPopContainer .LogPopContent').animate({
						height: h
					}, function () {
						$('#LogPopContainer .LogPopTitle').html(title);
						$('#LogPopContainer form').fadeIn(200, function () {
							$('#LogPopContainer #LoginEmail').focus();

							//alert('d: '+actionURL);
							
							//SET ACTION
							$('#LogPopForm').attr("action", actionURL);
							

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#LogPopContainer .LogPopButton').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) {
		$('#LogPopContainer .LogPopMessage').fadeOut();
		$('#LogPopContainer .LogPopTitle').html('Goodbye...');
		$('#LogPopContainer form').fadeOut(200);
		$('#LogPopContainer .LogPopContent').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	}
};
