jQuery(document).ready(function($) {
	//FOUTMELDINGEN
	$('#message .close').click(function()
	{
		$(this).parent().fadeOut();
		return false;
	});
		 	
	//POPUP
	$('.login').fancybox({ 'type':'iframe', 'overlayShow': false});
    $('.iframe').fancybox({
        'width'             : '65%',
        'height'            : 455,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe'
    });
    
    $('.fancy_edit').fancybox({
        'width'             : '65%',
        'height'            : 455,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
         'onClosed': function() {
            parent.location.reload(true);
      }
    });
    

	//BACKGROUND
	if (typeof backgrounds !== undefined && backgrounds.length)
	{
		changeBackGround(backgrounds);
	
		$('.bg_toggle').live('click', function()
		{	
			var winWidth = $(window).width();
			var offset = $('#container').offset();
			
			if ((offset.left < winWidth))
			{
				$('#container')
					.css({'position':'absolute', left:offset.left+'px'})
					.animate({left:winWidth +'px'}, 350, function(){
															$('.bg_toggle.hide').fadeIn('normal');
															$('.bg_nav').show(300);
															});
			}
			else
			{
				var newPos = (winWidth-1005)/2;
				$('#container')
					.animate({left:newPos+'px'}, 350, function(){
													$('.bg_toggle.hide').fadeOut('normal');
													$('.bg_nav').hide(300);
													$(this).css({'position':'relative', left:0})
															});
			}
	 	});
	}
});

function changeBackGround(backgrounds) {
	// SETTINGS //////////////////////////////////////////////////////////////////
    
    if(!window.backgroundUrl) {
        window.backgroundUrl = '/media/background/';
    }
    var bkgTimer = 8; // frequency in seconds the background will change
    var contWidth = false;
    var bWidth = 1500;
    var bHeight = 1000;

    
    //////////////////////////////////////////////////////////////////////////////
    
    /*--- Rotating Backgrounds ---*/
    
    var of = backgrounds.length;
    var at = Math.round(Math.random()*(of - 1));
    var changeInterval;
    
    loadBackground(at);
    
    backgrounds[at].img.appendTo("#background");
    //$('#main_content').attr('style',backgrounds[at].style);
       
    
    $('#background img').one('load', function() {
        $('#loader').fadeOut();
        adjustBg(bWidth, bHeight);
        for (var i = 0; i < backgrounds.length; i++) {
            if(backgrounds[i].loaded) continue;
            loadBackground(i);
        }
    });
    
    function loadBackground(index) {
        var data = backgrounds[index];
        return data.img = $('<img>').attr('src', window.backgroundUrl + data.imgUrl).load(function() {
            data.loaded = true;
        });
    }
    
    // update the background with the new number from "at"
    function updateBackground(direction) {
        var data = backgrounds[at],
            img = $('#background img'),
            next = data.img,
            transition;

        img.css('position', 'absolute');
        next.css('position', 'absolute');
        next.hide().appendTo('#background');

        var speed = 500;
        next.show('slide', { direction: direction }, speed, function()
        {
            img.detach();
        });
        img.hide('slide', { direction: direction == 'left' ? 'right' : 'left' }, speed);
    }

    $('div.bg_nav').live('click',function(evt)
    {
        evt.preventDefault(); 
        var direction = $(this).hasClass('prev-link') ? 'left' : 'right';
        var nextPos = direction == 'left' ? at - 1 : at + 1;
        nextPos = ((nextPos + of) % of);
        // Prevent background switching if animation is not complete or next image not loaded
        var img = backgrounds[at].img;
        var data = backgrounds[nextPos];
        
        var fxQueue = img.queue("fx");
        if(fxQueue.length || !data.loaded)
            return;
        
        at = nextPos;
        updateBackground(direction);
        clearInterval(changeInterval);
        changeInterval = setInterval(nextBkg, bkgTimer*1000);
	 });
    
    function nextBkg() {
        $('.bg_nav .next-link').trigger('click');
    }
     
    $(window).resize(function()
    {
    	adjustBg(bWidth, bHeight);
    });
    
    function adjustBg(bWidth, bHeight)
    {
    	var wWidth = $(window).width();
    	var wHeight = $(window).height();
    	var background = $('#background img');
    	bWidth = background.width();
    	bHeight = background.height();
    	var scale;
         
       if (wHeight/wWidth > bHeight/bWidth) //hoogte 100% breedte met -/- marge centreren
        {
         	scale = wHeight/bHeight;
            background.css({'margin-top':0,'margin-left':(wWidth - (bWidth*scale))/2, 'width':'auto','height':wHeight});
        } else {
            scale = wWidth/bWidth;
            background.css({'margin-left':0,'margin-top':(wHeight - (bHeight*scale))/1.5, 'width':wWidth,'height':'auto'});           
        }
    }	
}
