if(!GLG){var GLG = {};}
if(!GLG.Widget){GLG.Widget = {};}


/*
This structure provides a means to map keys to widgets
Namespaces lower than GLG.Widget can append to this list. 
(i.e. GLG.Widget.Portal may add portal specific widgets)
*/
GLG.Widget.Mappings = new Array();
GLG.Widget.Instance = new Array();


GLG.Widget.init = function(){
	body = document.getElementsByTagName('body')[0];
	//find widgets
	potentialWidgets = body.className.split(' ');
	var w;
	for (i=0;i<potentialWidgets.length;i++){
			if(GLG.Widget.Mappings[potentialWidgets[i]] != undefined){
				widget = GLG.Widget.Mappings[potentialWidgets[i]];
				//create widgets!
				if(widget.el){
						w = widget.constructor.call(this, widget.el)
						GLG.Widget.Instance.push(potentialWidgets[i]);
						GLG.Widget.Instance[potentialWidgets[i]]= w;
					}
					else{
						wEl= Dom.getElementsByClassName(potentialWidgets[i],
							widget.tag, widget.root);
						ws = new Array();
						for(j=0;j<wEl.length;j++){
							ws[j]=widget.constructor.call(this, wEl[j]);
						} 
						GLG.Widget.Instance.push(potentialWidgets[i]);
						GLG.Widget.Instance[potentialWidgets[i]]= ws;
					}
			}
	}
}
Event.onDOMReady(GLG.Widget.init);



/***************************************************************************
Framework Widgets (subclasses may exist in lower namespaces)
***************************************************************************/

GLG.Widget.XhrContentLoader = function(el) {
	/*
	Abstract class that loads content (from the dataSource) into 
	the element passed in. 
	
	Subclasses must provide a dataSource!
	*/
	Dom.addClass(el, 'XhrLoading');
	callback = 
				{ 
				  success:function(o){ 
				    	if(o.responseText !== undefined){ 
					    	el.innerHTML = o.responseText ; 
					    	Dom.removeClass(el, 'XhrLoading');
				    	}
				   	}, 
				  failure:function(o){ 
				    	if(o.responseText !== undefined){ 
								//leave default text
					    	Dom.removeClass(el, 'XhrLoading');
				    	}
				   	}
				}; 	
	return YAHOO.util.Connect.asyncRequest('GET', dataSource , callback); 
};


GLG.Widget.Modal = function(el) {
			config = { 
				zIndex:1003,
				width:"400px", 
				height:"auto", 
				fixedcenter: true, 
				constraintoviewport: true,
				modal: true, 
				underlay:"none", 
				close:true,
				visible:false, 
				draggable:false};
		dialog = new YAHOO.widget.Panel(el, config );
		dialog.render(document.body);
		return dialog;
};
GLG.Widget.Mappings["GWidget-Modal"] = {
	el:function(){d = document.createElement("div");
								d.id = "GWidget-Modal";
								document.body.appendChild(d);
								return d;}(), 
	constructor:GLG.Widget.Modal}

GLG.Widget.IframeModal = function(el) {
			config = { 
				zIndex:1003,
				width:"auto", 
				height:"auto", 
				fixedcenter: true, 
				constraintoviewport: true,
				modal: true, 
				underlay:"none", 
				close:true,
				visible:false, 
				draggable:false};
		dialog = new YAHOO.widget.Panel(el, config );
		
		YAHOO.lang.augmentObject(dialog,
		{setURL: function(URL){
			dialog.setBody("<iframe frameborder='0' "+
				"style='display:block;width:788px;height:562px;' "+
				"src='"+URL+"'></iframe>");
			dialog.render(document.body);}
		});
		return dialog;
};
GLG.Widget.Mappings["GWidget-IframeModal"] = {
	el:function(){d = document.createElement("div");
								d.id = "GWidget-IframeModal";
								document.body.appendChild(d);
								return d;}(), 
	constructor:GLG.Widget.IframeModal}


GLG.Widget.Toggle = function(el) {
		var ToggleContainer = Dom.getAncestorBy(el, function(el){return Dom.hasClass(el, "ToggleContainer");});
		toggleAction = function(el){
			if(Dom.hasClass(ToggleContainer, "ToggleOn")){
					Dom.replaceClass(ToggleContainer, "ToggleOn", "ToggleOff");
				}else{
					Dom.replaceClass(ToggleContainer, "ToggleOff", "ToggleOn");
			}
		}
	Event.addListener(ToggleContainer, 'click', toggleAction);
}
GLG.Widget.Mappings["ToggleHandle"] = {
	root:document.body, 
	constructor:GLG.Widget.Toggle}
