
(function($) {
	$.fn.homeimages = function(options) {
		
		var slideshow_interval = 8000; // 8 seconds
		
		return this.each(function() {
			$this = $(this);
			/* preload */
			var plis = new Array();
			for (var i in options){
				if (i == parseInt(i)){
					var tmpimg = new Image();
					tmpimg.src = options[i].PATH;
					tmpimg.alt = options[i].NAME;
					plis.push(tmpimg);
				}
			}
			
			/* create object to track state */
			var sobj = {
				pli : plis,
				ref : $this,
				opts : options,
				move : function(dir){
					var c = this.getCurrent();
					if (c == -1 || c == this.opts.length-1) c = 0;
					else c++;

					$(".imageHolder img", this.ref).attr("src", this.pli[c].src);
					$(".imageHolder img", this.ref).attr("alt", this.pli[c].alt);
					if (this.opts[c].RELOCATE == '') {
						$(".imageHolder", this.ref).css("cursor", "default");
					}	else {
						$(".imageHolder", this.ref).css("cursor", "pointer");
					} 
					$(".imageHolder img", this.ref).css('z-index', '-1');
				},
				go : function(){
					var c = this.getCurrent();
					if (this.opts[c].RELOCATE != "") location.href = this.opts[c].RELOCATE;
				},
				getCurrent : function(){
					var img = $(".imageHolder img", this.ref);
					var imgpath = img.attr("src");
					var c = -1;
					for (var i in this.opts){
						if (imgpath.indexOf(this.opts[i].PATH) != -1) c = i;
					}
					if (c == -1) c = 0;
					return c;
				}
			};
			
			/* add events */
			//$("#controls a:first", $this).click(function(){sobj.move("+");return false;});
			//$("#controls a:last", $this).click(function(){sobj.move("-");return false;});
			setInterval(function(){sobj.move("+");return false;},slideshow_interval); 
			$(".imageHolder", $this).bind("click", function(){sobj.go();});
			if (sobj.opts && sobj.opts[sobj.getCurrent()].RELOCATE != '') {
				$(".imageHolder", $this).css("cursor", "pointer");
			}
		});
	};

	$.fn.homeimages.defaults = {
	};

})(jQuery);