var wsDir = '/webcore-ws/';


$(function() {
	
	$('.open').click( function(){
		clientLogin();
		return false;
	});
	
	$('.client_login').click( function(){
		clientLogin();
		return false;
	});
	
	$('.client_login').live( "click", function(){
		clientLogin();
		return false;
	});
	
	$('.client_create').click( function(){
		clientCreate();
		return false;
	});
	
	$('.client_create').live( "click", function(){
		clientCreate();
		return false;
	});
	
	$('.client_forgot_username').click( function(){
		clientForgot();
		return false;
	});
	
	$('.client_forgot_username').live( "click", function(){
		clientForgot();
		return false;
	});
	
	$('.client_forgot_password').click( function(){
		clientForgot();
		return false;
	});
	
	$('.client_forgot_password').live( "click", function(){
		clientForgot();
		return false;
	});
	
	$(".client_logout").click( function() {
		clientUnLoad()
		return false;
	});
	
	$(".client_logout").live("click", function() {
		clientUnLoad()
		return false;
	});
	
	
	/* ---------- Listing - Engage ---------- */

	$('#content_area').delegate('.listing_subscribe', 'click', function() { 
		var listingKey = $(this).closest('.gd-listing').attr('id').replace('listing-', '');
		listingSubscribe(listingKey);
		return false;
	});	
	
	$('#content_area').delegate('.listing_unsubscribe', 'click', function() { 
		var listingKey = $(this).closest('.gd-listing').attr('id').replace('listing-', '');
		listingUnsubscribe(listingKey);
		return false;
	});
	
	$('#content_area').delegate('.listing_hide', 'click', function() { 
		var listingKey = $(this).closest('.gd-listing').attr('id').replace('listing-', '');
		listingHide(listingKey);
		return false;
	});	
	
	/* ---------- Listing - Functions ---------- */
	
	function listingSubscribe(listingKey){
		
		var message;
		
		if ($("body").hasClass('loggedin')) {
		  
			$.ajax({
				method: "post",
				url: wsDir + 'listing_subscribe.cfm',
				dataType: "json",
				data:{
					listingKey: listingKey
				},
				success: function( data ) {
					$("#client-listings-count").text(data.clientListingCount);
					
					saveButton = '#listing-' + listingKey + ' #nav-listing-save';
					$(saveButton).attr('class', 'gd-updated active');
					$(saveButton + ' a').attr('class', 'listing_unsubscribe');
					$(saveButton + ' a').text('Remove Listing');
					//modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})		  
		  
		} else {
			message = 'You must create an account in order to save listings.';
			clientLogin(message);  
		}		
	};		
	
	function listingUnsubscribe(listingKey){
		
		var message;
		
		if ($("body").hasClass('loggedin')) {
		  
			$.ajax({
				method: "post",
				url: wsDir + 'listing_unsubscribe.cfm',
				dataType: "json",
				data:{
					listingKey: listingKey
				},
				success: function( data ) {
					$("#client-listings-count").text(data.clientListingCount);
					
					saveButton = '#listing-' + listingKey + ' #nav-listing-save';
					$(saveButton).attr('class', 'gd-updated inactive');
					$(saveButton + ' a').attr('class', 'listing_subscribe');
					$(saveButton + ' a').text('Save Listing');
					//modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})		  
		  
		} else {
			message = 'You must create an account in order to save listings.';
			clientLogin(message);  
		}		
	};				
	
	function listingHide(listingKey){
		
		var message;
		
		$.ajax({
			method: "post",
			url: wsDir + 'listing_hide.cfm',
			dataType: "html",
			data:{
				listingKey: listingKey
			},
			success: function( data ) {
				modal(data, message);
			},
			error: function( data ) {
				alert('error');
			}
		})

	};		
	
	
	/* ---------- Search - Engage ---------- */
	
	$('.modules').delegate('.search_subscribe', 'click', function() {
		
		if ($( 'input[name="gdSearchString"]:first-child', $( this ).parents ( '.module' ) ).attr('value') !== undefined) {
		    var searchString = $( 'input[name="gdSearchString"]:first-child', $( this ).parents ( '.module' ) ).attr('value');
		} else {
		    var searchString;
		}
		
		searchSubscribe(searchString);
		
		return false;
	});	
	
	/* ---------- Search - Functions ---------- */
	
	function searchSubscribe(searchString){
		
		var searchString;
		var message;
		
		if ($("body").hasClass('loggedin')) {
		  
			$.ajax({
				method: "post",
				url: wsDir + 'search_subscribe.cfm',
				dataType: "html",
				data:{
					gdSearchString: searchString
				},
				success: function( data ) {
					modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})		  
		  
		} else {
			message = 'You must create an account in order to save searches.';
			clientLogin(message);  
		}		
	};		
	
	$("#search_subscribe_form").live("submit", function() {

		if ($('input[name="gdSearchTitle"]').val().length < 1) {
			// put a message here 
		    return false;
		}
		
		$.fancybox.showActivity();
		
		$.ajax({
			type		: "post",
			cache	: false,
			url		: wsDir + 'search_subscribe.cfm',
			dataType:	"html",
			data		: $(this).serializeArray(),
			success: function(data) {
					modal(data);
		   			 $.fancybox.resize();
			},
				error: function( data ) {
					alert('error');
				}
		});
		
		return false;
	});	
	
	/* ---------- Report - Engage ---------- */
	
	$('#content_area').delegate('.report_subscribe', 'click', function() { 
		var reportKey = $(this).closest('.gd-report').attr('id').replace('report-', '');
		reportSubscribe(reportKey);
		return false;
	});
	
	$('#content_area').delegate('.report_unsubscribe', 'click', function() { 
		var reportKey = $(this).closest('.gd-report').attr('id').replace('report-', '');
		reportUnsubscribe(reportKey);
		return false;
	});
	
	/* ---------- Report - Functions ---------- */
	
	function reportSubscribe(reportKey){
		
		var message;
		
		if ($("body").hasClass('loggedin')) {
		  
			$.ajax({
				method: "post",
				url: wsDir + 'report_subscribe.cfm',
				dataType: "html",
				data:{
					reportKey: reportKey
				},
				success: function( data ) {
				
					subscribeButtonId = '#report-' + reportKey;
					
					$(subscribeButtonId + ' .button-report-subscribe').attr('class', 'gd-updated button-report-subscribe active');
					$(subscribeButtonId + ' .button-report-subscribe a').attr('class', 'report_unsubscribe');
					
					$(subscribeButtonId + ' .button-report-subscribe a').text(function(index, text) {
					    return text.replace("Subscribe", "Unsubscribe");
					});
					
					modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})		  
		  
		} else {
			message = 'You must create an account in order to subscribe to reports.';
			clientLogin(message);  
		}		
	};		
	
	function reportUnsubscribe(reportKey){
		
		var message;
		
		if ($("body").hasClass('loggedin')) {
		  
			$.ajax({
				method: "post",
				url: wsDir + 'report_unsubscribe.cfm',
				dataType: "html",
				data:{
					reportKey: reportKey
				},
				success: function( data ) {
				
					subscribeButtonId = '#report-' + reportKey;
					
					$(subscribeButtonId + ' .button-report-subscribe').attr('class', 'gd-updated button-report-subscribe inactive');
					$(subscribeButtonId + ' .button-report-subscribe a').attr('class', 'report_subscribe');
					
					$(subscribeButtonId + ' .button-report-subscribe a').text(function(index, text) {
					    return text.replace("Unsubscribe", "Subscribe");
					});
					
					modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})		  
		  
		} else {
			message = 'You must create an account in order to subscribe to reports.';
			clientLogin(message);  
		}		
	};	

	
	
	
	
	
	
	/* ---------- Client Login Process ---------- */
	
	$("#login_form").live("submit", function() {

		if ($("#login_name").val().length < 1 || $("#login_pass").val().length < 1) {
		    $("#login_error").show();
		    $.fancybox.resize();
		    return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'client_process.cfm',
			dataType:	"json",
			data		: $(this).serializeArray(),
			success: function(data) {
				
				if( data.status == 1 ) {
					this_status = data.status;
					this_message = data.message;
					this_id = data.id;
					this_firstname = data.firstname;
					this_lastname = data.lastname;					
					clientLoad(this_status, this_message, this_id, this_firstname, this_lastname);		
				} else {
					this_message = data.message;
					client(this_message);
				}
				
				$.fancybox.hideActivity();

			}
		});
	
		return false;
	});
	
	/* ---------- Widget Client Login Process ---------- */
	
	$("#widget-client-login-form").live("submit", function() {

		if ($("#login_name").val().length < 1 || $("#login_pass").val().length < 1) {
		    $("#login_error").show();
		    $.fancybox.resize();
		    return false;
		}
		
		//modal('Loading');
		
		$.fancybox.showActivity();
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'client_process.cfm',
			dataType:	"json",
			data		: $(this).serializeArray(),
			success: function(data) {
				
				if( data.status == 1 ) {	
					this_status = data.status;
					this_message = data.message;
					this_id = data.id;
					this_firstname = data.firstname;
					this_lastname = data.lastname;					
					clientLoad(this_status, this_message, this_id, this_firstname, this_lastname);		
				} else {
					this_message = data.message;
					client(this_message);
				}			
				
				$.fancybox.hideActivity();
				
			}
		});
		
		return false;
	});
	
	/* ---------- Client Create ---------- */
	
	function clientCreate(message){
		
		var message;
		
		formKey = 69;		
		
		$.ajax({
				method: "post",
				url: wsDir + 'client_create_form.cfm',
				dataType: "html",
				data:{
					formKey: 69
				},
				success: function( data ) {
					modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})
			
		return false;
		
	};	
	
	/* ---------- Client Create Process ---------- */
	
	$("#client_create_form").live("submit", function() {

		
		if ($("#reg_firstname").val().length < 1 || $("#reg_lastname").val().length < 1 || $("#reg_username").val().length < 1) {
		    $("#client_create_error").show();
		    $.fancybox.resize();
		    return false;
		}
		
		$.fancybox.showActivity();
		
		$.ajax({
			type		: "post",
			cache	: false,
			url		: wsDir + 'client_create_process.cfm',
			dataType:	"json",
			data		: $(this).serializeArray(),
			success: function(data) {
					//alert('huh' + data.status);
				if( data.status == 1 ) {
					this_status = data.status;
					this_message = data.message;
					this_id = data.id;
					this_firstname = data.firstname;
					this_lastname = data.lastname;					
					clientLoad(this_status, this_message, this_id, this_firstname, this_lastname);		
				} else {
					this_message = data.message;
					clientCreate(this_message);
				}
	
			},
				error: function( data ) {
					alert('error');
				}

		});
		
		return false;
	});
	
	/* ---------- Client Forgot ---------- */
	
	function clientForgot(message){
		
		var message;
		
		formKey = 69;		
		
		$.ajax({
				method: "post",
				url: wsDir + 'client_forgot.cfm',
				dataType: "html",
				data:{
					formKey: 69
				},
				success: function( data ) {
					modal(data, message);
				},
				error: function( data ) {
					alert('error');
				}
			})
			
		return false;
		
	};	
	
	/* ---------- Client Forgot Username ---------- */

	$("#client_forgot_username_form").live("submit", function() {

		if ($("#client_email").val().length < 1 || $("#client_email").val().length < 1) {
		    $("#client_forgot_username_error").show();
		    $.fancybox.resize();
		    return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'client_forgot.cfm',
			dataType:	"html",
			data		: $(this).serializeArray(),
			success: function(data) {
			
				modal(data);	
				
			}
		});
	
		return false;
	});		
	
	/* ---------- Client Forgot Password ---------- */

	$("#client_forgot_password_form").live("submit", function() {

		if ($("#client_username").val().length < 1 || $("#client_username").val().length < 1) {
		    $("#client_forgot_password_error").show();
		    $.fancybox.resize();
		    return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'client_forgot.cfm',
			dataType:	"html",
			data		: $(this).serializeArray(),
			success: function(data) {
			
				modal(data);	
				
			}
		});
	
		return false;
	});	
	
	/* ---------- List Subscribe ---------- */
	
	$(".list_subscribe").live("submit", function() {
		listHTMLId = '#' + $(this).attr("id");
		//alert(listHTMLId);
		
		parentElement = $(listHTMLId).parent();
		
		parentHTMLId = '#' + (parentElement).attr("id");
		
		listToken = $(listHTMLId + ' input:hidden[name=listToken]').val();
		
		if ($(listHTMLId + ' .subscriber_email').val().length < 1 || $(listHTMLId + ' .subscriber_name').val() == 'your name' || $(listHTMLId + ' .subscriber_email').val() == 'your email') {
		    //$(listHTMLId + ' .list_error').css({ display: "block" });
		   	alert('Your Name and Email Address are required to subscribe.')
		    //$.fancybox.resize();
		    return false;
		}
	
		//$.fancybox.showActivity();
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'list_subscribe.cfm',
			dataType:	"html",
			data		: $(this).serializeArray(),
			success: function(data) {
				
				$(listHTMLId).remove();
				$(parentHTMLId).prepend(data);
				
				//alert(listToken);
				//alert(listHTMLId);
				//alert(data);
//				$(listHTMLid).replaceWith(data);
				
				
			}
		});
	
		return false;
		
	});	
	
		
	
	
	
	
	
	
	
	function modal(data, message){
		//alert('modal?!');
		var modalMessage = message;
		var modalContent = data;
		
		
		if (undefined != modalMessage) {
			modalMessageInsert = '<div class="modal-message">' + modalMessage + '</div>';
			modalContent = modalContent.replace(/<div class="modal-message"><\/div>/i, modalMessageInsert)
		}		
		
		$.fancybox({
			/*
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: 'This is the Title',
			'width'			: 680,
			'height'		: 495,
			*/
			'content'		: modalContent
		});	
		
		if (undefined != modalMessage) {
		
			$('.modal-message').css("display", "block");
		
		}
		
	};	
	
	function client(message){
		
		var message;
		
		formKey = 69;		
		
		$.ajax({
				method: "post",
				url: wsDir + 'client_login.cfm',
				dataType: "html",
				data:{
					formKey: 69
				},
				success: function( data ) {
					//alert(data);
					//alert('ajax?!');
					//clientLoad(data);
					modal(data, message);
				},
				error: function( data ) {
					//alert(data);
					alert('error');
				}
			})
			
		return false;
		
	};
	
	function clientLogin(message){
		
		var message;
		
		formKey = 69;		
		
		$.ajax({
				method: "post",
				url: wsDir + 'client_login.cfm',
				dataType: "html",
				data:{
					formKey: 69
				},
				success: function( data ) {
					//alert(data);
					//alert('ajax?!');
					//clientLoad(data);
					modal(data, message);
				},
				error: function( data ) {
					//alert(data);
					alert('error');
				}
			})
			
		return false;
		
	};
	
	function clientLoad(status,message,clientId,firstName,lastName){
						
						
		/*						
		var messageContent = '<p>' + firstName + ' ' + lastName + ' has been logged in.' + '</p>';
		
		$('#message_bubble').removeClass("message_none");
		$('#message_bubble p').remove();
		$('#message_bubble').append(messageContent);
			
		var clientArea = '<div id="client_area" class="wrap"><div id="client" class="section"><ul id="client_badge"><li class="greeting"><a href="/client/dashboard">Hello!</a></li><li class="name"><a href="/client/dashboard">' + firstName + ' ' + lastName + '</a></li></ul><ul id="client_menu"><li class="alpha"><a href="/client/dashboard">Dashboard</a></li><li class=""><a href="/client/searches">Searches</a></li><li class=""><a href="/client/listings">Listings <span>(<span id="client-listings-count">...</span>)</span></a></li><li class=""><a href="javascript:alert(\'We are updating this functionality. It will return soon!\');">TourBook</a></li><li class=""><a href="/client/reports">Reports</a></li><li class="omega"><a href="/logout">Logout</a></li></ul></div></div>';
			
		$('body').prepend(clientArea);	
				
		*/
				
		$.fancybox.close();
		
		location.reload();
		
				//modal(modalContent);
				/*	
				this_status = data.status;
				this_message = data.message;
				this_id = data.id;
				this_firstname = data.firstname;
				this_lastname = data.lastname;
				modal(this_status + ' ' + this_message + ' ' + this_id + ' ' + this_firstname + ' ' + this_lastname);
				*/
	};
	
	function clientUnLoad(){
		
		
		
		//$('#client_area').remove();

		$.ajax({
			type		: "POST",
			cache	: false,
			url		: wsDir + 'client_logout.cfm',
			dataType:	"json",
			data		: $(this).serializeArray(),
			success: function(data) {

				messageLoad(data.message);
				location.reload();
			}
			
		});
		
		
		
	};
	
	function messageLoad(message){
		var messageContent = '<p>' + message + '</p>';
		$('#message_bubble').removeClass("message_none");
		$('#message_bubble p').remove();
		$('#message_bubble').append(messageContent);
	};
	
	
	

	
	
	
	
	
	
	
	/*
	$.ajax({
		method: "get",
		url: "ws/contacts.cfm",
		dataType: "json",
		data: {
			lkupMaxows: 20,
			lkupListId: lkupListId
		},
		success: function( data ) {
			if (data.contacts.length == 1) {
				resultsHeader = '<div class="results_header">' + data.contacts.length + ' Contact</div>';
			} else {
				resultsHeader = '<div class="results_header">' + data.contacts.length + ' Contacts</div>';
			};
			$('.results_header').replaceWith(resultsHeader);		
			var resultsContent = '<div class="results_content">';
			 $.map( data.contacts, function( item ) {
				resultsContent += '<div iid="' + item.contactId + '"class="new_contact">' + item.contactLastName + ', ' + item.contactFirstName + '</div>';
			});
			resultsContent += '</div>';
			$('.results_content').replaceWith(resultsContent);		
		},
		error: function( data ) {
			resultsHeader = '<div class="results_header">No Contacts</div>';
			$('.results_header').replaceWith(resultsHeader);	
			var resultsContent = '<div class="results_content">...</div>';
			$('.results_content').replaceWith(resultsContent);						
		}	

	*/
	
	

	
	

	
	
	
	
	
	
	$('.contact_delete')
		.live('click',function(){
			var id = $('.contactAvatar').attr('cid');
			
			var check = confirm('Are you sure you want to delete this Contact?');
				if(check == true){
					
					$.ajax({
						url: 'http://go.gdwebdev01.com/addressbook/qry/deleteContact.cfm',
						data:{
							contactId: id
						},
						success:function(){
							$('.detail_item_header').remove();
							$('.detail_content').remove();
							$('.results_content').children('.current').remove();
						}
					})
				}
			})
	
	$( '.new_contact').live
		('mouseover',function(){
			$(this).draggable({
				helper: "clone",
	    		cursor: "move",
	    		iframeFix:"false",
	    		appendTo:".container",
	    		stack: ".gd-state-hover"
			
			})
		})
	
	$( '.results_footer' )
		.bind('click',function(){
			$.get('http://go.gdwebdev01.com/addressbook/ws/includes/new_contact.cfm',function(data){
				$('.detail_content').replaceWith(data);
				$('.detail_footer').hide();
				
				
		});
	});
	$('#discard_button').live
	('click',function(){
		var check = confirm('Are you sure you want to discard new list?');
			if(check==true){
				$('#list_form').remove();
				$('.source_item_all[lid="-1"]').trigger('click');
			};
	});
		
	$( ".contactAvatar" ).live('mouseover',function(){
	    $(this).draggable({
	    	helper: "clone",
	    	cursor: "move",
	    	appentTo: "gd_app_main",
	    	stack: ".gd-state-hover"
	    });
	});
	
	$('.sources_footer')
		.bind('click',function(){
			$.get('http://go.gdwebdev01.com/addressbook/ws/includes/new_list.cfm',function(data){
				$('.detail_content').replaceWith(data);
				$('.detail_footer').hide();
				
				loadResults(-1);
			});
		});
	
	/*	
	$( ".source_item" ).droppable({
		accept: ".contactAvatar",
		hoverClass: "gd-state-hover",
		drop: function( event, ui ) {
			$( this )
				.addClass( "gd-state-highlight" )
				
				
			
				alert('Contact ID: ' + $(ui.draggable).attr("cid") + ' was added to List ID: ' + $(this).attr("lid"));
		}
	});
	*/
	
	$( ".container" )
		.live('mouseover',function(){	
			$( ".container" ).droppable({
				accept: ".new_contact",
				hoverClass: "gd-state-hover",
				drop: function( event, ui ) {
					$( this )
						.addClass( "gd-state-highlight" )
						var drag_contact_id = $(ui.draggable).attr("iid");
						var drag_name = $(ui.draggable).text();
						var drag_item = '<p>' + drag_name + '</p><input type="hidden" name="contact_id" class="contact_id" value="' + drag_contact_id + '">'; 
						
						$('.container_text').append(drag_item);
					
						alert('Contact ID: ' + $(ui.draggable).attr("iid") + ' was added to List ID: ' + $(this).attr("lid"));
				}
			});
	});

	
	var cardRevert ='<div id="gd_contact_detail" class="gd_detail" id="0"><div class="detail_header">Add - Edit - Etc.</div><div class="detail_content">hi</div><div class="detail_footer"></div></div>';
	
	$(".sources_content div").live('click',
	function()
	{	
		$(".sources_content div").removeClass('current');
		$(".sources_content div").removeClass('gd-state-highlight');
		$(this).addClass('current');
		if($('.container').length ==0){
			$('#gd_contact_detail').replaceWith(cardRevert);
		};
	loadResults($(this).attr("lid"));
	});	
	
	$(".results_content div").live('click',
	function()
	{	
		$(".results_content").children('.current').removeClass('current');
		$(this).addClass('current');
		loadDetail($(this).attr("iid"));
	});		
	
	$(".gd_detail .open").live('click',
	function()
	{	
		loadFocus($('.gd_detail').attr("iid"));
	});		
	
	$(".gd_detail .cards div").live('click',
	function()
	{	
		$(".gd_detail .cards div").removeClass('current');
		$(this).addClass('current');
		loadCard($('.gd_detail').attr("iid"),$(this).attr("card"));
	});				
	

	$(".gd_focus .close").live('click',
	function()
	{
		$(".gd_focus").remove();
	});
	
	$(document).keyup(function(e) {
	  if (e.keyCode == 13) { $('.save').click(); }     // enter
	  if (e.keyCode == 27) { $('.gd_focus .close').click(); }   // esc
	});
	
	/*function editShow(){ $('.hoverEdit').show();}
	
	function editHide() { $('.hoverEdit').hide();}
	
	$('.detail_item_header').hoverIntent({
		over: editShow,
		out: editHide,
	});*/
	
	/*$('.detail_item_header').live('hover', 
		function()
			{
				$('.hoverEdit').toggle();
			}
	);*/
	

	
	
	

	/*


	$( "#contact" ).autocomplete({
		source: function( request, response ) {
			lkupListId = $(this).attr("listId");
			$.ajax({
				url: "ws/contacts.cfm",
				dataType: "json",
				data: {
					lkupMaxows: 20,
					lkupVal: request.term
				},
				success: function( data ) {
					response( $.map( data.contacts, function( item ) {
						return {
							id: item.contactId,
							label: item.contactFirstName + ' ' + item.contactLastName,
							value: ''//item.contactFirstName + ' ' + item.contactLastName
						}
					}));
				}
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			clearResults();
			loadDetail(ui.item.id);
			//var this_queryString = 'index.cfm?fuseaction=results&gdSearchMethod=' + ui.item.id + '&gdHood=' + ui.item.label;
			//alert('rad');
			//log( ui.item ?
			//	"Selected: " + ui.item.label :
			//		"Nothing selected, input was " + this.value);
		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
	
	*/
	
});


	// Arrow Keys
	/*
	$('#lists li').keydown(function (e) {
	  var keyCode = e.keyCode || e.which,
	      arrow = {left: 37, up: 38, right: 39, down: 40 };
	
	  switch (keyCode) {
	    case arrow.left:
	      $()
	    break;
	    case arrow.up:
	      //..
	    break;
	    case arrow.right:
	      //..
	    break;
	    case arrow.down:
	      //..
	    break;
	  }
	});
	*/



// Clear Results
function clearResults() {
	$(".sources_content div").removeClass('current');
	resultsHeader = '<div class="results_header">See Detail</div>';
	$('.results_header').replaceWith(resultsHeader);	
	var resultsContent = '<div class="results_content">---&gt;</div>';
	$('.results_content').replaceWith(resultsContent);		
};

// Load Results

function loadResults(id) {
	var lkupListId = id;

	$.ajax({
		method: "get",
		url: "ws/contacts.cfm",
		dataType: "json",
		data: {
			lkupMaxows: 20,
			lkupListId: lkupListId
		},
		success: function( data ) {
			if (data.contacts.length == 1) {
				resultsHeader = '<div class="results_header">' + data.contacts.length + ' Contact</div>';
			} else {
				resultsHeader = '<div class="results_header">' + data.contacts.length + ' Contacts</div>';
			};
			$('.results_header').replaceWith(resultsHeader);		
			var resultsContent = '<div class="results_content">';
			 $.map( data.contacts, function( item ) {
				resultsContent += '<div iid="' + item.contactId + '"class="new_contact">' + item.contactLastName + ', ' + item.contactFirstName + '</div>';
			});
			resultsContent += '</div>';
			$('.results_content').replaceWith(resultsContent);		
		},
		error: function( data ) {
			resultsHeader = '<div class="results_header">No Contacts</div>';
			$('.results_header').replaceWith(resultsHeader);	
			var resultsContent = '<div class="results_content">...</div>';
			$('.results_content').replaceWith(resultsContent);						
		}	
	});

    return false;
};

// Load Detail

function loadDetail(id) {
	var lkupItemId = id;
	if ($('.container').length != 0){
		var check = confirm('are you sure you want to leave list function')
		if (check==false){
			return false;
		}
	}

		
	$('.gd_focus').remove();

	$.ajax({
		method: "get",
		url: "ws/contact.cfm",
		dataType: "json",
		data: {
			lkupItemId: lkupItemId
		},
		success: function( data ) {
			 $.map( data.contacts, function( item ) {
				html_start = '<div id="gd_contact_detail" class="gd_detail" iid="' + item.contactId + '">';
				html_header = '<div class="detail_header">Add - Edit - Etc.</div><div class="detail_item_header"><div class="contactAvatar" cid="' + item.contactId + '"></div><div class="nameHeader">' + item.contactLastName + ', ' + item.contactFirstName + '<div class="hoverEdit">Edit</div><div class="contact_delete"></div></div><div class="cards"><div card="contact" class="current">Contact</div><div card="activity">Activity</div><div card="websites">Website Account</div><div card="reports">Reports</div><div card="listings">Searches/Favorites</div><div card="notes">Notes</div></div></div>';
			});
			
			var html_content = '<div class="detail_content">';
			
			html_content += '<fieldset class="lists"><legend>Lists/Groups</legend>';
			 $.map( data.lists, function( item ) {
				html_content += '<div listId="' + item.listId + '">' + item.listTitle + '</div>';
			});
			html_content += '</fieldset></div>';
			
			html_end = '</div>';
			
			html = html_start + html_header + html_content + html_end;
			$('.gd_detail').replaceWith(html);
		},
		error: function( data ) {
			var html = '<div id="gd_contact_detail" class="gd_detail" id="-1"><div class="detail_header">Add - Edit - Etc.</div><div class="detail_item_header">Contact</div><div class="detail_content"><p>Something is wrong...</p></div></div></div>';
			$('.gd_detail').replaceWith(html).fadeIn('slow');					
		}	
	});
	
	 return false;
};	

// Load Card

function loadCard(id, card) {
	var lkupItemId = id;
	var lkupUrl = 'ws/contact_' + card + '.cfm';
	$('.gd_focus').remove();

	$.ajax({
		method: "get",
		url: lkupUrl,
		dataType: "html",
		data: {
			lkupItemId: lkupItemId
		},
		success: function(data) {
		    
		    html = '<div class="detail_content">' + data + '</div>';
		    
			$('.detail_content').replaceWith(html);
		},
		error: function( data ) {
			var html = '<div id="gd_contact_detail" class="gd_detail" id="-1"><div class="detail_header">Add - Edit - Etc.</div><div class="detail_item_header"></div><div class="detail_content"><p>Something is wrong...</p></div></div></div>';
			$('.gd_detail').replaceWith(html).fadeIn('slow');					
		}	
	});
	
	 return false;
};

	


// Load Focus

function loadFocus(id) {
	
	var lkupItemId = id;
	
	$.ajax({
		method: "get",
		url: "ws/contacts.cfm",
		dataType: "json",
		data: {
			lkupItemId: lkupItemId
		},
		success: function( data ) { 
			 $.map( data.contacts, function( item ) {
				html = '<div class="gd_focus" did="' + item.contactId + '"><div class="focus_header">Edit!?</div><div class="focus_item_header">' + item.contactLastName + ', ' + item.contactFirstName + '</div><div class="focus_content"><div class="close">Close Detail</div></div></div>';
			});
			$("#gd_app_main").prepend(html).fadeIn('slow');
		},
		error: function( data ) {
			var html = '<div class="gd_focus" did="-1"><div class="focus_header">Contact</div><div class="focus_content"><div class="close">Close Detail</div><p>Something is wrong...</p></div></div></div>';
			$('#gd_app_main').prepend(html).fadeIn('slow');				
		}	
	});

    return false;
};

;









