/**
 * =============================================================
 *  movie.js
 * =============================================================
 * @package		javascript
 * @author		sYoshioka
 * @copyright	COPYRIGHT (C) 2009 JCity Inc., ALL RIGHTS RESERVED.
 * @version		$Id$
 */

var movie = {
	scriptHome: null,
	preLoad: function(){
		var scripts = document.getElementsByTagName("script");
		var current;

		if(scripts){
			if(current = scripts[scripts.length - 1]){
				this.scriptHome = current.src.match(/.*\//g);
				this.scriptHome = this.scriptHome ? this.scriptHome : "./";
			}
		}

		if(this.scriptHome){
			var script = document.createElement("script");

			script.type = "text/javascript";
			script.src = this.scriptHome + "adapter/swfobject.js?" + (new Date().getTime());

			return document.getElementsByTagName("head").item(0).appendChild(script) ? true : false;
		}

		return false;
	},
	load: function(options){
		if(!options) return false;

		var scriptHome = this.scriptHome;
		var getObject = this.getObject;

		this.addEvent(window, "load", function(){
			if(getObject(options.containerId)){
				options.height = options.controller ? options.height + 20 : options.height;

				var movie = new SWFObject(scriptHome + "adapter/mediaplayer.swf", options.containerId, options.width, options.height, "8");
				var moviePath = "../../";

				movie.addParam("allowfullscreen", true);
				movie.addParam("quality", "high");
				movie.addParam("base", ".");

				movie.addVariable("autostart", options.autostart);
				movie.addVariable("shownavigation", options.controller);
				movie.addVariable("usefullscreen", options.usefullscreen);
				movie.addVariable("width", options.width);
				movie.addVariable("height", options.height);
				movie.addVariable("file", moviePath + options.flvPath);
				movie.addVariable("repeat", options.repeat ? true : false);
				movie.addVariable("volume", options.volume ? options.volume : 0);

				if(!options.autostart && options.thumbnailPath){
					movie.addVariable("image", moviePath + options.thumbnailPath + "?" + (new Date().getTime()));
				}

				movie.write(options.containerId);
			}
		});

		return true;
	},
	getObject: function(objectId){
		return document.all ? document.all(objectId) : document.getElementById ? document.getElementById(objectId) : document.layers[objectId];
	},
	addEvent: function(target, eventName, fn){
		eventName = eventName.toLowerCase();

		if(target.attachEvent){
			target.attachEvent("on" + eventName, fn);
		}else if(target.addEventListener){
			target.addEventListener(eventName, fn, false);
		}else{
			target["on" + eventName] = target["e" + eventName + addObject];
		}
	}
}

if(movie.preLoad()){
	movie.load({
		flvPath: "movies/toppage.flv",
		thumbnailPath: "movies/thumbnails/toppage.jpg",
		containerId: "movie",
		width: 210,
		height: 157,
		autostart: true,
		usefullscreen: false,
		controller: true,
		repeat: true,
		volume: 60
	});
}
