/* ---------------------------------
  @ CingoKit.base  
----------------------------------- */
if (isUndefinedOrNull(CingoKit)) {
	var CingoKit = new Object();
};

// dialog constructor
CingoKit.Base = function ()
{
	// register baseclass
	this._class = CingoKit.Base;
};

CingoKit.Base.prototype = {
	
	// transfer and application of object data amongst object structures
	applyObject : function( obj )
	{
		for(var i in obj) {
			if (typeof(obj[i]) != "function") {
				this[i]=obj[i];
			};
		};
	},
	
	// quick-access wrapper to isUndefinedOrNull()
	isVoid : function(obj)
	{
		return isUndefinedOrNull(obj);
	},
	
	// store any handler
	overloadHandler : function( elem, handler )
	{
		var func = elem[handler];
		elem["_" + handler] = isUndefinedOrNull(func) ? new Function : func;
	},
	
	
	
	// Ajax handling
	extractResponseHTML : function(req, attributeName, attributeValue)
	{
		// set to default node
		var node = req.html[0];
		
		// search for it if params defined
		if (!isUndefinedOrNull(attributeName, attributeValue)) {
			for (var i=0; i<req.html.length; i++) {
				var attb = req.html[i].getAttribute(attributeName);
				if (!isUndefinedOrNull(attb)) {
					if (attb == attributeValue) {
						node = req.html[i];
						break;
					}
				};
			};
		};
		
		var html = !isUndefinedOrNull(node) ? scrapeText(node) : null;
		return html;
	},
	
	extractResponseJSON : function(req, attributeName, attributeValue)
	{
		// set to default node
		var node = req.json[0];

		// search for it if params defined
		if (!isUndefinedOrNull(attributeName, attributeValue)) {
			for (var i=0; i<req.json.length; i++) {
				var attb = req.json[i].getAttribute(attributeName);
				if (!isUndefinedOrNull(attb)) {
					if (attb == attributeValue) {
						node = req.json[i];
						break;
					}
				};
			};
		};
		
		var json = !isUndefinedOrNull(node) ? eval( '(' + scrapeText( node ) + ')' ) : null;
		return json;
	},
	
	extractResponseJS : function(req)
	{
		var node = req.js[0];
		var js = !isUndefinedOrNull(node) ? scrapeText(node) : null;
		return js;
	},
	
	extractJSFromResponseText : function( responseText )
	{
		var text = responseText;
		var arr = responseText.split("--SPLIT--");
		if(arr[1]!=undefined){
			var text=arr[0];
		};
		return text;
	},
	
	
	
	
	/* ----------------------------------------
	  Zapatec Popup Calendar Controls
	 ----------------------------------------- */
	showCalendar : function( src, inputfield, clickcb, hidecb, dateformat )
	{
		// set functions
		this.calendar = new Widgets.Calendar(
			0,
			null,
			function(calendar, date)
			{
				inputfield.value = date;
				if (calendar.dateClicked) {
					
					// for CingoKit form elements, the class 'active' needs to be applied as well...
					addElementClass(inputfield, 'active');
					calendar.callCloseHandler();
					
					// call the callback if available
					if (!isUndefinedOrNull(clickcb)) {
						clickcb.src[clickcb.func]();
					};
					
				};
			},
			function(calendar) {
			  calendar.hide();
			  
			  // call the callback if available
				if (!isUndefinedOrNull(hidecb)) {
					hidecb.src[hidecb.func]();
				};
			  
			}
		);

		this.calendar.cache = false,
		this.calendar.weekNumbers = false;
		this.calendar.showOthers = true;
		this.calendar.showsTime = false;
		this.calendar.timeFormat = "24";
		this.calendar.defaultDateFormat = "%A, %B %d, %Y";
		this.calendar.step = 2;
		this.calendar.electric = false;
		this.calendar.singleClick = true;
		this.calendar.numberMonths = 1;
		this.calendar.monthsInRow = 1,
		this.calendar.vertical = false;
		
		
		// set the date formats
		var format = !isUndefinedOrNull(dateformat) ? dateformat : this.calendar.defaultDateFormat;
		
		this.calendar.setDateFormat(format);
		this.calendar.setTtDateFormat("%a, %b %d, %Y");

		// set the range
		this.calendar.setRange(2007, 2037);

		// generate the calendar
		this.calendar.create();

		// show
		this.calendar.showAtElement(src, 'bR');
	},
	
	
	// DEPRECIATED
	applyObjectValues : function( obj )
	{
		if (!this._stored) this._stored = new Object;
		for(var i in obj) {
			if (typeof(obj[i]) != "function") {
				this._stored[i]=obj[i];
			};
		};
	}

};

setdefault(CingoKit, CingoKit.Base.prototype);