function PTPCheckList(targetDiv) {
	this.targetDiv = targetDiv;
	this.me = "PTPChecklist_"+targetDiv;
	window[this.me] = {
		Model:{
			foobar:"Hello World",
			choices:{},
			criteria:{},
			clearCount:function() {
				jQuery.each(__.Model.choices, function(i){
					var self = __.Model.choices[i];
					self.count=0;
				});
				
				jQuery.each(__.Model.criteria, function(i){
					var self = __.Model.criteria[i];
					self.checked=0;
				});
				
				return true;
			},
			recordResults:function(){
					
			}
		},
		View:{
			init:function(goodstuff, badstuff){
				$("#"+__.Controller.settings.targetDiv).html("<p style='margin-top:90px;'>Calculating Results</p><p><img border='0' src='/_layouts/peopletopeople/includes/images/loadingAnimation.gif'/></p>");
				
				
				var goodstuff = goodstuff;
				var badstuff = badstuff;
				if (__.Controller.lastCall != "init") {
					__.View.fireThickbox(1);
					setTimeout(function(){__.View.drawResults(goodstuff, badstuff)}, 4000);
				}
			},
			showAll:function(goodstuff, badstuff){
				__.View.drawResults(goodstuff, badstuff)
			},
			drawResults:function(goodstuff,badstuff){
					$("#"+__.Controller.settings.targetDiv).empty();
					jQuery.each(goodstuff, function(i){
						var self = goodstuff[i];
						$("<div>&nbsp;</div>")
							.attr({id:self.id+self.on, title:"Click to learn more about this program."})
							.css({cursor:"pointer"})
							.click(function(){
								window.location = self.uri;								
							})
							.appendTo("#"+__.Controller.settings.targetDiv);									   
					});
					
					/*jQuery.each(badstuff, function(i){													 
						var self = badstuff[i];
						$("<div>&nbsp;</div>")
							.attr({id:self.id+self.off, title:"Click to learn more about this program."})					
							.css({cursor:"pointer"})
							.click(function(){
								window.location = self.uri;								
							})
							.appendTo("#"+__.Controller.settings.targetDiv);
					});	*/
						
					
					
				__.View.fireThickbox();
				
				
			},
			fireThickbox:function(modalval) {
				
				$.openDOMWindow({
						closeImagePath:'/PTP%20Photo%20Galleries/SuccessPlan/CloseButton.gif',						
						loader:1,
						modal:modalval,
						loaderHeight:16, 
						loaderWidth:17, 
						windowSourceID:'#'+__.Controller.settings.targetDiv 
				}); 
				return false;
			},
			clearAllChoices:function(){
				$("#"+__.Controller.settings.targetDiv).empty();
			},
			hideAllChoices:function(){
				/*jQuery.each(__.Model.choices, function(i){
					$("#"+i).hide();									   
		 		});*/
			}
		},
		Controller:{
			settings:{},
			init:function(targetForm) {
				__.Controller.settings.targetForm = targetForm;
				
				
				
				//Apply Pre Recorded Data to Criteria
				if (document.cookie || (document.cookie != "")) {
					var noCookies = false;
				} else {
					var noCookies = true;
				}
				jQuery.each(__.Model.criteria, function(i) {
					var target= $("#"+__.Controller.settings.targetForm+" #"+__.Model.criteria[i].id);
					target
						.attr("value","1")
						.data("choice", __.Model.criteria[i].choices);
					if((noCookies && __.Model.criteria[i].checked) || $.cookie(__.Model.criteria[i].id)) {
						//pre check values
						target.val(["1"])
					}					
					
				});
				
				//Bind Submit Event to someething
				//Bind events to a click
				$("#"+__.Controller.settings.targetForm+" input:button").click(function(event){
					__.Controller.checkOptions("button")																																			
					event.preventDefault();
				});
				
				$("#LearnMoreGraphic").css({cursor:"pointer"}).click(function(){
						__.Controller.lastCall = "seeAll"	;																	
						__.View.showAll(__.Model.choices,"");
				});
				
				//Bind events to a form's submit action
				/*$("#"+__.Controller.settings.targetForm).attr("action","javascript:void(0);").submit(__.Controller.checkOptions, function(event){
					event.preventDefault();
				});*/
				
				
				//Bind events to Criteria
				/*for(var i=0; i < __.Model.criteria.length; i++) {
					
					$("#"+__.Controller.settings.targetForm+" #"+__.Model.criteria[i].id);
						//.change(__.Controller.checkOptions);
				
				}*/
				
				//Draw initial Choice boxes
				__.Controller.checkOptions("init");
				
				//$("<a>HelloWorld</a>").click(__.View.doSomething).appendTo("body");	
			},
			checkOptions:function(caller) {
				//__.View.hideAllChoices();
				__.View.clearAllChoices();
				__.Model.clearCount();
				__.Controller.lastCall = caller;
				
				
				var selected = $("#"+__.Controller.settings.targetForm+" input:checked");
				var unselected = $("#"+__.Controller.settings.targetForm+" input:checkbox").not(":checked");
				
				selected.each(function(t) {
					var self = $(this).attr("id");
					$.cookie(self, "1", {path:"/",expires:30});
				});
				
				
				unselected.each(function(t) {				   
					if($(this).data("choice")) {							
						jQuery.each($(this).data("choice"), function(i,val) {
							var self = val;
							__.Model.choices[self.id].count+=self.weight;
							
						});
					}
					var self = $(this).attr("id");
					if(__.Model.criteria[self]) {
						__.Model.criteria[self].checked = 1;	
					}
					if($.cookie(self)){
						$.cookie(self, null, {path:"/",expires:30});
					}
					
				});
				__.Controller.displayResults();
				
				
				
				
			},
			
			compareCounts:function(a,b) {
				return b.count - a.count;
			},
			displayResults:function(){
				
				pool = new Array;
				drain = new Array;
				
				jQuery.each(__.Model.choices, function(i){
					var self = __.Model.choices[i];
					if(self.count >= self.threshold) {
						pool.push(self);
								   
					} else {
						drain.push(self);	
					}
				});
				pool.sort(__.Controller.compareCounts);
				
				if(pool.length) {
					//pool wasn't empty
					__.View.init(pool, drain);
				} else {
					//there were no valid choices
					__.View.init([__.Model.choices["SGC"]],"");
				}
			}
		}
	};
	
	var __ = window[this.me]
	
	this.Controller = __.Controller;
	this.Controller.settings.targetDiv = this.targetDiv;
	this.Model = __.Model;
		
	
}

PTPCheckList.prototype.addChoice = function(object) {
	this.Model.choices[object.id] = object;
	this.Model.choices[object.id].count = 0;
}

PTPCheckList.prototype.addCriteria = function(object) {
	this.Model.criteria[object.id] = object;			
}

PTPCheckList.prototype.init = function(targetForm) {
	if (!$.cookie("hasVisited")) {
		$.cookie("hasVisited", "true", {path:"/",expires:30});			  
	}
	this.Controller.init(targetForm)
}

