﻿/// <reference path="jquery-1.4.1.min-vsdoc.js" />

(function($) {

    $.fn.izeBgSlideShow = function(settings) {
        var config = {
            //url: '/backgroundslideshow/getrandomimage', //url to call for next image
        	url: 'http://picasaweb.google.com/data/feed/base/user/the.izeman/albumid/5539051918942728193?alt=json-in-script&kind=photo&authkey=Gv1sRgCIKkl4ThkP3T6AE&&hl=en_US&callback=?',
            interval: 15000 //interval for next change
        };
        var timer = 0;
        var stopped = false;
        var feed = null;
        var index = -1;

        if (settings) $.extend(config, settings);

        init = function(el) {
            
            //create back and front layer
            $(el).css("background", "none");            
            $(el).prepend($("<img />").attr("id", "front").css({ 'position' : 'absolute',  'width': '100%', 'height': '100%', 'top': '0px', 'margin': '0', 'padding': '0', 'line-height': '0px', 'opacity': '1' }));
			$(el).prepend($("<img />").attr("id", "back").css({ 'position' : 'absolute',  'width': '100%', 'height': '100%', 'top': '0px', 'margin': '0', 'padding': '0', 'line-height': '0px', 'opacity': '1' }));
			
			$.getJSON( config.url , function(data) {
				feed = data.feed;
				fisherYates( data.feed.entry ); //randomize the picasa array
				getNextPicasaImage(el);
			});
        }
        
        getNextPicasaImage = function(el)
        {        		
        		if (stopped) return;
        		index++;
        		if (index >= feed.entry.length) index = 0;
        		
        		if (feed.entry.length > 0)
        		{
        			cycleImage(el, feed.entry[index].content.src, "0.7");
        		}
        }
        
        cycleImage = function(el, newUrl, opacity) {
            $("<img />")
            .attr('src', newUrl)
            .load(function() {
                //move front image to back
            	$("#back").css( "opacity", opacity);
            	$("#front").css( "opacity", opacity);
                
            	$("#back").attr("src", $("#front").attr("src"));
                $("#back").show();
                $("#front").hide();
                
                $("#front").attr("src", newUrl);
                
                    $("#front").fadeIn("slow", function() {
                        //call next
                        if (!stopped && config.interval > 0)
                            timer = setTimeout(function() { getNextPicasaImage(el); }, config.interval);
                    });
                    $("#back").fadeOut("slow", function() {});
                


            });	
        }
        
        fisherYates = function(myArray) {
            var i = myArray.length;
            if (i == 0) return false;
            while (--i) {
                var j = Math.floor(Math.random() * (i + 1));
                var tempi = myArray[i];
                var tempj = myArray[j];
                myArray[i] = tempj;
                myArray[j] = tempi;
            }
        }
        
        this.showImage = function(url, opacity) {
        	cycleImage($(this), url, opacity);
        }
        
        this.stop = function() {
        	stopped = true;
        	clearTimeout(timer);
        }
        
        this.start = function() {
        	stopped = false;
            if (config.interval > 0)
                timer = setTimeout(function() { getNextImage(el); }, config.interval);
        }

        this.each(function() {
            //setup element

            //set interval for next image
            init(this);

        });

        return this;

    };

})(jQuery);


