/* hgImgSwap - RBurnie - hybridgarden.com *************************

Requires: script.aculo.us effects library found at http://script.aculo.us
*** USEAGE ***
init the script onload of page
ex.
<body onload="hgImgSwap.Init('FOO');"> 
or on the onload of one of the img tags of the imageLoader

To load an image use: 
hgImgSwap.LoadImg('pathtoimagefile','alttext');

*** LOADER - XHTML MARKUP *****************************************
*** Default mode markup - for images all same size/aspect ratio ***
** NB: <div class="loading"></div> is optional but must have class="loadDisp"
	   I simply apply dimensions and a background image to this so it acts as a quasi loading display
<div id="FOO" class="loader">
	<img src="images/blank.gif" alt="" />
	<img src="images/blank.gif" alt="" />
	<div class="loadDisp"></div>
</div>

*** Framed mode markup *******************************************
<div id="FOO" class="loader">
	<div><img src="images/blank.gif" alt="" /></div>
	<div><img src="images/blank.gif" alt="" /></div>
	<div class="loadDisp"></div>
</div>

*** CSS MARKUP ***************************************************

*****************************************************************/

var hgImgSwap = {
	Version: '1.0',
	Mode: "default",
	
	Init: function(e){
		this.element = document.getElementById(e);
		var imgs = this.element.getElementsByTagName('img');
		if(imgs.length==2){
			this.topImage = imgs[0];
			this.botImage = imgs[1];
		}
		var divs = this.element.getElementsByTagName('div');
		if(divs.length<4){
			for(var i=0; i<divs.length; i++){
				if(divs[i].className=="loadDisp"){
					this.loadDisplay = divs[i];
					this.loadDisplay.style.display = "none";
				}else{
				/* set up frame divs if applicable */
					if(!this.topImageFrame){
						this.topImageFrame = divs[i];
					}else if(!this.botImageFrame){
						this.botImageFrame = divs[i];
					}
				}
			}
		}
		if(this.topImageFrame && this.botImageFrame){
			this.Mode = "framed";
		}
	},
	
	LoadImg: function(img, alt){
		this.SwitchImg(img, alt);	
	},
	
	SwitchImg: function(img, alt){
		var bot = this.botImage;
		this.botImage = this.topImage;
		this.topImage = bot;
		
		this.iTop = this.topImage;
		this.iBot = this.botImage;
		
		if(this.Mode=="framed"){
			var botFrame = this.botImageFrame;
			this.botImageFrame = this.topImageFrame;
			this.topImageFrame = botFrame;
			/* a parent div being used around image - the div is now target of the effects */
			this.iTop = this.topImageFrame;
			this.iBot = this.botImageFrame;
		}
		
		this.Loading(true);
		
		this.HideImg(this.iTop);
		/* set opacity to 0 */
		this.iTop.style.zIndex = 1;
		
		hgImgSwap.topImage.onload = null;
		this.topImage.src = img;
		this.topImage.alt = alt;
		/* see explaination in imgAppear about why im doing it this way */
		this.topImage.onload = hgImgSwap.ImgAppear;
		
		this.iBot.style.zIndex = 0;
	},
	
	ImgAppear: function(){
		hgImgSwap.Loading(false);
		Effect.Appear(hgImgSwap.iTop);
		/* in this case 'this' refers to the image
		because this function is called from the img onload attribute
		that way it only fades once the image has loaded.
		*/
	},
	
	HideImg: function(img){
		img.style.opacity = 0;
	    img.style.filter  = "alpha(opacity:0)";
	},
	
	Loading: function(flag){
		if(!this.loadDisplay){return;}
		if(flag){
			this.loadDisplay.style.display = "";
		}else{
			this.loadDisplay.style.display = "none";
		}
	}
	
};

/* add-on for 'hgImgShow' */
var hgImgSlide = {
	Version: '1.0',
	
	Init: function(e,list,alt){
		hgImgSwap.Init(e);
		if(arguments.length >= 4){
			this.path = arguments[3];
			if(arguments.length == 5){
				this.type = arguments[4];
			}else{
				this.type = '.jpg';
			}
		}else{
			this.path = '';
		}
		this.alt = alt;
		this.list = list;
		this.current = this.list.length -1;
		/*Load latest slide in
		hgImgSwap.LoadImg(this.path+this.list[this.current]+this.type, this.alt);
		
		- This worked fine except in Internet Explorer when a back/forward button was
		used to return to the page. It was quite a bizare bug and only happend if back/forward button 
		was used???
		Instead using Effect.Appear() directly on 1st image load
		*/
	},
	
	Next: function(){
		this.current += 1;
		if(this.current >= this.list.length){
			this.current = 0;
		}
		hgImgSwap.LoadImg(this.path+this.list[this.current]+this.type, this.alt);
	},
	
	Prev: function(){
		this.current -= 1;
		if(this.current < 0){
			this.current = this.list.length -1;
		}
		hgImgSwap.LoadImg(this.path+this.list[this.current]+this.type, this.alt);
	}
}
