
var firstResizeDone = false;
var maxCarouselsPerPage = 3;

function fn_moveCarousel_left (aCrsIndex) {
  var e = document.getElementById("crscontainer"+aCrsIndex);
  var crs = e.crsObject;
  crs.moveleft();
}

function fn_moveCarousel_leftmost (aCrsIndex) {
  var e = document.getElementById("crscontainer"+aCrsIndex);
  var crs = e.crsObject;
  crs.moveleftmost();
}

function fn_moveCarousel_right (aCrsIndex) {
  var e = document.getElementById("crscontainer"+aCrsIndex);
  var crs = e.crsObject;
  crs.moveright();
}

function fn_moveCarousel_rightmost (aCrsIndex) {
  var e = document.getElementById("crscontainer"+aCrsIndex);
  var crs = e.crsObject;
  crs.moverightmost();
}

function fn_motionengine (evnt, aCrsIndex) {
  var e = document.getElementById("crscontainer"+aCrsIndex);
  var crs = e.crsObject;
  crs.motionengine(evnt);	
}

function myCarousel () {
  this.prevEvent="";
  this.crsIndex=0;	
  this.imgGap=0;
  this.maxwidth=100;  
  this.restarea=25; 
  this.scrollres=1;
  this.maxspeed=7; 
  this.fastscrollspeed=25;  
  this.startpos=2; 
  this.scrollspeed=0;
  this.movestate='';
  this.menuwidth=0;  
  this.actualwidth=0;
  this.theContainer;
  this.theGallery;  
  this.theContainer_in;
  this.loadedyes=0;
  this.lefttime=0;
  this.righttime=0;

  this.ietruebody = function (){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
  }

  this.getposOffset = function (what, offsettype) {
    var totaloffset = (offsettype=="left")? what.offsetLeft: what.offsetTop;
    var parentEl = what.offsetParent;
    while (parentEl!=null){
      totaloffset = (offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
      parentEl = parentEl.offsetParent;
    }
    return totaloffset;
  }


  this.moveleft = function() {
	with (this) {   	  	  
      if (loadedyes) {
        movestate = 'left';
        if (parseInt(theGallery.style.left)>(menuwidth-actualwidth)) {
          theGallery.style.left = parseInt(theGallery.style.left)-scrollspeed+"px";
        }
      }
      lefttime = setTimeout("fn_moveCarousel_left("+crsIndex+")",scrollres);
	}
  }
  
  this.moveleftmost = function() {
	with (this) {   	  	  
      if (loadedyes) {
        movestate = 'left';
        if (parseInt(theGallery.style.left)>(menuwidth-actualwidth)) {
          theGallery.style.left = parseInt(theGallery.style.left)-fastscrollspeed+"px";
          lefttime = setTimeout("fn_moveCarousel_leftmost("+crsIndex+")",scrollres);		  
        }
		else
          theGallery.style.left = parseInt(menuwidth-actualwidth)+"px";		
      }
	}
  }  

  this.moveright = function() {
	with (this) {   	  	  
      if (loadedyes) {
        movestate = 'right';
        if (parseInt(theGallery.style.left)<0) {
          theGallery.style.left = parseInt(theGallery.style.left)+scrollspeed+"px";
        }
      }
      righttime = setTimeout("fn_moveCarousel_right("+crsIndex+")",scrollres);
	}
  }

  this.moverightmost = function() {
	with (this) {   	  	  
      if (loadedyes) {
        movestate = 'right';
        if (parseInt(theGallery.style.left)<0) {
          theGallery.style.left = parseInt(theGallery.style.left)+fastscrollspeed+"px";
          righttime = setTimeout("fn_moveCarousel_rightmost("+crsIndex+")",scrollres);		  
        }
		else
		  theGallery.style.left = "0px";
      }
	}
  }

  this.motionengine = function (e) {
	with (this) {   	  
        var mainobjoffset=getposOffset(theContainer, "left");
        var dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
        var dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
        var curposy=window.event? event.clientX : e.clientX? e.clientX: "";
        curposy-=mainobjoffset-dsocx;
        var leftbound=(menuwidth-restarea)/2;
        var rightbound=(menuwidth+restarea)/2;
        if (curposy>rightbound){
          scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed;
          clearTimeout(righttime);
          if (movestate!="left") moveleft();
        }
        else if (curposy<leftbound){
          scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
          clearTimeout(lefttime);
          if (movestate!="right") moveright();
        }
        else 
          scrollspeed=0;
	}
  }  

  this.contains_ns6 = function (a, b) {
    if (b !== null)
      while (b.parentNode)
        if ((b = b.parentNode) == a) return true;
    return false; 
  }

  this.stopmotion = function (e) {
    if (!window.opera || (window.opera && e.relatedTarget !== null)) {
      if ((window.event && !this.theContainer.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !this.contains_ns6(e.currentTarget, e.relatedTarget))) {
        clearTimeout(this.lefttime);
        clearTimeout(this.righttime);
        this.movestate = '';
      }
	}
  }
  
  this.showTip = function () {  
  }
  
  this.hideTip = function () {  
  }  

  this.init = function (aCrsIndex,aImgGap) {
    this.crsIndex=aCrsIndex;	
    this.imgGap=aImgGap;
    this.theContainer = document.getElementById("crscontainer"+this.crsIndex);
    this.theContainer.crsObject = this;  
	this.theContainer.prevEvent="";
	this.theGallery = document.getElementById("crsgallery"+this.crsIndex);
	this.theContainer_in = document.getElementById("crscontainer"+this.crsIndex+"_in");
    with (this) {
	  maxwidth=parseInt(theContainer.style.width);
  	  restarea=Math.round(maxwidth/4);
      if (typeof theContainer.style.maxWidth !== 'undefined') theContainer.style.maxWidth = maxwidth+'px';
      menuwidth = theContainer.offsetWidth;
	  theGallery.style.fontSize = Math.round(this.imgGap*2.5)+"px";
      actualwidth = theContainer_in.offsetWidth;
      if (startpos) theGallery.style.left = (menuwidth-actualwidth)/startpos+'px';
      loadedyes=0;
    }
    this.theContainer.onclick = function(e){ 
	  //this.showTip();
	}
    this.theContainer.onmousemove = function(e){ 
      if ((this.prevEvent=="")||(this.prevEvent.type!="click")) {
        this.crsObject.motionengine(e); 
	    this.prevEvent=e;		
      }
      else if ((abs(e.clientX-this.prevEvent.clientX)>10)||(abs(e.clientY-this.prevEvent.clientY)>10)) {
	    this.crsObject.motionengine(e); 
	    this.prevEvent=e;
	  }
    }
    this.theContainer.onclick = function(e){ 
	  this.prevEvent=e;	
      this.crsObject.stopmotion(e);	  
    }	
    this.theContainer.onmouseout = function(e){ 
	  this.prevEvent="";
      this.crsObject.stopmotion(e);
	  //this.hideTip();
    }	
    if (document.body.filters && firstResizeDone == false) {
	  firstResizeDone = true;
	  onresize();    
	}
  }
  
  this.changeContent = function(aHTML) {
    with (this) {	  
	  theContainer_in.innerHTML = aHTML;
      actualwidth = theContainer_in.offsetWidth;
      if (startpos) theGallery.style.left = (menuwidth-actualwidth)/startpos+'px';
	  loadedyes=0;
	  if (aHTML != "" && aHTML != "&nbsp;") loadedyes=1;	  
	}
  }
  
}  // end of object


onresize = function(){
  var e;	
  var crs;	
  for (var i=1; i<=maxCarouselsPerPage; i++) { 	
    e = document.getElementById("crscontainer"+i);
	if (e) {
  	  crs = e.crsObject;
	  if (crs) {
	    with (crs) {
	      if (typeof theContainer !== 'undefined' && theContainer.filters) {
//		    theContainer.style.width = "0";
//		    theContainer.style.width = "";
		    theContainer.style.width = Math.min(theContainer.offsetWidth, maxwidth)+'px';
		  }
		  menuwidth = theContainer.offsetWidth;
		  theGallery.style.left = startpos? (menuwidth-actualwidth)/startpos+'px' : 0;
		}
	  }
	}
  }
}
