/*  ###########################################################################
    company:    42
    client:     Marjolein Delhaas
    client:     Marcel van Doorn
    date:       August 2007
    author:     C. Egor Kloos
    ########################################################################### */
    

    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
        // RUN GOOGLE ANALYTICS
        var pageTracker = _gat._getTracker("UA-8523884-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    });
    

    
    $(document).ready(function(){ 
    
        // NEW IMAGE VIEWER

        var $box = $('#thumbs');
        var $thumbs = $('#thumbs > img')
        var $boxWidth = 0;
       
        $thumbs.each(function(i,elm){
            $boxWidth = $boxWidth + $(elm).width() + 8;
            $(elm).wrap('<a href="'+window.location.href+'/'+(i+1)+'/"/>');
        });
        
        $box.css('width',$boxWidth);

        $('#slides > .image-list > img').hide();
        $('#slides > .image-list > img').each(function(i,elm){
            var segments = location.pathname.split('/');
            var counter = segments[(segments.length)-2];
            segments.pop();
            segments.pop();
            var link = segments.join('/');
            $(elm).addClass('slide');
            $("#pagination").append('<li><a href="'+link+'/'+(i+1)+'/">'+(i+1)+'</a></li>');
            if(counter == (i+1)){
                $(elm).show();
                $("#pagination li:nth-child("+(i+1)+")").addClass('active');
            } else {
                $(elm).remove();
            }
        });
        
        $('#pagination').width(new Number($('#slides > div > img').attr('width')));      
        $('<li class="next">&gt;</li>').insertAfter('#pagination > li:last')
        $('<li class="close">CLOSE X</li>').insertAfter('#pagination > li:last')
        $('#pagination .next').click(function(){
            var n = $('#pagination > li').length-1;
            var i = new Number(location.pathname.split('/')[location.pathname.split('/').length-2]);
            var j = (i==(n-1)) ? (1) : (i+1);
            location.href = location.href.replace('/'+i+'/','/'+j+'/')
        })
        $('#pagination .close').click(function(){
            var n = (location.href.split('/')[(location.href.split('/').length)-2]>9)? 4 : 3;
            var h = location.href.substr(0,location.href.length-n);
            location.href=h;
        })
        
        
        var h = $("#contact #slides img:first").attr("height");
        if (h>190){
            var l = $("#contact #slides a:first").attr("href");
            $("#contact #slides a:first").remove();
            $("#contact #slides img:first").wrap('<a href="'+l+'"></a>');
        }
        
        //$("#homepage img").attr("width","255").attr("height","330");
        $("#homepage img").each(function(){
            var w = $(this).attr('width');
            var h = $(this).attr('height');
            if(h>w) $(this).attr("width","255").attr("height","330");
            if(w>h) $(this).attr("width","436").attr("height","330");
        })
        $("#contact #slides a img").each(function(){
            var sh = ($(this).attr("height"))/2;
            $("#image-crop, #image-crop a").css({height:sh});
        })
        $("#contact #slides a img").hover(function(){
            var sh = ($(this).attr("height"))/2;
            $(this).css({top:(sh-(sh*2))})
        },function(){
            var sh = ($(this).attr("height"))/2;
            $(this).css({top:"0"})
        })

    });
    
    
