$(document).ready(function(){
    $('#content-main a.img-menu').each(function(){
        $(this).html(make_image_menu($(this)));
    });

    if ($('body:first').hasClass('home')){
        make_home_menu();
    }
    if ($('#showcase')){
        setup_showcase();

    }
    if ($('#product-images')){
        product_showcase();
    }

    
    $('#block-menu-primary-links ul.menu').append('<li><a href="#" id="lang-switch">Languages</a><ul class="langs">'+$('#block-menu-menu-land-taalkeuze ul.menu').html()+'</ul></li>');
    $('#lang-switch').click(function(){
        $(this).parent().children('ul').toggleClass('vis');
        if ($(this).parent().children('ul').hasClass('vis')){
           $(this).parent().children('ul').css('display', 'block');
           
        } else {
            $(this).parent().children('ul').css('display', 'none');
            
        }
        return false;
    });
});



function make_image_menu(source){
    if (source.attr('alt')){
        var html = '<img src="'  +base_url + '/sites/default/files/imagecache/Nav_img_medium/' +source.attr('alt')+'" />';
        html += '<span>' +source.html() + '</span>';
        return html;
    } else {
        return source.html();
    }
}



function make_home_menu(){
    var nav = '<a href="#" class="arrowleft">&nbsp;</a><div class="hscroller"><div id="home-scroller" style="width: 3000px">';
    $('#block-menu-menu-catalog-browser a').each(function(){
        item = '<a  id="img'+$(this).attr('id')+' "href="'+$(this).attr('href')+'"><span class="img-home">';
        if ($(this).attr('alt')){
            item += '<img src="'  +base_url + '/sites/default/files/' +$(this).attr('alt')+'" />';
        }
            
        
        item += '</span><span class="label-home">'+$(this).html()+'</span></a>e';
        nav += item;
    });

    nav += '</div></div><a href="#" class="arrowright">&nbsp;</a>';
    $('#footer').before('<div id="home-bottom-nav"><div class="container">'+nav+'</div></div>');
    $('a.arrowleft').mousedown(function(){
        var pos = parseFloat($('#home-scroller').css('marginLeft'));
        
        newpos = pos + 249;
        if (newpos>0){
            newpos = 0;
        }
        $('#home-scroller').animate({marginLeft: newpos + 'px'},200 );
        return false;
    });
    $('a.arrowleft').click(function(){
        return false;
    });

    $('a.arrowright').mousedown(function(){
        var pos = parseFloat($('#home-scroller').css('marginLeft'));
        var max = -996;
        newpos = pos - 249;
        if(newpos<max){
            newpos = max;
        }
        $('#home-scroller').animate({marginLeft: newpos + 'px'},200 );
        return false;
    });
    $('a.arrowright').click(function(){
        return false;
    });
}

var sc_user_interact = false;
function setup_showcase(){
    
    $('#showcase').css('height', '388px');
    /*$('#showcase').css('overflow', 'hidden'); */
   
    
    $('#showcase img:first').attr('id', 'showcase-current');
   
    $('#showcase').css('backgroundImage', 'url(' + $('#showcase-current').attr('src') + ')');
    $('#showcase').css('backgroundRepeat', 'no-repeat');
    $('#showcase').css('backgroundPosition', 'center');
    $('#showcase').prepend('<div id="showcase-over" style="height: 388px;  position: absolute; width: '+$('#showcase').width()+'px"></div>');
    $('#showcase').append('<a href="#" id="show-prev">Prev</a>');
    $('#showcase').append('<a href="#" id="show-next">Next</a>');
    setTimeout ( "_autoplay()", 4000 );
    $('#show-next').click(function(){
        sc_user_interact = true;
        _next_image();
        return false;
    });

    $('#show-prev').click(function(){
        sc_user_interact = true;
        var index = 0;
        var nxt = false;
        $('#showcase img').each(function(i){
           
            if ($(this).attr('id') == 'showcase-current'){
                index = i - 1;
            }
        });

        
        if (index == -1){
            index = $('#showcase img').length-1;
        } else if (index > $('#showcase img').length - 1){
            index = 0;
        }

        var obj =  $($('#showcase img')[index]);
      
        obj.attr('id', 'showcase-next');
        switch_showcase();
        return false;
    });

}

function _autoplay(){
    if (! sc_user_interact){
        _next_image();
        setTimeout ( "_autoplay()", 4000 );
    }
}

function _next_image(){
    
    var index = 0;
    var nxt = false;
    $('#showcase img').each(function(i){
        if(nxt){
            $(this).attr('id','showcase-next');
            index = i;
        }
        if ($(this).attr('id') == 'showcase-current'){
            nxt =true;
        } else{
            nxt = false;
        }
    });

    if (index == 0){
        $('#showcase img:first').attr('id', 'showcase-next');
    }
    switch_showcase();
}




function switch_showcase(){

  
    $('#showcase-over').css('backgroundImage', 'url(' + $('#showcase-next').attr('src') + ')');
    $('#showcase-over').css('backgroundRepeat', 'no-repeat');
    $('#showcase-over').css('backgroundPosition', 'center');
    $('#showcase-over').css('backgroundPosition', 'center');
    $('#showcase-over').css('display', 'none');

  
    
    $('#showcase-over').fadeIn('400' , function(){
        
        $('#showcase-current').attr('id', '');
        $('#showcase-next').attr('id', 'showcase-current');
        $('#showcase').css('backgroundImage', 'url(' + $('#showcase-current').attr('src') + ')');
        $('#showcase').css('backgroundRepeat', 'no-repeat');
        $('#showcase').css('backgroundPosition', 'center');

    });

}



function product_showcase(){
  
    $('div.sc-image').each(function(){
        
        $(this).css('display', 'none');
    });
    $('div.sc-image:first').addClass('current');
    $('div.sc-image:first').css('display', 'block');

   


   
    if ($('a.thumb').length > 3){
        $('div.browser:first').html('<div id="product-show-scroller"><div id="product-show-scroller-content">'+$('div.browser:first').html()+'</div></div>');
        $('div.browser:first').prepend('<a href="#" id="product-show-prev" class="arrowleft">Prev</a>');
        $('div.browser:first').prepend('<a href="#" id="product-show-next" class="arrowright">Next</a>');

        $('#product-show-prev').click(function(){
            var pos = parseFloat($('#product-show-scroller-content').css('marginLeft'));

            newpos = pos + 146;
            if (newpos>0){
                newpos = 0;
            }
            $('#product-show-scroller-content').animate({marginLeft: newpos + 'px'},200 );
            return false;
        });
     $('#product-show-next').click(function(){
            var pos = parseFloat($('#product-show-scroller-content').css('marginLeft'));
            var max = -146 * $('a.thumb').length + 438;
            newpos = pos - 150;
            if(newpos<max){
                newpos = max;
            }
            $('#product-show-scroller-content').animate({marginLeft: newpos + 'px'},200 );
            return false;
        });

    }




     $('a.thumb').click(function(){
        var target = '#' + $(this).attr('href');
        if (! $(target).hasClass('current')){
            $(target).addClass('nextImage');
            $('div.current').fadeOut('200' , function(){
                 $('div.nextImage').fadeIn('200', function(){
                    $('div.current').removeClass('current');
                    $('div.nextImage').addClass('current');
                    $('div.nextImage').removeClass('nextImage');
                 });
            });
        }

        return false;
    });



    
}
