
var i_lb_id = 'lightbox';
var i_lb_inner_id = 'lightbox_content_inner';
var i_lb_id_post = '';
var lb_count = 0;

var browser = 'firefox';
if ( navigator.userAgent.indexOf("MSIE") !=-1 ) { browser = 'ie'; }


document.observe("dom:loaded", function() {

  servMenuAddClassLastLi();
  addLoginCode();
  //correctFlash();
  if ( $('sitemap') ) { buildsiteMap() }

  var btn_subscribe = $('btn_newsletter_subscribe');
  if ( btn_subscribe ) { btn_subscribe.observe('click', newsletter_subscribe.bindAsEventListener()); }

  Event.observe(document.onresize ? document : window, "resize", positionBox );
  Event.observe(document.onscroll ? document : window, "scroll", positionBox );

  var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var ie6 = (rslt != null && Number(rslt[1]) < 8);
  if ( ie6 == true ) { replHrs() }

});


function servMenuAddClassLastLi() {
  var servmenu_li = $$('ul#servmenu li');
  servmenu_li[servmenu_li.length-1].addClassName('li_last');
}


function replHrs() {
  $$('#col_left hr').each(function(i) { i.replace('<div class="dv_hr"></div>') });
}


function addLoginCode() {
  if ( $('atlogin') ) { $('atlogin').observe('click', atlogin.bindAsEventListener()); }
}

function correctFlash() {

  var arr_flashobj = $$('#col_center object');
  if ( arr_flashobj.length > 0 ) {
    var arr_flashmovies = $$('#col_center embed');
    arr_flashobj.each( function(i) { i.insert({top : '<PARAM NAME="wmode" VALUE="transparent">'}) } );
    arr_flashmovies.each( function(i) { i.writeAttribute({'wmode': 'transparent'}) } );
  }

}

function orderTicket( program_id ) {

  var iframe_url =
    'http://gebouwt.activetickets.com/ProgrammaDetail.aspx'
    + '?id=' + program_id
    + '&display=edit';

  iframe_url2 =
    'http://gebouw-t.sb.local/fake_login.asp'
    + '?id=' + program_id
    + '&display=edit';

  var str_content =
    '<iframe id="lightbox_frame" src="' + iframe_url + '" frameborder="0"></iframe>'

  showMessageBox( 'Ticket bestellen', str_content, 700, 570 );

}


function newsletter_subscribe() {

  var iframe_url = 'http://gebouwt.activetickets.com/CreateAccount.aspx';

  //var iframe_url = 'http://gebouw-t.sb.local/fake_subscribe.asp';

  var str_content =
    '<iframe id="lightbox_frame" src="' + iframe_url + '" frameborder="0"></iframe>'

  showMessageBox( 'Inschrijven', str_content, 700, 570 );

}


function atlogin() {

  var iframe_url = 'http://gebouwt.activetickets.com/login.aspx';

  var str_content =
    '<iframe id="lightbox_frame" src="' + iframe_url + '" frameborder="0"></iframe>'

  showMessageBox( 'Inloggen', str_content, 700, 570 );

}

function showMessageBox(header,message,boxwidth,boxheight) {

  var lb_id = i_lb_id;
  var lb_inner_id = 'lightbox_content_inner';
  var lb_exists = 0;

  grayOut(true);

  var html =
    '<div id="lightbox_header">'
    + '<div id="lb_h_l">' + header + '</div>'
    + '<div id="lb_h_r"><img id="lb_closer" src="/i/elements/lightbox/lb_close.gif" onclick="hideLightBox(\'' + lb_id + '\');" /></div>'
    + '</div>'
    + '<div id="lightbox_content">'
    + '</div>';

	if ( !$(lb_id) ) {
    //alert(lb_id + ' bestaat niet');
  	var lightbox = document.createElement('div');
  	lightbox.setAttribute('id', lb_id);
    lb_count++;
	}
	else {
    //alert(lb_id + ' bestaat wel');
  	var lightbox = $(lb_id);
    lb_exists = 1;
	}

  lightbox.innerHTML = html;

  var boxWidth = parseFloat(boxwidth);
  var boxHeight = parseFloat(boxheight);
  var boxHeightInner = parseFloat(boxheight-68);

  var body_dim = document.viewport.getDimensions();
  var body_scroll = document.viewport.getScrollOffsets();
  var body_w = body_dim.width;
  var body_h = body_dim.height;
  var body_t = body_scroll.top;

  if ( lb_exists == 1 ) {

    var lb_left_end = (body_w/2)-(boxWidth/2);
    var lb_top_end = (body_h/3)-(boxHeight/2) + body_t;
    if ( lb_top_end < 10 ) { lb_top_end = 10 }
    $('lightbox_content').update(message);

  }
  else {

    document.getElementsByTagName('body')[0].appendChild(lightbox);

    var lb_left_end = (body_w/2)-(boxWidth/2);
    var lb_top_init = (body_h/3)-(boxHeight/2) + body_t;

    lightbox.style.width = boxWidth + 'px';
    lightbox.style.height = boxHeight + 'px';

    $('lightbox_content').update(message);
    $('lightbox_frame').style.width = boxWidth-20 + 'px';
    $('lightbox_frame').style.height = boxHeight-50 + 'px';

  }

  $('lightbox_header').style.width = boxWidth + 'px';
  positionBox();

}


