/*
IE BROWSER CHECK
*/
var ie  = (document.all ? true : false);
/*
DOM ELEMENTS
*/
var dom = {
		
	/*	
		gets and test if id is an object	
	*/
	id : function(id, obj)
	{
		return ( !obj ? document : obj ).getElementById(id);
	},
	
	
	/*
		create a simple html tag element.
	*/
	create : function(tagType, attributes)
	{
		var tag = doc.createElement(tagType);
		//	add object attributes
		if( attributes ){
			//	if the id is only been requested
			if( typeof(attributes) == "string" ) attributes = {"id":attributes};
			//	add attributes to newly created element
			for(var key in attributes){
				tag[key] = attributes[key];
			}	
		}
		return  tag;
	},

	
	/*  
		quick test for object, will create one if a string via id	
	*/
	object : function(value)
	{
		return ( typeof(value) == "object" ? value : (dom.id(value) ? dom.id(value) : false) );
	},
	
	
	/*	
		Add events to an object.  can pass mulitple objects via an array	
	*/
	event : function(obj, evType, func)
	{
		if( dom.isArray(obj) ){
			for (var e = 0; e < obj.length; e ++) {
				dom.event(obj[e], evType, func);
			}
		}else if( dom.object(obj) ){
			if( ie ){
				//	IE
				obj.attachEvent("on" + evType, func);	
			}else if( obj.addEventListener ){
				//	firefox
				obj.addEventListener(evType, func, false);
			}else{ 
				//	other browser
				obj['on' + evType] = func; 
			}
		}else{alert("Could not find the object to attach event too");} 
	},
	
	
	/*	
		gets node elements attributes	
	*/
	attribute : function(att, obj, ret)
	{
		// typical MS IE bug, "for" will return NULL
		// this bug extends IE5 - IE 7, when will this end? 
		if( att == "for" && ie ){
			return ( !obj.attributes["for"].nodeValue ? false : ((!ret) ? true : obj.attributes["for"].nodeValue)) ;
		}
		return ( !obj.getAttribute(att) ? false : ((!ret) ? true : obj.getAttribute(att))) ;
	},
	
	
	/*
		get element/node/objects classes
	*/
	getClass : function(obj)
	{
		// tests for IE naming covention that differs from FF
		var classes = dom.attribute(((document.all) ? "className" : "class"), obj, true);
		//	return array of	all classes	
		return (classes ? classes.split(" ") : false ); 
	},
	
	
	/*
		does object have a class name of...xxx
	*/
	hasClass : function(classname, obj)
	{
		return (dom.inArray(classname, dom.getClass(obj)) ? true : false );
	},
	
	
	/*
		replaces an existing class with another one.
	*/
	swapClass : function(from, to, obj)
	{
		if( !dom.isArray(obj) ) obj = Array(obj);
		//	
		for(var i = 0; i < obj.length; i++) {
			if( obj[i] ) obj[i].className = obj[i].className.replace(from, to);
		}
	},
	
	
	/*
	
	*/
	addClass : function(classname, obj)
	{
		if( !dom.isArray(obj) ) obj = Array(obj);	
		//
		for(var i = 0; i < obj.length; i++) {
			//	replace it if it already exists and add new class to the object class
			if( obj[i] ) obj[i].className = obj[i].className.replace(classname, "") +" "+ classname;
		}
	},
	
	
	/*
		get all nodes which have the a class name of 	
	*/
	byClass : function(classname, obj, evnt, func)
	{
		//	store found class objects
		var objs = [];
		obj = dom.object(obj)
		// get all elements under passed obj
		var elems = ( !obj ? document : obj ).getElementsByTagName("*");
		for (var e = 0; e < elems.length; e ++) {
			if( dom.hasClass(classname, elems[e]) ){
				objs.push(elems[e]);
				//	pass an event trigger
				if( typeof evnt == "string" ){ 
					dom.event(elems[e], evnt, func);	
				}
			}
		}
		return objs;
	},
	
	
	
	/*
		get elements by tag type
	*/
	byTag : function(tag, obj, toArray)
	{
		obj = dom.object(obj);
		var collection = ( !obj ? document : obj ).getElementsByTagName(tag);
		//	return collection as an array?
		return ( toArray ? dom.toArray(collection) : collection);
	},

	
	/*
		simple test for item in an array
	*/
	inArray : function(needle, haystack, returnKeys)
	{
		var keys = [];
		for(var i = 0; i < haystack.length; i++) {
			if( haystack[i] === needle ){
				if( !returnKeys ) return true;
				//	add to array
				keys.push(i);
			}
		}
		return ( returnKeys ) ? keys : false;
	},
	
	
	/*
		basic test to see if obj is an array
	*/
	isArray : function(obj)
	{
		return ((typeof(obj) == "object" && (obj instanceof Array)) ? true : false);
	}

}	
	

