var alreadysetup = new Object();
var currents = new Object();
var slideshow = {width:620,height:320};

function setup_slideshow(slideshow_name) {
	// We are listening to the window.load event, so we can be sure
	// that the images in the slideshow are loaded properly.

	// Testing wether the current browser supports the canvas element:
	supportCanvas = 'getContext' in document.createElement('canvas');

	// The canvas manipulations of the images are CPU intensive,
	// this is why we are using setTimeout to make them asynchronous
	// and improve the responsiveness of the page.

    function timeoutBody(slideshow_name) {
        return function() {

            slides = $('#' + slideshow_name + ' li');

            if (!(slideshow_name in alreadysetup)) {
                currents[slideshow_name] = 0;
                alreadysetup[slideshow_name] = true;

                window.console && window.console.time && console.time(slideshow_name + ' Generated In');
                
                if(supportCanvas){
                    $('#' + slideshow_name + ' img').each(function(){
                        // Rendering the modified versions of the images:
                        createCanvasOverlay(this);
                    });
                }
                
                window.console && window.console.timeEnd && console.timeEnd(slideshow_name + ' Generated In');
            }

            // Make sure that the current one is actually showing
            $(slides).removeClass('slideActive').hide();
            $(slides).eq(currents[slideshow_name]).addClass('slideActive').show();

            function clickButton(slideshow_name, supportCanvas, slides) {
                return function() {
                    li			= $(slides).eq(currents[slideshow_name]),
                    canvas		= $(li).find('canvas'),
                    nextIndex	= 0;

                    // Depending on whether this is the next or previous
                    // arrow, calculate the index of the next slide accordingly.
                    
                    if($(this).hasClass('next')){
                        nextIndex = currents[slideshow_name] + 1;
                    } else {
                        nextIndex = currents[slideshow_name] - 1;
                    }

                    // Only show arrows as appropriate
                    if (nextIndex == slides.length/2-1) {
                        $('#' + slideshow_name + ' .arrow').show();
                        $('#' + slideshow_name + ' .next').hide();
                    } else if (nextIndex == 0) {
                        $('#' + slideshow_name + ' .arrow').hide();
                        $('#' + slideshow_name + ' .next').show();
                    } else {
                        $('#' + slideshow_name + ' .arrow').show();
                    }

                    next = $(slides).eq(nextIndex);
                    
                    if(supportCanvas){

                        // This browser supports canvas, fade it into view:

                        canvas.fadeIn(function(){
                            
                            // Show the next slide below the current one:
                            next.show();
                            $(next.children()[2]).hide();
                            $(li.children()[2]).hide();
                            currents[slideshow_name] = nextIndex;
                            
                            // Fade the current slide out of view:
                            li.fadeOut(function(){
                                li.removeClass('slideActive');
                                canvas.hide();
                                next.addClass('slideActive');

                                // Finally, show the next text
                                $(next.children()[2]).show();
                            });

                        });
                    }
                    else {
                        
                        // This browser does not support canvas.
                        // Use the plain version of the slideshow.
                        
                        currents[slideshow_name]=nextIndex;
                        next.addClass('slideActive').show();
                        li.removeClass('slideActive').hide();
                    }
                };
            }
            
            $('#' + slideshow_name + ' .arrow').click(
                clickButton(slideshow_name, supportCanvas, slides)
            );

            // Finally, show the arrows
            $('#' + slideshow_name + ' .arrows').hide();
            $('#' + slideshow_name + ' .next').show();
        };
    }

	setTimeout(timeoutBody(slideshow_name),500);

	// This function takes an image and renders
	// a version of it similar to the Overlay blending
	// mode in Photoshop.
	
	function createCanvasOverlay(image){

		canvas			= document.createElement('canvas'),
        canvasContext	= canvas.getContext("2d");
		
		// Make it the same size as the image
		canvas.width = slideshow.width;
		canvas.height = slideshow.height;
		
		// Drawing the default version of the image on the canvas:
		canvasContext.drawImage(image,0,0);
		

		// Taking the image data and storing it in the imageData array:
		imageData	= canvasContext.getImageData(0,0,canvas.width,canvas.height),
        data		= imageData.data;
		
		// Loop through all the pixels in the imageData array, and modify
		// the red, green, and blue color values.
		
		for(i = 0,z=data.length;i<z;i++){
			
			// The values for red, green and blue are consecutive elements
			// in the imageData array. We modify the three of them at once:
			
			data[i] = ((data[i] < 128) ? (2*data[i]*data[i] / 255) : (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));
			data[++i] = ((data[i] < 128) ? (2*data[i]*data[i] / 255) : (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));
			data[++i] = ((data[i] < 128) ? (2*data[i]*data[i] / 255) : (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));
			
			// After the RGB elements is the alpha value, but we leave it the same.
			++i;
		}
		
		// Putting the modified imageData back to the canvas.
		canvasContext.putImageData(imageData,0,0);
		
		// Inserting the canvas in the DOM, before the image:
		image.parentNode.insertBefore(canvas,image);
	}
}	

