var mn=["Leden", "Únor", "Březen", "Duben", "Květen", "Červen", "Červenec", "Srpen", "Září", "Říjen", "Listopad", "Prosinec"];
var week=["Po", "Út", "St", "Čt", "Pá", "So", "Ne"];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
var avail_days=[1,2,3,4,5,6,7];

var CalendarObjects = new Array();

function BuildCalendar( InputID, DivID, e ){
	var cdc1 = new Calendar( InputID, InputID ); cdc1.build( DivID );
	var div = document.getElementById( DivID );

	if(document.all) {
			div.style.top=event.clientY+document.body.scrollTop-40;
			div.style.left=event.clientX+document.body.scrollLeft-40;
	} else {
			div.style.top=(e.pageY-40)+'px';
			div.style.left=(e.pageX-40)+'px';
	}

	//var left = window.innerWidth/2 - 125;
	//div.style.left = left+'px';
	//div.style.top = '200px';
	div.style.display = 'block';
}

function CloseCalendar( DivID )
{
	var div = document.getElementById( DivID );
	div.style.display = 'none';
}

function select_day( prefix, day ){
	CalendarObjects[ prefix ].select_day( day );
}

function swap_style( prefix, day, obj, is_over ){
	if ( CalendarObjects[ prefix ].d != day ){
		obj.className = is_over ? 'overed' : '';
	}
}

function change_month( prefix, sel ){
	CalendarObjects[ prefix ].change_month( sel.options[ sel.selectedIndex ].value );
}

function change_year( prefix, sel ){
	CalendarObjects[ prefix ].change_year( sel.options[ sel.selectedIndex ].value );
}

function change_hours( prefix, sel ){
	CalendarObjects[ prefix ].change_hour( sel.options[ sel.selectedIndex ].value );
}

function change_minut( prefix, sel ){
	CalendarObjects[ prefix ].change_minut( sel.options[ sel.selectedIndex ].value );
}

