// months as they appear in the calendar's title
var ARR_MONTHS = ["Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December"];
// week day titles as they appear on the calendar
var ARR_WEEKDAYS = ["V", "H", "K", "Sz", "Cs", "P", "Sz"];
var ARR_BUTTONS = ["Bezár", "Előző év", "Következő év", "Előző hónap", "Következő hónap"];
// day week starts from (normally 0-Mo or 1-Su)
var NUM_WEEKSTART = 1;

// path to the directory where calendar images are stored. trailing slash req.
var STR_ICONPATH = 'img/';

var re_url = new RegExp('datetime=(\\-?\\d+)');
var re_id = new RegExp('id=(\\d+)');
var num_id = (re_id.exec(String(window.location))
	? new Number(RegExp.$1) : 0);
//var obj_caller = (window.opener ? window.opener.calendars[num_id] : null);

var calendar_source='js_calendar_content';
var calendar_target='js_calendar';
var calendar_content_target='longcontent';

var calendar_item_link;

//call_calendar('/calendar_url','/calendar_item_link','longcontent','js_calendar','js_calendar_content')

function call_calendar(c_url,c_item_link,c_content_target,c_target,c_source) {
calendar_url=c_url;
calendar_item_link=c_item_link;

if (typeof c_content_target!='undefined' || c_content_target!=''){
calendar_content_target=c_content_target;
}
if (typeof c_source!='undefined' || c_source!=''){
calendar_source=c_source;
}
if (typeof c_target!='undefined' || c_target!=''){
calendar_target=c_target;
}

if (isObject(document.getElementById(calendar_target))){
var input_current = new Date();
date_object=document.getElementById(calendar_target);
set_datetime(input_current)
}
}

function set_datetime(n_datetime) {

var dt_current =  new Date(n_datetime) ;
var xtarget=calendar_target
if (isObject(document.getElementById(date_object.id + '.script'))){
xtarget=date_object.id + '.script'
}
//if (obj_caller && obj_caller.year_scroll) {
	// get same date in the previous year
	var dt_prev_year = new Date(dt_current);
	dt_prev_year.setFullYear(dt_prev_year.getFullYear() - 1);
	if (dt_prev_year.getDate() != dt_current.getDate())
		dt_prev_year.setDate(0);
	
	// get same date in the next year
	var dt_next_year = new Date(dt_current);
	dt_next_year.setFullYear(dt_next_year.getFullYear() + 1);
	if (dt_next_year.getDate() != dt_current.getDate())
		dt_next_year.setDate(0);
//}

// get same date in the previous month
var dt_prev_month = new Date(dt_current);
dt_prev_month.setMonth(dt_prev_month.getMonth() - 1);
if (dt_prev_month.getDate() != dt_current.getDate())
	dt_prev_month.setDate(0);

// get same date in the next month
var dt_next_month = new Date(dt_current);
dt_next_month.setMonth(dt_next_month.getMonth() + 1);
if (dt_next_month.getDate() != dt_current.getDate())
	dt_next_month.setDate(0);

// get first day to display in the grid for current month
var dt_firstday = new Date(dt_current);
dt_firstday.setDate(1);
dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - NUM_WEEKSTART) % 7);

var td_script='none';
if (isObject(document.getElementById(date_object.id))){
if (date_object.nodeName=='INPUT') {td_script='input';}
else if (isObject(document.getElementById(calendar_source))){td_script='info';}
}

var xf;
xf='<table class="js_calendar"' 
//if (td_script=='info') {xf=xf +  ' onmouseout="hide_tip()"' }
xf=xf +  ' cellspacing="1" cellpadding="0" border="0" width="100%">'
xf=xf +  '<tr><td><table class="js_calendar_year_navy" cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td><a href="javascript:set_datetime('+dt_prev_year.valueOf()+')" title="' + ARR_BUTTONS[1] + '" class="c_prev_year"></a></td><td width="3%"><br/></td><td align="center" width="80%"><span class="c_year_title">'+dt_current.getFullYear() + '</span></td><td width="3%"><br/></td><td><a href="javascript:set_datetime('+dt_next_year.valueOf()+')" title="' + ARR_BUTTONS[2] + '" class="c_next_year"></a></td>'
if (td_script=='input'){
xf=xf +  '<td><a class="close" title="' + ARR_BUTTONS[0] + '" href="javascript:date_close()"></a></td>'
}
xf=xf +  '</tr></table></td></tr>'
xf=xf +  '<tr><td><table class="js_calendar_month_navy" cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td width="3%"><br/></td><td><a href="javascript:set_datetime('+dt_prev_month.valueOf()+')" title="' + ARR_BUTTONS[3] + '" class="c_prev_month"></a></td><td align="center" width="80%"><span class="c_month_title">'+ARR_MONTHS[dt_current.getMonth()]+'</span></td><td><a href="javascript:set_datetime('+dt_next_month.valueOf()+')" title="' + ARR_BUTTONS[4] + '" class="c_next_month"></a></td><td width="3%"><br/></td>'

