// preloading images
jQuery.preloadImages = function () {
    for (var i = 0; i<arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
};
jQuery.preloadImages("images/shopbycategory-over.png", "images/freeresources-over.png", "images/customerservices-over.png", 
        "images/specialoffer-over.png");

        
function slideSwitch() {
    var $active_image = jQuery('#slideshow a img.active');
    var $active_button = jQuery('#slideshow-buttons img.active').not("#pause-resume-button");

    if ( $active_image.length == 0 ) $active_image = jQuery('#slideshow a img:last');
    if ( $active_button.length == 0 ) $active_button = jQuery('#slideshow-buttons img:last').not("#pause-resume-button");

    var $next_image = $active_image.parent().next().find("img").length ? $active_image.parent().next().find("img") : jQuery('#slideshow img:first');

    var $next_button = $active_button.next(".slideshow-button").length ? $active_button.next(".slideshow-button") : jQuery('#slideshow-buttons img:first').not("#pause-resume-button");

    var $prev_button = $active_button.prev();

    $active_button.animate({opacity: 0.5}, 500);

    $active_image.addClass('last-active');
    $active_button.addClass('last-active');

    $next_image.css({opacity: 0.0, visibility: "visible" })
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active_image.removeClass('active last-active');
        });

    $next_button.animate({opacity: 0.5}, 500)
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active_button.removeClass('active last-active');
        });
}

jQuery(document).ready(function(){  

    jQuery("li.headlink").hover(
			function() { jQuery(this).children("ul").css('display', 'block'); },
			function() { jQuery(this).children("ul").css('display', 'none'); }
    );
  
    // Mouseover event for the "Add To Cart" button
    jQuery("img[src*='add-to-cart']").each(function () {
        jQuery(this).hover(
            function () {
                jQuery(this).attr("src", "images/add-to-cart-button-hover.png");
            }, 
            function () {
                jQuery(this).attr("src", "images/add-to-cart-button.png");
        });
    });

    // Slideshow
        
    jQuery(function() {
        slideshow_interval = setInterval( "slideSwitch()", 5000 );
    });

    var $pause_resume_button_state = 'resumed';

    jQuery("#pause-resume-button").click(function () {
        if ($pause_resume_button_state == 'resumed') {
            clearInterval(slideshow_interval);
            jQuery(this).attr("src", "/mas_assets/images/slideshow/buttons/resume.png");
            $pause_resume_button_state = 'paused';
        }
        else {
            slideSwitch();
            slideshow_interval = setInterval( "slideSwitch()", 5000 );
            jQuery(this).attr("src", "/mas_assets/images/slideshow/buttons/pause.png"); 
            $pause_resume_button_state = 'resumed';
        }
    });
    
    jQuery("#slideshow-buttons img.slideshow-button").each(function () {
            
        jQuery(this).click(function () {
      
            if ($pause_resume_button_state == 'resumed') {
                jQuery('#pause-resume-button').attr("src", "/mas_assets/images/slideshow/buttons/resume.png");     
                $pause_resume_button_state = 'paused';
            }
      
            clearInterval(slideshow_interval);
            
            var index = jQuery("#slideshow-buttons img.slideshow-button").index(this);
              
            jQuery("#slideshow img").eq(index)
                .css({ visibility: "visible" })
                .animate({opacity: 1.0}, 1000)
                .addClass("active");
            
            var spek = jQuery("#slideshow img:not(:eq("+ index +"))").each(function () {
                
                jQuery(this)
                    .animate({opacity: 0.0}, 1000)
                    .removeClass("active");
            });
         
            jQuery("#slideshow-buttons img.slideshow-button:not(:eq("+ index +"))").each(function () {
            
                jQuery(this)
                    .animate({opacity: 0.5}, 1000)
                    .removeClass("active");
            });
            
            jQuery(this).addClass("active");
                                                  
        })
        .css({opacity: 0.5})
        .hover(
            function() {
                jQuery(this).css({opacity: 1.0});
            }, 
            function () {
                if ( !(jQuery(this).hasClass("active")) )
                jQuery(this).css({opacity: 0.5});            
            }
        );
    });

});  
