﻿//==============================================
//Create By: Ruby Gao (Gao Xiang)
//Create Date: 2009-07-28
//Description: Rotate flash on banner.
//==============================================

function SwfPlayer(showid,urlArr){
    this.Width = 0;
    this.Height = 0;
    this.Index = 0;
    this.Length = urlArr.length - 1;
    this.ShowID = showid;
    this.UrlArr = urlArr;
}

SwfPlayer.prototype.playSwf = function(x){
    var strBanner = '';
    var re = /^(.*),(.*),(.*)$/g;
    strBanner = '<object id="'+ this.ShowID + '_banner" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + this.Width + '" height="' + this.Height + '">\
    <param name="movie" value="' + this.UrlArr[x].replace(re, '$1') + '">\
    <param name="quality" value="high">\
    <param name="LOOP" value="true">\
    <embed name=banner src="'+ this.UrlArr[x].replace(re, '$1') + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + this.Width + '" height="' + this.Height + '"></embed>\
    </object>';

    document.getElementById(this.ShowID).innerHTML = strBanner;
}

SwfPlayer.prototype.nextMedia = function(){
    this.Index < this.Length ? this.Index++ : this.Index = 0
    this.playSwf(this.Index);
}

SwfPlayer.prototype.changeUrl = function(){

    var swfObj = document.getElementById(this.ShowID + '_banner');
    var currIndex = -1;
    var total = 0;
    
    try {
        currIndex = swfObj.CurrentFrame();
        total = swfObj.TotalFrames;
    }
    catch (e) {
    }
    
    if(currIndex >= total - 1) {
        var _this = this;
        var callback = function() {
            _this.nextMedia();
        };
        
        //Update By Ruby on 2009-08-12
        if (total <= 10) {
            setTimeout(callback, 10000);
        }
        else
            setTimeout(callback, 5000);
    }
}

SwfPlayer.prototype.applyInterval = function(){
    var _this = this;
    var callback = function() {
        _this.changeUrl();
    };
    setInterval(callback, 5000);//Update By Ruby on 2009-08-12
}

SwfPlayer.prototype.show = function(width, height){
    this.Width = width;
    this.Height = height;
    this.playSwf(0);
    if (this.UrlArr != null && this.UrlArr.length > 1) {
        this.applyInterval();
    }
}