xf=xf +  '</tr></table></td></tr>'
xf=xf +  '<tr><td><table class="js_calendar_body" cellspacing="1" cellpadding="3" border="0" width="100%"><tr>'
// print weekdays titles
for (var n=0; n<7; n++) {
	xf=xf + '<th>'+ARR_WEEKDAYS[(NUM_WEEKSTART+n)%7]+'</th>';
	}
xf=xf + '</tr>';

// print calendar table
var dt_current_day = new Date(dt_firstday);

var dt_date = new Date();
	//dt_date.setDate(1);


while (dt_current_day.getMonth() == dt_current.getMonth() ||
	dt_current_day.getMonth() == dt_firstday.getMonth()) {
	// print row heder
	xf=xf + '<tr>';
	for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
		
		xf=xf + '<td id="jsc_' + (dt_current_day.getMonth() < 9 ? '0' : '') + (dt_current_day.getMonth() + 1) + '_' + (dt_current_day.getDate() < 10 ? '0' : '') + dt_current_day.getDate() + '"'
		
		if (td_script=='input' && dt_current_day.getDate() == dt_current.getDate() &&
			dt_current_day.getMonth() == dt_current.getMonth()) {
			// print current date
			xf=xf + ' class="day_current"'; }

		else if ( dt_date.getDate() == dt_current_day.getDate()  && dt_date.getMonth() == dt_current_day.getMonth() && dt_date.getFullYear() == dt_current_day.getFullYear()) {
			// print current date
			xf=xf + ' class="day_current'; }
		else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6) {
			// weekend days
			xf=xf + ' class="day_weekend'; }
		else {
			// print working days of current month
			xf=xf + ' class="day_working'; }

		if (dt_current_day.getMonth() != dt_current.getMonth()) {
			// print days of current month
			xf=xf + '_inactive'; }
			xf=xf + '"';

		if (td_script=='input'){
				xf=xf + ' onclick="add_datetime('+dt_current_day.valueOf() +')" style="cursor:pointer"';}
		else if (td_script=='info' && dt_current_day.getMonth() == dt_current.getMonth()) 	{
			xf=xf + ' onclick="call_datetime_content('+dt_current_day.valueOf() +')" onmouseover="show_datetime_content('+dt_current_day.valueOf() +',event)" onmouseout="hide_tip()" style="cursor:pointer"';
		}
			xf=xf + '><span>'

			
		xf=xf + dt_current_day.getDate()+'</span></td>';
		dt_current_day.setDate(dt_current_day.getDate()+1);
	}

	xf=xf + '</tr>';
	
}

xf=xf + '</table></tr></td></table>';
document.getElementById(xtarget).innerHTML=xf;
document.getElementById(xtarget).style.display="block"
if (td_script=='info' && calendar_url!='') {
	var url=calendar_url
url=replace(url,"jsc_year", dt_current.getFullYear())
url=replace(url,"jsc_month", (dt_current.getMonth() < 9 ? '0' : '') + (dt_current.getMonth() + 1))
	makeRequest(url,'',calendar_source,'','calendar')
}

}





//---------------- Calendar public functions 

var date_object;

function date_add(code, obj, start_obj, n_obj) {
var n_datetime=document.getElementById(start_obj).value
var n=(parseInt(document.getElementById(n_obj).value) ? parseInt(document.getElementById(n_obj).value) : 0)
	
var temp_date = new Date(parse_date(n_datetime));
temp_date.setMonth(temp_date.getMonth() + n); 

document.getElementById(obj).value=format_date(temp_date)
date_change(document.getElementById(obj))
}

function add_datetime(n_datetime) {
	//if (!obj_caller) return;

var temp=format_date(n_datetime)
if (isObject(document.getElementById(date_object.id))){date_object.value=temp}
date_close()
}


function  show_datetime_content(n_datetime,e) {
if (isObject(document.getElementById(calendar_source))){
if (document.getElementById('page_tip').title==n_datetime.valueOf()) { 		
	drag=false;
		show_tip(e);
}
else {
var temp=format_date(n_datetime)
	var xfi=0
	var xf='<p>' + temp + '</p>';
		var allObjects = document.getElementById(calendar_source).getElementsByTagName('P');

			for(var i=allObjects.length-1;i>=0;i=i-1){
				
			if(allObjects[i].title==temp){
				
				if (xfi<5){xf=xf+ '<p>' +  allObjects[i].innerHTML + '</p>'}
				xfi=xfi+1
					
			}
			
			}
if (xfi>5){xf=xf+ '<p>More about <b>'+ (xfi-5) +'</b> items - read more...</b></p>'}
		
		document.getElementById('page_tip').innerHTML=xf ;
document.getElementById('page_tip').title=n_datetime.valueOf();
		drag=false;
		show_tip(e);
		
}
}
}





