/*
 * FormDialog
 *
 * Depends:
 *	ui.core.js
 *	ui.draggable.js
 *	ui.resizable.js
 *  ui.dialog.js
 */

(function($) {
	$.fn.extend({
		FormDialog: function(href, exts) {
			var dialog = false,
			createDialog = function(href, exts) {
				
				if (dialog === false) {
					dialog = $('<div title="Loading"><div class="loading"><img src="/i/loading.gif" alt="" /> Loading...</div></div>').dialog({
						dialogClass: (exts && exts.dialogClass) ? exts.dialogClass : '',
						bgiframe: true,
						modal: true,
						resizable: false,
						width: (exts && exts.dialogWidth) ? exts.dialogWidth : 450,
						position: ['center','top']/*,
						/*open: function() {
							$("object").css("visibility", "hidden");
						},
						close: function() {
							$("object").css("visibility", "");
						}*/
					});
				} else {
					dialog.html('<div class="loading"><img src="/i/loading.gif" alt="" /> Loading...</div>');
					dialog.dialog("option", "title", "Loading");
				}
				dialog.find(".close").live("click", function(e) {
					if($(this).parents("div.ui-dialog-content")[0] != dialog[0]) {
						return; // cancel the event if it is not coming from within the active dialog
					}
					dialog.dialog('close');
					$("object", dialog).remove();
					return false;
				});
				
				$.get(href, null, htmlReturn, "html");
				dialog.dialog("open");
			},
			htmlReturn = function(data) {
				var rootNode = $(data);
				//dialog.html(rootNode.html()); /* IE didn't like this. Was outputting <form /> */
				dialog.html(data);
				$("form", dialog).bind("submit", function(e) {
					var form = $(this),
					str = form.serialize();
					
					$.post(form.attr("action"), str, htmlReturn, "html");
					e.preventDefault()
				});
				if (typeof(rootNode.attr("title")) != "undefined") {
					if (rootNode.attr("title") == "") {
						var dialogClass = dialog.dialog('option', 'dialogClass');
						dialog.dialog('option', 'dialogClass', dialogClass + ' noTitle');
					}
					dialog.dialog('option', 'title', rootNode.attr("title"));
				}
				if (typeof(rootNode.attr("autoclose")) != "undefined") {
					setTimeout(function() {
						dialog.dialog('destroy');
					}, rootNode.attr("autoclose"));
				}
				if (typeof(rootNode.attr("redirect")) != "undefined") {
					var redirectTimer = 2000;
					if (typeof(rootNode.attr("redirectTimer")) != "undefined") {
						redirectTimer = rootNode.attr("redirectTimer");
					}
					setTimeout(function() {
						window.location = rootNode.attr("redirect");
					}, redirectTimer);
				}
				dialog.dialog("close").dialog("open");
				
				var loadedImgs = [];
				$("img", dialog).load(function(){
					loadedImgs.push($(this).attr('src'));
				});
				
				Cufon.refresh();
				
				// init the carousel...
				$(".carousel", dialog).jcarousel({
					scroll: 1,
					wrap: 'both',
					initCallback: function(carousel) {
						$('.jcarousel-control a').bind('click', function() {
							$('.jcarousel-control li').removeClass('selected');
							$(this).parent().addClass('selected');
							carousel.scroll($.jcarousel.intval($(this).text()));
							return false;
						});
						carousel.scroll(1); // Show first item (Carousel didn't work in Safari without this)
					},
					itemVisibleInCallback: {
						onAfterAnimation: function(carousel, item, idx, state) {
							var minHeight = 650,
							resize = function() {
								if ($("img", item).length != 0) {
									var img = $("img", item).attr('src'),
									itemHeight = $("img", item).height();
								} else {
									itemHeight = $(item).height();
								}
								if (!img || jQuery.inArray(img, loadedImgs) !== -1) {
									$(item).height(itemHeight);
									$(carousel.list).animate({
										height: (itemHeight < minHeight) ? minHeight : itemHeight
									});
									return true;
								} else {
									return false;
								}
							}
							if (resize() === false) {
								var t = setInterval(function() {
									if (resize() === true) {
										clearInterval(t);
									}
								}, 100);
							}
	
							$('h3.title', dialog).html($("img", item).attr('alt'));
							$('p.text', dialog).html($("img", item).attr('title'));
							Cufon.refresh();
							$('.jcarousel-control li').removeClass('selected');
							$('.jcarousel-control a.item_'+idx).parent().addClass('selected');
						}
					}
				});
				
			}
			if (href) {
				createDialog(href, exts);
			} else {
				this.live("click", function(e) {
					var _this = $(this),
					exts = _this.ParseClass(),
					href = (exts && exts.ajaxHref) ? exts.ajaxHref : _this.attr('href');
					if (typeof(href) === "undefined") {
						return;
					}
					
					createDialog(href, exts);
					e.preventDefault();
				});
			}
			return this;
		}
	});
	
	$("a.formdialog").FormDialog();
})(jQuery);
