function Calendar()
{
	this.url;
	this.activeDay;
	this.activeMonth;
	this.activeYear;
	this.activeFiles;
	this.type;
	this.cat_id;
	this.section;
	this.id;
	this.action;

	this.setUrl = setUrl;
	this.setDay = setDay;
	this.setMonth = setMonth;
	this.setYear = setYear;
	this.setDays = setDays;
	this.setFiles = setFiles;
	this.setId = setId;
	this.setType = setType;
	this.setCat_id = setCat_id;
	this.setSection = setSection;
	this.setId = setId;
	this.setAction = setAction;
	this.setLanguage = setLanguage;

	this.getDaysMonth = getDaysMonth;
	this.getFirstDayWeekNumber = getFirstDayWeekNumber;

	this.changeMonth = changeMonth;
	this.changeMonthResult = changeMonthResult;

	function setUrl(url)
	{
		this.url = url;
	}

	function setDay(day)
	{
		this.activeDay = day;
	}

	function setMonth(month)
	{
		this.activeMonth = month;
	}

	function setYear(year)
	{
		this.activeYear = year;
	}
	
	function setType(type)
	{
		this.type = type;
	}
	
     function setCat_id(cat_id)
	{
		this.cat_id = cat_id;
	}
	
	function setSection(section)
	{
		this.section = section;
	}
	
	function setId(id)
	{
		this.id = id;
	}

	function setAction(action)
	{
		this.action = action;
	}

	function setLanguage(lang)
	{
		this.language = lang;
	}

	function getDaysMonth()
	{
		var minutes = 1000*60;
		var hours = minutes*60;
		var days = hours*24;

		firstDate = new Date(this.activeYear,this.activeMonth-1,1);
		firstDateNextMonth = new Date(this.activeYear,this.activeMonth,1);

		countDaysMonth = firstDateNextMonth.getTime()-firstDate.getTime();
		countDaysMonth = countDaysMonth/days;
		countDaysMonth = Math.floor(countDaysMonth);

		return countDaysMonth;
	}

	function getFirstDayWeekNumber()
	{
		firstDate = new Date(this.activeYear,this.activeMonth-1,1);

		firstDayWeekNumber = firstDate.getDay();

		return firstDayWeekNumber;
	}

	function setFiles(setFiles)
	{
		this.activeFiles = setFiles;
	}

	function setDays(activeDays, activeFiles)
	{
		var file,day=0,a=0;

		firstDayWeekNumber = this.getFirstDayWeekNumber();
		countDaysMonth = this.getDaysMonth();		
		//alert ("setDays");
		//alert (activeDays+"days");

		if ($('month')) {
			$('month').innerHTML = this.activeMonth+'.'+this.activeYear;

			if(!firstDayWeekNumber) firstDayWeekNumber=7;

			for(i=1;i<42;i++)
			{	
				if( i>=firstDayWeekNumber && i<firstDayWeekNumber+countDaysMonth )
				{
					day++;
					//alert ("activeDays[a]:"+activeDays[a]);
					if(day==activeDays[a])
					{
						if(day==this.activeDay) {
							$('day_'+i).innerHTML = '<span>'+day+'</span>';
						} else {
							if(this.section!="analytics_bfb_summary") {
//alert(day);
								$('day_'+i).innerHTML = '<a href="'+this.url+'type='+this.type+'&cat_id='+this.cat_id+'&section='+this.section+'&date='+day+'.'+this.activeMonth+'.'+this.activeYear+'&id='+this.id+'">'+day+'</a>';
							} else {
								$('day_'+i).innerHTML = '<a href="'+this.url+activeFiles[a]+'">'+day+'</a>';
							}
						}

						a++;
						continue;
					}

					if(day==this.activeDay)
					{
						$('day_'+i).innerHTML = '<span>'+day+'</span>';
						continue;
					}

					$('day_'+i).innerHTML = day;
				}
				else
					$('day_'+i).innerHTML = '&nbsp;';
			}
		}
	}

	function changeMonth ( action, section, type, cat_id, article_id, lang )
	{
		var url = '../getCalendar.php';
		var pars = 'year='+this.activeYear+'&month=';

		if ( (cat_id-1000)>0 ) {
			cat_id -= 1000;
		}

		if (action == 'next') {
				pars += Math.round(this.activeMonth)+1;
				pars += '&direction=next';
		} else if (action == 'pre') {
				pars += Math.round(this.activeMonth)-1;
				pars += '&direction=prev';
		} else if (action == 'current') {
				pars += Math.round(this.activeMonth);
				pars += '&direction=prev';
		}
		
		this.setAction(action);
		this.setLanguage(lang);
		
	// adding section to the getCalender
		pars += '&section='+section;
		this.section = section;

	// adding type of the article to the getCalender
		pars += '&type='+type;
		this.type = type;

	// adding type of the article to the getCalender
		pars += '&cat_id='+cat_id;
		this.cat_id = cat_id;
		
	// adding type of the article to the getCalender
		pars += '&language='+lang;

		this.id = article_id;

		var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: changeMonthResult.bind(this) } );
	}

	function changeMonthResult (originalRequest)
	{
//alert("monthResult");
		var day = originalRequest.responseXML.getElementsByTagName('day').item(0).firstChild.data;
		var month = originalRequest.responseXML.getElementsByTagName('month').item(0).firstChild.data;
		var year = originalRequest.responseXML.getElementsByTagName('year').item(0).firstChild.data;
		var activeDays = originalRequest.responseXML.getElementsByTagName('activeDays').item(0).firstChild.data;
		var activeFiles = originalRequest.responseXML.getElementsByTagName('activeFiles').item(0).firstChild.data;

		activeDays = activeDays.split(",");
		activeFiles = activeFiles.split(",");

		if ( ( this.activeMonth == Math.round(month) ) && ( this.activeYear == Math.round(year) ) && (this.action != 'current') ) {
			switch (this.language) {	
				case 'bg': msg = "Няма повече записи!";
					break;
				case 'en': msg = "No more records!";
					break;
			}
			alert(msg);
		} else {
			this.setDay(day);
			this.setMonth(month);
			this.setYear(year);
			this.setDays(activeDays, activeFiles);
		}
	}
}

var calendar = new Calendar();

