//  @import com.hedgehoglab.application.view.BaseView;
//  @import com.hedgehoglab.ui.TabPane;
//  @import com.hedgehoglab.collection.LinkedList;
//  @import com.hedgehoglab.solomon.helper.DealsHelper;
//  @import com.hedgehoglab.solomon.helper.ContactViewHelper;
//	@import com.hedgehoglab.solomon.helper.UserHelper;

//	@import com.hedgehoglab.solomon.helper.TaskViewHelper;
//	@import com.hedgehoglab.solomon.helper.NoteViewHelper;
//	@import com.hedgehoglab.solomon.helper.QTipViewHelper;
		
function DealsEditView(scope){
	BaseView.call(this,scope);
	DealsHelper.call(this,scope);
	ContactViewHelper.call(this,scope);
	UserHelper.call(this,scope);
	
	TaskViewHelper.call(this,scope);
	NoteViewHelper.call(this,scope);
	QTipViewHelper.call(this,scope);
	
	var _this=this;
	var _users=null;
	
	this.edit=function(model){

		_this.render_template_with_view('deals/deals-edit-it',model,_this,function(rendered_template){
			//	Hook onto the deal and clone it!
			var _working_deal=$.extend({},model.deal,true);
			
			$('.general_toolbar ul li.the-progress-indicator').hide().attr('style','');
			
			$('.working-pane .scroll-pane',scope.element).removeClass('loading-pane');
			$('.working-pane',scope.element).html(rendered_template);
			
			//	Hook onto the cancel button
			$('#link-deal-cancel',scope.element).bind('click',function(e){
				_this.raise_event('on_cancel_edit_clicked',{});
				return false;
			});
	
			//	Hook onto the save button
			$('#save-deal-action',scope.element).bind('click',function(e){
				// Show the save dooda!
				// Hide the buttons
				$('.gen-toolbar ul li',scope.element).hide();
				$('.gen-toolbar ul li.the-progress-indicator',scope.element).show();
				_this.show_loading();
				_this.raise_event('on_save_deal_clicked',{deal:_working_deal});
				return false;
			});
			
			//	Name field
			$("input[name='name']",scope.element).bind('keyup',function(e){
				_working_deal.name=$(this).val();
			});
			
			//	Assigned to field
			$("select[name='assignedTo']",scope.element).bind('change',function(e){
				_working_deal.assignedTo=$(this).val();
			}).trigger('change');
			 			
			//	Description field
			$("textarea[name='description']",scope.element).bind('keyup',function(e){
				_working_deal.description=$(this).val();
			});
			
			//	Amount field
			$("input[name='amount']",scope.element).bind('keyup',function(e){
				_working_deal.amount=$(this).val();
			});
			
			//  Expected Close field
			$("input[name='expectedClose']",scope.element).datepicker({
				clickInput:true,
				constrainInput:true,
				dateFormat:'dd M yy',
				minDate:Date.today(),
				showButtonPanel:true,
				numberOfMonths:1
			});
			
			$("input[name='expectedClose']",scope.element).bind('change',function(e){
				var expectedClose=$("input[name='expectedClose']",scope.element).val();
				expectedClose = Date.parse(expectedClose);
				utc_expectedClose = expectedClose.toString("yyyy-MM-ddTHH:mm:ssZ");
				_working_deal.expectedClose=utc_expectedClose;
			});
			
			
			$("#slider-range-min",scope.element).slider({
				range: "min",
				value: parseInt($("input[name='probability']",scope.element).val()),
				min: 0,
				max: 100,
				slide: function(event, ui) {
					$("input[name='probability']",scope.element).val(ui.value);
					_working_deal.probability=ui.value;
					$("span.inner-label",scope.element).html(ui.value+"%");
				}
			});
		});
		
		//	Render the edit view
		//	Is this a new or exisitng deal?
		// var deal={deal:lookups['deals'][model.model[0]]};
		// 		
		// 		var _model=$.extend({},lookups,deal,{users:users});
		// 		_this.render_template_with_view('deals/deals-edit-it',_model,_this,function(rendered_template){
		//             $('.working-pane',scope.element).html(rendered_template);
		// 
		// 			//	Hook onto save button
		// 			
		// 			//	Hook onto cancel button
		// 			$('#link-deal-cancel',scope.element).bind('click',function(e){
		// 				_this.raise_event('on_edit_deal_canceled',{});
		// 				return false;
		// 			});
		// 			
		// 			$("#slider-range-min",scope.element).slider({
		// 				range: "min",
		// 				value: parseInt($("input[name='probability']",scope.element).val()),
		// 				min: 0,
		// 				max: 100,
		// 				slide: function(event, ui) {
		// 					$("input[name='probability']",scope.element).val(ui.value);
		// 					// $("#amount").val('$' + ui.value);
		// 				}
		// 			});
		// 			// $("#amount").val('$' + $("#slider-range-min").slider("value"));
		// 			
		// 		
		// 		});
	};
	
	this.render=function(model,lookups,users,filter,__selected){
		// var length=model.model.length;
		// 	var _model=$.extend({},lookups,{deal:model.model[0]},{users:users});
		// 	if(length==0){
		// 		_this.render_template_with_view('deals/deals-non-selected',{},_this,function(rendered_template){
		// 			$('.working-pane',scope.element).html(rendered_template);
		// 		});
		// 	}else if(length==1){
		// 		render_one_deal($.extend({},lookups,{deal:model.model[0]},{users:users}));
		// 	}else if(length>1){
		// 		render_many_deals($.extend({},lookups,{selected:model.model},{users:users}),filter);
		// 	}
	};
	
	this.render_no_deals=function(e){
		_this.render_template_with_view('deals/deals-non-selected',{},_this,function(rendered_template){
			$('.working-pane',scope.element).removeClass('loading-pane');
			$('.working-pane',scope.element).html(rendered_template);
		});
	};
	
	this.render_one_deal=function(model){
		render_one_deal(model);
	};
	
	this.render_many_deals=function(model){
		render_many_deals(model);
	};
		
	function render_many_deals(model){
		_this.set_users(model.users);
		
		_this.render_template_with_view('deals/deals-many-selected',model,_this,function(rendered_template){
			$('.working-pane',scope.element).removeClass('loading-pane');
			$('.working-pane',scope.element).html(rendered_template);
			
			$('.hide',scope.element).hide().removeClass('hide');
			
			$('#link-deal-delete',scope.element).bind('click',function(e){
				_this.raise_event('on_delete_deal_clicked',{
					"paint":function(e){
						alert("PAINTING!!!");
					}
				});
				return false;
			});
			
			
			// Handle bulk assign
			$('#link-deal-assign',scope.element).bind('click',function(e){
				$('span#link-deal-assign-options #assign-to-user-select',scope.element).val("-1");
				$('#general_toolbar',scope.element).hide();
				$('span#link-deal-assign-options',scope.element).show();
				return false;
			});
			
			$('span#link-deal-assign-options #assign-to-user-select',scope.element).bind('change',function(e){
				hide_assign_options();
				
				_this.raise_event('on_reassign_deal_clicked',{
					user:model.users[$(this).val()]
				});
			});
			
			$('span#link-deal-assign-options a#cancel-deal-assign',scope.element).bind('click',function(e){
				hide_assign_options();
				return false;
			});
			
			function hide_assign_options(){
				$('span#link-deal-assign-options',scope.element).hide();
				$('#general_toolbar',scope.element).show();
			}
			
			
			
			// Handle the status change click
			$('#link-deal-status',scope.element).bind('click',function(e){
				$('#general_toolbar',scope.element).hide();

				$('span#link-deal-status-options',scope.element).show();
				
				_this.raise_event('on_status_deal_clicked',{});
				return false;
			});
			
			// On status cancel
			$('span#link-deal-status-options a#cancel-deal-status',scope.element).bind('click',function(e){
				hide_status_options();
				return false;
			});
			
			function hide_status_options(){
				$('span#link-deal-status-options',scope.element).hide();
				$('#general_toolbar',scope.element).show();
			}

			// Handle the status clicks
			$('span#link-deal-status-options a#inprogress-deal-status',scope.element).bind('click',on_handle_deal_status_change);
			$('span#link-deal-status-options a#won-deal-status',scope.element).bind('click',on_handle_deal_status_change);
			$('span#link-deal-status-options a#lost-deal-status',scope.element).bind('click',on_handle_deal_status_change);
			
			function on_handle_deal_status_change(e){
				alert("asdasd");
				hide_status_options();
				return false;
			};
		});
	}
	
	//	Delegate methods required by note behaviour
	this.get_note_template_name=function(){
		return 'deal_note';
	};
	
	this.get_note_holder_template_name=function(){
		return 'deal_note_holder';
	};
	
	//	

	
	
	function render_one_deal(model){
		_this.set_users(model.users);
		_this.set_deal_contacts(model.contacts);
		// Sort the contact!
		_sorted_indeces = _this.sort_contacts(model.all_contacts);
		model._sorted_indeces = _sorted_indeces;
		
				// console.log(model);
		_this.render_template_with_view('deals/deals-edit',model,_this,function(rendered_template){
			$('.working-pane',scope.element).removeClass('loading-pane');
            $('.working-pane',scope.element).html(rendered_template);
			
			new TabPane({
				element:$(scope.element),
				tabs:'div.Tabs ul li',
				pane:'div.contact-tab-panel',
				current:'current',
				hide:'hide'
			});
			
			//	Edit link
			$('#link-deal-edit',scope.element).bind('click',function(e){
				_this.raise_event('on_edit_clicked',{});
				return false;
			});

			//	Edit link
			$('#link-deal-delete',scope.element).bind('click',function(e){
				_this.raise_event('on_delete_deal_clicked',{
					"paint":function(e){
						alert("PAINTING!!!");
					}
				});
				return false;
			});
			
			
			// Format the date
			// $('.timeago',scope.element).timeago();
			
			//	Hook onto the deal status links
			$('ul.status li a',scope.element).bind('click',function(e){
				// alert("Changing the status");
				var id=$(this).attr('id');
				var t=this;
				// var u=inprogress
				_this.raise_event('on_status_change_clicked',{
					status:_this.what_status(id),
					update:function(e){
						//	Re-render the chosen item
						$('ul.status li.current',scope.element).removeClass('current');
						$(t).parent().addClass("current");
						
						//	Change the overview !
						
					}
				});
				return false;
			});
			
			var deal_task_list=$('.working-pane #deal-tasks-list',scope.element);
			_this.task_behaviour(model,deal_task_list,_this);

			//	Overide on_add_note_clicked to add model data for deal and contact
			_this.on_add_task_clicked=function(e){
				e.task.contact=$("#add-task-contact",scope.element).val();
				e.task.deal=model.deal;
				// console.info("The task is ",e.task);
				_this.raise_event('on_add_new_task',e);
			};

			
			var deal_note_list=$('.working-pane #deal-notes-list',scope.element);
			_this.note_behaviour(model,deal_note_list,_this);
			
			//	Overide on_add_note_clicked to add model data for deal and contact
			_this.on_add_note_clicked=function(e){
				e.contact=$("#add-note-contact",scope.element).val();
				e.deal=model.deal;
				_this.raise_event('on_add_note_clicked',e);
			};
			
			/**
			*	Render Associated users, contacts and companies
			*/
			var deal_involved_list=$('.deals-meta-pane #deals-involved-list',scope.element);
			
			render_contacts_from_groups(model.contacts);
			render_users_from_groups(model.deals_users_lookup);
			
			function add_contact_to_group(contact){
				// var dd=$('<dd>'+_this.contact_fullname(contact)+'</dd>');
				_this.render_template_with_view('contact_involved',{contact:contact},_this,function(contact_markup){
					var dd=$(contact_markup);
					var dt=$('#contacts-involved',deal_involved_list);
					dd.insertAfter(dt);	
					
					// Render the qtip template for this user
					// console.log(contact);
					_this.render_template_with_view('contact_involved_qtip',{contact:contact},_this,function(qtip_markup){
						_this.qtip_that_mofo(dd,_this.contact_fullname(contact),qtip_markup);
					});
				});
			}
			
			function add_user_to_group(user){
				// var dd=$('<dd>'+_this.contact_fullname(contact)+'</dd>');
				_this.render_template_with_view('user_involved',{user:user},_this,function(user_markup){
					var dd=$(user_markup);
					var dt=$('#users-involved',deal_involved_list);
					dd.insertAfter(dt);	
					
				
					// Render the qtip template for this user
					_this.render_template_with_view('user_involved_qtip',{user:user},_this,function(qtip_markup){
						_this.qtip_that_mofo(dd,_this.user_fullname(user),qtip_markup);
					});
				});
			}

			function render_contacts_from_groups(contacts){
				$.each(contacts,function(contact_index,contact){
					add_contact_to_group(contact);
				});
			}

			function render_users_from_groups(users){
				$.each(users,function(user_index,user){
					add_user_to_group(user);
				});
			}			
			
			function render_contact(contact){
				
			}

			// function on_task_added(task){}
		});
	}
	
	this.show_loading=function(e){
		// scope.element.html("");
		$('.working-pane #deals-NT',scope.element).html("");
		$('.working-pane #deals-NT',scope.element).addClass('loading-pane');
	}
}