
Global = {
    
    activePhotoId: 1, // var for photoGallery function
    
    init: function() {
        if ($('.product-thumbs').length > 0) {
            this.photoGallery();
        } 
        
        if ($('.faq-questions-list').length > 0) {
            this.faqScroll();
        }
        
        if ($('.tooltip-icon').length > 0) {
            this.tooltipClick();
        }
        
        if ($('.change-amount').length > 0) {
            this.cartChangeAmount();
        }
        
        if ($('.small-shopping-cart').length > 0) {
            this.smallCart();
        }
    },
    
    
    /******
     *
     * functions for product page photogallery  
     *
     ******/
    
    photoGallery : function() {
                
        $('.product-thumbs .img-wrapper').click(function (){
            // get the id of the photo clicked
            getId = $(this).attr('id');

            Global.activePhotoId = getId.replace('image-', '');

            // get the large image src
            var image = $(this).find('input[name=large_image]').val();
            $('.img-wrapper-large img').attr('src', image);
            
        });
        
        $('.img-wrapper-large').click(function (){
            var image = $('#image-'+Global.activePhotoId).find('input[name=xlarge_image]').val();

            var elmImage = $('<img id="xl-image" />');
            var elmOverlay = $('<span class="img-overlay">&nbsp;</span>');
            $('#picture').html('').append(elmOverlay);
            $('#picture').append(elmImage);
            $(elmImage)
                .attr('src', image)
                .attr('alt', '')
            ;
            $('#picture').addClass('img-wrapper img-wrapper-xlarge');
            
            $('#picture').modal({
                closeHTML: '<div class="modal-close">Sluiten</div>',
                opacity: 0,
                onClose: function (dialog) {
                    $.modal.close();

                    $('#picture').html('');
                }
            });
                        

        });
    },
    
    
    /******
     *
     * functions for FAQ scroll to answers 
     *
     ******/
    
    faqScroll : function() {
        $('.faq-questions-list a').click(function(){
            var scrollToElement = $(this).attr('href');
            $.scrollTo($(scrollToElement), 400 );
            return false;
        });
        
        $('.top-link').click(function(){
            var scrollToElement = $(this).attr('href');
            $.scrollTo({top:'0px',left:'0px'}, 400 );
            return false;
        });
        
    },
    
    
    /******
     *
     * functions for small slide down cart 
     *
     ******/
    
    /* Setup */
    buildTooltip : function(element) {
        var tooltipText = $(element).attr('alt');
        var pos = $(element).position(); // get position of image to position the tooltip

        var width = 110;
        var bubblepointPosition = width - 25;
        
        var parent = $(element).parent();
        
        $(parent).append('<div class="tooltip"></div>');
        
        $('.tooltip').html('<div class="top"></div><div class="tooltip-content">'+ tooltipText +'</div>');

        var tooltipHeight = $('.tooltip').height(); // get height to position the tooltip
        var tooltipTop = pos.top - tooltipHeight;
        var tooltipLeft = pos.left - bubblepointPosition;

        $('.tooltip').css('position', 'absolute')
                     .css('top', tooltipTop)
                     .css('left', tooltipLeft);
    },
    

    
    /* Click events */
    tooltipClick : function() {
        $('.tooltip-icon').each(function(){
            $(this).bind("mouseenter",function(){
                Global.buildTooltip($(this));
            }).bind("mouseleave",function(){
                Global.removeTooltip($(this));
            }).wrap('<div class="tooltip-wrapper">');
        })
    },
    
    /* close tooltip */
    removeTooltip : function(element) {
        $('.tooltip').remove();
    },
    
    
    /******
     *
     * change amount on shopping cart page  
     *
     ******/
    
    cartChangeAmount : function() {
        $('.confirm-amount').hide();
        $('.change-amount .input-button').click(function() {
            $(this).parent().hide();
            $(this).parent().parent().find('.confirm-amount').show();
            
        });
        
    },
    
    
    /******
     *
     * functions for small slide down cart   
     *
     ******/
    
    /* Setup */
    smallCart : function() {
        var pos = $('#small-cart-link').position(); // get position of the cart link

        var smallCartTop = pos.top + 5;
        var smallCartLeft = pos.left - 81;
        
        if(/Safari/.test(navigator.userAgent)) {
            smallCartTop = pos.top + 25;
        }
        
        $('.small-shopping-cart').css('position', 'absolute')
                                 .css('top', smallCartTop)
                                 .css('left', smallCartLeft)
                                 .css('z-index', 100);
                                 
        $('#small-cart-link').bind("mouseenter", Global.smallCartOpen);
    },
    
    /* Open cart */
    smallCartOpen : function() {
        $('.small-shopping-cart').slideDown('normal', function() {

            
            
        });
        $('#small-cart-link').unbind("mouseenter");
        if ($.browser.msie && $.browser.version == '6.0') {
                elmDiv = $('#ie6-close');
                elmDiv.bind('click', Global.smallCartClose);
            } else {
                $('.small-shopping-cart').bind("mouseleave", Global.smallCartClose);
            }
    },
    
    /* Close cart */
    smallCartClose : function() {
        $('.small-shopping-cart').slideUp('slow', function() {
            $('.small-shopping-cart').unbind("mouseleave");
            $('#small-cart-link').bind("mouseenter", Global.smallCartOpen);
        });
    }
    
}

// Run any/all init functions.
$(document).ready(function() {
    Global.init();
});

$(window).load(function() { 
});