﻿function CreateFlash(box,width,height,url,arrow) {
	this.Box = $("#" + box);
	this.Name = "图片切换";
	this.Author = "Keboy";
	this.Version = "v1.1";
	this.CreateDate = "2011-9-6";
}
CreateFlash.prototype = {
	Width: new Number(),
	Height: new Number(),
	LoadUrl: new String(),
	Arrow: new String(),
	MaxLength: new Number(),
	Speed: new Number(),
	BigPic: new Array(),
	SmallPic: new Array(),
	LinkUrl: new Array(),
	Stay: new Number(),
	Start: function() {
		var $this = this;
		$.ajax( {
			type: "GET",
			url: $this.LoadUrl,
			cache: false,
			data: "",
			dataType: "xml",
			error: function() { alert("Error loading XML document"); },
			success: function(xml) {
				with($this) {
					$(xml).find("l").each( function(i) {
						BigPic[i] = $(this).attr("big");
						SmallPic[i] = $(this).attr("small");
						LinkUrl[i] = $(this).attr("url");
					} );
					MaxLength = $(xml).find("l").length;
					if ( MaxLength == 0 ) {
						Box.next().andSelf().hide();	//----- 记录不存在时隐藏当前切换盒子（公司网站页面需要，同时隐藏了盒子的下一个元素，具体隐藏什么根据页面需要）
						return;
					}
					CreateBox();
				}
			}
		} );
	},
	CreateBox: function() {
		var BoxWidth = this.MaxLength * 53;
		var htmldata = "";
		htmldata += "<img class='boxbigger' src='" + this.BigPic[0] + "' width='" + this.Width + "' height='" + this.Height + "' style='cursor:pointer;' />";
		htmldata += "<div id='sbox' style='padding-right:10px;position:relative;margin-top:-50px;width:" + BoxWidth + "px;height:40px;margin-left:auto;overflow:hidden;'>";
		for ( var i = 0; i < this.SmallPic.length; i++ ) {
			htmldata += "<div class='sboxes' rel='" + i + "' style='width:43px;height:40px;float:left;margin-right:10px;'>";
			htmldata += "	<div class='sboxarrow' style='width:43px;height:8px;overflow:hidden;clear:both;'><!----></div>";
			htmldata += "	<div class='sboximg' style='width:41px;height:30px;border:1px #FFF solid;overflow:hidden;clear:both;background:#000;'><img src='" + this.SmallPic[i] + "' width='41' height='30' style='cursor:pointer;filter:alpha(opacity=30);opacity:0.3;' /></div>";
			htmldata += "</div>";
		}
		htmldata += "</div>";
		this.Box.html(htmldata);
		this.Stay = this.MaxLength - 1;
		this.MovieStart();
	},
	MovieStart: function() {
		var $this = this;
		GoCross(0);
		var mar = setInterval(GoCross2,this.Speed * 1000);
		function GoCross(newStay) {
			$("#sbox div.sboxes:eq(" + $this.Stay + ") div.sboxarrow:first").css("background","none").next().find("img").css( {
				"filter" : "alpha(opacity=30)",
				"opacity" : "0.3"
			} );
			$this.Stay = newStay;
			var img = new Image();
			img.src = $this.BigPic[$this.Stay];
			$("#sbox div.sboxes:eq(" + $this.Stay + ") div.sboxarrow:first").css("background","url(" + $this.Arrow + ") no-repeat center").next().find("img").css( {
				"filter" : "alpha(opacity=100)",
				"opacity" : "1"
			} );
			$this.Box.find("img.boxbigger").fadeTo("fast", 0.3, function() {
				var imgThis = $(this);
				if ( img.complete ) {
					imgThis.attr("src",$this.BigPic[$this.Stay]).attr("rel",$this.LinkUrl[$this.Stay]).fadeTo("fast",0.99);
				}
				img.onload = function() {
					imgThis.attr("src",$this.BigPic[$this.Stay]).attr("rel",$this.LinkUrl[$this.Stay]).fadeTo("fast",0.99);
				};
			} );
		}
		function GoCross2() {
			$("#sbox div.sboxes:eq(" + $this.Stay + ") div.sboxarrow:first").css("background","none").next().find("img").css( {
				"filter" : "alpha(opacity=30)",
				"opacity" : "0.3"
			} );
			if ( $this.Stay == $this.MaxLength - 1 ) {
				$this.Stay = 0;
			}
			else {
				$this.Stay++;
			}
			var img = new Image();
			img.src = $this.BigPic[$this.Stay];
			$("#sbox div.sboxes:eq(" + $this.Stay + ") div.sboxarrow:first").css("background","url(" + $this.Arrow + ") no-repeat center").next().find("img").css( {
				"filter" : "alpha(opacity=100)",
				"opacity" : "1"
			} );
			$this.Box.find("img.boxbigger").fadeTo("fast", 0.3, function() {
				var imgThis = $(this);
				if ( img.complete ) {
					imgThis.attr("src",$this.BigPic[$this.Stay]).attr("rel",$this.LinkUrl[$this.Stay]).fadeTo("fast",0.99);
				}
				img.onload = function() {
					imgThis.attr("src",$this.BigPic[$this.Stay]).attr("rel",$this.LinkUrl[$this.Stay]).fadeTo("fast",0.99);
				};
			} );
		}
		$("#sbox div.sboxes").click( function() {
			clearInterval(mar);
			if ( $(this).attr("rel") != $this.Stay ) {
				GoCross(parseInt($(this).attr("rel")));
			}
			mar = setInterval(GoCross2,$this.Speed * 1000);
		} );
		this.Box.find(".boxbigger").click( function() {
			if ( $(this).attr("rel") != "" ) {
				window.open($(this).attr("rel"));
			}
		} );
	}
};
