
var WidgetRunner = new Class.create({
	initialize : function(fancyWidgets, lazyLoadWidgets, args)
	{
		//this.widgets = widgets.evalJSON();
		this.client = new AjaxClient();

		var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
		
		for(var counter = 0; counter < fancyWidgets.length; counter++)
		{
			if(fancyWidgets[counter] == 'dashboardConfigLink')
				$('loadingSearchSpinner').hide();
				
			new FancyZoom(fancyWidgets[counter], function(passedCounter)
			{
				// disable the selects
				if(isIE6)
				{
					//$('filter_jurisdiction').hide();
					//$('filter_subject_type').hide();
				}
				// look for zoom content as thats where everything will be
				$('zoom_content').select('select','input').each(function(el)
				{
					switch(el.type)
					{
						case 'select-one':
							var val = $('hidden_'+el.id).value;
							// with a select box we hold the value in a hidden field
							// with the same name but hidden_ tacked onto the front
							for(var counter = 0; counter < el.options.length; counter++)
							{
								if(el.options[counter].value == val)
								{
									el.selectedIndex = counter;
									break;
								}
							}
							break;
						case 'checkbox':
							var val = $('hidden_'+el.id).value;
							if(val == 'Yes')
								el.checked = true;
							else
								el.checked = false;
							break;
						
						case 'radio':
							var val = $('hidden_'+el.name).value;
							if(val == el.value)''
								el.checked = true;
							break;
					}
				});
				
			}.bind(this,counter), function()
			{
				if(isIE6)
				{
					//$('filter_jurisdiction').show();
					//$('filter_subject_type').show();
				}
			});
			// show the configure buttons
			$(fancyWidgets[counter]).show();
		}
		
		$('dashboardSearchJurBut').show();
		$('dashboardSearchSubBut').show();

		// do this async for speed
		if(lazyLoadWidgets.length > 0)
		{
			this.asyncLoader(lazyLoadWidgets, function(widget){
				this.getLazyLoadItem(widget.id,widget.widgetClassName, args);
			}.bind(this));
		}
		
		if(completedFrames.length > 0)
		{
			//alert('got here');
			for(var counter = 0; counter < completedFrames.length; counter++)
			{
				this.showFrameCompleted(completedFrames[counter]);
			}
		}
	},
	getLazyLoadItem : function(id, widgetClassName, args)
	{
		var queryString = this.buildQueryString();
		// run this synchronously
		this.client.RunCommand(widgetClassName, 'GetLazyWidget', {'id' : id, 'queryString': args.toJSON() },function(results){

			// get the javascript code
			var theHtml = results.theHtml;
			var divContainer = results.divContainer;
			if(theHtml != undefined)
			{
				$(divContainer).update();
				$(divContainer).update(theHtml);
			}
		},undefined,true);
	},
	asyncLoader : function(theArray, callback)
	{
		var busy = false;
		var self = this;
		var counter = 0;
		var processor = setInterval(function()
		{
			if(!busy)
			{
				busy = true;
				
				if(counter >= theArray.length)
					clearInterval(processor);
				else
					callback(theArray[counter]);

				// if not more items
				counter++;
				busy = false;
			}
		 }, 100);
	},
	buildQueryString : function()
	{
		// Build an empty URL structure in which we will store
		// the individual query values by key.
		var objURL = new Object();
		 
		// Use the String::replace method to iterate over each
		// name-value pair in the query string. Location.search
		// gives us the query string (if it exists).
		window.location.search.replace(
			new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
		  
			// For each matched query string pair, add that
			// pair to the URL struct using the pre-equals
			// value as the key.
			function( $0, $1, $2, $3 )
			{
				objURL[ $1 ] = $3;
			}
		);
		return objURL;
	},
	processItem : function(widget)
	{
		if(widget.fancyZoomLink != undefined)
		{
			new FancyZoom(this.widgets[widget].fancyZoomLink);
		}
		
		if(this.widgets[widget].widgetClassName != undefined)
		{
			this.getLazyLoadItem(widget,this.widgets[widget].widgetClassName);
		}	
	},
	onHighlightBox : function(element)
	{
		// simple for the moment
		if(!$(element).hasClassName('highlightBox'))
			$(element).addClassName('highlightBox');
	},

	onLowlightBox : function(element)
	{
		//$(element).hide();
		if($(element).hasClassName('highlightBox'))
			$(element).removeClassName('highlightBox');
	},

	
	onWidgetVisibleChange : function(className,widgetName,isChecked,doConfigScreenUpdate)
	{
		// show/hide the element
		var widgetDiv = 'widget_'+widgetName;
		if(isChecked && !$(widgetDiv).visible())
			$(widgetDiv).show();
		else if(!isChecked && $(widgetDiv).visible())
			$(widgetDiv).hide();

		// save for later when we'll update the checkbox
		$('hidden_selectorWidget_'+widgetName).value = (isChecked)?'Yes':'No';

		// TBD call ajax to update the database
		this.client.RunCommand(className, 'SaveVisibility', {'id' : widgetName, 'visible' : isChecked },function(ob){});
		return true;
	},
	
	showFrameCompleted : function(iframeDiv)
	{
		$('waiting_'+iframeDiv).hide();
		var theFrame = document.getElementById(iframeDiv);
//		alert(theFrame);
		theFrame.style.display = 'block';
	}
	
});

//dynamic class for use with search popup
var cls_dashboardSearchLink = new Class.create({
	initialize : function()
	{
		debugger
		this.client = new AjaxClient();
		$('dashboardSearchSpinner').show();
		$('dashboardSearchContent').hide();
		
		// show the popup
		FancyZoomBox.showImmediate($('dashboardSearch'),680,440,event.pointerX(), event.pointerY(), function()
		{
			// popup now built so do ajax call
			this.client.RunCommand('Dashboard', 'GetDashboardSettings', {}, function(ob){
				callback(ob.profile);
			});

		}.bind(this));		
		
	}
});
	

