$(
	function() {
		$.blockUI.defaults.overlayCSS.backgroundColor = '#fff';
		/* $.ajax(
			{
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
					$('body').append(XMLHttpRequest.responseText);
				}
			}
	    ); */
		$('#wl_modal_dialog').dialog(
			{
				autoOpen : false,
				bgiframe : true,
				closeOnEscape : false,
				draggable : false,
				height : 'auto',
				modal : true,
				width : 'auto',
				stack : true,
				resizable : false,
				position : ['center','center']
			}
		);
		if ($('input[name=zipcode]').length > 0) {
			WishList.content_controller.setup_zip_form();
		};
		$("#wish_dialog").dialog({
			bgiframe: true,
			resizable: false,
			height:140,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			autoOpen : false,
			buttons: {
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});
		$('div.wish_item a.del').click(
			function() {
				WishList.content_controller.current_wish = $(this).attr('id').split('_')[1];
				$('#wish_dialog').dialog(
					'option',
					'buttons',
					{
						'Delete' : function() {
							WishList.content_controller.delete_wish(WishList.content_controller.current_wish);
							$(this).dialog('close');
						},
						Cancel : function() {
							$(this).dialog('close');
						}
					}
				).dialog('open');
				return false;
			}
		);
		$('div.comments a.show').click(
			function() {
				var wishid = $(this).attr('id').split('_')[1];
				if ($(this).siblings('div#com_' + wishid).css('display') == 'none') {
					$(this).text('hide');
					WishList.ajax_controller.load_html_get('/user/cf_in_wish_comments.htm?wid=' + wishid,
														   function(sData,sTextStatus) {
																$('#comAjax_' + wishid).html(sData).parent('#com_' + wishid).show();   
																
														   });
				} else {
					$(this).text('show').siblings('div#com_' + wishid).hide();
					
				}
				return false;
			}
		);
		$('div.wl_list div.list_item .delete').click(
			function() {
				WishList.content_controller.current_wishlist = $(this).attr('id').split('_')[1];
				$('#wish_dialog').dialog(
					'option',
					'buttons',
					{
						'Delete' : function() {
							WishList.content_controller.delete_wishlist(WishList.content_controller.current_wishlist);
							$(this).dialog('close');
						},
						Cancel : function() {
							$(this).dialog('close');
						}
					}
				).dialog('open');
				return false;
			}
		);
	}
);		

//use this when you don't need the returned data to have executable javascript
var WishList = {
	ajax_controller : {
		load_html_get : function(sUrl,fCallback) {
			if (arguments.length == 1 || typeof(arguments[1]) != 'function')
				fCallback = function() {return true};
//			var sUrl = '/user/test_ajax_html.htm';
			$.ajax(
				{
					url : sUrl,
					cache : false,
					dataType : 'html',
					success : fCallback,
					error : function (XMLHttpRequest, textStatus, errorThrown) {
						alert(XMLHttpRequest.responseText);
					}
				}
			);
		},
		load_html_post : function(oForm,fCallback) {
			if (arguments.length == 1 || typeof(arguments[1]) != 'function')
				fCallback = function() {return true};
			var sQueryString = $(oForm).formSerialize();
			var sUrl = $(oForm).attr('action');
//			var sUrl = '/user/test_ajax_html.htm';
			$.ajax(
				{
					url : sUrl,
					cache : false,
					dataType : 'html',
					success : fCallback,
					type : 'post',
					data : sQueryString
				}
			);
		}
	},
	modal_controller : {
		load_modal_html_get : function(sUrl,sTitle) {
			var modal_dialog = $('#wl_modal_dialog');
			if (arguments.legnth == 1) {
				if (modal_dialog.dialog('isOpen'))
					var sTitle = modal_dialog.dialog('option','title');
				else
					var sTitle = 'WishList.com';
			}
			WishList.ajax_controller.load_html_get(
				sUrl,
				function(sData,sTextStatus) {
					modal_dialog.html(sData).css('padding-top',0).dialog('option','title',sTitle).dialog('open');
				}
			);
		},
		load_modal_html_post : function(oForm,sTitle) {
			var modal_dialog = $('#wl_modal_dialog');
			if (arguments.legnth == 1) {
				if (modal_dialog.dialog('isOpen'))
					var sTitle = modal_dialog.dialog('option','title');
				else
					var sTitle = 'WishList.com';
			}
			WishList.ajax_controller.load_html_post(
				oForm,
				function(sData,sTextStatus) {
					modal_dialog.html(sData).css('padding-top',0).dialog('option','title',sTitle).dialog('open');
				}
			);
			return false;
		},
		close_modal : function() {
			$('#wl_modal_dialog').dialog('close');
		}
	},
	friend_controller : {
		add : function(iId) {
//			WishList.modal_controller.load_modal_html_get('/user/frm_become_friends.htm?request_friend_id_list=' + iId,'WishList.com!');
			ColdFusion.navigate('/user/frm_become_friends.htm?cfdebug=true&request_friend_id_list=' + iId, 'thecfwindow_body');
			resizeWindow('thecfwindow',800,200);
			ColdFusion.Window.show('thecfwindow');
		},
		remove : function(iId) {
			return true;
		}
	},
	content_controller : {
		current_wish : 0,
		current_wishlist : 0,
		load_zip : function(sZip,sState,sCity,sCountry) {
			WishList.ajax_controller.load_html_get(
				'/templates/cf_in_zip_city_state.htm?do_zip_lookup=yes&markup=new&zipcode=' + sZip + '&city=' + sCity + '&state=' + sState + '&country=' + sCountry,
				function(sData,sTextStatus) {
					$('#city_state_id').html(sData).slideDown('normal');
				}
			);
		},
		setup_zip_form : function() {
			if ($('input[name=zipcode]').length > 0) {
				if ($('input[name=zipcode]').val().length == 0 && $('select[name=state] option:selected').val() == 'State' && $('input[name=city]').val().length == 0) {
					$('#city_state_id').hide();
				} else {
					$('#city_state_id').show();
				}
				$('input[name=zipcode]').keyup(
					function(e) {
						if (this.value.length == 5) {
							WishList.content_controller.load_zip(this.value,$('select[name=state] option:selected').val(),$('input[name=city]').val(),$('select[name=country] option:selected').val());
						}
					}
				);

			}
		},
		check_loginid : function(loginId) {
			if (loginId.length) {
				$('#loginId_error').text("").slideUp('fast').parent('div.set').removeClass('error');
				$.getJSON(
					'/user/ajax_check_loginid.htm?loginid=' + loginId,
					function(oData,sTextStatus) {
						if (!oData.response) {
							$('#loginId_error').text("An account already has this email address.").parent('div.set').addClass('error').end().slideDown('fast');
						}
						var conf_email_value = $('input[name=conf_email]').val();
						if (conf_email_value.length > 0 && conf_email_value == loginId)
							$('#conf_loginId_error').slideUp('fast').text("").parent('div.set').removeClass('error');
					}
				)
			}
		},
		confirm_email : function(conf_email,email) {
			$('#conf_loginId_error').text("").slideUp('fast').parent('div.set').removeClass('error');
			if (conf_email != email) {
				$('#conf_loginId_error').text("'Confirmm Email' and 'Email' do not match.").parent('div.set').addClass('error').end().slideDown('fast');
			}
		},
		check_username : function(username) {
			if (username.length) {
				$('#username_error').text("").slideUp('fast').parent('div.set').removeClass('error');
				$.getJSON(
					'/user/ajax_check_username.htm?username=' + username,
					function(oData,sTextStatus) {
						if (!oData.response) {
							$('#username_error').text("An account already has this screen name.").parent('div.set').addClass('error').end().slideDown('fast');
						} else { 
							if (username.match(/[^\da-zA-Z\-]/)) {
								$('#username_error').text("Please only use numbers and letters for your screen name.").parent('div.set').addClass('error').end().slideDown('fast');
							}
						}
					}
				)
			}
		},
		add_wish : function(oForm) {
			$(oForm).ajaxSubmit(
				{
					url : '/user/ajax_add_wish.htm?form=' + $(oForm).attr('name'),
					dataType : 'json',
					beforeSubmit : function() {
						$('#ajax_add_wish',oForm).block(
							{
								message : "Adding..."
							}
						)
					},
					success : function(oData,sStatusText) {
						$('form[name=' + oData.response.form + '] #ajax_add_wish').unblock();
						alert(oData.response.form);
					},
					error : function(oXMLHttpRequest,sTextStatus,sErrorThrown) {
						alert(oXMLHttpRequest.responseText);
					}
				}
			);
		},
		delete_wish : function(iWid) {
			var this_wish = iWid;
			$('#wish_' + iWid).block(
				{
					message : 'Deleting Wish...'
				}
			);
			$.getJSON(
				'/user/ajax_delete_wish.htm?wid=' + iWid,
				function(oData,sTextStatus) {
					if (oData.response) {
						$('#wish_' + this_wish).slideUp(
							'slow',
							function() {
								$('#wish_' + this_wish).remove();
							}
						);
					} else {
						alert('There was an error deleting this wish.\nIt may have already been deleted, or a system error occurred.\nPlease refresh the page and try again.');
						$('#wish_' + iWid).unblockUI();
					}
				}
			)
		},
		delete_wishlist : function(iWlid) {
			var this_wishlist = iWlid;
			$('#wishlist_' + iWlid).block(
				{
					message : 'Deleting WishList...'
				}
			);
			$.getJSON(
				'/user/ajax_delete_wishlist.htm?wlid=' + iWlid,
				function(oData,sTextStatus) {
					if (oData.response) {
						$('#wishlist_' + this_wishlist).slideUp(
							'slow',
							function() {
								$('#wishlist_' + this_wishlist).remove();
							}
						);
					} else {
						alert('There was an error deleting this WishList.\nIt may have already been deleted, or a system error occurred.\nPlease refresh the page and try again.');
						$('#wish_' + iWid).unblockUI();
					}
				}
			)
		}
	}
};