// JavaScript Document
var ImageCache = new function() {
	this.imageArray = new Array;
	this.loadedCount = 0;
	this.errorCount = 0;
	this.abortedCount = 0;
	this.changeListener;
	var me=this;
	
	this.addImage = function(imageurl) {
	  image = new Image();
	  this.imageArray.push(image);	
 	  image.onload  = function() {me.loadedCount++;  me.notifyChangeListener()};
	  image.onerror = function() {alert("error");  me.errorCount++;   me.notifyChangeListener()};
	  image.onAbort = function() {alert("aborted"); me.abortedCount++; me.notifyChangeListener()};
	  image.src = imageurl; 		
	}
	
	this.totalImages          = function()         { return this.imageArray.length; }
	this.cachedImages         = function()         { return this.loadedCount + this.errorCount + this.abortedCount; }
	this.setChangeListener    = function(listener) { this.changeListener = listener; }
	this.notifyChangeListener = function()         { this.changeListener(); }
}

























function ActivateSlideInPlace() {

  $('.slideinplace').each( function(index) {
    var self = $(this);	

	self.css('position','relative');
	self.animate({
		   left: '+=' + self.data("left"),
		   top: '+=' + self.data("top")
		},0)
		.animate({
		   left: '-=' + self.data("left"),
		   top: '-=' + self.data("top")
		},self.data("speed"))
  })
}





function ActivateCyclers() {

  $('.cycler').each( function(index) {
    var self = $(this);	
	self.cycle({
		fx:            self.data("fx"),
		speed:         self.data("speed"),
		timeout:       self.data("timeout"),
		delay:         500,
		cleartype:     true,
		cleartypeNoBg: true 
	})	  
  })
}



function ActivateCarousels() {
  $('.carousel').each( function(index) {
	var self = $(this);
	
	// read parameters from the DOM element
	self.duration    = self.data("duration");
	self.pause       = self.data("pause");
    self.orientation = self.data("orientation");
    self.predelay    = self.data("predelay");
    self.scrollpane  = null;
	
	// Create a div (scrollpane) of the correct orientation that is WIDER or LONGER than this div, so that we can pan it
	if (self.orientation == 'horizontalleft') self.scrollpane =  $('<div style="position:relative; width:200%"></div>');
	if (self.orientation == 'verticalup')     self.scrollpane =  $('<div style="position:relative; height:200%"></div>');   
	
	// Move all the current children INTO the scrollpane, then add the scrollpane a child
  	self.scrollpane.append(self.children());
	self.append(self.scrollpane);

    // Create a function to cycle myself
	self.cycle = function(){
 	       var scrollpanefirstchild  = this.scrollpane.children(':nth-child(1)');
           var amountleft = '-=0';
		   var amountup = '-=0';           
		   
		   if (this.orientation == 'horizontalleft') amountleft = '-=' + this.scrollpane.children(':nth-child(2)').position().left;
		   if (this.orientation == 'verticalup')     amountup   = '-=' + this.scrollpane.children(':nth-child(2)').position().top;

		   this.scrollpane.animate({left: amountleft, top: amountup  },self.duration,"linear",
		                   function(me) {
			                 setTimeout(function() {self.cycle()},self.pause);
		                     self.scrollpane.append(scrollpanefirstchild); 
							 self.scrollpane.animate({left: 0, top: 0},0);
		                   });
	}

    setTimeout(function() {self.cycle()},self.predelay);
  })
}

