/*
 * in_array function
 */
function in_array( what, where )
{
	var a=false;
	for(var i=0;i<where.length;i++)
	{
		where[i] = trim(where[i]);
	  if(what == where[i])
	  {
	    a=true;
        break;
	  }
	}
	return a;
}

/*
 * Verklein het scherm als het venster dat toestaat
 */ 
function verklein()
{
    var height = $(window).height(); 
    var width = $(window).width();

    //browser fix
    if(jQuery.browser.msie){width = width + 2; height = height + 5;}

    //var max = 800;
    var max = 878;
    var body_height = $('body').height();
    
    
    //hoogte van contentvakje
 	var textheight = 430;
	
	//scrollbar wel of niet laten zien 
	if(height < 570)
	{
		//max hoogte van tekstveld, wanneer er wel een scrollbar wordt getoond rechts
		textheight = 594;
			 
	 	$('body').css({'overflow-x':'hidden', 'overflow-y':'visible'});
		 
		 if(width < 1000)
	      {
		   $('body').css({'overflow-x':'visible', 'overflow-y':'visible'});
		 }
		
     	if(jQuery.browser.version == '7.0' || jQuery.browser.version == '6.0'){$('html').css({'overflow':'auto'});}	
		 
	}
	else
	{   
		
		$(window).scrollTop(0);
		$('body').css({'overflow-x':'hidden', 'overflow-y':'hidden'});
		 
		//scrollbar-x laten zien of niet..
	      if(width < 1000)
	      {	
		   $('body').css({'overflow-x':'visible', 'overflow-y':'hidden'});
		 }
		 else
		 {
		   	$(window).scrollLeft(0);
			 $('body').css({'overflow-x':'hidden', 'overflow-y':'hidden'});
		 }
		 	
		if(jQuery.browser.version == '7.0' || jQuery.browser.version == '6.0'){$('html').css({'overflow':'hidden'});}		 		 
	}
 
 	
 	//wanneer gaan we verkleining toepassen? (bij een schermhoogte kleiner dan 800)
    if(height < max)
    {
		//verschil tussen max 
         var margin = (max-height);
         if(margin < 90)
         {         
         		$('#body').css({'background-position':'50% -0px', 'margin-top':'-'+margin+'px'});			
	    }
	    else
	    {	    		
	    		margin = margin/1.65;
	    		//if(margin > 110){margin=145;}
	    		if(margin > 145){margin=145;}
			$('#body').css({'background-position':'50% -0px', 'margin-top':'-'+margin+'px'});    
			
			//textheight = textheight - margin - 20		
	    }
	    	//console.log(height,(max-height),margin,((max-height)-margin)-margin)
	    	if (height >= 570) {
	    		
	    		textheight = (78+370+(145-(max-height-margin)))
	    	}
    }
    else
    {
    	 	$('#body').css({'background-position':'50% 0', 'margin-top':'0'});
    	 	//max hoogte van tekstveld, wanneer er wel een scrollbar wordt getoond rechts
		textheight = 594;
		
    	 	
    }     
    
    //$("#text").css({"height":(textheight+(height-max+160))+"px"})
    $("#text").css({"height":(textheight)+"px"})
    $('#base').css({'margin-top':'0px'});  
}


/**
 * menu object
 * hiermee worden de animaties, hovers bijgehouden en gemaakt.  
 */ 
