/*-----------------
* OBJECT  : imageChange
* VERSION : 1.0
* DATE    : 21.02.2010
* AUTHOR  : rumbero www.netcodev.de
*-----------------
*/
(function ($) {
	$.fn.imageChange = function (options) {
		var conf = $.extend({}, {loadimg:false,isfade:false,isreset:false,slctIndex:0} , options);
		var _inst = this;
		this.init = function(){
			// when image list not empty
			if(this.get_confcheck){
				// set image container
				$("<div>").addClass(conf.imgcss).appendTo($(this));
				// set menu container
				$("<div>").css('display','none').appendTo($(this));
				// set menu
				this.set_menu();
				// start animation
				if(conf.autoChange && ! conf.showmenu){
					// set auto changing
					this.set_autochange();
				}else{
					// set image
					this.set_img();
				}	
			}
		}
		this.reset = function(option){
			if(typeof option == "object" && ! conf.isreset){
				for(var e in option){
					if(typeof conf[e] != "undefined"){
						conf[e] = option[e];
					}
				}
				conf.isreset = true;
				this.resetinit(0);
			}
		}
		this.resetinit = function(x){
			// safety variable count calls
			var n = x+1;
			if( ! conf.isfade){
				if(conf.showmenu){
					$(this).children("div:eq(1)").children("ul").children("li").removeClass(conf.menucss.activ).addClass(conf.menucss.normal);
					$(this).children("div:eq(1)").children("ul").children("li:eq(0)").removeClass(conf.menucss.normal).addClass(conf.menucss.activ);
					$(this).children("div:eq(1)").fadeIn("slow");
					conf.slctIndex = 0;
					this.set_img();
				}else if(conf.autoChange && ! conf.showmenu){
					$(this).children("div:eq(1)").fadeOut("slow");
					conf.slctIndex = -1;
					this.set_autochange();
				}
				conf.isreset = false;
			}else{
				// when calls less 15
				if(n < 15){self.setTimeout(function(){_inst.resetinit(n)},200);}
			}
		}
		this.get_confcheck = function(){
			var res = false;
			if(typeof conf.imglist != "undefined"){ if(typeof conf.imglist == "object" && conf.imglist.length > 0){res=true} }
			return res;
		}
		this.set_img = function(){
			var imgsrc = (typeof conf.imglist[conf.slctIndex] != "undefined" ) ? conf.imgpath+""+conf.imglist[conf.slctIndex] : "";
			if(imgsrc!="" && ! conf.isfade){
				conf.isfade=true;
				if( ! conf.loadimg){
					conf.loadimg = true;
					$("<img />").appendTo($(this).children("div:eq(0)")).attr("src",imgsrc).load(function(){
						$(this).fadeIn(conf.fadeTime , function(){
							$(this).parent().css('backgroundImage','url('+$(this).attr("src")+')').css('backgroundRepeat','no-repeat');
							conf.isfade=false;
						})
					});
				}else{
					$(this).children("div:eq(0)").children("img").css('display','none').attr("src",imgsrc);
				}
			}
		}
		this.set_autochange = function(){
			if(conf.autoChange){
				if( ! conf.loadimg){
					this.set_img();
				}else if( ! conf.isfade){
					conf.slctIndex = (conf.slctIndex+1 <= conf.imglist.length-1) ? conf.slctIndex+1 : 0;
					this.set_img();	
				}
				self.setTimeout(function(){_inst.set_autochange()},conf.autoTime);	
			}
		}
		this.set_menu = function(){
			var clss = "";
			$("<ul>",{"class":conf.menucss.container}).appendTo($(this).children("div:eq(1)"));
			for(var i = 0; i < conf.imglist.length;i++){
				clss = (conf.slctIndex==i) ? conf.menucss.activ:conf.menucss.normal;
				$("<li>",{"class":conf.imgitemcss+' '+clss}).appendTo($(this).children("div:eq(1)").children("ul"));
			}
			$("<li>",{"class":'clearL'}).appendTo($(this).children("div:eq(1)").children("ul"));
			$(this).children("div:eq(1)").children("ul").children("li."+conf.imgitemcss).bind({
				click:function(){
					if(conf.slctIndex != $(this).index() && ! conf.isfade){
						$(this).parent().children("li:eq("+conf.slctIndex+")").removeClass(conf.menucss.activ);
						$(this).parent().children("li:eq("+conf.slctIndex+")").addClass(conf.menucss.normal);
						conf.slctIndex = $(this).index();
						_inst.set_img()
					}
				},
				mouseover:function(){
					if(conf.slctIndex != $(this).index()){
						$(this).removeClass(conf.menucss.normal);
						$(this).addClass(conf.menucss.activ);
					}	
				},
				mouseout:function(){
					if(conf.slctIndex != $(this).index()){
						$(this).removeClass(conf.menucss.activ);
						$(this).addClass(conf.menucss.normal);
					}	
				}
			});
			if(conf.showmenu)
			$(this).children("div:eq(1)").fadeIn("slow");
		}
		return this;
	}
})(jQuery);
