
/*
loader.js
*/
if(!Control) var Control = {};
Control.Loader = Class.create();
Control.Loader.prototype = {

  initialize: function(parentElement) {
    var parent = $(parentElement);
    //var pos = Position.positionedOffset(this.parent);
    this.element = document.createElement('div');
    this.element.id = 'javascript-loader';
    parent.appendChild(this.element);   
    this.element.style.left = '0px';
    this.element.style.top = '-24px';
    this.element.style.position = 'relative';
  },
  
  show: function() {
    this.element.style.display = '';  
  }, 

  hide: function() {    
    this.element.style.display = 'none';
  }
}
/*
gallery.js
*/
if(!Control) var Control = {};
Control.Gallery = Class.create();
Control.Gallery.prototype = {

  initialize: function(galleryId, images, text) { 	
		//Skip if zero or one image
  	if (images.length < 2)
  	{
  		return;
  	}
  	
		//Setup Events
   	this.onAnimate = this.animate.bindAsEventListener(this); 
   	this.onImageFadeOut = this.imageFadeOut.bindAsEventListener(this);    	
   	this.onImageFadeIn = this.imageFadeIn.bindAsEventListener(this);    	   	
   	this.onIconClick = this.iconClick.bindAsEventListener(this);    	   	

		//Gallery Container
    this.gallery = $(galleryId);

		//Get Gallery Image
		var all = this.gallery.getElementsByTagName('img');
		this.image = all[0];
		this.imageContainer = Element.findParentTag(this.image, 'div');

		//Get Gallery Text
		if (text)
		{
			all = this.gallery.getElementsByTagName('h1');
			this.headline = all[0];
			all = this.gallery.getElementsByTagName('p');
			this.subheadline = all[0];
			if (!this.headline && this.subheadline)
			{
			  this.headline = this.subheadline;
			  this.subheadline = false;
			}
		}
			
		//Loader
		this.loader = new Control.Loader(this.imageContainer);
		
		//Preload Images
    this.images = new Array();
    for (var i=0; i<images.length; i++)
    {
	    var img = new Image();
	    if (text && text.length >= i)
	    {
	      if (text[i].length)
	      {
	    	  img.headline = text[i][0];
	    	  img.subheadline = text[i][1];
	      } else
	      {
	        img.headline = text[i];
	      }
	    }
	    img.loaded = false;
	    img.onload = function () { this.loaded = true; };
	    img.src = images[i];
	    this.images.push(img);
   	}
		
		//Gallery Setup
		this.imageNo = 0;
		this.imageNextNo = false;		
		this.imageCount = this.images.length;		
		this.imageStandTime = 5.0;
		this.imageFadeTime = 1.0;		

		//Get Links
		this.links = this.gallery.getElementsByTagName('a');
    for (var i=0; i<this.links.length; i++)
    {
	    this.links[i].imageNo = i;
	    this.links[i].href = "JavaScript:nop();";
	    Event.observe(this.links[i], "mousedown", this.onIconClick);
   	}		
   	
   	//Lets rock
   	this.animate();
  },
  
  dispose: function() {
  }, 

  nextImageNo: function() {
  	if (this.imageNextNo != false)
  	{
  		nextImage = this.imageNextNo;
  	} else
  	{
  		var nextImage = this.imageNo + 1;
			if (nextImage > this.imageCount - 1) nextImage = 0;
		}
  	return nextImage;
  },  

  updateImage: function(no) {
  	//Update Image
  	this.imageNo = no;
  	this.imageContainer.className = this.images[this.imageNo].width == 480 ? 'image-landscape' : 'image-potrait';
  	this.image.src = this.images[this.imageNo].src;
  	this.image.width = this.images[this.imageNo].width;
 		this.imageNextNo = false;
 		//Update Text
 		if (this.headline)
 		{
 			this.headline.innerHTML = this.images[this.imageNo].headline;
 			this.subheadline.innerHTML = this.images[this.imageNo].subheadline; 			
 		}
 		if (this.subheadline)
 		{
 			this.subheadline.innerHTML = this.images[this.imageNo].subheadline; 			
 		} 		
  	//Update Timeline
		for(var i=0; i<this.links.length; i++) 
		{
			if (i < this.imageNo)
			{
				this.links[i].className="blind solid";
			  this.links[i].style.backgroundPosition = '0px 0px';
			} else
			{
				this.links[i].className="blind";
			  this.links[i].style.backgroundPosition = '0px 0px';				
			}
			var src = this.links[i].childNodes[0].src;
			src = src.replace(/_over.jpg/, '');
			src = src.replace(/.jpg/, '');
			if (i == this.imageNo)
			{
				this.links[i].childNodes[0].src = src + "_over.jpg";
			} else
			{
				this.links[i].childNodes[0].src = src + ".jpg";
			}
		}		
		//Update Print Image
		setPrintImage(no);
  },

  checkLoaded: function(handler) {
  	if ((!this.images[this.imageNo].loaded) ||
				(!this.images[this.nextImageNo()].loaded))
		{
			this.loader.show();
			setTimeout(handler, 1000);
			return false;
		}
		this.loader.hide();  
		return true;
  },

  animate: function() {
  	if (!this.checkLoaded(this.onAnimate))
  	{
  		return;
  	}
		this.links[this.imageNo].className="blind current";
		new Effect.GalleryTimeline(this.links[this.imageNo],
		    { duration: this.imageStandTime,
			    afterFinish: this.onImageFadeOut });
  },
    
  imageFadeOut: function() {
		new Effect.Opacity(this.imageContainer,
		    { duration: this.imageFadeTime, 
		      transition: Effect.Transitions.linear, 
		      to: 0.0,
		      afterFinish: this.onImageFadeIn});
  },  
  
  imageFadeIn: function() {		
		this.updateImage(this.nextImageNo());
		new Effect.Opacity(this.imageContainer,
		    { duration: this.imageFadeTime, 
		      transition: Effect.Transitions.linear, 
		      to: 1.0,
		      afterFinish: this.onAnimate});     
  },

  iconClick: function(event) {
		Effect.Queues.get('global').each(function(e) { e.cancel() });		
		Element.setOpacity(this.imageContainer, 1.0);
  	var element = Event.findElement(event, 'a');
		this.updateImage(element.imageNo);
		this.animate();
  }
}



if(!Effect) var Effect = {};
Effect.GalleryTimeline = Class.create();
Object.extend(Object.extend(Effect.GalleryTimeline.prototype, 
  Effect.Base.prototype), {

	initialize: function(element) {
	  var options = arguments[1] || {};
	  this.element = $(element);
	  this.element.style.backgroundPosition = '-500px 0px';
	  this.width = parseInt(Element.getDimensions(this.element).width);
	  this.start(options);
	},

	update: function(position) {
	  var x = 500 - Math.floor(this.width * position);
	  this.element.style.backgroundPosition = '-'+x+'px 0px';
	}
});