function menu()
{
	var obj = this;
	this.lis;
	this.menu_type;
	
	obj.done;
	obj.ajax;
	
	//url goed zetten
	obj.link  = ""+document.location;
	
	//zet de url goed
	//private
	menu.prototype.parseUrl = function()
	{	
		obj.link = obj.link.split('/');
		obj.link.pop();
		obj.link = obj.link.join('/');
		
		obj.link = obj.link.split('.nl');
		obj.link.pop();
		obj.link = obj.link.join('.nl');
	}
	obj.parseUrl();
	

	//ajax pagina ophalen
	//private
	menu.prototype.getPage = function(page)
	{  	
		obj.parseUrl();

		obj.link = obj.link+""+$(page).attr('href');
				 
		//ajax abort inbouwen
	    //if(obj.ajax != undefined){ console.log(obj.ajax); obj.ajax.abort();  }
       //	obj.link = 'http://www.heemgaard.nl'+obj.link;

 	    obj.ajax = $.post(obj.link, {ajax:true}, function(html){
 			$('#text').html(html);		
 	    });
 	    
//  	     $.ajaxSetup({
// 		    type: "POST",
// 		    timeout: 10000,
// 		    contentType: "application/x-www-form-urlencoded; charset=iso-8859-1"        
// 		}); 

//  	    obj.ajax = $.ajax({
// 		  url: obj.link,
// 		  type: "POST",
// 		  data: "ajax=true",
// 		  dataType: "html",	
// 		  success: function(html){
// 		    $('#text').html(html);
// 		  }   
// 		});
		
		
		
		

 	    
	

 	    
	}
	
	//get the li`s to bind
	//private
	menu.prototype.getLi = function(item)
	{
		var p = $(item).parents().eq(1);
		var select;
		 if( $(p).hasClass('submenu') )
		 {
		 	return $('.submenu li').not('.menu_selected');
		 }
		 else
		 {
			return $('#menu li').not('.submenu li').not('.menu_selected');
		 }
	}
	
	//get the li`s to bind
	//private
	menu.prototype.setType = function(item)
	{
		var p = $(item).parents().eq(1);
		 if( $(p).hasClass('submenu') )
		 {
		 	return 'submenu';
		 }
		 else
		 {
		 	return 'menu';
		 }
	}
	
	//hide de overige sub uls
	//private
	menu.prototype.hideUl = function()
	{
		$("#menu li:not(.menu_selected) ul").hide();
	}
	
	//unbind the li
	//private
	menu.prototype.unbindLi = function(selector)
	{
		$(selector).unbind('click');	
	}
	
	//hovers opbouwen
	//private
	menu.prototype.makeHovers = function(selectors, submenu)
	{
		if(submenu)
		{
			$(selectors).hover(function(){
				$(this).css({'color':'#000000'});	
			},function(){
				$(this).css({'color':'#ffffff'});		
			});
			
			$(selectors).css({'color':'#fff'});
			
		}                      
		else
		{
			$(selectors).hover(function(){
				$(this).css({'color':'#e2001a'});	
			},function(){
				$(this).css({'color':'#000000'});		
			});
		}		
	}
	
	//anmiate the menu
	//private
	menu.prototype.animateMenu = function(e)
	{
		
		e.stopPropagation();
          
          //set de type
          obj.type = obj.setType(this);
          
          //waar is er op geklikt
          obj.target = e.target;
          
          //slideUp berekenen
		if($(this).parents().eq(1).attr('id') == 'menu' && $(this).hasClass('menu_selected') == false  )
		{
	   		$('#menu').find('.submenu').slideUp();
		}
		
		//als het een hoofd menu item is, class handelen
		if(obj.type == 'menu')
		 {
		 	
		 	//remove submenu selected
		 	$('.submenu li a').removeClass('submenu_selected');
		 
			//handle the selected class
			$('#menu li a').removeClass('menu_selected');
			$('#menu li').removeClass('menu_selected');
			
			 //animate		
			$(this).find('a').addClass('menu_selected');
			$(this).addClass('menu_selected');
			$(this).parent().find('.submenu').slideDown();

			//unbind
			obj.unbindLi('#menu li a.menu_selected');

			
			//bind again
			$(obj.getLi(this)).find('a').bind('click', obj.animateMenu);
		}  
		else
		{
			//handle the selected class
			$('#menu li a').removeClass('submenu_selected');
			$('#menu li').removeClass('submenu_selected');
			
			 //animate		
			$(this).find('a').addClass('submenu_selected');
			$(this).addClass('submenu_selected');
			$(this).parent().find('.submenu').slideDown();

			//unbind
			obj.unbindLi('#menu li a.submenu_selected');
			
			//bind again
			$(obj.getLi(this)).find('a').bind('click', obj.animateMenu);
		}                                             
		
		//make hovers
		obj.makeHovers($(obj.getLi(this)).find('a:not(.menu_selected)'));
 		obj.makeHovers(".submenu li a", true);	
           
		 //loading bar 
		//$('#text').html('<div  style="width:200px; text-align:center;margin:50px 0 0 130px">Pagina laden. <br /><img src="/images/loading.gif"/></div>');
		  
		//ajax load
		obj.getPage(this);
		
		//console.log($(this).attr('href'))
		//console.log(top.location)
		//return false;
		//zet de hash goedis
		
		if (!jQuery.browser.safari) {
			top.location.hash = '#'+this;
		}
		
		var parts = top.location.hash.split('/');
		parts = parts.pop();
		
		if (!jQuery.browser.safari) {
			try {
				top.location.hash = '#'+parts;
			} catch (errrr) {}
		}
		
		
		return false;
	}
	
	//maak het menu
	//public
	menu.prototype.makeMenu = function()
	{	
		obj.hideUl();  
		obj.makeHovers("#menu li a:not(.menu_selected, .submenu li)");
		obj.makeHovers(".submenu li a", true);
	   	$("#menu li a:not(.menu_selected)").bind('click',obj.animateMenu);
	}	
}

