var action = function(observer) {
	var s = document.getElementsByTagName('script');
	var index = s.length - 1;
	observer._element = s[index];
	observer._element.id = 'action-script-' + index;
	action.observers[index] = observer;
	return index;
};
action.observers = [];

var JSAction = function(id, length, execer, releaser) {
	action(this);
	this.count = 0;
	this.length = length;
	this.id = 'js' + id;
	document.write('\
	 <div class="js-container">\
	  <div class="js" id="' + this.id + '" style="width: 13.1em; height: 13.1em"></div>\
	 </div>');
	this.element = document.getElementById(this.id);
	this.execer = execer;
	this.releaser = releaser;
	this.finishShow = this.startShow = this.finishHide;
};
JSAction.prototype = {
	finishHide: function() {
		this.count = 0;
		this.releaser(this.element);
	},
	startHide: function() {
		if (this.count < this.length) {
			this.execer(this.element, this.count);
			this.count++;
			return false;
		}
		else {
			return true;
		}
	}
};

var SwfAction = function(id, width, height) {
	var index = action(this);
	document.write('\
		<div class="swf-container" id="swfContainer' + id + '">\
		 <div class="swf-dummy" style="width: ' + (width * 0.131) +  'em; height: ' + (height * 0.131) +  'em" id="swfDummy' + id + '"><a class="not-important" onclick="action.observers[' + index + '].swap()" href="javascript:void(0)">Click and load swf.</a></div>\
		</div>\
	');

    this.started = false;
    this.playing = false;
    this.first = true;
    this.container = document.getElementById('swfContainer' + id);
    this.dummy = document.getElementById('swfDummy' + id);
	this.id = id;
	this.width = width;
	this.height = height;
	action(this);

	this.startShow = this.finishHide = this.finishShow;
}
SwfAction.prototype = {
	swap: function() {
				this.dummy.style.display = 'none';
				this.container.className = 'swf-container playing';
				if (/MSIE/.test(navigator.userAgent)) {
					this.container.innerHTML += '<object id="swf' + this.id + '" data="sample/' + this.id + '.swf" class="swf" style="width: ' + (this.width * 0.131) + 'em; height:' + (this.height * 0.131) + 'em" type="application/x-shockwave-flash">\
				     <param name="movie" value="sample/' + this.id + '.swf" />\
					 <param name="play" value="false" />\
					 <param name="loop" value="false" />\
					 <param name="quality" value="low" />\
					 <param name="allowScriptAccess" value="sameDomain" />\
					</object>';
    				this.element = document.getElementById('swf' + this.id);
				}
				else {
					var swf = document.createElement('object');
					swf.type = 'application/x-shockwave-flash';
					swf.id = 'swf' + this.id;
					swf.data = 'sample/' + this.id + '.swf';
					swf.style.width = this.width * 0.131 + 'em';
					swf.style.height = this.height * 0.131 + 'em';
					swf.style.background = 'white';
					swf.className = 'swf';
					var params = {
					movie: 'sample/' + this.id + '.swf',
						play: 'false',
						loop: 'false',
						quality: 'low',
						allowScriptAccess: 'sameDomain'
					};
					for (var n in params) {
						var p = document.createElement('param');
						p.setAttribute('name', n);
						p.setAttribute('value', params[n]);
						swf.appendChild(p);
					}
					this.container.appendChild(swf);
    				this.element = swf;
				}


	},
    finishShow: function() {
		this.container.className = 'swf-container';
		if (!this.dummy.parentNode) {
			this.container.appendChild(this.dummy);
		}
		this.dummy.style.display = '';
		if(this.element) {
			this.container.removeChild(this.element);
			this.element = null;
		}
        this.started = false;
        this.playing = false;
    },
    startHide: function() {
        if (this.started || !canSwfStart) {
			this.finishShow();
			return true;
		}
        try {
            if (!this.playing) {
				this.swap();

                this.playing = true;
                var self = this;
                setTimeout(function() {
                    self.first = false;
                    self.started = true;
                }, this.first ? 500 : 1);
            }
        }
        catch (e) {
            if (canSwfStart)
                alert(e.message);
            canSwfStart = false;
        }
        return false;
    }
};
var canSwfStart = true;

var SwfActionEx = function(id, width, height, length) {
	var a = new SwfAction(id, width, height);
	a.length = length;
	a.count = 0;
	a.startShow = a.finishShow = a.finishHide = function() {
		this.count = 0;
		return SwfAction.prototype.finishShow.apply(this);
	}
	a.startHide = function() {
		if(this.started) {
			if (this.count < this.length) {

				document.getElementById('swf' + id)['click_' + this.count++]();
				return false;
			}
		}
		else {
			return SwfAction.prototype.startHide.apply(this);
		}
	};
	return a;
};

if(/^mac/i.test(navigator.platform)) {
	document.documentElement.className = 'mac';
}
