home_slide = (function() {
  var slides = [];
  var current_slide = 0;
  var timeout = false;
  
  return {
    init : function($obj, data) {
      slides = data;
      
      $obj.append('<ul id="slideshow_pag"></ul>');
      var first = true;
      $(slides).each(function(i) {
        $("#slideshow_pag").append('<li><a href="javascript:void(0);" onClick="home_slide.show('+i+')"><img src="'+$._+'images/slide-bullet'+(first ? "-select" : "")+'.png" alt="'+this.caption+'" width="10" height="10" border="0" /></a></li>');
        
        if (first) {
          first = false;
          return true;
        }
        
        $("#content_half_right_container").append(
          '<div class="right_slider"><div class="title_overlay"><p>'+
          '<a href="'+this.link+'">'+this.caption+'</a></p></div>'+
          '<div class="home_box"><a href="'+this.link+'">'+
          '<img width="443" height="270" border="0" alt="'+this.caption+'" src="'+this.image+'"/></a></div></div>'
        );
      });
      
      timeout = setTimeout(
        (function(slide) {
          return function() {
            home_slide.show(slide, 1)
          };
        })(current_slide+1),
        
        5000
      );
    },
    
    show : function(num, auto) {
      if (num == current_slide) return;
      if ($("#content_half_right_container:animated").length) return;
      
      current_slide = num;

      $("#content_half_right_container").animate({ 'margin-left': -num * 455 + 'px' }, function() {
        $("ul#slideshow_pag li a img").attr({ src : $._+'images/slide-bullet.png' });
        $("ul#slideshow_pag li a img:eq("+num+")").attr({ src : $._+'images/slide-bullet-select.png' });
      });

      if (typeof(auto) != "undefined") {
        timeout = setTimeout(
          (function(slide) {
            return function() {
              home_slide.show(slide, 1)
            };
          })((current_slide+1) % slides.length),
          
          5000
        );
      } else {
        clearTimeout(timeout);
      }

    }
  }
})();
