var playdiv_id = "playdiv";	//----- 设置滚动部分的id
var IntagName = "a";	//----- 设置滚动图片框的标签
var btnleft_id = "btnleft" , btnright_id = "btnright";	//----- 设置左右两侧的箭头的id
var picl_1 = "images/textl.gif" , picl_2 = "images/textl.gif";	//----- 设置左侧的箭头的地址
var picr_1 = "images/textr.gif" , picr_2 = "images/textr.gif";	//----- 设置右侧的箭头的地址
var slowspeed = 500 , fastspeed = 100;	//----- 设置滚动速度，越大越慢
var innerspace = 4;	//----- 设置图片间距



$(document).ready( function() {
	if ( $("#" + playdiv_id).find(IntagName).length != 0 ) {
		scrollpro(playdiv_id,IntagName,btnleft_id,btnright_id,picl_1,picl_2,picr_1,picr_2,slowspeed,fastspeed,innerspace);
	}
} );

function scrollpro(p_id,tagName,left_id,right_id,picl_1_src,picl_2_src,picr_1_src,picr_2_src,slowspeeds,fastspeeds,innerspaces) {
	var playdiv = $("#" + p_id);
	var btnleft = $("#" + left_id) , btnright = $("#" + right_id);
	var maxlength = 0 , marqing , type = "left";
	for ( var i = 0 ; i < playdiv.find(tagName).length ; i++ ) {
		maxlength += playdiv.find(tagName)[i].offsetWidth + innerspaces;
	}
	playdiv.append(playdiv.html()).css( { width : (maxlength * 2) + "px" } );
	
	var playdiv1 = $("#" + p_id + "1");
	for ( var i = 0 ; i < playdiv1.find(tagName).length ; i++ ) {
		maxlength += playdiv1.find(tagName)[i].offsetWidth + innerspaces;
	}
	playdiv1.append(playdiv1.html()).css( { width : (maxlength * 2) + "px" } );
	
	/*---------- 向左滚动 ----------*/
	btnleft.mouseover( function() {
		//$(this).attr("src",picl_2_src);
		if ( type != "left" && type != "bigleft" ) {
			type = "left";
			clearInterval(marqing);
			putleft();
			marqing = setInterval(putleft,slowspeeds*6);
		}
	} ).mousedown( function() {
		if ( type != "bigleft" ) {
			type = "bigleft";
			clearInterval(marqing);
			putleft1();
			marqing = setInterval(putleft1,fastspeeds+10);
		}
	} ).mouseup( function() {
		if ( type == "bigleft" ) {
			type = "left";
			clearInterval(marqing);
			putleft();
			marqing = setInterval(putleft,slowspeeds*6);
		}
	} ).mouseout( function() {
		//$(this).attr("src",picl_1_src);
	} );

	/*---------- 向右滚动 ----------*/
	btnright.mouseover( function() {
		//$(this).attr("src",picr_2_src);
		if ( type != "right" && type != "bigright" ) {
			type = "right";
			clearInterval(marqing);
			putright();
			marqing = setInterval(putright,slowspeeds*6);
		}
	} ).mousedown( function() {
		if ( type != "bigright" ) {
			type = "bigright";
			clearInterval(marqing);
			putright1();
			marqing = setInterval(putright1,fastspeeds+10);
		}
	} ).mouseup( function() {
		if ( type == "bigright" ) {
			type = "right";
			clearInterval(marqing);
			putright();
			marqing = setInterval(putright,slowspeeds*6);
		}
	} ).mouseout( function() {
		//$(this).attr("src",picr_1_src);
	} );
	
	/*---------- 控制滚动 ----------*/
	playdiv.mouseover( function() {
		clearInterval(marqing);
		$("#" + p_id + " " + tagName + ":first").stop();
		$("#" + p_id + "1 " + tagName + ":first").stop();
	} ).mouseout( function() {
		clearInterval(marqing);
		switch (type) {
			case "left":
				putleft();
				marqing = setInterval(putleft,slowspeeds*6);
				break;
			case "bigleft":
				putleft();
				marqing = setInterval(putleft,slowspeeds*6);
				break;
			case "right":
				putright();
				marqing = setInterval(putright,slowspeeds*6);
				break;
			case "bigright":
				putleft();
				marqing = setInterval(putright,slowspeeds*6);
				break;
		}
	} );
	
	playdiv1.mouseover( function() {
		clearInterval(marqing);
		$("#" + p_id + " " + tagName + ":first").stop();
		$("#" + p_id + "1 " + tagName + ":first").stop();
	} ).mouseout( function() {
		clearInterval(marqing);
		switch (type) {
			case "left":
				putleft();
				marqing = setInterval(putleft,slowspeeds*6);
				break;
			case "bigleft":
				putleft();
				marqing = setInterval(putleft,slowspeeds*6);
				break;
			case "right":
				putright();
				marqing = setInterval(putright,slowspeeds*6);
				break;
			case "bigright":
				putleft();
				marqing = setInterval(putright,slowspeeds*6);
				break;
		}
	} );
	
	
	
	function putleft() {
		$("#" + p_id + " " + tagName + ":first").stop().animate( { "marginLeft" : "-" + ( playdiv.find(tagName)[0].offsetWidth + innerspaces ) + "px" } , { queue: false, duration: slowspeeds, complete:function() {
			$("#" + p_id + " " + tagName + ":first").appendTo( playdiv );
			$("#" + p_id + " " + tagName + ":last").css( "marginLeft" , "0" );
		} } );
		$("#" + p_id + "1 " + tagName + ":first").stop().animate( { "marginLeft" : "-" + ( playdiv1.find(tagName)[0].offsetWidth + innerspaces ) + "px" } , { queue: false, duration: slowspeeds, complete:function() {
			$("#" + p_id + "1 " + tagName + ":first").appendTo( playdiv1 );
			$("#" + p_id + "1 " + tagName + ":last").css( "marginLeft" , "0" );
		} } );
	}
	
	function putright() {
		$("#" + p_id + " " + tagName + ":first").stop().animate( { "marginLeft" : "0" } , { queue: false, duration: slowspeeds, complete:function() {
			$("#" + p_id + " " + tagName + ":last").css( "marginLeft" , "-" + ( playdiv.find(tagName)[playdiv.find(tagName).length-1].offsetWidth + innerspaces ) + "px" );
			$("#" + p_id + " " + tagName + ":last").prependTo( playdiv );
		} } );
		$("#" + p_id + "1 " + tagName + ":first").stop().animate( { "marginLeft" : "0" } , { queue: false, duration: slowspeeds, complete:function() {
			$("#" + p_id + "1 " + tagName + ":last").css( "marginLeft" , "-" + ( playdiv1.find(tagName)[playdiv1.find(tagName).length-1].offsetWidth + innerspaces ) + "px" );
			$("#" + p_id + "1 " + tagName + ":last").prependTo( playdiv1 );
		} } );
	}
	
	
	
	function putleft1() {
		$("#" + p_id + " " + tagName + ":first").stop().animate( { "marginLeft" : "-" + ( playdiv.find(tagName)[0].offsetWidth + innerspaces ) + "px" } , { queue: false, duration: fastspeeds, complete:function() {
			$("#" + p_id + " " + tagName + ":first").css( "marginLeft" , "0" );
			$("#" + p_id + " " + tagName + ":first").appendTo( playdiv );
		} } );
		$("#" + p_id + "1 " + tagName + ":first").stop().animate( { "marginLeft" : "-" + ( playdiv1.find(tagName)[0].offsetWidth + innerspaces ) + "px" } , { queue: false, duration: fastspeeds, complete:function() {
			$("#" + p_id + "1 " + tagName + ":first").css( "marginLeft" , "0" );
			$("#" + p_id + "1 " + tagName + ":first").appendTo( playdiv1 );
		} } );
	}
	
	function putright1() {
		$("#" + p_id + " " + tagName + ":first").stop().animate( { "marginLeft" : "0" } , { queue: false, duration: fastspeeds, complete:function() {
			$("#" + p_id + " " + tagName + ":last").css( "marginLeft" , "-" + ( playdiv.find(tagName)[playdiv.find(tagName).length-1].offsetWidth + innerspaces ) + "px" );
			$("#" + p_id + " " + tagName + ":last").prependTo( playdiv );
		} } );
		$("#" + p_id + "1 " + tagName + ":first").stop().animate( { "marginLeft" : "0" } , { queue: false, duration: fastspeeds, complete:function() {
			$("#" + p_id + "1 " + tagName + ":last").css( "marginLeft" , "-" + ( playdiv1.find(tagName)[playdiv1.find(tagName).length-1].offsetWidth + innerspaces ) + "px" );
			$("#" + p_id + "1 " + tagName + ":last").prependTo( playdiv1 );
		} } );
	}
	
	clearInterval(marqing);
	putleft();
	marqing = setInterval(putleft,slowspeeds*6);
}