jQuery.fn.not_exists = function(){return jQuery(this).length==0;}

jQuery.fn.SuperDropDownCheckbox = function(o) {
 
	var loc = "";
	var proj = "";
	
	 // Defaults
	 var o = jQuery.extend( {
		SchemeShortcutMain: 'SchemeShortcutMain',
		Collapser: 'collapser',
		LocationHiddenField: 'hidLocations',
		SchemeHiddenField: 'hidProjects',
		SelectedLocation:'',
		SelectedScheme:''
	 },o);
 
	//alert("I am in!");
	USSchemeShortcut = {
		
		OpenSchemeShortcut: function () {
			//alert("Open");
		    $("#" + o.SchemeShortcutMain + " .SchemeShortcutOpen").show();
		    $("#" + o.SchemeShortcutMain + " .SchemeShortcutClosed").hide();
		}

		, CloseSchemeShortcut: function () {

			//USSchemeShortcut.SaveSchemeShortcut();
			
			loc = "";
			proj = "";
			
			var locationCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedLocationCheckBox']");
			var projectCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedSchemeCheckBox']");

			$.each(locationCheckBoxes, function () {
				if ($(this).attr('checked')) {
					if (loc != "") loc += ",";
					loc += $(this).parent().parent().attr("id");
				}
			});

			$.each(projectCheckBoxes, function () {
				if ($(this).attr('checked')) {									
					if(USSchemeShortcut.IsDuplicate($(this).parent().parent().attr("id"),proj)==-1){
						if (proj != ""){
							proj += ",";
						}
						proj += $(this).parent().parent().attr("id");
					}
				}
			});

			//alert(loc);
			//alert(proj);

            $("#" + o.LocationHiddenField).val(loc);
            $("#" + o.SchemeHiddenField).val(proj);

			if (proj.length != 0 && proj.split(',').length != 0) {
			    $("#" + o.SchemeShortcutMain + " .SchemeShortcutOpenTitle").html(proj.split(',').length + " Schemes selected");
			} else {
			    $("#" + o.SchemeShortcutMain + " .SchemeShortcutOpenTitle").html("Choose Location or Scheme");
			}

			$("#" + o.SchemeShortcutMain + " .SchemeShortcutOpen").hide();
			$("#" + o.SchemeShortcutMain + " .SchemeShortcutClosed").show();
		}

		, CheckboxClick: function () {
			//USSchemeShortcut.SaveSchemeShortcut();
			
			loc = "";
			proj = "";

			var locationCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedLocationCheckBox']");
			var projectCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedSchemeCheckBox']");

			$.each(locationCheckBoxes, function () {
				if ($(this).attr('checked')) {
					if (loc != "") loc += ",";
					loc += $(this).parent().parent().attr("id");
				}
			});

			$.each(projectCheckBoxes, function () {
				if ($(this).attr('checked')) {									
					if(USSchemeShortcut.IsDuplicate($(this).parent().parent().attr("id"),proj)==-1){
						if (proj != ""){
							proj += ",";
						}
						proj += $(this).parent().parent().attr("id");
					}
				}
			});

			//alert(loc);
			//alert(proj);

            $("#" + o.LocationHiddenField).val(loc);
            $("#" + o.SchemeHiddenField).val(proj);
		}

		, DeselectAll: function () {
		    var checkBoxes = $("#" + o.Collapser + "  input:checkbox");

			$.each(checkBoxes, function () {
				this.checked = false;
			});
		}
		, SaveSchemeShortcut: function () {
			loc = "";
			proj = "";

			var locationCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedLocationCheckBox']");
			var projectCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedSchemeCheckBox']");

			$.each(locationCheckBoxes, function () {
				if ($(this).attr('checked')) {
					if (loc != "") loc += ",";
					loc += $(this).parent().parent().attr("id");
				}
			});

			$.each(projectCheckBoxes, function () {
				if ($(this).attr('checked')) {									
					if(USSchemeShortcut.IsDuplicate($(this).parent().parent().attr("id"),proj)==-1){
						if (proj != ""){
							proj += ",";
						}
						proj += $(this).parent().parent().attr("id");
					}
				}
			});

			//alert(loc);
			//alert(proj);

            $("#" + o.LocationHiddenField).val(loc);
            $("#" + o.SchemeHiddenField).val(proj);
		}
		,IsDuplicate: function(searchString, searchIn){
			if(searchIn.length){
				var arr = searchIn.split(',');
				return $.inArray(searchString,arr);
			}else{
				return -1;
			}
		}
		, ReloadCheckbox: function() {

		    var locationCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedLocationCheckBox']");
		    var projectCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedSchemeCheckBox']");

			
			if(o.SelectedLocation.length){
				var selectedLocationArray = o.SelectedLocation.split(',');
				$.each(locationCheckBoxes, function () {
					if ($.inArray($(this).parent().parent().attr("id"),selectedLocationArray) != -1) {						
						this.checked = true;
					}
				});
			}

			if(o.SelectedScheme.length){
				var selectedSchemeArray = o.SelectedScheme.split(',');
				$.each(projectCheckBoxes, function () {
					if ($.inArray($(this).parent().parent().attr("id"),selectedSchemeArray) != -1) {						
						this.checked = true;
					}
				});
			}
		}
	};	
 
	 return $(this).each(function(){
	     $("#" + o.SchemeShortcutMain + " .SchemeShortcut-open").click(USSchemeShortcut.OpenSchemeShortcut);
	     $("#" + o.SchemeShortcutMain + " .SchemeShortcut-close").click(USSchemeShortcut.CloseSchemeShortcut);

	     $("#" + o.SchemeShortcutMain + " input:checkbox[id$='_InterestedSchemeCheckBox']").live('click', function () {				
			proj = "";

			var projectCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedSchemeCheckBox']");		

			$.each(projectCheckBoxes, function () {
				if ($(this).attr('checked')) {									
					if(USSchemeShortcut.IsDuplicate($(this).parent().parent().attr("id"),proj)==-1){
						if (proj != ""){
							proj += ",";
						}
						proj += $(this).parent().parent().attr("id");
					}
				}
			});
			
			//alert(proj);
            $("#" + o.SchemeHiddenField).val(proj);
		});

        $("#" + o.SchemeShortcutMain + " input:checkbox[id$='_InterestedLocationCheckBox']").live('click', function () {
		
			loc = "";

			var locationCheckBoxes = $("#" + o.SchemeShortcutMain + " input[id$='_InterestedLocationCheckBox']");
			

			$.each(locationCheckBoxes, function () {
				if ($(this).attr('checked')) {
					if (loc != "") loc += ",";
					loc += $(this).parent().parent().attr("id");
				}
			});
			
			//alert(loc);			
            $("#" + o.LocationHiddenField).val(loc);			
		});



        $("#" + o.SchemeShortcutMain + " .DeselectAll").click(USSchemeShortcut.DeselectAll);		
		
		//need to select the checkbox, when page reload
		if(o.SelectedLocation.length || o.SelectedScheme.length){
			USSchemeShortcut.ReloadCheckbox();
		}
	 });
 
};

