var type; // public of internal

function init_page(is_intern) {

  type = is_intern ? 'internal' : 'public';

  scale_background();

  today_date = this_date = new Date();
	day = this_date.getDate();
	month = this_date.getMonth() + 1;
	year = this_date.getFullYear();
  update_picker();
  
  // png fix
  if ( typeof(pngfix) == 'function' ) {
    pngfix();
  }

}

function open_window( url, width, height, left, top, extra_params ) {

  extra_params = extra_params || 'dependent=no,directories=no,location=no,menubar=no,personalbar=no,status=no,resizable=yes,scrollbars=yes';
  width  = width  || 450;
  height = height || 600;
  if ( !left && !top ) {
    var screenX = 30; var screenY = 30; var screenTop = 30; var screenLeft = 30;
    if ( typeof(window.screenLeft) != 'undefined' ) {
    	left = screenLeft + window.screenLeft;
    	top	 = screenTop + window.screenTop;
    } else {
    	left = screenX + window.screenX;
    	top	 = screenY + window.screenY;
    }
  }

  window.open( url, 'popupWindow', 'width='+width+',height='+height+',left='+left+',top='+top+','+extra_params);

}


function menu_item_onmouseover( which ) {
  document.getElementById('menu_item_'+which).style.backgroundImage = 'url(' + igorurl + '/gfx/raster.gif)';
  document.getElementById('submenu_container_'+which).style.display = 'block';
}

function menu_item_onmouseout( which ) {
  document.getElementById('menu_item_'+which).style.backgroundImage = '';
  document.getElementById('submenu_container_'+which).style.display = 'none';
}

function scale_background() {

  return 0;

  var org_width = 1024, org_height = 768;

  var width = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
  var height = height = Math.round( ( width / org_width ) * org_height );

  document.getElementById('background_container').style.width  = width  + 'px';
  document.getElementById('background_container').style.height = height + 'px';

  document.getElementById('page_container').style.zIndex = '1000';
  document.getElementById('background_container').style.zIndex = '2';

}

function ignore_mouse_down(e) {
  e = (e) ? e : window.event;
  target = e.target || e.srcElement;
  return false;
}

function make_switch_language(lang) {
		var url ;
		url = "/switch.language.pl?language="+lang+"&rand="+Math.floor(Math.random()*10000)+"&referer="+encodeURIComponent(document.location.href);
		document.location.href=url;
}

function switch_language(lang) {

	var appname = navigator.userAgent.toLowerCase();
	if ( appname.indexOf("safari") != -1 && ! document.cookie ) {
		//Safari kent geen sessions in een iframe of frame. dus dan maar ff zo
		url= "/close.me.pl";
		open_window(url,1,1,'dependent=no,directories=no,location=no,menubar=no,personalbar=no,status=no,resizable=no,scrollbars=no');
		setTimeout("make_switch_language('"+lang+"');",1000);
	}
	else {
		make_switch_language(lang);
	}
}

// ----------------------------------------------------------------------------------------------------
// agenda functies

var day = 9;
var month = 5;
var year = 2010;

var min_date = new Date( 2005, 0, 1 );
var max_date = new Date( 2020, 11, 31 );

