/*
Plugin Name: changeImg-plugin
Plugin URI: http://www.soft36.cn
Description: null.
Version: 1.0
Author: king
Author URI: http://www.soft36.cn
*/

(function($){

	$.fn.changeImg=function(obj){
	
	var defaults={
			direction:"top",
			transition:false,
			time:1000,
			path:"",
			nav:false,
			navHoverImgChange:false,
			width:this.width(),height:this.height()
		}	
	
	var obj=$.extend(defaults,obj);
	var imgW=obj.width;
	var imgH=obj.height;
	var path=getUrl(obj.path);
	
	var img_arr=obj.data.split("||");
	var total=img_arr.length;
	var imgH_arr=new Array();
	var imgS_arr=new Array();
	var imgT_arr=new Array();
	$.each(img_arr,function(i,n){
			imgS_arr.push(n.split(",")[0]);
			imgT_arr.push(n.split(",")[1]);
			imgH_arr.push(n.split(",")[2]);
	});
	
	function crateHtml(){
		
		var img_tmp="";
		var nav_tmp="";
		$.each(imgS_arr,function(i,n){
				img_tmp+="<a href='"+imgH_arr[i]+"'><img width='"+imgW+"' height='"+imgH+"' src='"+path+imgS_arr[i]+"' title='"+imgT_arr[i]+"'></a>";
				nav_tmp+="<a title='"+imgT_arr[i]+"'>"+(i+1)+"</a>";
		});
		
		var firstOne="<a href='"+imgH_arr[0]+"'><img src='"+path+imgS_arr[0]+"' title='"+imgT_arr[0]+"'></a>"
		return "<div class='imglist_plugin'>"+img_tmp+firstOne+"</div><div class='numNav'>"+nav_tmp+"</div>";
		
	}

	//obj=$.extend(defaults,obj);
	
	// 查看参数对象
/* 	var a="";
	$.each(obj,function(i,n){
		a+=i+" : "+n+"<br/>";
	})*/
	//$(this).append(obj.width+","+obj.height/*$(crateHtml())*/);
	$(this).append($(crateHtml()));
//-------------------------------------------init
	var eq_arr=new Array();
	var type=1;
	var cssN="top";
	var it=0;
	var timeout;
	var BoxObj=$(this);
	var msgDiv=$(".msg");
	var imgList=BoxObj.find(".imglist_plugin");
	var img=imgList.find("img");

	//alert(imgList.length)
	var navA=BoxObj.find(".numNav a");
	navA.css("opacity",0.5)
	imgList.css("position","absolute");
	
	switch(obj.direction){
		
		case "top":
			
			//imgList.height(100000);
			for(var i=0;i<=total;i++){
				eq_arr.push(i);
				}
				
			moveVal=-imgH;
			
		break;
		case "bottom":
			//imgList.height(100000);
			for(var i=total;i>=0;i--){
				eq_arr.push(i);
				}
				
			moveVal=-imgH;

		break;
		case "left":
			imgList.width(100000);
			imgList.find("a").addClass("f-l");
			type=0;
			cssN="margin-left";
			for(var i=0;i<=total;i++){
				eq_arr.push(i);
				}
			moveVal=-imgW;
	
		break;
		case "right":
			imgList.width(100000);
			imgList.find("a").addClass("f-l");
			type=0;
			cssN="margin-left";
			for(var i=total;i>=0;i--){
				eq_arr.push(i);
				}
			moveVal=-imgW;
	
		break;
		default:
			return false;
		}
		
		arrFirst=eq_arr[0];
		arrLast=eq_arr[total];		
		cssInit();	

//-------------------------------------------nav change fn	
	function mouseOver(obj){
		clearAnimate();
		navInit();
		$(obj).attr("class","hover");
		//msgDiv.append("<br/> obj : "+$(obj).text());
		it=$(obj).text()-1;
		change(it);
	}
//-------------------------------------------img change fn
	function clearTimeOut(){
		clearTimeout(timeout);
	}
	function navInit(){
			$.each(navA,function(){
							           $(this).attr("class","");
							      });
		}
	function cssInit(){
			imgList.css(cssN,""+eq_arr[0]*moveVal+"px");
		}
	function clearAnimate(){
			imgList.stop(true, obj.navHoverImgChange);
		}
	function auto_nav(num){
		navInit();
		if(num==0){
			navA.eq(0).attr("class","hover");
		}else{
			navA.eq(num).attr("class","hover");
		}

	}
	function auto_change(){
		//BoxObj.append(it);
		
		change(eq_arr[it]);
		auto_nav(it);
		timeout=setTimeout(auto_change,(obj.time+600));
		
		if(it==total){
			it=0;
		}else{
			it++;
		}
		
	}
	function change(num){
		//change img
		if(type==1){
		
			if(num==arrLast){
					//msgDiv.append("init <br/>");
					imgList.animate({top:(eq_arr[it]*moveVal)},600,"",function(){	
																						cssInit();
																					   });
					it=0;
			}else{	
					//msgDiv.append("<br/> num : "+eq_arr[it]*moveVal/*eq_arr.length*/);
					imgList.animate({top:(eq_arr[it]*moveVal)},600);		
			}			
		
		}else{
			
			if(num==arrLast){
					imgList.animate({marginLeft:(eq_arr[it]*moveVal)},600,"",function(){	
																						cssInit();
																					   });
					it=0;
			}else{		
					imgList.animate({marginLeft:(eq_arr[it]*moveVal)},600);		
			}			
		
		}

		//msgDiv.append("<br/> it : "+it);
	}

//-------------------------------------------event
	navA.bind('mouseover',function(){
										mouseOver(this);
								   })
//-------------------------------------------event
	BoxObj.hover(function(){			
				clearTimeOut();
				//clearAnimate()
				
	},
	function(){
				//clearAnimate();
				//alert("1");
				//imgList.stop(false, true);
				clearTimeOut();	
//				function ggg(){
//						alert("11");
//					}
				//timeout=setTimeout(ggg,2000);
				timeout=setTimeout(auto_change,2000);
	});
//-------------------------------------------event
	auto_change()	
	
	}
})(jQuery);

function getUrl(str){
	return document.URL.replace(/(\\|\/)[^\\\/]*$/, '/')+str;
}