function  call_datetime_content(n_datetime) {
	if (calendar_item_link!='')
	{
	
var temp=format_date(n_datetime)
	var url=calendar_item_link
url=replace(url,"jsc_date", temp)
	makeRequest(url,'',calendar_content_target,'','')
}
}

function date_change(ob,e,yearstart,yearend) {
	var ystart = (typeof yearstart=='undefined') ? 1970 : yearstart;
	var yend = (typeof yearend=='undefined') ? 2010 : yearend;
	
var xtarget=calendar_target



if (isObject(document.getElementById(ob.id + '.script'))){
xtarget=ob.id + '.script'
}


var keycode;
if (window.event) {keycode = window.event.keyCode;}
else if (e) {keycode = e.which;}


var ivalue=ob.value
	if (ivalue=='none')	{ivalue=''	}
	var temp_date = new Date();
var temp=temp_date.getFullYear() + '-' +  (temp_date.getMonth() < 9 ? '0' : '') + (temp_date.getMonth() + 1) + '-' + (temp_date.getDate() < 10 ? '0' : '') + temp_date.getDate() 
//alert(temp)
if (keycode!=8 ){


if (ivalue.length==4 ){
ivalue=ivalue + "-"
ob.value=ivalue
}
if (ivalue.length>4 && ivalue.substring(4,5)!="-"){
ivalue=ivalue.substring(0,4)  + "-" + ivalue.substring(6,ivalue.length)
ob.value=ivalue
}

if (parseInt(ivalue.substring(5,6))>1){
ivalue=ivalue.substring(0,4)  + "-0" + ivalue.substring(5,6) + "-"
ob.value=ivalue
}

else if ( (ivalue.substring(5,7)=="1-")){
ivalue=ivalue.substring(0,4)  + "-01-"
ob.value=ivalue
}



if (ivalue.length>6 && ivalue.substring(7,8)!="-"){
ivalue= ivalue.substring(0,7) + "-"
ob.value=ivalue
}
}
ivalue=ivalue + temp.substring(ivalue.length,temp.length)

if (isObject(date_object)) {
if (date_object.id != xtarget && date_object.id!=calendar_target){
	date_close();
		}
}

date_object=ob;
//showChooser(ob, ivalue, xtarget, ystart, yend, 'Y-m-d', false)
//alert(Date(ivalue))

set_datetime(parse_date(ivalue))

}

function parse_date(ivalue) {
var arr_date = ivalue.split('-');
	var dt_date = new Date();
	dt_date.setDate(1);
	if (arr_date[0]) {dt_date.setFullYear(arr_date[0])}
	if (arr_date[1]) {dt_date.setMonth(arr_date[1]-1)};
	if (arr_date[2]) {dt_date.setDate(arr_date[2])};
return dt_date
}

function format_date(n_datetime) {
var temp_date = new Date(n_datetime);
var temp=temp_date.getFullYear() + '-' +  (temp_date.getMonth() < 9 ? '0' : '') + (temp_date.getMonth() + 1) + '-' + (temp_date.getDate() < 10 ? '0' : '') + temp_date.getDate() 
return temp
}



function init_calendar() {
var dt_current_day = new Date()
var allObjects = document.getElementById(calendar_source).getElementsByTagName('P');
		for(var i=0;i<allObjects.length;i++){
			if(allObjects[i].className!='nameday'){
					
					dt_current_day=parse_date(allObjects[i].title)
					var xtarget='jsc_' + (dt_current_day.getMonth() < 9 ? '0' : '') + (dt_current_day.getMonth() + 1) + '_' + (dt_current_day.getDate() < 10 ? '0' : '') + dt_current_day.getDate()

					//alert(xtarget)
				if (isObject(document.getElementById(xtarget))){
					if (document.getElementById(xtarget).className!='day_event' || document.getElementById(xtarget).className!='day_current')	{
						document.getElementById(xtarget).className='day_' + allObjects[i].className;
					}
					
				}
				
			}
		}

}

function date_close() {
	
	var xtarget=calendar_target
if (isObject(document.getElementById(date_object.id + '.script'))){
xtarget=date_object.id + '.script'
}
document.getElementById(xtarget).style.display="none"
}

