CMScontent = new Object(); // for AJAX caching!

$(document).ready(function () {
	//Achtergrond herpositioneren!
	bg_repos();
	$(window).resize(function() { bg_repos(); });


	//Accordion
	$('#accordion').find('dt').css({"display":"block" });
	$('#accordion').find('dd').css({"display":"block" });
	$('#accordion').easyAccordion({ 
			autoStart: false, 
			slideInterval: 3000,
			slideNum: false
	});

	//bg content_rechts
	$(".click-rechts").click(function(){ 
		$('#container_rechts').css({"background-image":"url('img/bg-content.jpg')" });
		return false;
	});
	$(".click-off").click(function(){ 
		$('#container_rechts').css({"background-image":"none"});
		return false;
	});

	// Cool Ajax in action :)
	$('#accordion').find('dt').each(function(){
    if (jQuery(this).attr('class').indexOf('active') >= 0 ) { // remember first id for #
      hash_init = jQuery(this).attr('id');
			/* verry Q&D*/
			if (hash_init == 'contact') {
				$('#container_rechts').css({"background-image":"url('img/bg-content.jpg')" });
			}
    }
    jQuery(this).click(function(){
      the_id = jQuery(this).attr('id');
      CMS_id = the_id.replace(/-/g,'/')+'/';
//			alert(CMS_id);
      if (hash_init != the_id) { // new page = # => back possible :)
        window.location.hash = "#"+the_id;
      }
      if (typeof(CMScontent[CMS_id]) == 'undefined') {
//  		alert('./?nl/'+the_id+'/1/&amp;ajax=1');
        $.ajax({
  				url: './?'+CMS_id+'&a'+a_bit_of_salt+'=1',
  				success: function(data) {
//          alert(data);
//          alert(the_id+"_content");
            $("#"+the_id+"_content").html(data);
            CMScontent[CMS_id] = data; // cache it :D
            re_scan();
  				}
  			});
      } else {
        $("#"+the_id+"_content").html(CMScontent[CMS_id]);
        re_scan();
      }
  		return false;
  	});
	});
	
	re_scan(1);


/* */	
});

function re_scan(init){
	if (typeof(init) == 'undefined' || init != 1 ) { init = 0 }
	if (!init) { Cufon.refresh(); }
	$("a[rel^='lightbox']").colorbox();

	$('#accordion').find('a').each(function(){
		if (jQuery(this).attr('href').indexOf('http://www.knoopmail.nl/?') == 0
		 || jQuery(this).attr('href').indexOf('./?') == 0 ) {
			if (!jQuery(this).attr('class')
			 || jQuery(this).attr('class').indexOf('accordion_link') == -1 ) {
				test_id = jQuery(this).attr('href');
				test_id = test_id.substr(test_id.indexOf('?')+1,100);
				test_id = test_id.replace(/\//g,'-').replace(/-$/i,'');
				if ($('#'+test_id).length > 0 ) {
					jQuery(this).attr('href'   , "javascript:void('')");//http://www.knoopmail.nl/?nl/pro/1/
					jQuery(this).attr('target' , "_self");//http://www.knoopmail.nl/?nl/pro/1/
					jQuery(this).attr('onclick', "$('#"+test_id+"').click();");
				} else {
					jQuery(this).attr('class'  , "lees_meer");
				}
/*				alert(jQuery(this).attr('href')+"\n"+
							jQuery(this).attr('class')+"\n"+
							test_id+"\n"+
							$('#'+test_id).length);
/* */
			}
		}
	});
		
//    if (jQuery(this).attr('class').indexOf('active') >= 0 ) { // remember first id for #

//echo '		<a href="javascript:void(\'\')" onclick="$(\'#nl-pro-1\').click();">test</a>'."\n";


	//Colorbox Iframe.
	$(".iframe").colorbox({
		width:"80%",
		height:"80%",
		iframe:true
	}); 	

/*
	//Colorbox.
	$(".more").colorbox({
		width:"700px",
		height:"500px",
		inline:true,
		href:"#read_more"
	});
/* */
	$(".lees_meer").colorbox({
		width:"80%",
		height:"80%",
		inline:true,
		href:"#internal_link",
		onOpen:function(){
			$.ajax({
				url: this.href+'&a'+a_bit_of_salt+'=1',
				success: function(data) {
//          alert(data);
//          alert(the_id+"_content");
					$("#internal_link").html(data);
//					CMScontent[CMS_id] = data; // cache it :D
					re_scan();
				}
			});
		}
	});
}
//./?nl/home/10/" class="internal_link

function internal_link_ajax(url){
//colorbox({width:"80%", height:"80%", inline:true, href:"#internal_link"});
}

// make page back possible :)
var hash_init = '';
var hash = window.location.hash; 
function checkHash(){
//  document.title = hash_init+' '+hash; 
  if(window.location.hash != hash) { 
    hash = window.location.hash; 
    if (hash.length == 0 || hash.replace('#','') == hash_init ) {
      $('#'+hash_init).click();
    } else {
      $('#'+hash.replace('#','')).click();
    }
  }
  setTimeout("checkHash()",400); 
}
checkHash();

// perfect background positioning :)
function bg_repos() {
	if ((parseFloat(navigator.appVersion.split("MSIE")[1])<7) ) {	//alert('crappie IE 6');
		return;
	}
	var offset = $('#container').offset();
	schaduwL = 40;
	schaduwB = 40;
	pos_left = (offset.left < -schaduwL) ? -schaduwL : offset.left-schaduwL;
	pos_top  = (offset.top  < -schaduwB) ? -schaduwB : offset.top -schaduwB;
	$('#bg').css({"height": "100%",
								"width" : "100%"});
//	document.title = pos_left+' '+pos_top+' '+get_height(0)+' '+$('body').height();
	$('#bg').css({"height": (get_height(0)-1)+"px",
								"width" : (get_width(0)-1)+"px",
								"background-position":" "+pos_left+"px "+pos_top+"px" }) ;
}
a_bit_of_salt = 'jax';
