/*
Generic widget properties
*/
var objWidgetProperties={
	installurl: '/scripts/widgets',
	serversidelang: 'asp',
	css: '/css/styles.css',
	customcss: '',
	wrapper: '/assets/basic_window.html',
	mootools: '1.1'
};

/*
Test and load mootools if needed
*/
if(typeof MooTools == 'undefined'){
	//load mootools via httpxml
	var objXmlHttp = widgetCreateRemote();
	objXmlHttp.open("GET", objWidgetProperties.installurl+'/scripts/mootools_no-documentation.js', false);
	objXmlHttp.send(null);
	var strResponse=objXmlHttp.responseText
	eval(strResponse);	
	
	/*
	TO DO - ERROR HANDLING IN CASE LOAD FAILS
	*/
}else{
	//alert('NO need to load mootools');
	
	
}

/*
Initiation script
*/
function widgetInitiate(objArguments){
	
	
	if(MooTools.version.indexOf('1.2')>-1)objWidgetProperties.mootools='1.2';	
	if(MooTools.version.indexOf('1.3')>-1)objWidgetProperties.mootools='1.3';
	
	if(objWidgetProperties.mootools.toFloat() >= 1.3){
		Object.append(objWidgetProperties, objArguments);
	}else{
		$extend(objWidgetProperties, objArguments);
	}
	var strRootPath=objWidgetProperties.installurl;
	var strComponentBasePath;
	var bolLoadWrapperHtmlWindow=false;
	

	
	//load the css
	new Asset.css(strRootPath+objWidgetProperties.css, {id: 'widget-style_generic'});
	if(objWidgetProperties.customcss!=""){
		new Asset.css(objWidgetProperties.customcss, {id: 'widget-style_custom'});
	}
	
	//retrieve the widgets to load
	var arrWidgets=objArguments.usewidgets.split(',');

	//load the different component libraries in
	arrWidgets.each(function(strElement){
		//test if we need to load specific html wrappers
		strComponentBasePath=strRootPath+'/widget_types/'+strElement+'/lib.js';
		new Asset.javascript(strComponentBasePath, {id: strElement});		
	});
	
}

/*
Utilities
*/
function widgetClose(strId){
	//closes the widget
	//clear the controls
	if(objWidgetProperties.mootools.toFloat() >= 1.2){
		$(strId).getElement('.widget_controls-top').set('html', '');
		$(strId).getElement('.widget_controls-bottom').set('html', '');	
		$(strId).destroy();
	}else{
		$(strId).getElement('.widget_controls-top').setHTML('');
		$(strId).getElement('.widget_controls-bottom').setHTML('');	
		$(strId).remove();
	}
}

function widgetFixPath(objWidgetComponentProperties){
	var strWidgetComponentUrl=objWidgetComponentProperties.widgeturl;
	strWidgetComponentUrl=strWidgetComponentUrl.replace(/^(.*widgets)(.*)$/gi, "$2");
	strWidgetComponentUrl=objWidgetProperties.installurl+strWidgetComponentUrl;
	objWidgetComponentProperties.widgeturl=strWidgetComponentUrl;
}

function widgetCreateRemote() {
	try {
		return(new XMLHttpRequest());
	}
	catch(e) {
		return(new ActiveXObject('Microsoft.XMLHTTP'));
	}
}

function isDefined(a) {
	//backward compatibility check for $defined function in mootools 1.1 and 1.2
	var bolDefined=(typeof a == 'undefined' || a ==null)?false:true;
	return bolDefined;
}

function retrieveType(a){
	//backward compatibility function for $type function in mootools 1.1 and 1.2
	if (MooTools.version.toFloat() >= 1.3){
		return typeOf(a);
	}else{
		return $type(a);
	}	
}

function extendObject(objSource, objAdd){
	//backward compatibility function for the $extend function in mootools 1.1 and 1.2
	if(objWidgetProperties.mootools.toFloat() >= 1.3){
		Object.append(objSource, objAdd);
	}else{
		$extend(objSource, objAdd);
	}
	return true;
}
