var Gallery = Class.create();
Gallery.prototype = {
	list:"ListImage",
	photoContent:"FullImage",
	slides:[],
	imHeight:77 + 3,
	scrollPos:0,
	prevNum:7,
	scrollNumber:2,
	initialize:function()
	{
		this.parseList();
		$('ImageTotal').innerHTML = this.slides.length;
		this.checkEvent();
		//this.show();
	},
	IEinit:function()
	{
		this.parseList();
		$('ImageTotal').innerHTML = this.slides.length;
	},
	parseList:function()
	{
		var loaded = 0;
		$A($(this.list).getElementsByTagName("A")).each(function(elem, pos){
			//var img = Builder.node('img', { src:elem.href });
			var img = new Image();
			img.onload = function(){
				loaded++;
				//$('ImageTotal').innerHTML = loaded;
				this.loaded = 1;
				if (loaded == 3) {
				if (Prototype.Browser.IE)
				{
					setTimeout("G.show()", 100);
				}
				else {
					G.show(); 
				} }
				if (this.startIndex && this.startPos)
				{
					G.pos(this.startIndex, { noScroll:true });
					this.startIndex = this.startPos = null;
				}
			}
			img.src = elem.href;
			this.slides.push(img);
			Event.observe(elem, "click", function(){
				this.pos(pos, { stopAfterUpdate:true});
			}.bind(this));
			elem.onclick = function(){ return false;}
			
			//alert(elem.href);
		}.bind(this));
	},
	show:function(pos)
	{
			//alert("rferg");
			this.slideshow = new Effect.SlideShow(this.photoContent, this.slides);
	},
	pos:function(pos, obj)
	{
		//alert(pos);
		this.slideshow.cancel();
		var opt = {from:pos/this.slides.length};
		if (obj && (obj instanceof Object)) Object.extend(opt, obj);
		this.slideshow = new Effect.SlideShow(this.photoContent, this.slides, opt);
		if (!obj) obj = {};
		if (!obj.noScroll) this.scrollNum(pos - this.scrollPos);
		//this.scrollPos = pos;
	},
	CalcPositionX:function(pos)
	{
		return 0 - this.imHeight * pos;
	},
	CalcDelta:function(pos)
	{
		return Math.abs(parseInt($(this.list).style.top || 0 )) + this.CalcPositionX(pos);
	},
	scrollNum:function(scrollNum)
	{
		if (scrollNum < 0)
		{
			var num = (this.scrollPos + scrollNum >= 0 ? this.scrollPos + scrollNum : 0);
		}
		if (scrollNum > 0)
		{
			var num = (this.scrollPos + scrollNum + this.prevNum <= this.slides.length ? this.scrollPos + scrollNum : this.slides.length - this.prevNum);
		}
		var delta = this.CalcDelta(num);
		var deltaNum = Math.abs(this.scrollPos - num);
		this.scrollPos = num;
			new Effect.Move(this.list, { y:delta, mode:'relative', duration:0.3 * deltaNum});
	},
	scrollUp:function()
	{
		this.scrollNum(0-this.scrollNumber);
	},
	scrollDown:function(num)
	{
		this.scrollNum(this.scrollNumber);
	},
	Pause:function()
	{
		this.paused = true;
		this.slideshow.cancel();
	},
	Play:function()
	{
		this.paused = false;
		this.pos(this.index + 1);
	},
	Next:function()
	{
		if (!this.index) this.index = 0;
		if (this.index < this.slides.length - 1)
		{
			if (!this.paused)
				this.paused = true;
			if (!this.index) this.index = 0;
			this.pos(this.index + 1, { stopAfterUpdate:true });
		}
	},
	Prev:function()
	{
		if (!this.index || this.index == 0 ) return;
		if (!this.paused)
			this.paused = true;
		this.pos(this.index - 1, { stopAfterUpdate:true });
	},
	update:function(event, params)
	{
		if (event == "update")
		{
			if (params && params.index)
				this.index = params.index;
				//alert("rger");
				$('ImageIndex').innerHTML = (this.index || 0) + 1;
				//alert(Mediator.observers[event].length);
		}
	},
	checkEvent:function()
	{
		if ($('SecondParent'))
		{
			var div = $('SecondParent');
			var li = div.parentNode;
			var href = $A(li.childNodes)[0];
			href.style.position = "relative";
			var beforeColor = href.style.color;
			li.onmouseover = function(){
				div.style.display = "block";
				href.style.color = "#488f00";
			}
			div.onmouseout = function(){
				div.style.display = "none";
				href.style.color = beforeColor;
			}
		}
	}
}
Effect.SlideShow = Class.create();
Object.extend(Object.extend(Effect.SlideShow.prototype, Effect.Base.prototype), {
  currentSlide: null,
  initialize:function(element,slides,opts) {
    this.element = $(element);
    //var slideTools=$('slideTools');
    var children = $A(this.element.childNodes);
    //alert(children);
    //this.element.innerHTML="";
    var from = ((opts && opts.from) ? opts.from : 0);
    var fromSlide = from*slides.length;
    //this.currentSlide = slides[fromSlide];
    //this.element.appendChild(slideTools);
    //Element.removeClassName(this.element,"hidden");
    this.slides=slides;
    if (!opts) opts = {}
    var interval=(opts && opts.interval) ? opts.interval : 7;
    var duration = ((from > 0 ? ((opts.to || 1) - from)*slides.length : false) || slides.length)*interval;
    //alert(duration);
    var options=Object.extend(
    {
         transition: Effect.Transitions.linear,
         duration: duration,
         fps: 1
    },
      opts || {}
    );
    Object.extend(options, {
    	afterSetup:function(){
    		//this.update(from, true);
    	}.bind(this)
    });
    if (children.length > 0)
    {
    	new Effect.Fade(children[0], {afterFinish:function(){
      		children[0].remove();
    		this.start(options);
    		//this.update(from, true);
      	}.bind(this)});
    }
    else
    {
    	this.start(options);
    	//this.update(from, true);
    }
    //alert(from);
   //this.start(options);
    //this.update(from, true);
    //this.element.appendChild(this.currentSlide);
  },

  update: function(position, begin) {
    //_debug("update("+position+")");
    if (this.currentSlide) var oldSlide=this.currentSlide;
    /*var children = $A(this.element.childNodes);
    if (children.length > 0)
    {
    	var oldSlide = children[0];
    }*/
    var len=this.slides.length;
    var index=Math.floor(position*len) + (begin ? 0 : 0);
    if (index>=len){ index=len-1; } //if position=1.0 exactly
    this.currentSlide=this.slides[index];
    
    this.index = index;
    //alert("currentSlide = "+index);
    var next = function(){
      		this.currentSlide.style.display = "none";
      		this.element.appendChild(this.currentSlide);
      		new Effect.Appear(this.currentSlide);
    }.bind(this);
    if (this.currentSlide!=oldSlide){
    	if (this.currentSlide.loaded !=1)
    	{
    		this.currentSlide.startIndex = index;
    		this.currentSlide.startPos = position;
    		this.cancel();
    		return;
    	}
      if (oldSlide){ 
      	//Element.remove(oldSlide); 
      	new Effect.Fade(oldSlide, { afterFinish:function(){
      		Element.remove(oldSlide);
      		next();
      	}.bind(this) });
      }else
      {
      	next();
      }
    //Mediator.event("update", { index:index });
    }
    Mediator.event("update", { index:index });
    if (this.options.stopAfterUpdate)
    	this.cancel();
  }

});
var G;
Event.observe(window, "load", function(){
	G = new Gallery();
	//alert(G);
	//if (Prototype.Browser.IE) G.IEinit();
	Mediator.observe("update", G);
});
var Mediator = {
	observers:{},
	observe:function(event, oObserver)
	{
		if (!Mediator.observers[event])
		{
			Mediator.observers[event] = [oObserver];
		}
		else
		{
			Mediator.observers[event].push(oObserver);
		}
	},
	event:function(event, params)
	{
		if (!Mediator.observers[event])
		{
			return;
		}
		else
		{
			Mediator.observers[event].each(function(elem){
				if (elem instanceof Function) elem(event, params);
				if (elem instanceof Object) elem.update(event, params);
			});
		}
	}
}