1111111111111

var SWFPuzzle;

function string()
{
    var list = new Array();
    for(var i=0; i<arguments.length; i++)
        list.push(arguments[i])
    return list.join("");
}

if (SWFPuzzle == undefined) {
 SWFPuzzle = function (settings) {
  this.initSWFPuzzle(settings);
 };
}

SWFPuzzle.prototype.initSWFPuzzle = function (settings) {
 try {
  this.settings = settings;
  this.eventQueue = [];
  this.movieName = "SWFPuzzle";
  this.movieElement = null;

  SWFPuzzle.instances[this.movieName] = this;

  this.initSettings();
  this.loadFlash();
 } catch (ex) {
  delete SWFPuzzle.instances[this.movieName];
  throw ex;
 }
};

SWFPuzzle.instances = {};
SWFPuzzle.version = "2.0.0 2009-06-29";

SWFPuzzle.WINDOW_MODE = {
 WINDOW : "window",
 TRANSPARENT : "transparent",
 OPAQUE : "opaque"
};


SWFPuzzle.completeURL = function(url) {
 if (typeof(url) !== "string" || url.match(/^http?:\/\//i) || url.match(/^\//)) {
  return url;
 }
 
 var indexSlash = window.location.pathname.lastIndexOf("/");
 if (indexSlash <= 0) {
  path = "/";
 } else {
  path = window.location.pathname.substr(0, indexSlash) + "/";
 }
 
 return path + url;
 
};

//初始化設置
SWFPuzzle.prototype.initSettings = function () {
 this.ensureDefault = function (settingName, defaultValue) {
  this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
 };
 
 this.ensureDefault("FlashUrl", "swfpuzzle.swf");
 this.ensureDefault("ImageUrl", "");
 this.ensureDefault("PlaceHolderID", "");
 this.ensureDefault("PlaceHolder", null); 
 this.ensureDefault("MinWidth", 200);
 this.ensureDefault("MinHeight", 200);
 this.ensureDefault("MaxWidth", 8000);
 this.ensureDefault("MaxHeight", 8000); 
 this.ensureDefault("SceneWidth", 200);
 this.ensureDefault("SceneHeight", 200);
 this.ensureDefault("PuzzleX", 0); 
 this.ensureDefault("PuzzleY", 0); 
 this.ensureDefault("PuzzleRows", 3);
 this.ensureDefault("PuzzleCols", 3);
 this.ensureDefault("WindowMode", SWFPuzzle.WINDOW_MODE.WINDOW);
 
 this.settings.ImageUrl = SWFPuzzle.completeURL(this.settings.ImageUrl);
 
 delete this.ensureDefault;
};


//加載Flash
SWFPuzzle.prototype.loadFlash = function () {
 var targetElement, tempParent;

 if (document.getElementById(this.movieName) !== null) {
  throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
 }

 targetElement = document.getElementById(this.settings.PlaceHolderID) || this.settings.PlaceHolder;

 if (targetElement == undefined) {
  throw "Could not find the placeholder element: " + this.settings.PlaceHolderID;
 }

 tempParent = document.createElement("div");
 tempParent.innerHTML = this.getFlashHTML(); 
 targetElement.parentNode.replaceChild(tempParent.firstChild, targetElement);

 if (window[this.movieName] == undefined) {
  window[this.movieName] = this.getMovieElement();
 } 
};

//生成加載Flash的Html
SWFPuzzle.prototype.getFlashHTML = function () {
 return ['<object id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.FlashUrl, '" width="', this.settings.SceneWidth, '" height="', this.settings.SceneHeight, '" class="swfpuzzle">',
  '<param name="wmode" value="', this.settings.WindowMode, '" />',
  '<param name="movie" value="', this.settings.FlashUrl, '" />',
  '<param name="quality" value="high" />',
  '<param name="menu" value="false" />',
  '<param name="allowScriptAccess" value="always" />',
  '<param name="flashvars" value="' + this.getFlashVars() + '" />',
  '</object>'].join("");
};

//取Flash參數
SWFPuzzle.prototype.getFlashVars = function () {
 return ["movieName=", encodeURIComponent(this.movieName),
   "&ImageUrl=", encodeURIComponent(this.settings.ImageUrl),
   "&MinWidth=", encodeURIComponent(this.settings.MinWidth),
   "&MinHeight=", encodeURIComponent(this.settings.MinHeight),
   "&MaxWidth=", encodeURIComponent(this.settings.MaxWidth),
   "&MaxHeight=", encodeURIComponent(this.settings.MaxHeight),
   "&PuzzleX=", encodeURIComponent(this.settings.PuzzleX),
   "&PuzzleY=", encodeURIComponent(this.settings.PuzzleY),
   "&PuzzleRows=", encodeURIComponent(this.settings.PuzzleRows),
   "&PuzzleCols=", encodeURIComponent(this.settings.PuzzleCols)
  ].join("");
};

//取得影片對象
SWFPuzzle.prototype.getMovieElement = function () {
 if (this.movieElement == undefined) {
  this.movieElement = document.getElementById(this.movieName);
 }

 if (this.movieElement === null) {
  throw "Could not find Flash element";
 }
 
 return this.movieElement;
};


//設置舞臺的最大尺寸
SWFPuzzle.prototype.setMaxSize = function(width, height) {
    if (width != null && height != null)
     this.callFlash("setMaxSize", [width, height]);
}

//設置舞臺的最小尺寸
SWFPuzzle.prototype.setMinSize = function(width, height) {
    if (width != null && height != null)
     this.callFlash("setMinSize", [width, height]);
}

//設置拼圖的尺寸
SWFPuzzle.prototype.setPuzzleSize = function(width, height) {
    if (width != null && height != null)
     this.callFlash("setPuzzleSize", [width, height]);
}

//設置Flash的圖像
SWFPuzzle.prototype.setImageUrl = function (url) {
    if (url != null)
     this.callFlash("setImageUrl", [url]);
};

//設置Flash圖像的位置
SWFPuzzle.prototype.setPosition = function (x, y) {
    if (x != null && y != null)
     this.callFlash("setPosition", [x, y]);
};

//設置拼圖行和列數
SWFPuzzle.prototype.setRowsAndCols = function(rows, cols) {
    if (rows != null && cols != null)
        this.callFlash("setRowsAndCols", [rows, cols]);
};

//重新進行生成拼圖塊
SWFPuzzle.prototype.setCutBitmap = function() {
    this.callFlash("setCutBitmap");
};

//移除拼圖效果
SWFPuzzle.prototype.removePuzzle = function() {
    this.callFlash("removeAllPiece");
};


//調用Flash的AS中的方法
SWFPuzzle.prototype.callFlash = function (functionName, args) {
 args = args || [];
 //debugger
 var movieElement = this.getMovieElement();
 var returnValue, returnString;

 try {
     returnString = movieElement.CallFunction(string('<invoke name="', functionName, '" returntype="javascript">', __flash__argumentsToXML(args, 0), '</invoke>'));
  returnValue = eval(returnString);

 } catch (ex) {
  throw string("Call to ", functionName, " failed");
 }
 
 if (returnValue != undefined && typeof returnValue.post === "object") {
  returnValue = this.unescapeFilePostParams(returnValue);
 }

 return returnValue;
};

你可能感兴趣的:(JavaScript,prototype,Flash)