﻿$(document).ready(function(){
	
	// initialize the jq dialogs
	var jqDialog_quals  = $("#edx-QualsDropDown_dialog").dialog({
			draggable: false,
			modal: true,
			autoOpen: false
		});
		
	
	var SubjectsDropDown = $(".QualsDropDown_Subjects");
	var QualsDropDown = $(".QualsDropDown_Quals");
		
	$(".QualsDropDown_Quals .edx-dropDownButton").click(function(event){
		if (!QualsDropDown.hasClass("disabled")){
			var href = "/_ajaxSearchPages/QualsDropDown/QualificationFamilies.aspx";
			doTheDropDown(QualsDropDown, href, null, jqDialog_quals, function(){
				// enable the subjects box, disable the subjects go button, they will need to pick again
				var thefilter = QualsDropDown.find("input[name='hiddenvalue']").val();
				
				//alert('blah: ' + QualsDropDown.find("input[name='hiddenvalue']").size());
				//alert(thefilter);
				SubjectsDropDown.removeClass("disabled");
				SubjectsDropDown.find("input[name='hiddenfilter']").val(thefilter);
				clearDropDown(SubjectsDropDown, "Subjects");
			});
			event.preventDefault();
			return false;
		}
	});
	
	$(".QualsDropDown_Subjects .edx-dropDownButton").click(function(event){
		// TODO: Get the query string
		if (!SubjectsDropDown.hasClass("disabled")){
			var QualUrl = QualsDropDown.find("input[name='hiddenurl']").val();
			var thefilter = SubjectsDropDown.find("input[name='hiddenfilter']").val();
			var href = "/_ajaxSearchPages/QualsDropDown/QualificationSubjects.aspx";
			var args = {QualFamily: thefilter};
			if (QualUrl!='') args.QualFamilyUrl = QualUrl;
			
			doTheDropDown(SubjectsDropDown, href, args, jqDialog_quals, function(bool){
				if (bool==true){
					//the user picked a subject
					
				}
			});
		}
		event.preventDefault();
		return false;
	});

	// GO BUTTONS
	$(".edx-goButton").click(function(){
		var gobutton = $(this);
		if (!gobutton.hasClass("disabled")){
			// find the link and open the location
			
			var url = gobutton.find("~ input[name='hiddenurl']").val();
			if (url!=null && url !=''){
				//disable everything, we are navigating away
				try{
					gobutton.addClass("disabled");
					SubjectsDropDown.addClass("disabled");
					QualsDropDown.addClass("disabled");
					window.location = url;
				}
				catch(e){
					gobutton.removeClass("disabled");
					SubjectsDropDown.removeClass("disabled");
					QualsDropDown.removeClass("disabled");
					alert("Sorry, there was an error taking you to that page.");
				}
			}
		
		}
	});
	
	
});

function clearDropDown(jqdropdown, selectMessage){
	jqdropdown.find(".edx-goButton").addClass("disabled");
	jqdropdown.find("input[name='hiddenurl']").val("");
	jqdropdown.find("input[name='hiddenvalue']").val("");
	jqdropdown.find(".edx-dropDownButton #content").text(selectMessage);
}


function doTheDropDown(jqobj, href, args, dialog, itemSelectedCallback){
	// cache the objects for performance
	var thedialog = dialog;
	var thedropdown = jqobj.find(".edx-dropDownButton");
	var thedropdowntitle = jqobj.find(".edx-dropDownButton #content");
	var thedropdownhiddenurl = jqobj.find("input[name='hiddenurl']");
	var thedropdownhiddenvalue = jqobj.find("input[name='hiddenvalue']");
	var thecontentarea  = thedialog.find("#dialogcontent");
	var thegobutton  = jqobj.find(".edx-goButton");
	//alert(thedropdownhiddenvalue.size());	
		
	thedropdown.addClass("progress");
	
	//go get the page via ajax
	var req = jQuery.ajax({
    	url: href,
    	data: args,
    	contentType: "application/x-www-form-urlencoded",
    	success: function(data, textstatus) 
    	{
            // TODO: cache objects for perf
            
            // get the resulting html frag
            var frag = document.createElement("div");
            frag.innerHTML = data;
            var result = $(frag).find(".ajaxresult");
            if (result.size()>0) frag = result[0];
            
            // append the frag inside the dialog, make it visible
            thecontentarea.find(".showing").removeClass("showing");  
            $(frag).addClass("showing");
            thecontentarea.get(0).appendChild(frag);
            
            // the ajax call is complete
            thedropdown.removeClass("progress");
            
            // set up the item links click
			thecontentarea.find("a").click(function(event){
				var clickedItem = $(this);

				// set the title/value of the current box.
				thedropdowntitle.text(clickedItem.text());
				thedropdownhiddenurl.val(clickedItem.attr("href"));
				if(clickedItem.attr("value")!=null){
					thedropdownhiddenvalue.val(clickedItem.attr("value"));
				}
				else{
					thedropdownhiddenvalue.val(clickedItem.text());
				}
				
				thegobutton.removeClass("disabled");
				if (itemSelectedCallback) itemSelectedCallback(clickedItem.attr("value")!='');
				
				// set the filter value of the subject box
				thedialog.dialog("close");		
				event.preventDefault();         
		        return false;	
			});
			//set up any close buttons
			thecontentarea.find(".close_button").click(function(event){
				thedialog.dialog("close");		
				event.preventDefault();         
		        return false;
			});

			
			// we can only open this when we have placed the sized html inside
            thedialog.dialog("open");
            
        },
        error:  function() {
        	//alert('IE6 Test Fail');
        	alert("error");
    	}
    });
}