﻿(function(jQuery) {
jQuery.fn.daBannerAnimate1 = function(para) {
	para = jQuery.extend({
		imgObj:false,
		prev:false,
		next:false,
		icons:false,
		move:false,
		speed:300,
		delay:1000
    }, para || {});
	var o = jQuery(this);
	if(para.imgObj){
		var img = jQuery(para.imgObj);
	}
	if(para.prev){
		var prev = jQuery(para.prev);
	}
	if(para.next){
		var next = jQuery(para.next);
	}
	if(para.icons){
		var icons = jQuery(para.icons);
	}
	
	if(!(para.imgObj && para.prev && para.next && para.icons && para.move)){
		alert("参数设置错误");
		return false;
	}
	
	var iconHtml = icons.html();
	var len = img.children().length;
	var s = para.speed;
	var totalWidth = 0;
	var Height = icons.height();
	
	icons.children().each(function(){
		totalWidth = totalWidth + (jQuery(this).width() + parseInt(jQuery(this).css("margin-left")) + parseInt(jQuery(this).css("margin-right")) + parseInt(jQuery(this).css("padding-left")) + parseInt(jQuery(this).css("padding-right")));
	});
	
	icons.empty();
	icons.css({ 
		"overflow": "hidden",
		"position": "relative"
	});
	
	icons.append('<div class="daBannerAnimate1"></div>');
	icons.append('<div class="daBannerAnimate2"></div>');
	var div1 = icons.children(".daBannerAnimate1");
	var div2 = icons.children(".daBannerAnimate2");

	div1.css({ 
		"width": totalWidth+"px", 
		"height": "100%",
		"position":"relative",
		"top":"0px",
		"left":"0px",
		"float":"left",
		"display":"inline",
		"overflow": "hidden",
		"padding": "0px",
		"margin": "0px"
	});
	div2.css({ 
		"width": totalWidth+"px", 
		"height": "100%",
		"position":"relative",
		"top": -Height+"px",
		"left": totalWidth+"px",
		"float":"left",
		"display":"inline",
		"overflow": "hidden",
		"padding": "0px",
		"margin": "0px"
	});
	div1.html(iconHtml);
	div2.html(iconHtml);
	
	div1.children().each(function(i){
		jQuery(this).attr("index",i);
	});
	div2.children().each(function(i){
		jQuery(this).attr("index",i);
	});
	var div1Left = parseInt(div1.css("left"));
	var div2Left = parseInt(div2.css("left"));
	
	var nextClick = function(){
		if( div1.width() < totalWidth ){
			return false;
		}
		if(div1.is(":animated") || div2.is(":animated")){
			return false;
		}
		if(div1Left>=0){
			div2Left = -(totalWidth-div1Left);
			div2.css("left",div2Left+"px");
		}
		if(div1Left >= totalWidth){
			div1Left = -(totalWidth-div2Left);
			div1.css("left",div1Left+"px");
		}
		div1.animate({
			left:div1Left+(para.move*1)
		},para.speed,function(){
			div1Left = parseInt(div1.css("left"));
		});
		div2.animate({
			left:div2Left+(para.move*1)
		},para.speed,function(){
			div2Left = parseInt(div2.css("left"));
		});
	}
	var prevClick = function(){
		if( div1.width() < totalWidth ){
			return false;
		}
		if(div1.is(":animated") || div2.is(":animated")){
			return false;
		}
		if(div1Left<=-(totalWidth)){
			div1Left = totalWidth+div2Left;
			div1.css("left",div1Left+"px");
		}
		if(div2Left<=-(totalWidth)){
			div2Left = totalWidth+div1Left;
			div2.css("left",div2Left+"px");
		}
		div1.animate({
			left:div1Left-(para.move*1)
		},para.speed,function(){
			div1Left = parseInt(div1.css("left"));
		});
		div2.animate({
			left:div2Left-(para.move*1)
		},para.speed,function(){
			div2Left = parseInt(div2.css("left"));
		});
	}
	
	jQuery(para.next).bind("click",nextClick);
	jQuery(para.prev).bind("click",prevClick);
	
	
	var imgHtml = img.html();
	var pic = img.children();
	img.empty();
	img.css({
		"position":"relative",
		"overflow":"hidden",
		"z-index":"0"
	});
	for(i=0;i<pic.length;i++){
		img.append("<div class='daBannerAnimateImg'></div>");
	}
	var imgs = jQuery(".daBannerAnimateImg");
	imgs.css({
		"width":"100%",
		"height":"100%",
		"position":"absolute",
		"display":"block",
		"top":"0px",
		"left":"0px",
		"overflow":"hidden"
	});
	
	imgs.each(function(i){
		jQuery(this).css({
			"z-index":-i
		});
		jQuery(this).attr("index",i);
		jQuery(this).append(pic.eq(i));
	});
	var fristZindex = jQuery(".daBannerAnimateImg:first").css("z-index");
	
	var daBannerAnimateChingePic = function(){
//		imgs.css("opacity",0);
		var thisIndex = jQuery(this).attr("index");
		var thisPic = imgs.eq(thisIndex);
		thisPic.css("opacity",0);
		fristZindex++;
		thisPic.css("z-index",fristZindex);
		thisPic.animate({
			"opacity":1
		},para.delay);
	}
	
	
	div1.children().bind("click",daBannerAnimateChingePic);
	div2.children().bind("click",daBannerAnimateChingePic);

};
})(jQuery);
