(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}

				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));

var imgsetuprun = false;
var fncSetupImages = function(loaded) {
        

        var ycount = 0;
        var yheight = 0;
        var ycurrent = 0;
        var youtubes = new Array();

        var fncDoSetup = function() {
            
            ycount = 0;
            var fncSetup = function() {
                var offset = 0;
                $('.youtube_play').hide();

                for(var i = 0; i < ycurrent; i++) {
                    offset -= youtubes[i].height();
                    if(!$.browser.msie) offset -= 10;
                }

                $('.youtube_belt').animate({ marginTop : offset + 'px' }, 500);
                
                if(ycurrent == 0) {
                    $('.youtube_up a').hide();
                }
                else {
                    $('.youtube_up a').show();
                }
                if(ycurrent < youtubes.length - 2) {
                    $('.youtube_down a').show();
                }
                else {
                    $('.youtube_down a').hide();
                }

                var yheight = 0;
                yheight += youtubes[ycurrent].height();
                if(youtubes.length > 1) yheight += youtubes[ycurrent + 1].height() + 10;
                $('.youtube_items').animate({ height : yheight + 'px'}, 500,function() {
                    youtubes[ycurrent].find('.youtube_play').css('width', youtubes[ycurrent].find('.youtube_image').width());
                    youtubes[ycurrent].find('.youtube_play').css('height', youtubes[ycurrent].find('.youtube_image').height());
                    if(youtubes.length > 1) youtubes[ycurrent + 1].find('.youtube_play').css('width', youtubes[ycurrent + 1].find('.youtube_image').width());
                    if(youtubes.length > 1) youtubes[ycurrent + 1].find('.youtube_play').css('height', youtubes[ycurrent + 1].find('.youtube_image').height());
                    youtubes[ycurrent].find('.youtube_play').fadeIn(500);
                    if(youtubes.length > 1) youtubes[ycurrent + 1].find('.youtube_play').fadeIn(500);
                });
                
                
            }

            $('.youtube_container').each(function() {
                ycount++;

                youtubes[youtubes.length] = $(this);
                if(ycount < 3) {
                    yheight += $(this).height();
                }

            });

            $('.youtube_items').css('height', yheight + 'px');

            $('.youtube_up a').click(function(e) {
                e.preventDefault();
                ycurrent--;
                fncSetup();
            })
            $('.youtube_down a').click(function(e) {
                e.preventDefault();
                ycurrent++;
                fncSetup();
            })

            fncSetup();
        }

        $('.youtube_container').each(function() {
                ycount++;
                $(this).find('.youtube_play').css('width', $(this).find('.youtube_image').width());
                $(this).find('.youtube_play').css('height', $(this).find('.youtube_image').height());
                $(this).find('.youtube_play').hide();
        });
        if(loaded == ycount || true) {
            fncDoSetup();
        }
        

}

$(document).load(function() {
    
    
});

$(document).ready(function() {
    
       

	// Lightbox
	$('a[rel*=lightbox]').lightBox();
	
	// Twitter plugin
	$(".tweet").tweet({
		username: "TheRealTVJ",
		avatar_size: 48,
		count: 5,
		loading_text: "Tweets laden...",
		refresh_interval: 30
	});
	
	// Facebook plugin
//	$(".fbwall").fbwall({
//		user: "527387112",
//		key: "3ffc4f507c",
//		avatar_image_url: 'images/fbavatar.png',
//		loading_text: "Facebook posts laden...",
//		limit: 5,
//		limit_to_user: "roy boots"
//	});


        $('.youtube_container a').click(function(e) {
            e.preventDefault();
            $.facebox('<object width="538" height="327"><param name="movie" value="http://www.youtube.com/v/' + $(this).attr('href').substr(1) + '&hl=nl_NL&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + $(this).attr('href').substr(1) + '&hl=nl_NL&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="538" height="327"></embed></object>');
        });

        $(document).bind('close.facebox', function() {
            $('#facebox').find('content').html('');
        });

        
        $('.youtube_up a').hide();
        $('.youtube_down a').hide();

//        var ycount = 0;
//        $('.youtube_container img').bind('load', function() {
//            fncSetupImages(++ycount);
//        }).bind('error', function() {
//             fncSetupImages(++ycount);
//        });
//        setTimeout('fncSetupImages(5)', 0);
fncSetupImages(5);
});



