var SLIDESHOW = {
    seconds: 5,
    blend: "slow",

    current: 0,
    images: null,
    playing: true,
    locked: false,
    site_root: "",
    load_image: function(step){
        var self = SLIDESHOW;
        if(self.locked) return;
        self.locked = true;
        self.current += step;
        if(self.current == -1) self.current = self.images.length - 1;
        else if(self.current == self.images.length) self.current = 0;
        if (self.images[self.current] == "") self.current = 0;
        oldimg = $("#home_header_img");
        newimg = oldimg.clone(true).css("background-image","url("+self.images[self.current]+")").hide().insertAfter(oldimg);
        oldimg.fadeOut(self.blend, function(){ oldimg.remove(); });
        newimg.fadeIn(self.blend, function(){ self.locked = false; });
    },
    play: function(state){
        var self = SLIDESHOW;
        self.playing = state;
        $("#slideshow-controls .stop img").attr("src",self.site_root + "/media/images/pause" + (self.playing ? "" : "-stopped") + ".png");
    },
    loop: function(){
        var self = SLIDESHOW;
        if(self.playing) self.load_image(1);
        self.timer = setTimeout("SLIDESHOW.loop()", self.seconds*1000);
    },
    init: function(images){
        var self = SLIDESHOW;
        self.images = images;
        $(function(){
            $("#slideshow-controls p").hover(function(){ $(this).css("cursor","pointer"); });
            $("#slideshow-controls .stop, #home_header_img").click(function(){ self.play(!self.playing); });
            $("#slideshow-controls .prev, #slideshow-controls .next").click(function(){ self.play(false); });
            $("#slideshow-controls .prev").click(function(){ self.load_image(-1); });
            $("#slideshow-controls .next").click(function(){ self.load_image(1); });
            self.timer = setTimeout("SLIDESHOW.loop()", self.seconds*1000);
        });
    }
};

