/**
 * @author Waldek
 */ 
var Idea = {
    DataPicker: {}
};
	
var Idea = {
	 Frontend: {
	 	DateConfig: {}
	 }
 }	

Idea.Frontend.DateConfig = function()
{
	this.DisplayMode;
	this.DataPicker;
	this.SelectBox1_day_id = 'checkin_d';
	this.SelectBox1_month_id = 'checkin_ym';
	
	this.SelectBox2_day_id = 'checkout_d';
	this.SelectBox2_month_id = 'checkout_ym';
	
	this.SelectBox_stay_id;
	
	this.input1_id = 'checkin_i';
	this.input2_id = 'checkout_i';
	
	this.monthsNames = Idea.Frontend.DateConfig.MonthNames ? Idea.Frontend.DateConfig.MonthNames : ['sty','lut','mar','kwi','maj','cze','lip','sie','wrz','paz','lis','gru'];
	this.daysNames = Idea.Frontend.DateConfig.DayNames ? Idea.Frontend.DateConfig.DayNames : ['nie','pon','wto','sro','czw','pia','sob'];
	
	this.MonthLength = 11; //value <0,11>!!
	
	this.calDiv1;
	this.calDiv2;
	
	this.calAnchor1;
	this.calAnchor2;
}	

Idea.Frontend.DateConfig.prototype = 
{
	renderDate:function(){
		if(this.DisplayMode==1)
		{
			var cal1 = new Idea.Frontend.DateSelector(this.SelectBox1_day_id,this.SelectBox1_month_id,this);
		}
		if(this.DisplayMode==2)
		{
			this.cal1 = new Idea.Frontend.DateSelector(this.SelectBox1_day_id,this.SelectBox1_month_id,this);
			this.cal2 = new Idea.Frontend.DateSelector(this.SelectBox2_day_id,this.SelectBox2_month_id,this);
			this.updateDate(this.cal1.daysContainer);
		}
		if(this.DataPicker==1&&DisplayMode==1)
		{
			var cal1x = new CalendarPopup(this.calDiv1);
			document.getElementById(this.calAnchor1).onclick = function()
			{
				cal1x.select(document.getElementById(this.input1_id),this.calAnchor1,'MM-dd-yyyy');
			}
			
		}
		if(this.DataPicker==1&&DisplayMode==2)
		{
			var cal1x = new CalendarPopup(this.calDiv1);
			var cal2x = new CalendarPopup(this.calDiv2);
			document.getElementById(this.calAnchor1).onclick = function()
			{
				cal1x.select(document.getElementById(this.input1_id),this.calAnchor1,'MM-dd-yyyy');
			}
			document.getElementById(this.calAnchor2).onclick = function()
			{
				cal2x.select(document.getElementById(this.input2_id),this.calAnchor2,'MM-dd-yyyy');
			}
		}
	},
	updateDate:function(input){
		if(this.DisplayMode==2)
		{
			if(document.getElementById(this.SelectBox1_month_id)==input)
			{
				this.cal2.monthContainer.selectedIndex = input.selectedIndex;
				this.cal2.ChangeMonth(this.cal2);
			}
			if(document.getElementById(this.SelectBox1_day_id)==input)
			{
				if(!this.cal2.daysContainer.options[input.selectedIndex+1])
					{
						this.cal2.monthContainer.selectedIndex += 1;
						this.cal2.ChangeMonth(this.cal2);
						return;
					}
				this.cal2.daysContainer.selectedIndex = input.selectedIndex+1;
				this.cal2.setDatePicker()
			}
		}
	}	
}
	
Idea.Frontend.DateSelector = function(daysContainer,monthContainer,caller)
{
	this.defDate = new Date();
	this.currDate = new Date();
	this.currDay = this.currDate.getDate();
	this.currDayName = this.currDate.getDay();
	this.currMonthName = this.currDate.getMonth();
	this.caller = caller;
	//get parameters from dateConfig variable
		this.monthsNames = typeof(caller.monthsNames)=='string'?caller.monthsNames.split(','):caller.monthsNames;
		this.daysNames = typeof(caller.daysNames)=='string'?caller.daysNames.split(','):caller.daysNames;
		this.MonthLength = caller.MonthLength;
	//@end get parameters	

	this.daysContainer = document.getElementById(daysContainer);
	this.monthContainer = document.getElementById(monthContainer);
		
	this.RenderDays(this.currDate);
	this.RenderMonths(this.defDate.getFullYear(),0,this.MonthLength);
	
	var $this = this;
	this.monthContainer.onchange = function(){
		$this.ChangeMonth($this);
		$this.caller.updateDate($this.monthContainer);
	}
	this.daysContainer.onchange = function(){
		$this.setDatePicker();
		$this.caller.updateDate($this.daysContainer);
	}
}

