$(window).load(function() {
  $("#searchbox").inputstyle().focus().addClass("focused");
  $("#search").focus();
  
  function getImageUrl(name){
    return _b_url_res + "/application/images/" + name;
  }

  var _slideShow = new slideShow(["french.jpg","mouse2.jpg","music.jpg","plant.jpg","man.jpg","piggy-bank.jpg","camera.jpg"],[50,50,150,170,70,220,170], [-15,50,-15,-10,-10,10,-10]);
  _slideShow.start();
  
  /******************
   * Slideshow for front screen
   */
  function slideShow(filenames,lefts,tops){
    var pos = Math.round(Math.random() * 6);
    var current = false;
    
    var photoshower  = $("#nzback");
    
    var showing = false;
    
    
    function frontImageLoader(id, completed){
      var lImage = $(new Image());
      var src = getImageUrl(filenames[id]);
      var _this = this;
      _this.removeImage = function(callback){
        lImage.animate({opacity:0},1000, function(){
          lImage.remove();
          callback();
        });
        
      };

      
      lImage.load(function(){
        if (last){
          last.removeImage(function(){
            lImage.css({opacity:0, left:lefts[id],top:tops[id]});
            photoshower.append(lImage);
            lImage.animate({opacity:1}, 1000,completed);
          });
        }
        else
        {
          lImage.css({opacity:0, left:lefts[id],top:tops[id]});
          photoshower.append(lImage);
          lImage.animate({opacity:1}, completed);
        }
      }).attr("src",src);
      
      return _this;
    }
    var timer = false;
    var last = false;
    
    function next(){
      
      current = new frontImageLoader(pos, function(){
        if (showing){
          last = current;
          timer = setTimeout(next,10000);
        }
      });
      pos++; 
      if(pos>=filenames.length){
        pos=0;
      }
    }
    
    this.start = function(){
      if (!showing)
      {
        showing = true;
        next();
      }
    };
    
    this.stop = function(){
      if (showing)
      {
        showing = false;
  
        if (timer){
          clearTimeout(timer);
        }
        
        if (current && current!=last){
          // stop current if animating
          //current.stop(callback);
          current.removeImage();
        }
      }
    };
  }
  
});
