document.observe("dom:loaded", function() {

  initFlashHeaders();
  HM.Tooltip.load("a.tooltip", {
    effect:  "fade",
    width:   221,
    backgroundColor: "transparent",
    effectDuration: 0,
    opacity: 1,
    left: -110,
    top: 15
   })
});

document.observe("dom:loaded", function() {
  Effect.Fade.delay(0.5,'loader', { duration: 0.5 });
}); 

document.observe("dom:loaded", function() {

	var db=document.body;
	var img=document.getElementById('holder').getElementsByTagName('img')[0];
	

	var dbsize={}; //needed to store body size
	var imgsrc=img.src; //needed to store images src
	var keyStop=function(e){
		var e=window.event||e||{};
		var tag=e.target.tagName.toLowerCase();
		if(tag!='textarea'&&!(tag=='input'&&(e.target.type=='text'||e.target.type=='password'))){ //are the user not writing?
			if(e.keyCode==32||e.keyCode==39||e.keyCode==40){ //Did he press any of the "scrolling-keys"? (down, right, and space key)
				if(e.preventDefault)e.preventDefault();
				else e.returnValue=false;
			}
		}
	}

	if(this.addEventListener)window.addEventListener('keydown',keyStop,false);
	else window.attachEvent('onkeydown',keyStop);

	setInterval(function(){
		window.scrollTo(0,0);
		if(img.complete){ //check if image has loaded
			if(db.clientWidth!=dbsize.w||db.clientHeight!=dbsize.h||img.src!=imgsrc){ //check if size or img size has changed
				imgsrc=img.src; //store current src
				dbsize.w=db.clientWidth; //store current body width
				dbsize.h=db.clientHeight; //store current body height

				var newwidth=Math.round(dbsize.h*(img.offsetWidth/img.offsetHeight)); //calculate new width based on height

				img.style.width=(dbsize.w>newwidth?dbsize.w:newwidth)+'px'; //use the largest value of body-width and newwidht
				//and this is the real trick: if there's no specified height, the height is automaticly calculated relative to the with
			}
		}
	},300);


});


function startOnlineboeken(OB,lng) {
  var w=1050,h=740,scroll='yes';
  var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
  window.open('http://boeken.tibos.nl/reeenwissel/start.lp?lng='+lng+'&value='+OB+'&type=GCB', 'winTIBOSthout', settings);
}


function initFlashHeaders () {
  HM.Flash.Text.load("h1", {
    src: "/flash/titel_content.swf"
  }) 
  HM.Flash.Text.load("h2", {
    src: "/flash/titel_subtitel.swf"
  })   
  HM.Flash.Text.load("h4", {
    src: "/flash/titel_submenu.swf"
  })  
  HM.Flash.Text.load("h5", {
    src: "/flash/titel_tooltip.swf"
  })    
  HM.Flash.Text.load("h6", {
    src: "/flash/titel_h6.swf"
  })   
}

function getContent(param) {
  var element;   
  if(!param) param = {};
  if(!(element = $(param.element) || $('contentBlock') )) {
    return;
  }
  element.update('<center><img src="/images/loading.gif" alt="" width="25" height="25" border="0"></center>');
  
  var URI = "/handlers/#{lng}/#{sitedeel}/#{fn}.lp".interpolate({ 
    lng     : param.lng, 
    sitedeel: param.sitedeel, 
    fn      : param.fn 
  });
  
  console.log(URI);
  
  new Ajax.Request(URI, {  
  	method          : 'post',   
  	parameters      : param,
  	onSuccess       : function(originalRequest) {     
  		try{
      
        new Effect.Fade(element, {
          duration: 0.5,
          afterFinish: function() {
            param.element = element;
            $(element).update(originalRequest.responseText);
            appearFromHidden.defer(param);  
          }
        });
        
  		}catch(e){
        console.log(e);
      }
  	}
  }); 
}

function appearFromHidden() {
  var param   = arguments[0] || {};
  var element;
  
  if(!(element = $(param.element))) {
    return;
  }
  
  new Effect.Appear(element, {
    duration: 0.5,
    afterUpdate: function() {
      initFlashHeaders();
      
      if(param.fn == "omgeving") {
        MyGMap_load();
      }else if(param.fn == "fotoboek" && param.fID) {
        initFormValidator();
      }
    },
    afterFinish: function() {
      if(param.fn == "omgeving") {
        replaceDocument.defer('mapPosition');
      }else if(param.fn == "omgeving-content" && param.ID) {
        replaceDocument.defer('ajax_content');
      }
    }
  }); 
}
function replaceDocument() {
  var target = "#"+arguments[0]; 
  document.location.replace(target);
}
function initFormValidator() {
  HM.Form.Validator.load();
}

/*
document.observe("dom:loaded", function() {
  new HM.NewsScroller("nieuwsScroller", {
    src: "/nl/scroller-content.lp",
    direction: "horizontal",
    interval: 7000
  });
});

*/