/* 
SPECIAL EFFECTS 
*/	
var sfx = {
	
	//	fade speed
	fadeSpeed		: 500,
	
	//	opacity 
	opacity : function(obj, percentage)
	{
		if(typeof obj != "object"){
			obj = document.getElementById(obj);
		}
		//	set objects opacity
		if( percentage !== false ){
			//	sorts out opacity bug
			if( percentage == 100 ) percentage == 101;
			//	set
			obj.style.opacity 	   = (percentage/101).toFixed(2);  
			obj.style.MozOpacity   = (percentage/101).toFixed(2);
			obj.style.KhtmlOpacity = (percentage/100).toFixed(2);
			obj.style.filter = "alpha(opacity=" + percentage + ")"; 
			return obj;
		}else{
			//	other return objects opacity
			var o = parseFloat(obj.style.opacity);
			if( isNaN(o) ){
				sfx.opacity(obj, 100);	
				return 101;
			} 
			return o*100; 	
		}
	},
	
	
	//	do Fade 
	fade : function(obj, start, end, millisec)
	{
		var speed = Math.round(millisec/100);
		var timer = 0;
		if(start > end){
	    	for(var i = start; i >= end; i--){
	        	setTimeout(function(){ sfx.opacity(obj, start--); }, (timer*speed));
	        	timer++;
	        }
	    }else if(start < end){
	    	for(var i = start; i <= end; i++){
	    		setTimeout(function(){sfx.opacity(obj, start++); }, (timer*speed));
	            timer++
	        }
	    }
	},
	
	//	
	fadeIn : function(obj, millisec)
	{
		if( !millisec ) millisec = sfx.fadeSpeed;
	    //	ensure that object is visible to fade in
	   	sfx.opacity(obj, 0);
	   	//
	   	obj.style.display = "block";
	   	//	start fade
	   	sfx.fade(obj, 0, 101, millisec);
	}, 
	
	//
	fadeOut : function(obj, hide, millisec)
	{
		if( !millisec ) millisec = sfx.fadeSpeed;
		
	    sfx.fade(obj, 101, 0, sfx.fadeSpeed);
	    //	remove item from display
	    if( hide ){
	    	setTimeout(function(){ obj.style.display = "none"; }, millisec);
	    }
	},
	
	/*
		simple switch to swap a object to hide and show	
	*/
	display : function(show, hide)
	{
		show = dom.object(show);
		hide = dom.object(hide);
		//	
		if( typeof(hide) == "object" ) hide.style.display = "none";
		if( typeof(show) == "object" ) {
			show.style.display = "block";
			return show;
		}
	}
	
};


function slideshow(){
	
	//	store loaded images
	this.images  = Array();
	this.current = false;
	//	whether the images have loaded
	// loaded = false,
	//	actions
	this.paused  = true;
	this.playing = false;
	this.stopped = false;
	//	delay in milliseconds
	this.delay   = 5000;
	this.speed   = 500;
	//	which image html tag to replace with new image
	this.target  = false;
	//	
	this.mode   = "fade";
	this.loop   = true;
	
	//	load the images
	this.preload = function(imgs, directory)
	{
		if( directory == false ) directory = "";
		
		for(var i = 0; i < imgs.length; i++){	
			//	preload the images		
			var img = new Image();
			img.src = directory + imgs[i];	
			//	save
			this.images.push(img);
		}
	},	
	
	
	this.run = function()
	{
		//	check that the slideshow is allowed to run
		if( this.paused ) return false; 	
		//	start the slideshow
		//alert(this.mode);
		switch(this.mode)
		{
			case "fade" :
				if( this.target ){
					//
					this.current++;
					//	ensure that show hasn't reached the end.
					if( this.current == this.images.length ){
						this.current = 0;
					}
					//	looping isn't allowed.
					if( this.loop == false && this.current === 0 ){
						return false;	
					}
					//
					target    = this.target;
					image     = this.images[this.current];
					paused    = this.paused;
					speed     = this.speed;
					slideshow = this;
					//	setup onclick event to start/stop
					target.onclick = function()
					{

					}

					//
					setTimeout(function(){
							//	check that the slideshow is allowed to run
							if( this.paused ) return false; 
							//	fade out current image
							sfx.fadeOut(target);
								//	bring in new image
								setTimeout(function(){
									//	swap image source
									target.src = image.src;
									//	fade in new image
									sfx.fadeIn(target);
									//	re-run
									slideshow.run();
								}, speed);
						//	set delay before transistions to next image	
						}, this.delay);
					return true;
				}
			break;
		}
	},
	
	
	/*
		set the type of slideshow
	*/
	this.display = function(typ)
	{
		this.mode = (typeof typ == "undefined" ? "fade" : typ) ;
	},
	
	
	//	Play 
	this.play = function(target, style)
	{
		//	ensure that the target is set
		if(typeof target != "object"){
			target = document.getElementById(target);
		}
		this.target = target;
		
		//	set the type of slideshow 
		this.display(style);
		//	run slideshow
		if( this.paused ){
			this.paused = false;
			this.run();
		}
	},
	
	
	this.auto = function()
	{
		if( this.paused ){
			this.play();
		}else{
			this.pause();
		} 
	}, 
		
	
	//	Pause 
	this.pause = function()
	{
		this.paused = true; 	
	}
	
};

/*
GENERAL SITE WIDE EXECUTIONS
*/
var site = {
	//	setup the ourwines page
	ourwines : function()
	{
		//	get "a" tag hover links so as to display the details of the product
		var wines = dom.byClass("wine", dom.id("ourwines"));
		var details = dom.byTag("tr", dom.byClass("details", dom.id("ourwines"))[0]);
		
		
		for(var w = 0; w < wines.length; w++){
			wines[w].onmouseover = function(){
				for(var i = 0; i < wines.length; i++){
					if( this == wines[i] ){
						sfx.display(details[i+1], details[0]);
						 
					}
				}
			}
			wines[w].onmouseout = function(){
				for(var i = 0; i < wines.length; i++){
					if( this == wines[i] ){
						sfx.display(sfx.display(details[0]), details[i+1]);	
					}
				}
			}
		}
	}	
}