function place_img(options)
{
	var obj = this;
	
	//option values
	obj.wrapper = options.wrapper;
	obj.min_height = options.minheight;
	obj.dummy = "<div id='dummy'></div>";
	obj.content_id = options.content;
	obj.content =  $(obj.content_id).html();
	obj.image_id =  options.image;
	
	if ( $(obj.image_id).html().indexOf('portrait') == -1 ) {	
		obj.image =  "<div id='plakfoto'>"+$(obj.image_id).html()+"</div>";
	} else {
		obj.image =  "<div id='plakfoto_portrait'>"+$(obj.image_id).html()+"</div>";
	}
		
	obj.offset = $(obj.content_id).parent().height() - $(obj.image_id).height() 	
	
	// default values
	obj.begin = 0;
	obj.end = options.end;
	obj.begin_content;
	obj.end_content;
	obj.limit = obj.content.length;
	obj.uitzondering = new Array('fotoalbum', 'contactformulier', 'personeel', 'wijzigwachtwoord', 'nieuws', 'login', 'downloads/', 'downloads');
	

	place_img.prototype.count_tags = function(text, open)
	{
		if(open)
		{
			obj.tags_open = text.match(/</g);
			obj.tags_open_full = text.match(/<[^<]+>/g);			
			if(obj.tags_open != null && obj.tags_open_full != null && obj.tags_open.length == obj.tags_open_full.length){obj.tags_open_ok = true;}else{obj.tags_open_ok = false;}	
		}
		else if(!open)
		{
			obj.tags_close = text.match(/>/g);	
			obj.tags_close_full = text.match(/<[^<]+>/g);
			if(obj.tags_close != null && obj.tags_close_full != null && obj.tags_close.length == obj.tags_close_full.length){obj.tags_close_ok = true;}else{obj.tags_close_ok = false;}	
		}
		else
		{
		    obj.tags = obj.text.match(/<[^<]+>/g);
		}
		
	}
	
	place_img.prototype.place_dummy = function(verklein)
	{     
		   
		if(obj.end > 0 && obj.content.length > 200)
		{
			
			if(verklein)
			{
				obj.end -= 10;	
				
				obj.place_dummy();
			}
			else
			{	
				
				obj.begin_content = obj.content.substring(obj.begin,obj.end);
				obj.end_content = obj.content.substring(obj.end,obj.limit);
				
				obj.count_tags(obj.begin_content, true);
				obj.count_tags(obj.end_content, false);
				
				if(obj.tags_close_ok && obj.tags_open_ok)
				{    
						
				   $(obj.content_id).html(obj.begin_content+obj.dummy+obj.end_content);
				   
				   var check_pos = $('#dummy').position();

				   if(check_pos.top < obj.min_height)
				   {
				   		
				   		$('#dummy').html(obj.image);	
				   		//$('#dummy').css("float","right")
				   }
				   else
				   {       
						obj.place_dummy(true);
				   }
				}
				else
			     {    
			     		
					obj.place_dummy(true);
			     }
			}
		}
		else
		{
		     $(obj.content_id).html(obj.content+obj.image);
		}										
	}
	
	place_img.prototype.init = function()
	{  
		if($(obj.image_id).size() > 0 )
		{
		 	var location = top.site.document.location.href;
			var check = location.split('/');
			var check2 = check;
			check = check.pop();  
			
			var check1 = top.site.document.location.hash.replace('#/', '');  
			if( check1 != '')
			{
				check = check1;	
			}
			
			var check3 = top.document.location.hash.replace('#', ''); 
			check3 = check3.split('/'); 
			if($.inArray(check3[0], obj.uitzondering) != -1)
			 {
			 	check = check3[0];	
			 }
		
		
			 if($.inArray(check2[check2.length-1], obj.uitzondering) != -1)
			 {
			 	check = check2[check2.length-1];	
			 }
	
	
			if($.inArray(check, obj.uitzondering) == -1)
			{     
				$(obj.content_id).html('<div style="margin:30px auto 0 40px;text-align:center;">Pagina inhoud ophalen<br /><img src="/images/loading.gif"/></div>');
				$(obj.image_id).html(' ');
				$(obj.image_id).remove();  
				
				var time = obj.content.length / 1000;
	
				if(time > 500)
				{
					setTimeout(function(){obj.place_dummy()}, time);	
				}
				else
				{
					obj.place_dummy();
				}
			}
			else
			{
			   	$(obj.image_id).html(' ');
			}
		}
		else
		{
		   	$(obj.image_id).html(' ');
		   	return false;
		}
		 							
	}	
} 

