var ConsumerInterface = {
		
	formSetup : function(formElem, contentElem, listElem, model){
		$(formElem).ajaxForm({
			success: function(response){
				$(contentElem).html(response);
				$(listElem).load('/zone/get-item-list/model/'+model);
				UserInterface.formSetup(formElem, contentElem, listElem, model);
			}
		});
	},
	
	
    findRestaurant : function() {
		$('#find-restaurant').ajaxForm({
			success: function(response) {
				if (response == 'delivery' || response == 'takeout'){
					window.location = '/restaurant/' + response;
				} 
				else {
					ConsumerInterface.dialogErrorMsg(response);
				}
			}
		});
	},
	
    displayCategoryRestaurants : function() {
		$('a.byCategory').click(function () {
			$('#contentContainer').load($(this).attr('href'), null, function(){
				ConsumerInterface.displayCategoryRestaurants();
				ConsumerInterface.toggleRestaurantDisplay();
				ConsumerInterface.hideAllRestaurantDisplay();
				ConsumerInterface.showAllRestaurantDisplay();
				ConsumerInterface.createDialog();
				$("[class^=item-description-]").hide();
			});
			return false;
		});
    },
    
    toggleRestaurantDisplay : function() {
		$('.restaurantTitle').click(function () {							
		   
		    $(this).toggleClass("restaurantOpen");	 
			$('.item-description-' + $(this).attr("id")).slideToggle("fast");

			if($(this).find("img").attr("src") == "/images/minusSign.gif"){
				$(this).find("img").attr("src", "/images/plusSign.gif");
			}
			else{
				$(this).find("img").attr("src", "/images/minusSign.gif");
			}
		});
		
		
    },
	
	showAllRestaurantDisplay : function() {
		$('#showDetails').click(function () {
			$('.restaurantTitle').addClass("restaurantOpen");
			$('[class^=item-description-]').show('fast', function(){
				$('.toggle_btn').attr('src', '/images/minusSign.gif');
			});
		});
    },
	
	
    hideAllRestaurantDisplay : function() {
		$('#hideDetails').click(function () {
			$('.restaurantTitle').removeClass("restaurantOpen");
			$('[class^=item-description-]').hide('fast', function(){
				$('.toggle_btn').attr('src', '/images/plusSign.gif');
			});
		});
    },
	

	
	
/************* OLD JS ******************************/

    clearText : function(object) {
        object.value = "";
    },
    
    fillText : function(object, value) {
        if (object.value.length == 0) {
                object.value = value;
        }
    },
    
    togglePanel : function() {
		$('img.toggleBtn').click(function () {
			$(this).parent().siblings('.sort').toggle();
			
			if($(this).attr("src") == "/images/minusSign.gif"){
				$(this).attr("src", "/images/plusSign.gif");
			}
			else{
				$(this).attr("src", "/images/minusSign.gif");
			}
		});
    },
    
    toggleAcctMoreInfo : function() {
		$('[id^=moreInfo-]').hide();
		$('a.moreInfo').click(function () {
			$('#moreInfo-'+$(this).attr('href')+'-container').slideToggle();
			return false;
		});
    },
    
    deleteCall : function() {
		$('[class^=delete-]').click(function () {
			var href = $(this).attr('href');
			var domElement = $(this).parent();
			$('.dialog-error-msg').dialog('open');
			$('.dialog-error-msg').dialog('option', 'title', 'Error Message');
			$('.dialog-error-msg').dialog('option', 'buttons', { 
				'Yes': function() { 
					$.ajax({
					   type: 'POST',
					   url: href,
					   success: function(msg) {
							domElement.effect('fold',{},500);
					   }
					});
					$(this).dialog('close');
				},
				'No': function() { $(this).dialog('close'); }
			});
			$('.message').html('Are you sure?');
			return false;
		});
    },
	
    
	/* Form Plugin */
	formAccountGroupInvitee : function() {
    	//
		$('#form-acct-invitee-group').ajaxForm({
			target: '.dialog-acct-invitee-group',
			success: function() {
				$('#acct-invitee-group-list').load('/account/invitee-group',null, function(response) {
					$('.dialog-acct-invitee-group').dialog('close');
					ConsumerInterface.dialogAccountInviteeGroup();
					ConsumerInterface.formAccountGroupInvitee();
					ConsumerInterface.allowDragDrop();
					ConsumerInterface.deleteCall();
				});
			}
		});
	},
	
	formAccountInvitee : function() {
    	//
		$('#form-acct-invitee').ajaxForm({
			target: '.dialog-acct-invitee-add',
			success: function() {
			$('.acct-invitee-list').load('/account/invitee',null, function(response) {
					$('.dialog-acct-invitee-add').dialog('close');
					ConsumerInterface.dialogAccountInvitee();
					ConsumerInterface.formAccountInvitee();
					ConsumerInterface.allowDragDrop();
					ConsumerInterface.deleteCall();
				});
			}
		});
	},
	
	formAccountCreditCard : function() {
    	//
		$('[id^=form-credit-card-]').ajaxForm({
			target: '.dialog-acct-payment-credit',
			beforeSubmit: function(formData, jqForm, options) {
				var len = formData.length;
				
				var regex = /^cancel/; //regular expression defining cancel @ beginning of line
				for (var i = 0; i < len; i++) {
					if (formData[i].name.match(regex) != null) {
						$('.dialog-acct-payment-credit').dialog('close');
						return false;
					}
				}
			},
			success: function() {
				$('#acct-payment').load('/account/display-information/update/payment-information',null, function(response) {
					$('.dialog-acct-payment-credit').dialog('close');
					ConsumerInterface.dialogAccountCreditCard();
					ConsumerInterface.formAccountCreditCard();
					ConsumerInterface.deleteCall();
				});
			}
		});
	},
	
	formAccountDeliveryAddress : function() {
		$('#form-delivery').ajaxForm({
			target: '.dialog-delivery-address',
			beforeSubmit: function(formData, jqForm, options) {
				var len = formData.length;
				for (var i = 0; i <= len; i++) {
					if (len.name == 'cancelAddress') {
						$('.dialog-delivery-address').dialog('close');
						return false;
					}
				}
			},
			success: function(response) {
				$('#acct-delivery-details').load('/account/display-information/update/delivery-address',null, function(response) {
					$('.dialog-delivery-address').dialog('close');
					ConsumerInterface.dialogAccountDeliveryAddress();
					ConsumerInterface.formAccountDeliveryAddress();
					ConsumerInterface.toggleAcctMoreInfo();
					ConsumerInterface.deleteCall();
				});
			}
		});
	},
	
	formAccountPersonalInfo : function() {
		$('#personal-form').ajaxForm({
			target: '.dialog-account-personal-info',
			beforeSubmit: function(formData, jqForm, options) {
				if (formData[8].name == 'cancelPersonal') {
					$('.dialog-account-personal-info').dialog('close');
					return false;
				}
			},
			success: function(response) {
				$('.dialog-account-personal-info').dialog('close');
				$('#acct-personal-info-details').load('/account/display-information/update/personal-information',null, function(response) {
					ConsumerInterface.dialogAccountPersonalInformation();
					ConsumerInterface.formAccountPersonalInfo();
				});
			}
		});
	},
	

	
    
    /* UI Dialog */
	createDialog : function() {
		$('[class^=dialog-]').hide().dialog({
			autoOpen: false,
			modal: true,
			bgiframe: true,
			resizable: false,
			draggable:false,
			height: 'auto',
			width: 'auto',
			hide: 'drop'
		});
	},
    
	dialogAccountInviteeGroup : function() {
		$('a.addInviteeGroup').click(function() {
			var address = $(this).attr('href');
			
			$('.dialog-acct-invitee-group').load(address, null, function() {
				ConsumerInterface.formAccountGroupInvitee();
				$('.dialog-acct-invitee-group').dialog('open');
				$('.dialog-acct-invitee-group').dialog('option', 'title', 'Invitee Group');
			});
			return false;
	    });
	},
    
	dialogAccountInvitee : function() {
		$('a.addInvitee').click(function() {
			var address = $(this).attr('href');
			
			$('.dialog-acct-invitee-add').load(address, null, function() {
				ConsumerInterface.formAccountInvitee();
				$('.dialog-acct-invitee-add').dialog('open');
				$('.dialog-acct-invitee-add').dialog('option', 'title', 'Invitee');
			});
			return false;
	    });
	},
    
	dialogAccountCreditCard : function() {
		$('a.modifyCredit').click(function() {
			var address = $(this).attr('href');
			
			$('.dialog-acct-payment-credit').load(address, null, function() {
				ConsumerInterface.formAccountCreditCard();
				$('.dialog-acct-payment-credit').dialog('open');
				$('.dialog-acct-payment-credit').dialog('option', 'title', 'Credit Card Information');
			});
			return false;
	    });
	},
    
	dialogAccountDeliveryAddress : function() {
		$('a.modifyAddress').click(function() {
			var address = $(this).attr('href');
			
			$('.dialog-delivery-address').load('/account/delivery-address/id/' + address, null, function() {
				ConsumerInterface.formAccountDeliveryAddress();
				$('.dialog-delivery-address').dialog('open');
				$('.dialog-delivery-address').dialog('option', 'title', 'Modify Delivery Addresses');
			});
			return false;
	    });
	},
    
	dialogAccountPersonalInformation : function() {
		$('#edit-personal-info').click(function() {
			$('.dialog-account-personal-info').dialog('open');
			$('.dialog-account-personal-info').dialog('option', 'title', 'Edit Personal Information');
			return false;
	    });
	},
    

    
	dialogErrorMsg : function(msg) {
		$('.dialog-error-msg').dialog('open');
		$('.dialog-error-msg').dialog('option', 'title', 'Error Message');
		$('.dialog-error-msg').dialog('option', 'buttons', { 'Close': function() { $(this).dialog('close'); } });
		$('.message').html(msg);
	},
    
	dialogHotel : function() {
		$('.hotelPosition').click(function() {
			$('.dialog-hotel').dialog('open');
			$('.dialog-hotel').dialog('option', 'title', 'Registration');
			$('.dialog-hotel').dialog('option', 'buttons', { 
				'Delivery': function() { $(this).dialog('close'); },
				'Takeout': function() { $(this).dialog('close'); },
				'Close': function() { $(this).dialog('close'); }
			});
			return false;
	    });
	},
	
	/* UI Accordion */

	createAccordion : function() {
		$('[id^=accordion-]').accordion({
			icons: {
			header: "ui-icon-triangle-1-e",
				headerSelected: "ui-icon-triangle-1-s"
			},
			autoHeight:false,
			collapsible: true,
			header: 'h3'
		});
	},
	
	/* UI Drag & Drop */

	allowDragDrop : function() {
		$(".header").droppable({
			activeClass: 'placeActive',
			hoverClass: 'placeHover',
	        drop: function(event, ui) {
				var container = this.id.split('_');
				$.ajax({
					type: "POST",
					url: "/account/add-invitee-to-group/invitee/" + ui.helper.attr('id') + "/group/" + this.id,
					success: function(request) {
						if (request == 'duplicate') {
							$('#acct-invitee-group-list #' + ui.sender[0].id).remove();
						} else if (request > 0) {
							$('#acct-invitee-group-list').load('/account/invitee-group',null, function(response) {
								ConsumerInterface.dialogAccountInviteeGroup();
								ConsumerInterface.formAccountGroupInvitee();
								ConsumerInterface.allowDragDrop();
								ConsumerInterface.deleteCall();
								ConsumerInterface.togglePanel();
								// show current group
								$('#containerGroup' + container[1]).children('.sort').show();
							});
						
						}
					},
					error: function(request, error){
						alert('error msg :' + request);
					}
				});
			}
		});

		$(".dropable > li").draggable({
		//$("#acct-invitee p.header").draggable({
			//connectToSortable: '.sort',
			containment: '#acct-invitee',
			cursorAt:{cursor: 'crosshair', bottom: -5, right: -5},
			helper: 'clone',
			opacity: 0.7
	        /*start: function(event, ui) {
				$('.sort').sortable("refresh");
				return false;
			},*/
		});
	}
	
}