function Calendar( prefix, outfield, show_hours ){
	this._prefix = prefix;
	this._outfield = outfield;
	this._show_hours = show_hours;
	this.d = 0;
	this.m = 0;
	this.y = 0;
	this.h = 0;
	this.s = 0;
	this.divID = 0;

	CalendarObjects[ prefix ] = this;

	this.set_show_hours = function (){
		this._show_hours = show;
	}

	this.get_day = function (){
		return this.d;//_get_object('_day').value;
	}

	this.get_month = function(){
		var sel =  this._get_object('_mouths');
		return sel.options[ sel.selectedIndex ].value;
	}

	this.get_year = function(){
		var sel =  this._get_object('_years');
		return sel.options[ sel.selectedIndex ].value;
	}

	this.get_hour = function(){
		var sel =  this._get_object('_hours');
		return sel.options[ sel.selectedIndex ].value;
	}

	this.get_minut = function(){
		var sel =  this._get_object('_minut');
		return sel.options[ sel.selectedIndex ].value;
	}

	this._normNumber = function ( num ){
		return String( num ).length==1 ? '0' + num : num;
	}

	this._get_object = function ( name ){
		return document.getElementById( this._prefix+name );
	}

	this.get_out_obj_value = function(){
		var obj = eval( "document.getElementById('"+this._outfield+"')" );
		return obj.value;
	}

	this.set_out_obj_value = function( value ){
		var obj = eval( "document.getElementById('"+this._outfield+"')" );
		obj.value = value;
        try{
            obj.onchange();
        }
        catch(e){}
	}

	this._build_out_date = function(){
		var m = this.get_month();
		var d = this.get_day();
		var y = this.get_year();

		var str=y+'-'+this._normNumber( m )+'-'+this._normNumber( d );

		if ( this._show_hours ){
			var h = this.get_hour();
			var s = this.get_minut();
			str +=' '+this._normNumber( h )+':'+this._normNumber( s );
		}
		this.set_out_obj_value( str );
	}

	this._rebuild_days = function(){
		var m = this.get_month();
		var d = this.get_day();
		var y = this.get_year();

		var dstr = this._build_day_table();
		var div=this._get_object( 'id_calendar' );
		div.innerHTML=dstr;
		this._build_out_date();
	}

	this._rebuild_months = function(){
		var m = this.get_month();
		var y = this.get_year();

		var mobj=this._get_object( '_mouths' );
		this._rebuild_days();
	}

	this._build_year_combo = function(){
		var str = '<select id='+this._prefix+'_years onchange="change_year( \''+this._prefix+'\', this )">'
		for( i=2008; i < 2030; i++ ){
			str+='<option value='+i+' '+( this.y == i ? 'selected' : '' )+' >'+i;
		}
		str += '</select>';
		return str;
	}

	this._build_month_combo = function(){
		var str='<select id='+this._prefix+'_mouths onchange="change_month( \''+this._prefix+'\', this )" style="width:80px;">';
		for( i = 1; i <= 12; i++ ){
			str+='<option value='+(i)+' '+( this.m  == i ? 'selected' : '' )+' >'+mn[ i-1 ];
		}
		str+='</select>';
		return str;
	}

	this._build_hours_combo = function(){
		var str='<select id="'+this._prefix+'_hours" onchange="change_hours( \''+this._prefix+'\', this )">';
		for( i = 0; i < 24; i++ ){
			str+='<option value='+(i)+' '+( this.h  == i ? 'selected' : '' )+' >'+this._normNumber( i );
		}
		str+='</select>';
		return str;
	}

	this._build_minut_combo = function(){
		var sd = ( this.s/10 - Math.floor( this.s/10 ) ) * 10;
		var s = sd <5 ? Math.floor( this.s/10 ) *10 : Math.floor( this.s/10 ) *10 + 5;
		var str='<select id="'+this._prefix+'_minut" onchange="change_minut( \''+this._prefix+'\', this )">';
		for( i = 0; i < 60; i+= 5 ){
			str+='<option value='+(i)+' '+( s  == i ? 'selected' : '' )+' >'+this._normNumber( i );
		}
		str+='</select>';
		return str;
	}

	this._build_day_table = function(){

		var str='<div id="'+this._prefix+'id_calendar">';
		str+='<table class="daycalendar" cellspacing="0"><tr>';
		for( i=0; i<7; i++ ){
			str+='<th>'+week[i]+'</th>';
		}
		str+='</tr>';
		var fdate=new Date(this.y, this.m-1, 1);
		str += '<tr>';

		var startDay = fdate.getUTCDay() + 1;
		if ( startDay == 0 )
			startDay = 7;

		for( i=0; i < startDay-1; i++) {
			str += '<td>&nbsp;</td>';
		}

		var j=0; var c=0;
		var today=new Date();
		for( i=0, j = startDay-1, c=startDay; i<dim[ this.m-1 ]; i++, j++, c++){
			if( j != 0 && j % 7 == 0 ){
				c=1;
				str+="</tr><tr>";
			}
			astr='&nbsp;<a href="javascript:void();" id="'+this._prefix+'href'+i+'" onclick="javascript:select_day( \''+this._prefix+'\', '+(i+1)+' );return false;">'+(i+1)+'</a>&nbsp;';
			str += '<td id="'+this._prefix+'tdday'+( i+1 )+'" '+( this.d==i+1 ? 'class="selected"' : 'class="notselected"' )+' onclick="javascript:select_day( \''+this._prefix+'\', '+(i+1)+' );return false;" onmouseover="swap_style( \''+this._prefix+'\', '+(i+1)+', this ,1 );" onmouseout="swap_style( \''+this._prefix+'\', '+(i+1)+', this , 0 );" ><span class="click">'+(i+1)+'</span></td>';
		}
		for( i=j;; i++ ){
			if( i%7 == 0 ) break;
			str+='<td>&nbsp;</td>';
		}
		str+='</tr>';
		str+='</table>';
		str+='</div>';
		return str;
	}

	this.select_day = function( day ){
		document.getElementById( this._prefix+'tdday'+Math.abs( this.d ) ).className='notselected';
		this.d = day;
		document.getElementById( this._prefix+'tdday'+Math.abs( this.d ) ).className='selected';
		this._build_out_date();
		CloseCalendar( this.divID );
	}

	this.change_month = function ( month ){
		this.m = month;
		this._rebuild_months();
		//this._build_out_date();
	}

	this.change_year = function ( year ){
		this.y = year;
		var oD = new Date(this.y, 1, 1);
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
		this._rebuild_months();
		//this._build_out_date();
	}

	this.change_hour = function ( hour ){
		this.h = hour;
		//this._build_out_date();
	}

	this.change_minut = function ( minut ){
		this.s = minut;
		//this._build_out_date();
	}

	this.build_today = function(){
		var todaydate=new Date();
		this.m=todaydate.getMonth()+1;
		this.y=todaydate.getFullYear();
		this.d=todaydate.getDate();
		this.h=todaydate.getHours();
		this.s=todaydate.getMinutes();
		//this.set_out_obj_value( this.y+'-'+this._normNumber( this.m )+'-'+this._normNumber( this.d )+' '+this._normNumber( this.h )+':'+this._normNumber( this.s ) );
	}

	this.build = function( divID ){
		this.divID = divID;
		var date = this.get_out_obj_value();
		if ( date != '' ){
			try {
				var r=new RegExp( /(\d{4})?-(\d{1,2})?-(\d{1,2})?(\s(\d{1,2})?:(\d{1,2})?)?/g );
				var parts = r.exec( date );
				this.y=parts[1];
				this.m=parts[2];
				this.d=parts[3];
				if ( parts[5] )
					this.h = parts[5];
				if (parts[6])
					this.s = parts[6];
			} catch(e) {
				this.build_today()
			}
		} else {
			this.build_today()
		}

		var oD = new Date(this.y, 1, 1);
		dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

		var out = '<table class="calendar" border="1"><tr>';
		out += '<td>Rok: '+this._build_year_combo()+'</td>';
		out += '<td>Měsíc: '+this._build_month_combo()+'</td>';
		if ( this._show_hours ){
			out += '<td>Čas: '+this._build_hours_combo()+'</td>';
			out += '<td>Minuty: '+this._build_minut_combo()+'</td>';
		}
		out += '</tr><tr><td colspan='+( this._show_hours ? 4 : 2 )+'>';
		out += this._build_day_table();
		var closeStr = '<a href="javascript:void(0)" onclick="javascript:CloseCalendar( \''+divID+'\' ); return false">Zavřít</a>';
		out += '</td></tr><tr><td colspan='+( this._show_hours ? 4 : 2 )+' align="center">'+closeStr+'</td></tr></table>';
		var div = document.getElementById( divID );
		div.innerHTML = out;
	}
}
