/*
 * simplemodal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 * http://www.ericmmartin.com/projects/simplemodal-demos/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: jQueryId: contact.js 212 2009-09-03 05:33:44Z emartin24 jQuery
 *
 */


// function for closing popups, regardless of style
function popupPageHidePopup(){
	jQuery.log("hide");
	// close modals
	if(jQuery.modal){
		jQuery.modal.close();
	}
}

jQuery(document).ready(function () {

	// get post id from var from shortcode
	if (typeof popupPagePostId == "undefined") {
		popupPagePostId = false;
	}

	// get popup style from var from shortcode
	if (typeof popupPageStyle == "undefined") {
		popupPageStyle = "modal";
	}

	// show popup based on style	
	if(popupPagePostId){
		// load post content using ajax
		jQuery.get("", {popup_post_id: popupPagePostId}, function(content){
			// show popup
			switch (popupPageStyle){

				// create a simplemodal dialog with the data
				case "modal":
					jQuery(content).modal({
						closeHTML: 		"<a href='#' title='Close' class='modal-close'>Close</a>",
						position: 		["15%", ],
						overlayId: 		'simplemodal-overlay',
						containerId: 	'simplemodal-container',
						overlayClose: 	true,
						escClose:		true,
						autoResize: 	true,
						autoPosition:	true,
						minHeight:		400,
						opacity: 		50,
						onOpen: 		simplemodal.open,
						onShow: 		simplemodal.show,
						onClose: 		simplemodal.close
					});
					break;
					
				// default style
				default:
					$.log("default popup style");
					break;
			}
		});
		
	}

	// preload images
	/*
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	jQuery(img).each(function () {
		var i = new Image();
		i.src = 'img/contact/' + this;
	});
	*/
});

var simplemodal = {
	message: null,
	
	/* open event */
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			// show container and data invisibly
			dialog.container.show().fadeTo(0,0);
			dialog.data.show().fadeTo(0,0);
			// set container height
			h = dialog.data.height();
			dialog.container.height(h+20);
			// fade in container and data
			dialog.container.fadeTo(200, 1, function () {
				dialog.data.fadeTo(200, 1, function () {
				});
			});
		});

	},
	
	/* show event */
	show: function (dialog) {
		
 	},
	
	/* close event */
	close: function (dialog) {
		/* hide dialog */
		dialog.container.fadeOut(200, function () {
			dialog.overlay.fadeOut(200, function () {
				jQuery.modal.close();
			});
		});
	},
	
	/* errors */
	error: function (xhr) {
		jQuery.log("error: %s",xhr.statusText);
	},
	
	/*
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.jQuery)/.test(local) || /(^\.|\.jQuery)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"jQuery/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#jQuery%*\/?|^{}`~&'+=_\.]*jQuery/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*jQuery/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	},
	
	showError: function () {
		jQuery('#simplemodal-container .simplemodal-message')
			.html(jQuery('<div class="simplemodal-error"></div>').append(simplemodal.message))
			.fadeIn(200);
	}
	*/
};