//	@import com.hedgehoglab.application.helper.BaseHelper;
	
Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function DealsHelper(scope){
	BaseHelper.call(this,scope);
	var _this=this;
	var _deal_contacts;
	
	var status_maps={
		"IN_PROGRESS":['current','',''],
		"WON":['','current',''],
		"LOST":['','','current']
	};
	
	this.set_deal_contacts=function(contacts){
		_deal_contacts = contacts;
	};
	
	this.get_deal_contact=function(id){
		$.each(_deal_contacts,function(i,n){
			   if (n.id == id) {result = n; return 0;}
		});
		return result;
	};
	
	this.is_selected=function(selected){
		if(selected){
			return 'checked=\"checked\"';
		}else{
			return sel="";
		}
		
	};
	
	this.format_created_date=function(date){
		var d=Date.parse(date);
		var e=d.toString('MMMM dS, yyyy');
		return e;
	};
	
	this.format_expectedClose=function(date){
		if(date == undefined)
			return '';
		var d=Date.parse(date);
		var e=d.toString('dd MMM yyyy');
		return e;
	};
	
	this.is_in_progress=function(deal){
		return deal.status=='IN_PROGRESS';
	};
	
	this.format_currency=function(value){
		return Number(value).formatMoney(0,'.',',');
	};
	
	this.shorten_description=function(description,len){
		return String(description); //.substring(0,len);
	};
	
	this.format_description=function(deal){
		return String(deal.description).replace(/\n/gi,'<br/>');
	};
	
	this.who_on_deal=function(deal,model){
		return model.deals[deal.id].contacts_lookup;
	};
	
	this.get_contact_from_deal=function(deal,id){
		 $.each(_deals.deal,function(i,n){
			   if (n.id == id) {result = n; return 0;}
		 });
		 return result;
	}
	
	this.status_map=function(deal,model){
		
		return status_maps[model[deal].status];
	};
	
	this.what_status=function(id){
		return ({"inprogress":0,"won":1,"lost":2})[id];
	};
	
	this.group_deals=function(deals,filter_by){
		var groups=[
			{
				"title":"In Progress",
				"deals":[]
			},
			{
				"title":"Won",
				"deals":[]
			},
			{
				"title":"Lost",
				"deals":[]
			}
		];
		
		$.each(deals,function(i,n){
			switch(n.status){
				case "IN_PROGRESS":in_progress(n);break;
				case "WON":won(n);break;
				case "LOST":lost(n);break;
			}
			
			function in_progress(deal){
				groups[0].deals.push(deal);
			}

			function won(deal){
				groups[1].deals.push(deal);
			}

			function lost(deal){
				groups[2].deals.push(deal);
			}

		});

		if(filter_by){
			switch(filter_by){
				case "-1":return groups;break;
				case "0":return [groups[0]];break;
				case "1":return [groups[1]];break;
				case "2":return [groups[2]];break;
			}
		}else{
			return groups;	
		}
		
	};
}