function positionBox() {

  if ( $('lightbox') ) {

    var el = $('lightbox');

    var popup_width = el.style.width.replace('px', '');
    var popup_height = el.style.height.replace('px', '');

    var window_scroll = getPageScroll();
    var window_height = getWindowHeight();

    var lb_top = Math.round((window_height/2) - (popup_height/2) + window_scroll);
    var lb_left = Math.round((document.viewport.getWidth()/2) - (popup_width/2));

    if ( lb_top < window_scroll ) { lb_top = window_scroll+20; }
    if ( lb_top < 20 ) { lb_top = 20; }
    if ( lb_left < 10 ) { lb_left = 10; }

    el.style.top = lb_top + 'px';
    el.style.left = lb_left + 'px';

    var str_debug =
      'window H x W : ' + window_height + ' x ' + document.viewport.getWidth()
      + '\npopup H x W : ' + popup_width + ' x ' + popup_height
      + '\nscroll : ' + window_scroll
      + '\ntop : ' + lb_top
      + '\nleft : ' + lb_left

      + '\n'
      + '\nel.style.top : ' + el.style.top
      + '\nel.style.left : ' + el.style.left
      ;

    //alert(str_debug);

  }

}


function grayOut(vis, options) {

  var dark=$('darkenScreenObject');
  var flash_slideshow = $('flashimageslider');

  if (!dark) {
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');
    tnode.id='darkenScreenObject';
    tbody.appendChild(tnode);
    dark=$('darkenScreenObject');
  }

  if (vis) {
    dark.style.height = getBrowserWindowSize().height + 'px'; dark.show();
    if ( flash_slideshow ) { flash_slideshow.addClassName('flashimageslider_hidden') }
  }
  else {
    dark.hide()
    if ( flash_slideshow ) { flash_slideshow.removeClassName('flashimageslider_hidden') }
  }
  //adaptToScreen();

}


function getBrowserWindowSize() {

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
	  xScroll = window.innerWidth + window.scrollMaxX;
	  yScroll = window.innerHeight + window.scrollMaxY;
  }
  else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	  xScroll = document.body.scrollWidth;
	  yScroll = document.body.scrollHeight;
	  }
	else {
    // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	  xScroll = document.body.offsetWidth;
	  yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) {	// all except Explorer
	  if(document.documentElement.clientWidth){
		  windowWidth = document.documentElement.clientWidth;
	  } else {
		  windowWidth = self.innerWidth;
	  }
	  windowHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	  windowWidth = document.documentElement.clientWidth;
	  windowHeight = document.documentElement.clientHeight;
  }
  else if (document.body) { // other Explorers
	  windowWidth = document.body.clientWidth;
	  windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight) { pageHeight = windowHeight; }
  else { pageHeight = yScroll; }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth) { pageWidth = xScroll; }
  else { pageWidth = windowWidth; }

  var rval = {width:pageWidth,height:pageHeight};
  return rval;
}


function hideLightBox(id) {
  if ( $(id) ) {
    document.body.removeChild($(id));
    lb_count--;
    if (lb_count==0) { grayOut(false); }
  }
}


function getWindowHeight() {

	var windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}

	return windowHeight;

}


function getPageScroll() {

  var yScroll;
  if (self.pageYOffset) {
  	yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
  	yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
  	yScroll = document.body.scrollTop;
  }

  return yScroll;

}


function buildsiteMap() {

  var arr_li = $$('#sitemap > li');
  arr_li[arr_li.length-1].addClassName('li_last');

  var arr_li_ul = $$('#sitemap > li > ul');

  var arr_li_ul_li, ul;
	for(var i=0;i < arr_li_ul.length;i++) {
    ul = arr_li_ul[i].identify();
    arr_li_ul_li = $$('#' + ul + ' > li');
	  for(var j=0;j < arr_li_ul_li.length;j++) {
      arr_li_ul_li[arr_li_ul_li.length-1].addClassName('li_last');
    }
	}

}

