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

function DealsListView(scope){
	BaseView.call(this,scope);
	DealsHelper.call(this,scope);
	ContactViewHelper.call(this,scope);
	
	var _this=this;
	var all_checked=false;
	var _all_checked_clicked=false;
	var _checkboxes=null;
   	var _firsttime=true;
	var _check_clicked=false;
	var _disabled=false;
	var _has_rendered=false;
	
	_helpers={};
	
	function update(){
		if(_all_checked_clicked!=true){
			var _indices=$(".working-pane .scroll-pane dd input[checked='true']",scope.element);
			var _sle=[];
			$.each(_indices,function(i,n){
				_sle.push($(n).attr('rel'));
			});
		    _this.raise_event('on_selected_deals_changed',{model:_sle});
		}
	}
	
	this.render=function(model){
		_this.render_template_with_view('deals/deals-list',model,_this,function(rendered_template){
			$('.working-pane .scroll-pane',scope.element).removeClass('loading-pane');
            $('.working-pane .scroll-pane',scope.element).html(rendered_template);

			_checkboxes=$('.working-pane .scroll-pane dd input',scope.element);

			$('.working-pane .scroll-pane dd',scope.element).hover(function(e){
				if(!_disabled) $(this).addClass('hover');						
			},function(e){
				if(!_disabled) $(this).removeClass('hover');						
			});
			
			$('.working-pane .scroll-pane dd a',scope.element).bind('click',function(e){
				// return false;
			});
			
			$('.working-pane .scroll-pane dd',scope.element).bind('click',function(){
				if(!_disabled){
					
					if(_check_clicked==true){
						_check_clicked=false;
						// update();
						return true;
					}else{
						$('dd input',scope.element).removeAttr('checked');
						$('dd ',scope.element).removeClass('checked');
						$(this).addClass('checked');
						$('input',this).attr('checked',true);
						update();
						return false;					
					}  
				
				}else{
					return false;
				}
            });

			//   bind events when contacts in the contacts list are checked
            _checkboxes.bind('click',function(e){
				if(!_disabled){
					$(this).parent().addClass('checked');
					_check_clicked=true;
					if(_all_checked_clicked==false) update();
				}else{
					return false;
				}
				
			});
		});
	
		if(_has_rendered==false){
			_has_rendered=true;
			$('#select-all-contacts-action',scope.element).bind('click',function(e){
				$('.working-pane .scroll-pane dd input',scope.element).attr('checked','checked');
				// if(_all_checked_clicked==false) 
				update();				
			});
			
			$('#select-no-contacts-action',scope.element).bind('click',function(e){
				$('.working-pane .scroll-pane dd input',scope.element).removeAttr('checked','checked');
				update();				
			});
		}
	};
	
	//	Hook into the dropdown to choose the types of deals to view
	$('#deals-filter',scope.element).bind('change',function(e){
		//alert("asdasd");
		_this.raise_event('on_deals_filter_changed',{filter_by:$(this).val()});
	});

	this.show_loading=function(e){
		// scope.element.html("");
		$('.working-pane .scroll-pane',scope.element).html("");
		$('.working-pane .scroll-pane',scope.element).addClass('loading-pane');
	}
	
	this.disable=function(e){
		_disabled=true;
		$('input,select',scope.element).attr('disabled','disabled');
		$(scope.element).addClass('disabled');
		
	};
	
	this.enable=function(e){
		_disabled=false;
		$('input,select',scope.element).removeAttr('disabled');
		$(scope.element).removeClass('disabled');
	};
}