;(function($){
	
	var signup = {
	
		getFormElements : function(){
			var obj = {};
			obj.keyword = $('#keyword');
			obj.searchBtn = $('#searchBtn');
			obj.popShopsFeedTarget = $('#popShopsFeedTarget');
			obj.dobaFeedTarget = $('#dobaFeedTarget');
			obj.popShopsCount = $('#popShopsCount');
			obj.dobaCount = $('#dobaCount');
			obj.popShopsTotal = $('#popShopsTotal');
			obj.dobaTotal = $('#dobaTotal');
			obj.totalCount = $('#totalCount');
			return obj;
		},
				
		getFeeds : function(){
			var obj = this.getFormElements();
			var sendData = arguments[0] ? {keyword:$.trim(arguments[0])} : $(obj.keyword).serialize();
			var popShopsURL = '/builder/ajaxPopShopsFeed.htm';
			var dobaURL = '/builder/ajaxDobaFeed.htm';
			$(obj.totalCount).html(0);
			this.displayLoading();
						
			$.ajax({
			  type: 'POST',
			  url: popShopsURL,
			  data: sendData,
			  success: function(data) {
			    var popShopsTotal = 0;
			    if(data.length){
				    var totalCount = $(obj.totalCount).text()*1;
				    $(obj.popShopsFeedTarget).html(data);
				    popShopsTotal = $('#popShopsTotal').val()*1;
				    totalCount += popShopsTotal;
				    $(obj.totalCount).text(totalCount);
				  }else{
				  	$(obj.popShopsFeedTarget).html('No product found.  Please try later.');
				  }
			  },
			  timeout:10000,
			  error: function(xhr, ajaxOptions, thrownError) {
			  	if(ajaxOptions=='timeout') {
			  		$(obj.popShopsFeedTarget).html('No product found.  Please try later.');
			  	}
			  }
			});
			
			$.ajax({
			  type: 'POST',
			  url: dobaURL,
			  data: $(obj.keyword).serialize(),
			  success: function(data) {
			    var dobaTotal = 0;
			    if(data.length){
				    var totalCount = $(obj.totalCount).text()*1;
				    $(obj.dobaFeedTarget).html(data);
				    dobaTotal = $('#dobaTotal').val()*1;
				    totalCount += dobaTotal;
				    $(obj.totalCount).text(totalCount);
				  }else{
				  	$(obj.dobaFeedTarget).html('No product found.  Please try later.');	
				  }
			  },
			  timeout:10000,
			 	error: function(xhr, ajaxOptions, thrownError) {
			  	if(ajaxOptions=='timeout') {
			  		$(obj.dobaFeedTarget).html('No product found.  Please try later.');	
			  	}
			  }
			});
		},
		
		bindSearchBtn : function(){
			var obj = this.getFormElements();
			var signupObj = this;
			$(obj.searchBtn).unbind('click').bind('click', signupObj, function(){
				var keyword = $.trim($(obj.keyword).val());
				$(this).blur();
				if(keyword.length > 0) {
					signupObj.getFeeds();
				}
			});
		},
			
		bindEnterKeyToKeyword : function() {
			var obj = this.getFormElements();
			var signupObj = this;
			$(obj.keyword).unbind('keypress').bind('keypress', signupObj, function(e){
				if(e.keyCode == 13) {
					var keyword = $.trim($(obj.keyword).val());
					$(this).blur();
					if(keyword.length > 0) {
						signupObj.getFeeds();
					}	
				} 
			});
		},
		
		bindKeywordOnFocus : function() {
			var obj = this.getFormElements();
			$(obj.keyword).focus();
		},
		
		displayLoading : function() {
			var obj = this.getFormElements();
			$(obj.popShopsFeedTarget).html('Loading...');
			$(obj.dobaFeedTarget).html('Loading...');
		}
		
	};
				
	$(document).ready(function(){
		signup.displayLoading();	
		signup.bindKeywordOnFocus();
		signup.getFeeds('converse');
		signup.bindEnterKeyToKeyword();
		signup.bindSearchBtn();
	});
	
})(jQuery);

