
var Calendar = Class.create();

Calendar.prototype = {

    initialize: function(idHour, idDate, idMonth, idYear, idMonthNext, idMonthBack, idYearNext, idYearBack, dateNow, selectday, limitnext, limitlast, clickHandler)
    {
        this.selectday = selectday;
		this.elHour = $(idHour);
        this.elDate = $(idDate);
        this.elMonth = $(idMonth);
        this.elYear = $(idYear);

        this.elMonthNext = $(idMonthNext);
        this.elMonthBack = $(idMonthBack);
        this.elYearNext = $(idYearNext);
        this.elYearBack = $(idYearBack);

        this.elMonthBack.className = 'optArrow optBack';
        this.elMonthNext.className = 'optArrow optNext';
        this.elYearBack.className = 'optArrow optBack';
        this.elYearNext.className = 'optArrow optNext';

        //this.dateLimitNext = new Date();
        //this.dateLimitBack = new Date(2004,8,13,0,0,0);
        this.dateLimitNext = new Date(limitnext);
        this.dateLimitBack = new Date(limitlast);

        this.status = new Array();

        this.clickHandler = clickHandler;

        this.dNow = dateNow;
        daysFeb = (this.dNow.getFullYear() %4 == 0) ? 29 : 28;
        this.daysInMonths = new Array(31, daysFeb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        $A(this.elHour.getElementsByTagName('a')).each(function(item)
        {
            if (!item.name)
                return;
            item.onclick = function(evt)
            {
                el = Event.element(evt);
                if (!el.name)
                    el = el.parentNode;
                this.setHour(el.name);
                return false;
            }.bindAsEventListener(this);
        }.bind(this) );

        this.update();
    },

    update: function()
    {

    	y = this.dNow.getFullYear();
    	m = this.dNow.getMonth();
    	d = this.dNow.getDate();
    	if(m == 11)
    	{
    		yNext = y + 1;
    		mNext = 0;
    		dNext = 1;
    	}
    	else
    	{
    		yNext = y;
	    	mNext = m + 1;
    		dNext = 1;
    	}
    	if(m == 0)
    	{
    		yLast = y - 1;
    		mLast = 11;
    		dLast = this.daysInMonths[mLast];
    	}
    	else
    	{
    		yLast = y;
	    	mLast = m - 1;
    		dLast = this.daysInMonths[mLast];
    	}

        dNextMonth = new Date(yNext,mNext,dNext,0,0,0);
        dLastMonth = new Date(yLast,mLast,dLast,0,0,0);
        dNextYear = new Date(this.dNow.getFullYear()+1,this.dNow.getMonth(),this.dNow.getDate(),0,0,0);
        dLastYear = new Date(this.dNow.getFullYear()-1,this.dNow.getMonth(),this.dNow.getDate(),23,59,59);


        this.elMonth.innerHTML = monthArray[this.dNow.getMonth()];
        this.elYear.innerHTML = this.dNow.getFullYear();

        this.elDate.innerHTML = '';

        this.elMonthBack.className = 'optArrow optBack';
        this.elMonthNext.className = 'optArrow optNext';
        this.elYearBack.className = 'optArrow optBack';
        this.elYearNext.className = 'optArrow optNext';

        if(dNextMonth.getFullYear() == this.dateLimitNext.getFullYear() && dNextMonth.getMonth() == this.dateLimitNext.getMonth())
        {
        	dNextMonth = this.dateLimitNext;
        }
        if(dNextMonth.getTime() > this.dateLimitNext.getTime())
        {
        	this.elMonthNext.className = 'optArrow optNextDisabled';
        }

        if(dLastMonth.getTime() <= this.dateLimitBack.getTime())
        {
        	this.elMonthBack.className = 'optArrow optBackDisabled';
        }

        if(dNextYear.getFullYear() > this.dateLimitNext.getFullYear())
        {
        	this.elYearNext.className = 'optArrow optNextDisabled';
        }
        if(dLastYear.getFullYear() < this.dateLimitBack.getFullYear())
        {
        	this.elYearBack.className = 'optArrow optBackDisabled';
        }

        this.daysInMonths[this.dNow.getMonth()].times(function (index)
        {
            day = index + 1;

            dayDate = new Date(this.dNow.getFullYear(), this.dNow.getMonth(), day);

           //if(weekEnd.indexOf(dayDate.getDay()) > -1)
            //{
            //    if (day == 1)
            //        return;
            //    s = document.createElement('span');
            //    s.className = 'daySpacer';
            //    s.innerHTML = '&nbsp;';
            //    this.elDate.appendChild(s);
            //    return;
            //}

            a = document.createElement('a');
            a.className = 'dayElement';

            dayPad = new String(dayDate.getDate() );
            if (dayPad.length < 2)
            {
                dayPad = '0' + dayPad;
            }

            monthPad = new String(dayDate.getMonth() + 1);
            if (monthPad.length < 2)
            {
                monthPad = '0' + monthPad;
            }

            if(weekEnd.indexOf(dayDate.getDay()) > -1)
            {
            	a.innerHTML = '<span class="dayWd_red">' + daysArray[dayDate.getDay()] + '</span><span class="dayD_red">' + dayPad + '</span>';
            }
            else
            {
            	a.innerHTML = '<span class="dayWd">' + daysArray[dayDate.getDay()] + '</span><span class="dayD">' + dayPad + '</span>';
            }
            a.name = day;
            a.href = 'javascript:void(0);';

            if (dbDays.indexOf(dayDate.getFullYear() + '-' + monthPad + '-' + dayPad) == -1)
            {
                a.className = 'dayElement-no';
                this.elDate.appendChild(a);
                if(weekEnd.indexOf(dayDate.getDay()) > -1)
	            {
	                s = document.createElement('span');
	                s.className = 'daySpacer';
	                s.innerHTML = '&nbsp;';
	                this.elDate.appendChild(s);
	            }
                return;
            }

            if(dayDate.getTime() > this.dateLimitNext.getTime() || dayDate.getTime() < this.dateLimitBack.getTime())
            {
                a.className = 'dayElement-no';
                this.elDate.appendChild(a);
                if(weekEnd.indexOf(dayDate.getDay()) > -1)
	            {
	                s = document.createElement('span');
	                s.className = 'daySpacer';
	                s.innerHTML = '&nbsp;';
	                this.elDate.appendChild(s);
	            }
                return;
            }

            a.onclick = function(evt)
            {
                el = Event.element(evt);
                if (!el.name)
                    el = el.parentNode;
                this.setDate(el.name);
                return false;
            }.bindAsEventListener(this);

            if (this.selectday && dayDate.getDate() == this.dNow.getDate() )
            {
                Element.addClassName(a, 'daySel');
            }

			this.elDate.appendChild(a);

			if(weekEnd.indexOf(dayDate.getDay()) > -1)
	        {
	            s = document.createElement('span');
	            s.className = 'daySpacer';
	            s.innerHTML = '&nbsp;';
	            this.elDate.appendChild(s);
	        }

        }.bind(this) );

        weekEndBoth = new Array(0, 6);
    	dayString = String(this.dNow);
    	dayFirstLetter = dayString.slice(0, 1);

        if(dayFirstLetter === "S")
        {
        	this.elHour.getElementsBySelector('a[name="14"]', 'a[name="15"]', 'a[name="16"]').each(function (elHide)
        	{
        		elHide.hide();
        		if (elHide.next().tagName !== 'SPAN' || elHide.next().innerHTML !== (elHide.name + ':00'))
        		{
        			new Insertion.After(elHide, '<span>' + elHide.name + ':00</span>')
       			}
       			else
       			{
       				elHide.next().show();
    			}

        	});
        }
		else
		{
			this.elHour.getElementsBySelector('a[name="14"]', 'a[name="15"]', 'a[name="16"]').each(function (elHide)
        	{
        		elHide.show();
        		if (elHide.next().tagName === 'SPAN' && elHide.next().innerHTML === (elHide.name + ':00'))
        		{
       				elHide.next().hide();
    			}

        	});
		}




        if (this.status.length)
        {
            if (this.status.indexOf('Hour') != -1)
                this.clickHandler(this.dNow, this.dNow.getHours() );
            else
                this.clickHandler(this.dNow, false);
            this.status = new Array();
        }

        this.selectday = true;

    },
    setHour: function(newH)
    {
        this.status.push('Hour');

        if (!newH)
            return;

        this.dNow.setHours(newH);

        if (this.status[0] == 'Hour')
            this.update();
    },
    setDate: function(newD)
    {
        this.status.push('Date');

        if (!newD)
            return;

        this.dNow.setDate(newD);

        if (this.status[0] == 'Date')
            this.update();
    },
    setAddDate: function(dif)
    {
        this.status.push('Date');

        day = this.dNow.getDate() + dif;

        if (day > (this.daysInMonths[this.dNow.getMonth()]) )
        {
            this.setMonth(1);
            day = 1;
        }
        if (day < 1)
        {
            this.setMonth(-1);
            day = this.daysInMonths[this.dNow.getMonth()];
        }

        this.dNow.setDate(day);

        //if(weekEnd.indexOf(this.dNow.getDay()) > -1)
        //{
        //    if (this.dNow.getDay() < 15)
        //        this.setAddDate(1);
        //    else
        //        this.setAddDate(-1);
        //}

        if (this.status[0] == 'Date')
            this.update();
    },
    setMonth: function(dif)
    {
        if(dif > 0)
        {
        	dNext = new Date(this.dNow.getFullYear(),this.dNow.getMonth(),this.dNow.getDate(),0,0,0);
    	}
    	else if(dif < 0)
    	{
        	dNext = new Date(this.dNow.getFullYear(),this.dNow.getMonth(),this.dNow.getDate(),23,59,59);
    	}
        month = dNext.getMonth() + dif;

        if (month > 11)
        {
            month = 0;
            dNext.setFullYear(dNext.getFullYear() + 1);
        }
        if (month < 0)
        {
            month = 11;
            dNext.setFullYear(dNext.getFullYear() - 1);
        }
        dNext.setMonth(month);

		if(dif > 0)
		{
	        if(dNext.getFullYear() == this.dateLimitNext.getFullYear() && dNext.getMonth() == this.dateLimitNext.getMonth())
	        {
	        	dNext = this.dateLimitNext;
	        }
	        if(dNext.getTime() > this.dateLimitNext.getTime())
	        {
	        	m = dNext.getMonth();
	        	if((m - 1) < this.dateLimitNext.getMonth())
	        	{
	        		m = m + 12;
	        	}

	        	if((m - 1) == this.dateLimitNext.getMonth())
	        	{
	        		dNext = new Date(this.dateLimitNext.getFullYear(),this.dateLimitNext.getMonth(),this.dateLimitNext.getDate(),0,0,0);
	        	}
	        }
	    }
		else if(dif < 0)
		{
	        if(dNext.getTime() < this.dateLimitBack.getTime())
	        {
	        	m = dNext.getMonth();
	        	if(m > this.dateLimitBack.getMonth())
	        	{
	        		m = m - 12;
	        	}

	        	if(m == this.dateLimitBack.getMonth())
	        	{
	        		dNext = new Date(this.dateLimitBack.getFullYear(),this.dateLimitBack.getMonth(),this.dateLimitBack.getDate(),0,0,0);
	        	}
	        }
	    }

        if(dNext.getTime() > this.dateLimitNext.getTime())
        {
        	return;
        }
        if(dNext.getTime() < this.dateLimitBack.getTime())
        {
        	return;
        }
        if(dNext.getFullYear() == this.dNow.getFullYear() && dNext.getMonth() == this.dNow.getMonth() && dNext.getDate() == this.dNow.getDate())
        {
        	return;
        }

        this.status.push('Month');

		this.dNow = new Date(dNext.getFullYear(),dNext.getMonth(),dNext.getDate(),0,0,0);

        //if(weekEnd.indexOf(this.dNow.getDay()) > -1)
        //{
        //    this.setAddDate(dif);
        //}

        if (this.status[0] == 'Month')
            this.update();
    },
    setYear: function(dif)
    {
        dNext = new Date(this.dNow.getFullYear() + dif,this.dNow.getMonth(),this.dNow.getDate(),0,0,0);

        if(dNext.getTime() > this.dateLimitNext.getTime())
        {
	        if(dNext.getFullYear() >= this.dateLimitNext.getFullYear())
	        {
	        	dNext = new Date(this.dateLimitNext.getFullYear(),this.dateLimitNext.getMonth(),this.dateLimitNext.getDate(),0,0,0);
	        	this.setTime(dNext.getTime());
	        	return;
	        }
        	return;
	    }

        if(dNext.getTime() < this.dateLimitBack.getTime())
        {
	        if(dNext.getFullYear() <= this.dateLimitBack.getFullYear())
	        {
	        	dNext = new Date(this.dateLimitBack.getFullYear(),this.dateLimitBack.getMonth(),this.dateLimitBack.getDate(),23,59,59);
	        	this.setTime(dNext.getTime());
	        	return;
	        }
        	return;
	    }

        this.status.push('Year');

        this.dNow.setFullYear(this.dNow.getFullYear() + dif);

        if (this.status[0] == 'Year')
            this.update();
    },
    setTime: function(time)
    {
        this.dNow = new Date(time);
        this.update();
    }
}