var months = new Array( 'jan', 'feb', 'mrt', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec' );

var this_date = new Date( year, month - 1, day );
var today_date = new Date();

function update_today_info( today_info_year, today_info_month, today_info_day ) {

  document.getElementById('today_info_day_image').src = igorurl + '/gfx/agenda/dagen/' + today_info_day + '.png';
  document.getElementById('today_info_month_image').src = igorurl + '/gfx/agenda/maanden/' + months[ today_info_month - 1 ] + '.png';

  document.getElementById('today_info_text').innerHTML = 'loading...';

	var http_request;
	if (window.XMLHttpRequest){ 
		http_request = new XMLHttpRequest(); 
	} else if(window.ActiveXObject){ 
		http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	http_request.open( "GET", igorurl + '/agenda/get.today.info.pl?year='+today_info_year+'&month='+today_info_month+'&day='+today_info_day + '&type=' + type + '&trigger=' + ( new Date().getTime() ), true );
	http_request.send(null);
	http_request.onreadystatechange = function () {
		if ( http_request.readyState == 4 ) {
			if ( http_request.status == 200 ) {
				var innerHTML = http_request.responseText;
				document.getElementById('today_info_text').innerHTML = innerHTML;
			} else {
			}
			http_request = null;
		}
	};

  if ( typeof(pngfix) == 'function' ) {
    pngfix();
  }

}

function update_active_days_this_month( today_info_year, today_info_month, today_info_day, clicked, update_picker ) {

  var first_active_day;
  var first_active_month;
  var first_active_year;

	var http_request;
	if (window.XMLHttpRequest){ 
		http_request = new XMLHttpRequest(); 
	} else if(window.ActiveXObject){ 
		http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
  var url = igorurl + '/agenda/get.active.days.this.month.pl?year='+today_info_year+'&month='+today_info_month + '&type=' + type + '&trigger=' + ( new Date().getTime() );
	http_request.open( "GET", url, true );
	http_request.send(null);
	http_request.onreadystatechange = function () {
		if ( http_request.readyState == 4 ) {
			if ( http_request.status == 200 ) {

        // resultaat
        var days = eval( "(" + http_request.responseText + ")" );

        // update de dagen in de agenda
        if ( update_picker ) {
   
          var base_date = new Date( today_info_year, today_info_month - 1, 1 );	// de eerste dag van de picker is de eerste
          var base_date_number = ( base_date.getDate() - base_date.getDay() );  // zondag voor de eerste dag van deze maand
   
          for ( var i in days ) {
            var date_number = days[i] - base_date_number;
            write_week = Math.floor( date_number / 7 );
            write_day  = date_number - 7 * Math.floor( date_number / 7 );
            document.getElementById( "week" + write_week + "day" + write_day ).className = document.getElementById( "week" + write_week + "day" + write_day ).className + ' calendar_day_active';
          }
        }

        if ( clicked ) {

          update_today_info( today_info_year, today_info_month, today_info_day )

        } else {

          if ( days.length ) {
            days = days.sort( function(a,b) { return a - b } );
            if ( today_info_year < ( today_date.getFullYear() ) || ( today_info_year == ( today_date.getFullYear() ) && today_info_month - 1 < today_date.getMonth() ) ) {
              first_active_day = days[days.length-1];
              first_active_month = today_info_month;
              first_active_year  = today_info_year;
            } else if ( today_info_year > ( today_date.getFullYear() ) || ( today_info_year == ( today_date.getFullYear() ) && today_info_month - 1 > today_date.getMonth() ) ) {
              first_active_day = days[0];
              first_active_month = today_info_month;
              first_active_year  = today_info_year;
            } else {
              for ( var i in days ) {
                if ( today_info_day <= days[i] ) {
                  first_active_day = days[i];
                  first_active_month = today_info_month;
                  first_active_year  = today_info_year;
                  break;
                }
              }
            }
          }
   
          if ( first_active_day ) {
            update_today_info( first_active_year, first_active_month, first_active_day )
          } else {
            var function_date = new Date(today_info_year, today_info_month, today_info_day);
            if ( Math.abs(this_date.getTime() - function_date.getTime()) < 1000*60*60*24*90 ) {
              var find_year = today_info_year;
              var find_month = today_info_month + 1;
              if ( find_month > 12 ) {
                find_year++;
                find_month = 1;
              }
              update_active_days_this_month( find_year, find_month, 1, false, false );
            } else {
              update_today_info( this_date.getFullYear(), this_date.getMonth() + 1, today_info_day )
            }
          }

        }
        
			} else {
			}
			http_request = null;
		}
	};

}

function update_picker( clicked ) {
	write_title();
	var loop_date = new Date( year, month - 1, 1 );										// de eerste dag van de picker is de eerste
	loop_date.setDate( loop_date.getDate() - loop_date.getDay() );		// zondag voor de eerste dag van deze maand
	for ( var week=0; week<6; week++ ) {
		for ( var weekday=0; weekday<7; weekday++ ) {
			write_cell( week, weekday, loop_date );
			loop_date.setDate( loop_date.getDate() + 1 );
		}
	}
  update_active_days_this_month( year, month, day, clicked, true );
}

function write_cell( write_week, write_day, write_date ) {
	var innerHTML; var className;
	if ( write_date > min_date && write_date < max_date	 ) {
		if ( write_date.getMonth() == this_date.getMonth() ) {
			// current
			innerHTML = "<a href=\"javascript: void click_date(" + write_date.getFullYear() + ", " + ( write_date.getMonth() + 1	) + ", " + write_date.getDate() + ")\">" + write_date.getDate() + "</a>";
      className = 'calendar_day';
		  if ( write_date.getDate() == today_date.getDate() && write_date.getMonth() == today_date.getMonth() && write_date.getYear() == today_date.getYear() ) {
        className = className + ' calendar_day_today';
      }
		} else {
			innerHTML = "<a href=\"javascript: void click_date(" + write_date.getFullYear() + ", " + ( write_date.getMonth() + 1	) + ", " + write_date.getDate() + ")\">" + write_date.getDate() + "</a>";
      className = 'calendar_day calendar_day_inactive';
		}
	} else {
		// inactive
		innerHTML = "<a href=\"javascript: void click_date(" + write_date.getFullYear() + ", " + ( write_date.getMonth() + 1	) + ", " + write_date.getDate() + ")\">" + write_date.getDate() + "</a>";
    className = 'calendar_day calendar_day_inactive';
	}
	document.getElementById( "week" + write_week + "day" + write_day ).innerHTML = innerHTML;
	document.getElementById( "week" + write_week + "day" + write_day ).className = className;
}

function write_title() {
	document.getElementById('calendar_month').innerHTML = months[ this_date.getMonth() ];
	document.getElementById('calendar_year').innerHTML = parseInt(this_date.getFullYear());
}

function click_date( click_year, click_month, click_day ) {

  // zet de agenda even naar daze datum, voor als je op een grijze datum hebt geklikt
	day = click_day;
	month = click_month;
	year = click_year;
  this_date = new Date( click_year, click_month - 1, click_day );
  update_picker( "clicked" );

  update_today_info( click_year, click_month, click_day );

}

function previous_month() {
	month--;
	if ( month < 1 ) {
		year--;
		month = 12;
	}
	this_date = new Date( year, month - 1, day );
	update_picker();
}

function next_month() {
	month++;
	if ( month > 12 ) {
		year++;
		month = 1;
	}
	this_date = new Date( year, month - 1, day );
	update_picker();
}

function previous_year() {
	year--;
	this_date = new Date( year, month - 1, day );
	update_picker();
}

function next_year() {
	year++;
	this_date = new Date( year, month - 1, day );
	update_picker();
}
// -->

// einde agenda functies
// ----------------------------------------------------------------------------------------------------


