jQuery.noConflict()

function toutSlider(options){
  var $=jQuery
  this.setting={displaymode:{type:'auto', pause:2000, cycles:2, pauseonmouseover:true}, orientation:'h', persist:true, slideduration:500}
  jQuery.extend(this.setting, options)
  var curslide=(this.setting.persist)? toutSlider.routines.getCookie("slider-"+this.setting.wrapperid) : 0
  this.curslide=(curslide==null || curslide>this.setting.imagearray.length-1)? 0 : parseInt(curslide)
  this.curstep=0
  this.zIndex=1
  this.animation_isrunning=false
  this.posprop=(this.setting.orientation=="h")? "left" : "top"
  options=null
  var slideshow=this, setting=this.setting, preloadimages=[], slidesHTML=''
  for (var i=0; i<setting.imagearray.length; i++){ 
    preloadimages[i]=new Image()
    preloadimages[i].src=setting.imagearray[i][0]
    slidesHTML+=toutSlider.routines.getSlideHTML(setting.imagearray[i], setting.dimensions[0]+'px', setting.dimensions[1]+'px', this.posprop)+'\n'
  }
  jQuery(function($){ 
    slideshow.init($, slidesHTML)
  })
  $(window).bind('unload', function(){ 
    if (slideshow.setting.persist) 
      toutSlider.routines.setCookie("slider-"+setting.wrapperid, slideshow.curslide)
  })
}

toutSlider.prototype={

  slide:function(nextslide, dir){
    if (this.curslide==nextslide)
      return
    var slider=this
    var nextslide_initialpos=this.setting.dimensions[(dir=="right"||dir=="left")? 0 : 1] * ((dir=="right"||dir=="down")? -1 : 1)
    var curslide_finalpos=-nextslide_initialpos
    var posprop=this.posprop
    if (this.animation_isrunning!=null)
      this.animation_isrunning=true
    this.$imageslides.eq(dir=="left"||dir=="top"? nextslide : this.curslide).css("zIndex", ++this.zIndex)
    this.$imageslides.eq(nextslide).css(toutSlider.routines.createobj(['visibility', 'visible'], [posprop, nextslide_initialpos]))
      .animate(toutSlider.routines.createobj([posprop, 0]), this.setting.slideduration, function(){slider.animation_isrunning=false})
    this.$imageslides.eq(this.curslide).animate(toutSlider.routines.createobj([posprop, curslide_finalpos]), this.setting.slideduration, function(){jQuery(this).css("visibility", "hidden")})
    this.curslide=nextslide
  },

  navigate:function(keyword){
    clearTimeout(this.rotatetimer)
    var dir=(keyword=="back")? (this.setting.orientation=="h"? "right" : "down") : (this.setting.orientation=="h"? "left" : "up")
    var targetslide=(keyword=="back")? this.curslide-1 : this.curslide+1
    targetslide=(targetslide<0)? this.$imageslides.length-1 : (targetslide>this.$imageslides.length-1)? 0 : targetslide
    if (this.animation_isrunning==false)
      this.slide(targetslide, dir)
  },

  rotate:function(){
    var slideshow=this
    if (this.ismouseover){
      this.rotatetimer=setTimeout(function(){slideshow.rotate()}, this.setting.displaymode.pause)
      return
    }
    var nextslide=(this.curslide<this.$imageslides.length-1)? this.curslide+1 : 0
    this.slide(nextslide, this.posprop)
    if (this.setting.displaymode.cycles==0 || this.curstep<this.maxsteps-1){
      this.rotatetimer=setTimeout(function(){slideshow.rotate()}, this.setting.displaymode.pause)
      this.curstep++
    }
  },

  init:function($, slidesHTML){
    var slideshow=this, setting=this.setting
    this.$wrapperdiv=$('#'+setting.wrapperid).css({position:'relative', visibility:'visible', overflow:'hidden', width:setting.dimensions[0], height:setting.dimensions[1]})
    if (this.$wrapperdiv.length==0){
      alert("Error: DIV with ID \""+setting.wrapperid+"\" not found on page.")
      return
    }
    this.$wrapperdiv.html(slidesHTML)
    this.$imageslides=this.$wrapperdiv.find('div.slide')
    this.$imageslides.eq(this.curslide).css(toutSlider.routines.createobj([this.posprop, 0]))
    if (this.setting.displaymode.type=="auto"){
      this.setting.displaymode.pause+=this.setting.slideduration
      this.maxsteps=this.setting.displaymode.cycles * this.$imageslides.length
      if (this.setting.displaymode.pauseonmouseover){
        this.$wrapperdiv.mouseenter(function(){slideshow.ismouseover=true})
        this.$wrapperdiv.mouseleave(function(){slideshow.ismouseover=false})
      }
      this.rotatetimer=setTimeout(function(){slideshow.rotate()}, this.setting.displaymode.pause)
    }
  }

}

toutSlider.routines={

  getSlideHTML:function(imgref, w, h, posprop){
    var posstr=posprop+":"+((posprop=="left")? w : h)
    var layerHTML=(imgref[1])? '<a href="'+imgref[1]+'">' : ''
    layerHTML+='<img src="'+imgref[0]+'" style="border-width:0;" />'
    layerHTML+=(imgref[1])? '</a>' : ''
    return '<div class="slide" style="position:absolute;'+posstr+';width:'+w+';height:'+h+';text-align:center;">'
      +'<div style="width:'+w+';height:'+h+';display:table-cell;vertical-align:middle;">'
      +layerHTML
      +'</div></div>'
  },


  getCookie:function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); 
    if (document.cookie.match(re))
      return document.cookie.match(re)[0].split("=")[1]
    return null
  },

  setCookie:function(name, value){
    document.cookie = name+"=" + value + ";path=/"
  },

  createobj:function(){
    var obj={}
    for (var i=0; i<arguments.length; i++){
      obj[arguments[i][0]]=arguments[i][1]
    }
    return obj
  }
}
