/**
* Ajax Calendar for web-T::CMS
* @version 1.2
* @author goshi
* @package javascript::share
* using JsHTTPRequest
*
* Changelog:
*	1.2	11.06.10/goshi	add popup layer
*	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},
		
	// flag for showed layer
	_layerShowed : false,
	// layer
	_layer : null,
	_layer_html : null,
	_block : false,
	_pos_overflow: 0, // cursor overflow over the popup block
	
	/* 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');
			}
		}
			
	},
	
	hideLayer : function(event){
	
		// checking for over block
		var pos = portal.dom.getAbsPosition(this._layer);
		
		// current position
		var curr_pos = portal.events.defPosition(event);
		if (this._layer && !this._block && this._layer.style.display != 'none'){ 
			
			/*alert((pos.x - this._pos_overflow - curr_pos.x) + '---' +
				(pos.y - this._pos_overflow - curr_pos.y) + '---' +
				(pos.x + this._pos_overflow + this._layer.offsetWidth - curr_pos.x) + '---' +
				(pos.y + this._pos_overflow + this._layer.offsetHeight - curr_pos.y));*/
						
			var inn = (pos.x - this._pos_overflow - curr_pos.x) <= 0 && 
				(pos.y - this._pos_overflow - curr_pos.y) <= 0 && 
				(pos.x + this._pos_overflow + this._layer.offsetWidth - curr_pos.x) >= 0 && 
				(pos.y + this._pos_overflow + this._layer.offsetHeight - curr_pos.y) >= 0 ;
			//alert(inn);
			if (!inn) this._layer.style.display = 'none';
		
		}
		
	},
	
	addBlock : function(){
		this._block = true;	
	},
	
	releaseBlock : function(){
		this._block = false;
	},
	
	showLayer : function(id){

		var athis = this;
		
		if (this._layerShowed)
			this.hideLayer();
			
		if (this._layer == null){
			
			this._layer = elem('div', {'class' : 'b-popup-events'});
			this._layer_html = elem('div', {'class' : 'b-inner', 'id' : 'cal_popup_html'});
			this._layer.appendChild(this._layer_html);
			
			document.body.appendChild(this._layer);
		
			$('.'+this._layer.className).bind('mouseenter', function(event){athis.addBlock(event)});
			$('.'+this._layer.className).bind('mouseleave', function(event){athis.releaseBlock(); athis.hideLayer(event)});
			
		}
		
		// detect element position
		var pos = portal.dom.getAbsPosition(id);
		this._layer.style.display = 'block';
					
		this._layer.style.left = (pos.x - this._layer.offsetWidth + 4) + 'px';
		this._layer.style.top = (pos.y - 4) + 'px';
		
		this._loader = portal.loader.create('cal_popup_html', {'loader_name' : 'loader.gif', 'mode' : 'standart'});
		
				
		var req = new JsHttpRequest();
		
		req.onreadystatechange = function (){
			if (req.readyState == 4){
				if (athis._loader)
					portal.loader.destroy(athis._loader);

				if (req.responseJS.status == '200'){
					athis._layer_html.innerHTML = req.responseJS.response;
					
				}
			}
			
		};
		
		req.open('POST', this._ajxhref, true);
		req.send({
			'tstamp': id.replace(/ts-/i, '', id)
			});
		
		
	},

	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-]').bind('mouseover', function(){athis.showLayer($(this).attr('id'))}).bind('mouseleave', function(event){athis.hideLayer(event)});;
					//$('#b-cal-container').bind('mouseleave', function(event){athis.hideLayer(event)});
										
					//
					// 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(), '/'); */
		// show uploader
		if (!this._calWasLoaded && $_(this._objID)){
			this._loader = portal.loader.create(this._objID, {'loader_name' : 'loader_square.gif', 'mode' : 'standart'});
		}
	
		req.open('POST', this._ajxhref, true);
		req.send({
			'date': dt,
			'elem' : this._elem,
			'month' : this._month 
			});
		
		/*} else {
			alert(this._wrongDateTxt);
		}*/
				
	}
	
}
