$(function() {
	// Set the transparent pixel
	$(document).pngFix();
});


$().ready(function(){
	
	// back to home: hide on home
	$('.homelink').hide();
	
	// homecontent payoff (als die er is) fade-in 
	$.timer(2000, function (timer) {
		$('#homecontent').fadeIn('slow');
		timer.stop();
	});
	
	// load pages en alter navigation
	$('#navigation a, #homebanner a').each(function(){ // not homepage
		
		// create anchor name / id
		var clas2id	= 	$(this)
						.attr('class')
						.replace('not-selected','')
						.replace('kwc-','')
						.replace(' ','');
		
		// load contents
		if($(this).parent().attr('id') != 'homebanner'){ // voor bannerlink geen pagina loaden
			var placeholder = $('<div/>').appendTo('#content'); // nodig voor volgorde
			$.queue.add(loadcontent($(this).attr('href'),clas2id,placeholder));
		}
		
		// maak anchor/scroller van a
		$(this)
		.attr('href',"#" + clas2id)
		.localScroll(
			{
				target:'#scroller',
				axis:'x',
				lazy:true,
				queue:false,
				duration:1000,
				onBefore:function( e, anchor, $target ){//'this' is the clicked link
										
					// hide absolute-pos homecontent
					$('#homecontent').fadeOut('slow',function(){
						$(this).remove();
					});
					
					// show absolute-pos logo
					$('#staticlogo').fadeIn('slow');
					
					// navigation styling
					$('#navigation a').removeClass('selected').filter(function (index) {
						return false || $(this).attr("href") == "#" + clas2id;
					}).addClass('selected');
					
					
					// homelink activeren
					$('.homelink').fadeIn('slow')
					.attr('href','#homepage')
					.localScroll(
						{
							target:'#scroller',
							axis:'x',
							lazy:true,
							queue:false,
							duration:1000,
							onBefore:function( e, anchor, $target ){//'this' is the clicked link
								
								// hide absolute-pos logo
								$('#staticlogo').fadeOut('slow');
								
								// navigation styling
								$('#navigation a').each(function(){
									$(this).removeClass('selected');
								});
								
								$('.homelink').fadeOut('slow');
								
							}
						}
						
					);
					
					
				}
			}
		);
	});
	
});

function loadcontent(href,classid,placeholder)  { 
		
	// laad html
	$.ajax({
		url: href,
		async: true,
		cache: true,
		dataType: 'html',
		success: function(html){
			
			$(placeholder).replaceWith($('.pagecontainer',html).attr('id',classid));
			initlightbox();
			
			// formulier ajax
			$('form').ajaxForm({
				target: $('form').parent(),
				success: checkAction  // post-submit callback 
			});
			
		}
	});
		
}


// post-submit callback 
function checkAction(responseText, statusText)  { 

    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	
 	if(!responseText){
 	
 		//alert('eh?');
		
		return false;
		
    } else {
    	
    	$('form').ajaxForm({
			target: $('form').parent(),
			success: checkAction  // post-submit callback 
		});
		
		return false;
    }
} 	


function initlightbox()
{
	//lightbox
	$('a.thickbox').lightbox({
	
		fitToScreen: true,
		displayHelp: false,
		fileLoadingImage: '../gui/lightbox/loadingAnimation_inv.gif',
		fileBottomNavCloseImage : '../gui/lightbox/close_small.gif',
		slideNavBar: false,
		navbarOnTop: true,
		strings : {
			help: ' \u2190 / P - vorig beeld\u00a0\u00a0\u00a0\u00a0\u2192 / N - volgend beeld\u00a0\u00a0\u00a0\u00a0ESC / X - sluit',
			prevLinkTitle: 'vorige',
			nextLinkTitle: 'volgende',
			prevLinkText:  '&laquo; Vorige',
			nextLinkText:  'Volgende &raquo;',
			closeTitle: 'sluit',
			image: 'Afbeelding ',
			of: ' van '
		}
		
	});
}

// qeue
$.queue = {
    _timer: null,
    _queue: [],
    add: function(fn, context, time) {
        var setTimer = function(time) {
            $.queue._timer = setTimeout(function() {
                time = $.queue.add();
                if ($.queue._queue.length) {
                    setTimer(time);
                }
            }, time || 12);
        }
 
        if (fn) {
            $.queue._queue.push([fn, context, time]);
            if ($.queue._queue.length == 1) {
                setTimer(time);
            }
            return;
        }
 
        var next = $.queue._queue.shift();
        if (!next) {
            return 0;
        }
        next[0].call(next[1] || window);
        return next[2];
    },
    clear: function() {
        clearTimeout($.queue._timer);
        $.queue._queue = [];
    }
};
