
(function($) {
	$.fn.homeimages = function(options) {
		
		var slideshow_interval = 8000;
		
		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();
					switch(dir) {
						case "b" : c++;	 break;
						case "+" : c++;clearInterval(theInterval); break;
						case "-" : c--;clearInterval(theInterval); break;
						case "strt" : theInterval = setInterval(function(){sobj.move("b");return false;},slideshow_interval);; c++; break;
						case "stp" : clearInterval(theInterval); break;
						default: c = dir;
					}
					if (c < 0) { c = this.opts.length - 1; }
					if (c > this.opts.length - 1) { c = 0; }
					///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');//gpc
				},
				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:eq(0)", $this).click(function(){sobj.move("-");return false;});
			$("#controls a:eq(1)", $this).click(function(){sobj.move("stp");return false;});
			$("#controls a:eq(2)", $this).click(function(){sobj.move("strt");return false;});
			$("#controls a:eq(3)", $this).click(function(){sobj.move("+");return false;});

			/* this is the beginning load, so send a 'b' */
			theInterval = setInterval(function(){sobj.move("b");return false;},slideshow_interval); 
			$(".imageHolder", $this).bind("click", function(){sobj.go();});
			/* setup first */
			//sobj.to($("#videolist .video", $this).eq(0));
			if (sobj.opts && sobj.opts[sobj.getCurrent()].RELOCATE != '') {
			  $(".imageHolder", $this).css("cursor", "pointer");
			}
		});
	};

	$.fn.homeimages.defaults = {
	};

})(jQuery);
