/**
* Ajax Calendar for web-T::CMS
* @version 1.1
* @author goshi
* @package javascript::share
* using JsHTTPRequest
*
* Changelog:
*	1.1	04.04.10/goshi	some functional update
*/

function ajxCalendar(params){

	this._init(params);

};

ajxCalendar.prototype = {

	_objID: '',
	_cookName: 'pphpCalDate',
	_cookLife: max_cookie_life,
	_calWasLoaded: false,
	_loader: null,
	_ajxhref: '',
	_wrongDateTxt: '',
	// callback function
	_callback: null,	
	// current base month
	_month : null,
	// current element
	_elem : null,
	// selected dates
	_dates : {'start' : null, 'end' : null},
	
	/* constructor */
	_init: function(params){
		
		for (var i in params){
			if (typeof this['_'+i] != 'undefined'){
				this['_'+i] = params[i];
			}
		}		
	},
	
	check: function(self, objid){
		
		// try to understand what type of date we pressed
		var seven = 24*60*60;
		if (self._dates.start == null || (self._dates.start != null && self._dates.end != null)){
			// if dont have any date, or always have any date
			// clear all dates
			if (self._dates.start != null && self._dates.end != null){
				
				for (var i = self._dates.start; i <= self._dates.end; i = parseInt(i)+parseInt(seven)){
					portal.css.removeClass('ts-'+i, 'act-n');
				}
				portal.css.removeClass('ts-'+self._dates.start, 'act');
				portal.css.removeClass('ts-'+self._dates.end, 'act');
			}
						
			portal.css.addClass(objid, 'act');
			self._dates.start = objid.replace('ts-', '');
			self._dates.end = null;
			if ($_('date_on'))
				$_('date_on').value = self._dates.start;
			if ($_('date_off'))
				$_('date_off').value = '';

		} else {
			// select second date - and try to determine it from date interval
			self._dates.end = objid.replace('ts-', '');
			
			// check if end date less then start date
			if (self._dates.end < self._dates.start){ portal.css.removeClass('ts-'+self._dates.start, 'act'); self._dates.start = null; return self.check(self, objid);}
			
			// checking interval
			var objs = new Array();
			for (var i = self._dates.start; i <= self._dates.end; i = parseInt(i)+parseInt(seven)){
				if (!$_('ts-'+i)){
					self._dates.end = parseInt(i)-parseInt(seven);
					break;
				} else if (i > self._dates.start) {
					portal.css.addClass('ts-'+i, 'act-n');
				}
			}
			
			portal.css.removeClass('ts-'+self._dates.end, 'act-n');
			portal.css.addClass('ts-'+self._dates.end, 'act');
			if ($_('date_off'))
				$_('date_off').value = self._dates.end;

		}
		
		// call callback
		if (self._callback != null)
			self._callback();
		
	},
	
	_restoreSelected : function (){
		
		if (this._dates.start != null && $_('ts-'+this._dates.start))
			portal.css.addClass('ts-'+this._dates.start, 'act');
				
		var seven = 24*60*60;
		if (this._dates.end != null){
			for (var i = this._dates.start; i <= this._dates.end; i = parseInt(i)+parseInt(seven)){
				if ($_('ts-'+i) && i > this._dates.start) {
					portal.css.addClass('ts-'+i, 'act-n');
				}
			}
			if ($_('ts-'+this._dates.end)){
				portal.css.removeClass('ts-'+this._dates.end, 'act-n');
				portal.css.addClass('ts-'+this._dates.end, 'act');
			}
		}
			
	},

	load: function(dt){
		var req = new JsHttpRequest();
		var yr;
		
		this._calWasLoaded = false;
		var athis = this;
		req.onreadystatechange = function (){
			if (req.readyState == 4){
				if (athis._loader)
					portal.loader.destroy(athis._loader);

				if (req.responseJS.status == '200'){
					athis._month = req.responseJS.response.month;
					athis._elem = req.responseJS.response.elem;
					athis._calWasLoaded = true;
													
					$_(athis._objID).innerHTML = req.responseJS.response.html;
					
					// connecting to the buttons listners
					var elms = getLikeElements('img', 'class', 'l-cal-btn');
					if (elms && elms.length){
						for (var i in elms){
							portal.events.attach(elms[i], 'click', function(){
								athis.load('prev');
							});
						}
					}
					var elms = getLikeElements('img', 'class', 'r-cal-btn');
					if (elms && elms.length){
						for (var i in elms){
							portal.events.attach(elms[i], 'click', function(){
								athis.load('next');
							});
						}
					}
					
					// connecting to the right days listeners
					$('li[id*=ts-]').click(function(){athis.check(athis, $(this).attr('id'))});
					
					// restore selected
					athis._restoreSelected();
					
				}
			}
			
		};
		
		// trying to understand what we have to load
		//if (dt == 'prev'){
		
			// get from cookie
			/*dt = getCookie(this._cookName);
			if (typeof dt == 'undefined'){
				oDate = new Date();
				oDate.getYear() < 1000 ? yr = oDate.getYear() + 1900 : yr = oDate.getYear();
				var mon = (oDate.getMonth() + 1) + "";
				dt = yr+'-'+ (mon.length == 1 ? '0' : '') + mon +'-'+'01';
				
			}*/
		//}

		// test date with regular expression

		/*if (regulars.date.test(dt)){
			
			var expires = new Date();
			expires.setTime(expires.getTime() + (60*60*24*this._cookLife));

			setCookie(this._cookName, dt, expires.toGMTString(), '/'); */
		req.open('POST', this._ajxhref, true);
		req.send({
			'date': dt,
			'elem' : this._elem,
			'month' : this._month 
			});
		// show uploader
		if (!this._calWasLoaded && $_(this._objID)){
			this._loader = portal.loader.create(this._objID, {'loader_name' : 'loader_square.gif'});
		}


		/*} else {
			alert(this._wrongDateTxt);
		}*/
				
	}
	
}