$(function() {
    
    $("a").click(function() {
        if (this.hostname && this.hostname !== location.hostname) {
			fixedLink = '/outgoing/' + $(this).attr('href');
			pageTracker._trackPageview(fixedLink);
        }
        return true;
    });

    var pos = 0;
    var img = null;
    
    $('figure').each(function(index) {
        var self  = this;
        var image = $(this).children()[0];
        var pic   = new Image();
        
        var clicktext = "\t[Klik] om volledige grootte te bekijken";
        var dblclicktext = "\t[Dubbelklik] om originele versie te bekijken";
        
        var title = image.title + '\n' + clicktext + ($(image).attr('itemprop') == undefined || $(image).attr('itemprop') == '' ? '' : '\n' + dblclicktext);
        
        $(image).remove();
        
        $(pic)
            .load(function () {                
                $(this).css('display', 'none');
                $(self)
                    .css('background', 'transparent')
                    .append(this);
                $(this).fadeIn('slow');
            })
            .attr({src: image.src, title: title, itemprop: $(image).attr('itemprop')});
        }
        
    );

    $('figure').toggle(function() {
        img = $(this).children()[0];
        pos = window.pageYOffset;
        $(this).animate({width: img.width, height: img.height}, 'slow');
        $('html, body').animate({scrollTop: $(this).offset().top-((window.innerHeight-img.height)/2)}, 'slow');
    }, function() {
        $(this).animate({width: '600px', height: '150px'}, 'slow');
        $('html, body').animate({scrollTop: pos}, 'slow');
    });
    
    $('figure')
        .bind(
            'contextmenu', function() { 
                return false; 
            })
        .bind('dblclick', function() {
                var image = $(this).children()[0];
                if ($(image).attr('itemprop') != undefined) {
                    if (confirm('Ben je zeker dat je deze website wil verlaten, en naar ' + $(image).attr('itemprop') + ' wenst te surfen?')) {
                        window.location = $(image).attr('itemprop');
                    }
                    return false;
                }
            });
    
    if ($("#content").find("figure").size() > 0) {
        var expand = $("<ul><li><a href=\"javascript:null;\" title=\"afbeeldingen openen\">Afbeeldingen openen</a></ul></li>");
        $("aside.article-share").append(expand);

        $(expand).find("a").toggle(function() {
            $("#content").find("figure").each(function() {
                img = $(this).children()[0];
                $(this).animate({width: img.width, height: img.height}, 'slow');
            });
            $(expand).find("a").eq(0).attr({title:"afbeeldingen sluiten"}).text("Afbeeldingen sluiten");
        }, function() {
            $("#content").find("figure").each(function() {
                $(this).animate({width: '600px', height: '150px'}, 'slow');
            });
            $(expand).find("a").eq(0).attr({title:"afbeeldingen openen"}).text("Afbeeldingen openen");
        });

    };

	setTimeout(function() {
		$('.article-share').fadeTo('fast', 0.5);
	}, 1000);
	
	$('.article-share').hover(function(){
		$('.article-share').fadeTo('fast', 1);
	}, function(){
		setTimeout(function() {
			$('.article-share').fadeTo('fast', 0.5);
		}, 1000);
	});
	
	if ($("img.thumbnail").length > 0) { 
	    $("img.thumbnail").contentSlider(); 
	    $("img.thumbnail:first").css('border-color', '#ff3366'); 
	}
	
	$('a.lightbox').lightBox({fixedNavigation:true});

});