function image_preloader(images)
{
	var obj = this;
	obj.images = images;
	obj.hide_selector;
	
	image_preloader.prototype.hide = function(selector)
	{
	 	obj.hide_selector = selector;
		$(obj.hide_selector).css('display','none');	
	}
	
	image_preloader.prototype.show = function()
	{
		$(obj.hide_selector).css('display','block');  	
	}
	
	image_preloader.prototype.check = function()
	{
		var loaded = false;
		$('body').append('<img style="display:none;" id="preload_image" src=""/>');	
		for(img in obj.images)
		{
			$('#preload_image').attr('src', obj.images[img]).load(function() { loaded = true;  });  		
		}	
		return obj.loaded;
	}
	
	image_preloader.prototype.preload = function()
	{
		for(img in obj.images)
		{
			pic = new Image();  	
	        	pic.src = img;
		}
	}
	
	image_preloader.prototype.init = function()
	{
		obj.preload();
		
		//obj.check();
		
	}
}


//redirect
var redirect = '';
var hash = '';
try {
	redirect = top.location.href;
	hash = top.location.hash;
} catch (errrr) {}

if(top == self)
{      
  	redirect = redirect.split('.nl');  
	var achterstukje = redirect.pop();
	achterstukje = achterstukje.replace(/\//,"")
	redirect = redirect+'.nl';
	
	hash = hash.split('#');
	hash = hash.pop();
	
	if (jQuery.browser.version == '6.0') {
		window.open(redirect+'/f'+(achterstukje==""?"":"#"+achterstukje))
	}
	else {
		document.location = redirect+'/f'+(achterstukje==""?"":"#"+achterstukje);
	}
}

if(hash.match(/^#[a-zA-Z0-9\/_-]+$/ig))  
{

	if (!jQuery.browser.safari) {
	
		redirect = redirect.split('.nl');
		
		redirect.pop();
		redirect = redirect+'.nl/';
	
		
		hash = hash.split('#');
		//hash = hash[1]
		hash = hash.pop();
		
	
		top.location.hash = ' ';
		document.location = redirect+hash;
	}
}


try
{
	var preloader = new image_preloader({1:'/images/background.jpg', 2:'/images/tabs_magister.png', 3:'/images/tabs_personeel.png', 4:'/images/tabs_rooster.png', 5:'/images/tabs_teletop.png'});
	$('body').prepend('<div style="color:white;font-size:12px;text-align:center;width:200px;margin:100px auto 0 auto;" id="loading">Loading site.<br /><img src="/images/loading.gif"/></div>');
	preloader.hide('#base');
	preloader.init();	
}
catch(e){}


function bindlinks_after_ajax() {
	$('#text:not(#downloads a,contact,#wwform) a, #agenda a, #personeel, #fotoalbum_links a ').not('.fotoSlide a, #personeel').bind('click',function(){
		
		var hash =  $(this).attr('href');
		var link =  $(this).attr('href');
		var target =  $(this).attr('target');
		var ext = hash.split('.');
		ext = ext.pop();
      
      	if (target == '_blank') {
		 	//window.open(link);
			//return false;
		}
		else if( (hash.match(/heemgaard.nl/) && !hash.match(/heemgaard.[^nl]/) ) || (!hash.match(/http/) && !hash.match(/www/) ) && !hash.match(/images/) && !hash.match(/mailto:/) )
		{
			hash = hash.substring(1);
			if(hash.match(/.nl/))
			{
				hash = hash.split('.nl');
				hash = hash.pop();
			}
			
			if (!jQuery.browser.safari) {
				top.document.location.hash = hash;
			}
			
			if(link.match(/.nl/))
			{
				link = link.split('.nl');
				link = link.pop();
			}

			//link = 'http://'+top.site.document.location.host+link;
			if (link.match(/.pdf/) || link.match(/.doc/)) {
				window.open(link);	
			}
			else {
				$.post(link, {ajax:true}, function(html){
		 			$('#text').html(html);		
		 		});
		 	}
		 	    
			return false;	
		
		}
		else
		{
			window.open(link);
			return false;	
		}
	
	});
}

    

//dom klaar is
//$(document).ready(function(){
$(window).bind('load', function(){

	$("#loading").remove();
	try {
  		preloader.show();
  	} catch (e) {}
	$("#base").css('display','block')	

 	//VERKLEIN	
	verklein();
	$(window).bind('resize', function() {
		verklein();
	});
	
	//MENU OPBOUWEN
	try
	{		
		var menus = new menu();
		menus.makeMenu();	
	} catch(e)
	{}
	
	 //plaksticker	
	try
	{		
	   	var plaksticker = new place_img({image:'#plakfoto',wrapper:'#text',content:'#content_text', minheight:'250', end:1000});
		plaksticker.init(menus.done);  
		
	} 
	catch(e){}

	//TAB HOVERS
	var tab = jQuery('#tabs img').attr('src');
	$('map[name="tabs"] area').hover(function(){
		var img = $(this).attr('alt');
		$('#tabs img').attr('src', '/images/tabs_'+img+'.png');	
	},
	function(){
		$('#tabs img').attr('src', tab);
	});
	
	//music
	$('#play').click(function(){
		
		var alt = $(this).attr('alt');
		
		if(alt == 'play')
		{	
			
			$(this).attr('alt', 'stop');
			//top.player.document.location.hash = 'play';  
			top.player.document.location = "http://www.heemgaard.nl/modules/audio.php?play"; //top.player.document.location;   
			
			//top.player.location.reload(); 
		}
		else
		{
		    $(this).attr('alt', 'play');
		    	//top.player.document.location.hash = '';  
			top.player.document.location = "http://www.heemgaard.nl/modules/audio.php";//top.player.document.location; 
			//top.player.location.reload();
		}	
	});
	
	//jquery slideshow
	$('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

	/*
	$('#text:not(#downloads a,contact,#wwform) a, #agenda a, #personeel, #fotoalbum_links a ').not('.fotoSlide a, #personeel').bind('click',function(){
		
		var hash =  $(this).attr('href');
		var link =  $(this).attr('href');
		var ext = hash.split('.');
		ext = ext.pop();
      
		if( (hash.match(/heemgaard.nl/) && !hash.match(/heemgaard.[^nl]/) ) || (!hash.match(/http/) && !hash.match(/www/) ) && !hash.match(/images/) && !hash.match(/mailto:/) )
		{
			hash = hash.substring(1);
			if(hash.match(/.nl/))
			{
				hash = hash.split('.nl');
				hash = hash.pop();
			}
			
			if (!jQuery.browser.safari) {
				top.document.location.hash = hash;
			}
			
			if(link.match(/.nl/))
			{
				link = link.split('.nl');
				link = link.pop();
			}

			//link = 'http://'+top.site.document.location.host+link;
			$.post(link, {ajax:true}, function(html){
		 		$('#text').html(html);		
		 	});
		 	    
			return false;	
		
		}
		else
		{
			window.open(link);
			return false;	
		}
	
	});*/
	
	bindlinks_after_ajax();

	$('#downloads a').unbind();
	
	$('#personeel, #roosterlink').click(function() {
		
		$.post($(this).attr('href'), {ajax:true}, function(html){
 			$('#text').html(html);
			bindlinks_after_ajax();		
 	    	});
 	    	
		
		return false;
	});
	
	
}); 
       
