//script manages rotating of background images
var Slideshow = Class.create();
Slideshow.prototype = {
	initialize: function(){
		this.I = new Array(); //array of images
		//this.target = (Cookies.getCookie("selectedBackground")!= null) ? Cookies.getCookie("selectedBackground") : 0;
		this.target=null;
		this.selectedIndex = (Cookies.getCookie("selectedBackground")!= null) ? Number(Cookies.getCookie("selectedBackground")) : 0;
		this.selectedSource = null;
	},
	init : function(aSource,bSource){
		this.A = $(aSource);
		this.B = $(bSource);
		this.A.style.display="";
		this.B.style.display="none";
		this.pe = null;
		
		if(this.I.length != 0){
			this.selectedSource = this.A;
			this.A.appendChild(this.I[this.selectedIndex]);
			
			
				switch(Cookies.getCookie("slideshow")){
					case "running":
						//alert("auto starting")
						this.start();
					break;
					case "stopped":
						//alert("auto pause")
						this.stop();				
					break;
					case null:
						//alert("no cookie: auto starting")
						this.start();
					break;
				}
		}
		else{
			alert("Slideshow Failed: You have not added any images.");	
		}
	},
	addImage : function(src){
		var img = new Image();
		img.src = src;
		this.I.push(img);
	},
	next : function(){
		if(this.pe!=null) this.pe.stop();
		this.show(1);
		if(Cookies.getCookie("slideshow")=="running") this.pe = new PeriodicalExecuter(function(){slideshow.show(1)},10);	
	},
	previous : function(){
		if(this.pe!=null) this.pe.stop();
		this.show(-1);
		if(Cookies.getCookie("slideshow")=="running") this.pe = new PeriodicalExecuter(function(){slideshow.show(-1)},10);	
	},
	stop: function(){
		if(this.pe!=null) this.pe.stop();
		Cookies.setCookie("slideshow","stopped");
		$("slideshow_stopstart").className = "toggle_go";
		$("slideshow_stopstart").onclick = function(){slideshow.start()};
		$("slideshow_stopstart").firstChild.title = "Click to resume the slideshow.";
	},
	start: function(){
		Cookies.setCookie("slideshow","running");
		this.pe = new PeriodicalExecuter(function(){slideshow.show(1)},10);
		$("slideshow_stopstart").className = "toggle_stop";
		$("slideshow_stopstart").onclick = function(){slideshow.stop()};
		$("slideshow_stopstart").firstChild.title = "Click to pause the slideshow.";
	},
	show : function(dir){
		if((this.selectedIndex + dir) >= this.I.length){
			this.target	= 0;
		}
		else if((this.selectedIndex + dir) < 0){
			this.target = this.I.length - 1;
		}
		else{
			this.target = this.selectedIndex + dir;	
		}
		//alert(this.selectedIndex + " to " + this.target);
		Cookies.setCookie("selectedBackground",this.target);
		if(this.selectedSource == this.A){
			this.selectedSource = this.B;
			if(this.B.firstChild != null){
				while(c = this.B.firstChild){
					this.B.removeChild(c);
				}
			}
			this.B.appendChild(this.I[this.target]);
			while(!this.I[this.target].complete){};
			this.doTransition(this.A,this.B);	
			//new Effect.Fade(this.A,{duration:1,afterFinish:function(){new Effect.Appear(slideshow.B,{duration:1})}});
		}
		else{
			this.selectedSource = this.A;
			if(this.A.firstChild != null){
				while(c = this.A.firstChild){
					this.A.removeChild(c);
				}
			}
			this.A.appendChild(this.I[this.target]);
			//new Effect.Fade(this.B,{duration:1,afterFinish:function(){new Effect.Appear(slideshow.A,{duration:1})}});
			while(!this.I[this.target].complete){};
			this.doTransition(this.B,this.A);	
		}
	},
	doTransition : function(objA,objB){
		if(this.I[this.target].complete){
			new Effect.Fade(objA,{duration:1,afterFinish:function(){new Effect.Appear(objB,{duration:1})}});
		}
		else{
			this.I[this.target].onload = function(){new Effect.Fade(objA,{duration:1,afterFinish:function(){new Effect.Appear(objB,{duration:1})}})};
		}
		this.selectedIndex = this.target;
		//new Effect.Appear(objB,{duration:1});
	}
}

var slideshow = new Slideshow("bg_a","bg_b");
slideshow.addImage("/templates/kkhib/images/bg_hib_front.jpg");
slideshow.addImage("/templates/kkhib/images/bg_front_2.jpg");
slideshow.addImage("/templates/kkhib/images/bg_entrance.jpg");
slideshow.addImage("/templates/kkhib/images/bg_penthouse_1.jpg");
slideshow.addImage("/templates/kkhib/images/bg_suite_1.jpg");
slideshow.addImage("/templates/kkhib/images/bg_deluxe.jpg");
slideshow.addImage("/templates/kkhib/images/bg_fireplace.jpg");
slideshow.addImage("/templates/kkhib/images/bg_jacobs_cottage.jpg");
slideshow.addImage("/templates/kkhib/images/bg_hibernian_bar.jpg");
slideshow.addImage("/templates/kkhib/images/bg_conference.jpg");
slideshow.addImage("/templates/kkhib/images/bg_morrisons.jpg");
//slideshow.addImage("http://hib.eventireland.org/templates/kkhib/images/bg_default.jpg");

Event.observe(window,'load',function(){
									 slideshow.init("bg_a","bg_b");
									 },false);