$(document).ready( function() {	
	// alle anchor-linkjes in de content die niet naar buiten gaan ombouwen zodat ze scrollen naar de bijbehorende alinea
	//
	// In plaats van ALLE anchor links ombouwen behalve die met classe "deeplink" gaan we het anders aanpakken.
	// Die oplossing was niet gebruiksvriendelijk voor gebruik door GAzelle DE
	// We gaan nu bij alle anchor links kijken of de url waarvan deze word opgeroepen gelijk is aan de huidige URL 
	// Een uitzondering zijn links die enkel bestaan uit #[nummer] deze moeten ook allemaal de "scrollToAnchor" aanroepen
	
    $('#contentContainer a, #contentContainerWide a').each( function(e) {
        var id = this.hash.substring(1),
        	path = this.pathname;
        
        // IE needs slash prefix
        if (path.substring(0,1) != '/') {
        	path = '/' + path;
        }

        if (path == location.pathname && id) {
            $(this)
            	.attr('href','#'+id)
            	.unbind("click")
            	.click(function () {
	                scrollToAnchor( id );
	                return false;
	            });
        }
    });
    
	
    // onclick toevoegen aan alle nav-items voor scrollen binnen dezelfde pagina.
    $('#nav a').bind( "click", function(e) {
    	var href = $(this).attr('href');
    	var id = parseInt(href.substring(href.indexOf('#')+1, href.length));
    	var currentUrl = document.location.href;
    	var pageUrl = href.substring(0, href.indexOf('#'));
    	var samePage = currentUrl.indexOf(pageUrl);
    	debugThis('Link Clicked [ goto: '+pageUrl+', id: '+id+', samePage: '+samePage+' ]');
    	if (samePage > 0) {
    		// de aangeklikte link bevind zich op dezelfde pagina... spring naar deze alinea.
    		scrollToAnchor( id );
    		$('.paraItem').removeClass('active');
			$('#paraNav_'+id).toggleClass('active');
    		return false;
    	}
    });
    
    loadHorNav();
    
    // transform the windows-style selectbox into a fancy dropdown
    $('.blog_select_boxes').selectbox();
    var bullets = '';
    // place background-bullets on the sliderline to indicate sliderpositions
    if (itemCount > 1) {
        var bulletSpacing = (940 / (itemCount-1));
        for (var x=0; x<=itemCount-1; x++) {
            bullets += '<img src="/de/site_files/img/scrollbar_bullet_transp.png" alt="O" border="0" id="bullet_'+x+'" class="scrollbar_bullet" style="left: '+((bulletSpacing*x)-7)+'px; height: 12px;" width="12" height="12" />';
		}
		$("#slider").html(bullets);
	}
    
	// set the starting postion of the scroll and the window
	currentUrlAnchor = self.document.location.hash.substring(1);
	if (currentUrlAnchor.substring(0,1)=='/') currentUrlAnchor = currentUrlAnchor.substring(1);
    startPosition = $('#para_id_'+currentUrlAnchor).prevAll().length;
    debugThis('startPosition [ '+startPosition+' ]');
    if ( $("#selectBlog_input").length > 0) {
        updateSliderAndPage( startPosition*100, true, true );
    } else {
        updateSliderAndPage( startPosition*100, false, true );
    }
	// turn the slidercontainer into a qorking slider
	if (itemCount > 1) {
        $("#slider").slider({
    		value: (startPosition*100),
    		min: 0,
    		max: (itemCount-1)*100,
    		step: 100,
    		slide: function(event, ui) {
    		    // this funciton is executed when the slider gets moved
    		    if ($('#selectBlog').length > 0) {
    		        var updateSelectbox = true;
    		    } else {
    		        var updateSelectbox = false;
    		    }
    		    updateSliderAndPage( ui.value, updateSelectbox );
    		    changeUrl( ui.value );
    		}
    	});
	} else {
	    $('#sliderWrapper').addClass('emptySlider');
	}
	$('#fadeOverlay').fadeOut(1200);
	
	// paginatype bepalen
	var pageType = determinePageType();
	// init auto animation. overal init, behalve bij blogpagina's, want die mogen niet animeren.
	if(pageType == 'ViewerCoverflowKlein'){
		
		var url = self.document.location.href;
		if(url.search(/#/)== -1){
			setTimeout("autoAnimateNext(8000)", 8000);
		}
	}else if (pageType != 'ViewerBlog') {
		setTimeout("autoAnimateNext(4000)", 4000);
	} else {
		// niks
	}
});
function movePageDown(id, button) {
	var currentTop = $('#blog_'+id).find('.actualContent').css('top');
	var currentTop = parseInt(currentTop.substring(0,currentTop.length-2));
	var newPos = currentTop - 251;
	var alineaCount = $('#blog_'+id).find('.totalAlineas').val();
	debugThis('movePageDown [ newPos: '+newPos+', alineaCount: '+alineaCount+', '+(-newPos / 251) +' ]');
	if ((-newPos / 251) < alineaCount) {
		$('#blog_'+id).find('.actualContent').animate({ 
	        top: newPos
	    }, 0 , 'linear');
	}
	var currentAlinea = (-newPos / 251);
	if (currentAlinea < alineaCount-1) {
		$('#down_'+id).show();
		$('#up_'+id).css('right','140px');
	} else {
		$('#down_'+id).hide();
		$('#up_'+id).css('right','0px');
	}
	if (currentAlinea > 0) {
		$('#up_'+id).show();
	} else {
		$('#up_'+id).hide();
	}
}
function movePageUp(id, button) {
	var currentTop = $('#blog_'+id).find('.actualContent').css('top');
	var currentTop = parseInt(currentTop.substring(0,currentTop.length-2));
	var newPos = currentTop + 251;
	var alineaCount = $('#blog_'+id).find('.totalAlineas').val();
	debugThis('movePageUp [ newPos: '+newPos+', alineaCount: '+alineaCount+' ]');
	if (newPos < 1) {
		$('#blog_'+id).find('.actualContent').animate({ 
	        top: newPos
	    }, 0 , 'linear');
	}
	var currentAlinea = (-newPos / 251);
	if (currentAlinea < alineaCount-1) {
		$('#down_'+id).show();
		$('#up_'+id).css('right','140px');
	} else {
		$('#down_'+id).hide();
		$('#up_'+id).css('right','0px');
	}
	if (currentAlinea > 0) {
		$('#up_'+id).show();
	} else {
		$('#up_'+id).hide();
	}
}
function determinePageType() {
	var pageType = $('#pageType').val();
	debugThis('initPage [ pageType: '+pageType+' ]');
	return pageType;
}
function scrollToAnchor( id ) {
    var anchorPos = $('#para_id_'+id).prevAll().length;
    if ( $("#selectBlog_input").length > 0 ) {
        updateSliderAndPage( anchorPos*100, true );
    } else {
        updateSliderAndPage( anchorPos*100, false );
    }
    $("#slider").slider('option', 'value', anchorPos*100 );
    changeUrl( anchorPos*100 );
}
function changeUrl( pos ) {
    var url = self.document.location.href;
    anchorPos = url.indexOf('#');
    noAnchor = url.substring(0,anchorPos);
    $('#alineaIdSpiekbriefje')[0].selectedIndex = (pos/100);
    var selId = $('#alineaIdSpiekbriefje').val();
    document.location.href = noAnchor + '#' + selId;
}
function updateSliderAndPage( sliderValue, updateSelectbox, pageInit, autoAni ) {
    // the position is devided by 100, because the values have to be high to assure smooth slider transitions when not using snapping
    var pos = sliderValue / 100;
    // animate the page content to it's new position
    var carrouselParaCount = $(".smallParagraph").length;
    if (carrouselParaCount > 0) {
    	scrollPage( pos * 270, 'carrousel', pos, pageInit, autoAni );
    } else {
    	scrollPage( pos * 1030, 'other', pos, pageInit, autoAni );
    }
    // hget the maximum slider value
    var max = $('#slider').slider('option', 'max') / 100;
    // change all backgournd-bullets into non-transparent ones before slider position
    for (var x=0; x<=pos; x++) {
        $('#bullet_'+x).attr('src','/de/site_files/img/scrollbar_bullet.png');
    }
    // change all backgournd-bullets into semi-transparent ones after slider position
    for (var x=pos; x<=max; x++) {
        $('#bullet_'+x).attr('src','/de/site_files/img/scrollbar_bullet_transp.png');
    }
    if (updateSelectbox) {
	    // change the selected value in the hidden selectbox
	    $('#selectBlog')[0].selectedIndex = pos;
	    // change the value in the fancy selectbox
	    $('#selectBlog_input').val( $('#selectBlog')[0][pos].innerHTML.substring(0,42).replace('&amp;','&') );
	    // unselect all items in fancy selectbox
	    $('.selectbox-wrapper li').removeClass();
	    // get the selected item from the hidden selectbox
	    var currentSelectedValue = $('#selectBlog').val();
	    // set the right selectbox item from the fancy selectbox active
	    $('#selectBlog_input_'+currentSelectedValue).addClass('selected');        
    }
	 
	   //items met "dubbele content" resetten
		var arrayList = $(".deel1");
			$.each(arrayList, function() {
				$(this).show();
			}
		); 
			
		var arrayList = $(".deel2");
			$.each(arrayList, function() {
				$(this).hide();
			}
		); 
}
function getNextPosition() {
	var maxSliderPos = $('#slider').slider('option', 'max');
	var currentPos = $("#slider").slider('option', 'value');
	var nextPos = currentPos + 100;
	if (nextPos > maxSliderPos) {
		nextPos = false;
	}
	return nextPos;
}
function autoAnimateNext(aniSpeed) {
	
	if (typeof aniSpeed == "undefined") {
		var aniSpeed = 4000;
	}

	var activity = $('#inactivityTimer').val();
	
	if (activity != 'active') {
		var currentScrollPos = parseInt($("#slider").slider('option', 'value'));
		var nextScrollPos = getNextPosition();
		if (!isNaN(currentScrollPos)) {
			debugThis('autoAnimateNext [ currentScrollPos, '+currentScrollPos+', nextScrollPos: '+nextScrollPos+' ]');
			updateSliderAndPage( nextScrollPos, false, false, true );
			$("#slider").slider('option', 'value', nextScrollPos );
			changeUrl( nextScrollPos );
			var currentUrlAnchor = parseInt(self.document.location.hash.substring(1));
			checkPosition( currentUrlAnchor );
			if (currentUrlAnchor > 0) {
				$('.paraItem').removeClass('active');
				$('#paraNav_'+currentUrlAnchor).toggleClass('active');
			}
			if (nextScrollPos != false) {
				// Als de volgende positie niet false (einde bereikt) is, dan moet ie gewoon naar de volgende animeren. 
				// Is ie wel false dan is ie naar de eerste gegaan, maar dan gaat ie niet meer verder.
				setTimeout("autoAnimateNext("+aniSpeed+")", aniSpeed);
			}
		}
	}
}
function scrollPage(amount, type, position, pageInit, autoAni) {
	if (pageInit == null) {
		var pageInit = false; 
	}
	if (pageInit != true && autoAni != true) {
		$('#inactivityTimer').val('active');
	}
    // convert the position to a negative value and a css string
    var leftPos = '-'+amount+'px';
    // stop the current animation (if one is running, else all animations get queued)
    $('.extraWideContainer').stop();
    // animate the contentfield to the new position
    if (pageInit == true) {
    	if (type == 'carrousel') {
    		$('.smallParagraph').each( function(i,e) {
				if (i == position) {
					$(this).prevAll().addClass('smaller');
					$(this).nextAll().addClass('smaller');
					$(this).removeClass('smaller');
				}
			});
    	}
    	$('.extraWideContainer').animate({ 
            left: leftPos
        }, 0 , 'linear').animate({ 
            opacity: 1
        }, 0 , 'linear');
    } else {
	    if (type == 'carrousel') {
	    	$('.extraWideContainer').animate({ 
	            left: leftPos
	        }, 300 , 'linear');
			$('.smallParagraph').each( function(i,e) {
				if (i == position) {
					$(this).prevAll().addClass('smaller');
					$(this).nextAll().addClass('smaller');
					$(this).removeClass('smaller');
				}
			});
		} else {
			$('#fadeOverlay').fadeIn(400, function(){
				$('.extraWideContainer').animate({ 
			        left: leftPos
			    }, 0 , 'linear', function() {
			    	$('#fadeOverlay').fadeOut(400);
			    });
			});
		}
    }
}

function openOtherCountries(appurl) {
    window.open(appurl+'other_countries.php','other_countries','width=1000,height=700,scrollbars=yes,toolbar=no,location=no'); return false;
}

function openSitemap(appurl) {
    window.open(appurl+'sitemap.php','sitemap','width=1000,height=700,scrollbars=yes,toolbar=no,location=no'); return false;
}

function openDisclaimer(appurl) {
	window.open(appurl+'impressum.php','disclaimer','width=1000,height=700,scrollbars=yes,toolbar=no,location=no'); return false;
}


function swapDivs(hide1, show1, hide2, show2){
	document.getElementById(hide1).style.display= 'none';
	document.getElementById(show1).style.display= 'block';	
	document.getElementById(hide2).style.display= 'none';
	document.getElementById(show2).style.display= 'block';		
}

var googleCount = function(){
	var image = new Image(1,1);
	image.src = "http://www.googleadservices.com/pagead/conversion/1060455589/?label=signup&cript=00";
	return;
}

function debugThis( string ) {
	if (string == 'reset') {
		$('#debug').val('');
	} else {
		var currentVal = $('#debug').val();
		$('#debug').val( "> " + string + "\n" + currentVal );	
	}
}
function inspect(obj, maxLevels, level)
{
 var str = '', type, msg;

   // Start Input Validations
   // Don't touch, we start iterating at level zero
   if(level == null)  level = 0;

   // At least you want to show the first level
   if(maxLevels == null) maxLevels = 1;
   if(maxLevels < 1)    
       return '<font color="red">Error: Levels number must be > 0</font>';

   // We start with a non null object
   if(obj == null)
   return '<font color="red">Error: Object <b>NULL</b></font>';
   // End Input Validations

   // Each Iteration must be indented
   str += '<ul>';

   // Start iterations for all objects in obj
   for(property in obj)
   {
     try
     {
         // Show "property" and "type property"
         type =  typeof(obj[property]);
         str += '<li>(' + type + ') ' + property +
                ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

         // We keep iterating if this property is an Object, non null
         // and we are inside the required number of levels
         if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
         str += inspect(obj[property], maxLevels, level+1);
     }
     catch(err)
     {
       // Is there some properties in obj we can't access? Print it red.
       if(typeof(err) == 'string') msg = err;
       else if(err.message)        msg = err.message;
       else if(err.description)    msg = err.description;
       else                        msg = 'Unknown';

       str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
     }
   }

     // Close indent
     str += '</ul>';

   return str;
}