Idea.Frontend.DateSelector.prototype = 
{
	SetMonthName:function(month)
	{
		return this.monthsNames[month];
	},
	
	SetDayName:function(day)
	{
		return this.daysNames[day];
	},
	setDatePicker:function(){
		
	},
	RenderDays:function(date)
	{
		var daysInMonth = this.chckDaysInMonth(date.getYear(),date.getMonth());
		var currDate = new Date();
		var days = daysInMonth;
		if((date.getYear()==currDate.getYear())&&(date.getMonth()==currDate.getMonth())&&(date.getDay()==currDate.getDay()))
		{
			var days = daysInMonth - date.getDate()+1;
			var currentDaySet = true;
		}
		
		for(var i=0; i<days;i++){
				var curr = currDate.getDate();
			if(currentDaySet)
			{
				
				date.setDate((curr+i));
			}
			else
			{
				date.setDate(i+1);
			}
			this.daysContainer.options[i] = new Option();
			this.daysContainer.options[i].text = date.getDate()  +' '+ this.SetDayName(date.getDay()) ;
			this.daysContainer.options[i].value = date.getDate();
		}
	},
	RenderMonths:function(year,start,monthiter)
	{
		var currmonth = 0;
		for(var i=start; i<monthiter;i++){
			if(this.currMonthName+i<=12)
			{
				if(!this.monthContainer.options[i])
				{
					this.monthContainer.options[i] = new Option();
				}
				if(start==0)
				{
					currmonth = this.currMonthName+i;
				}
				if(start>0)
				{
					currmonth = i-start;
				}
				this.monthContainer.options[i].text = this.SetMonthName(currmonth) +' '+ this.cutStr(year,2,4);
				var dd = new Array(year,currmonth+1);
				this.monthContainer.options[i].value = dd;
			}
			if(this.currMonthName+i>=12){
				this.currMonthName = 1;
				this.RenderMonths((year+1),i,monthiter);
				return;
			}
		}
	},
	ChangeMonth:function(obj){
		$this = obj;
		for(var i=0; i<$this.monthContainer.length;i++)
		{
			if($this.monthContainer.options[i].selected == true)
			{
				var newDate = $this.defDate;
				var val = $this.monthContainer.options[i].value.split(',');
				if(val[0]!=$this.defDate.getFullYear())
				{
					newDate.setFullYear(val[0]);
				}
				newDate.setMonth((parseInt(val[1])-1));
				$this.RenderDays(newDate);
			}
		}
	},
	setNewDate:function(month,day){
		this.monthContainer.options[month].selected = true;
		this.ChangeMonth(this);
		this.daysContainer.options[day].selected = true;
		this.setDatePicker();
	},
	chckDaysInMonth:function(year,month){
		var intMonth = month;
		var intYear = year;
		
		var dteMonth = new Date(intYear,intMonth);//
		var intDaysInMonth = 28;//the fewest number of days in a month
		var blnDateFound = false;	//Set a variable to check on the while loop
		
		while (!blnDateFound)
		{
			dteMonth.setDate(intDaysInMonth+1);//create the next possible day
			var intNewMonth = dteMonth.getMonth();//new month date
			
			if (intNewMonth != intMonth)//if the month has changed
			  blnDateFound = true;
			else
			  intDaysInMonth++;
		}
		return intDaysInMonth;
	},
	cutStr:function cutStr(strInpt, s, e)
	{
	 str = strInpt.toString();
	 if(s != null && e != null)
		 {
		 	return str.substring(s, e);
		 }
	 if(s != null && e == null)
		 {
		 	return str.substring(s, str.length);
		 }
	 if(e != null && s == null)
		 {
		 	return str.substring(0, e);
		 }
	 if(e != null && s != null){
		 str.substring(s,e);
		 	return str;
		 }
	}
}




