/*
 *
 * Taggy Video Embed JavaScript Program
 * TAGGYビデオ従軍記者
 *
 */

var _Taggy  = _Taggy || function () {};
var _Util   = _Util  || function () {};
var _Event  = _Event || function () {};
var _Video  = _Video || function () {};
var _Embed  = _Embed || function (mainVideoId, videoIdList, options) {

	this.protocol = 'http:';
	this.hostname = 'taggy.jp';
	this.port = '80';

	this.host = this.hostname + ':' + this.port;
	this.baseURI = this.protocol + '//' + this.host

	this.jsonPathName = '/videoprevs/jsonp.do';
	this.imagesPathName = '/images/embed';

	this.cssPathName = '/css';
	this.cssFileName = 'taggy_video_embed.css';

	this.mainVideoId = mainVideoId;
	this.videoIdList = videoIdList;
	this.videoParameters = null;
	this.options = options || {};
	this.sizeSmall = (this.options && this.options.size == 'small') ? true : false;

	this.box = null;
	this.boxId = 'taggy_videoembed_box_' + this.mainVideoId;
	this.headElement = null;

};
var _Browser = _Browser || {
	IE:     !!(window.attachEvent && !window.opera),
	Opera:  !!window.opera,
	WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
	Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
};

/*
 * Util
 */

_Util.prototype.getElementsBySelector = function (selector, parentObj) {

	var obj = (parentObj) ? parentObj : document;
	var tagName = null;
	var className = null;

	if (selector.match(/^(\w*)\.?(\w*)$/)) {
		tagName = RegExp.$1;
		className = RegExp.$2;
	}

	var elements = (tagName) ? obj.getElementsByTagName(tagName) : obj.childNodes;

	if (!className) return elements;

	var classNameRegex = new RegExp('\\b' + className + '\\b');
	var matches = [];

	for (var i = 0; i < elements.length; ++i) {
		if (elements[i].className.match(classNameRegex)) {
			matches[matches.length] = elements[i];
		}
	}

	return matches;

};
_Util.prototype.createLink = function (attribute) {
	var _link = document.createElement('LINK');
	_link.rel = attribute.rel;
	_link.type = attribute.type;
	_link.href = attribute.href;
	_link.media = attribute.media;
	return _link;
};
_Util.prototype.createScript = function (attribute) {
	var _script = document.createElement('SCRIPT');
	_script.type = attribute.type;
	_script.charset = attribute.charset;
	_script.src = attribute.src;
	return _script;
};
_Util.prototype.createEmbed = function (attribute) {
	var _embed = document.createElement('EMBED');
	_embed.setAttribute('src', attribute.url);
	_embed.setAttribute('quality', 'high');
	_embed.setAttribute('bgcolor', '#ffffff');
	_embed.setAttribute('width', attribute.width);
	_embed.setAttribute('height', attribute.height);
	_embed.setAttribute('name', 'root');
	_embed.setAttribute('id', 'root');
	_embed.setAttribute('scale', 'showall');
	_embed.setAttribute('align', 'middle');
	_embed.setAttribute('allowScriptAccess', 'sameDomain');
	_embed.setAttribute('type', attribute.type);
	_embed.setAttribute('pluginspage', attribute.plugin);
	_embed.setAttribute('flashvars', attribute.item);
	return _embed;
};
_Util.prototype.makeEmbed = function (attribute) {
	var _embed =
		'<embed' +
		' rel="' + attribute.rel + '"' +
		' src="' + attribute.src + '"' +
		' type="' + attribute.type + '"' +
		' width="' + attribute.width + '"' +
		' height="' + attribute.height + '"' +
		' flashvars="' + attribute.flashvars + '"' +
		' pluginspage="' + attribute.pluginspage + '"' +
		//' id="root"' +
		//' name="root"' +
		//' id="taggy_videoembed_' + attribute.id + '"' +
		//' name="taggy_videoembed_' + attribute.id + '"' +
		' align="middle"' +
		' scale="showall"' +
		' quality="high"' +
		' bgcolor="#ffffff"' +
		' allowScriptAccess="sameDomain"' +
		' />';
	return _embed
};
_Util.prototype.serialize = function (parameters) {
	var list = [];
	for (var key in parameters) {

		if (typeof parameters[key] == 'object') {
			for (var i = 0; i < parameters[key].length; ++i) {
				list[list.length] = key + '=' + encodeURIComponent(parameters[key][i]);
			}
			continue;
		}

		list[list.length] = key + '=' + encodeURIComponent(parameters[key]);

	}
	return list.join('&');
};

/*
 * Event
 */

_Event.prototype.observe = function (element, name, observer, useCaputure) {
	if (window.addEventListener) {
		element.addEventListener(name, observer, useCaputure);
	}
	else if (window.attachEvent) {
		element.attachEvent('on' + name, observer);
	}
};

/*
 * Video
 */

_Video.prototype.initialize = function (videoIdList, options) {

	var mainVideoId = videoIdList[0];

	if (!this.idList) this.idList = [];
	if (this.idList[mainVideoId]) return;

	this.idList[mainVideoId] = true;

	eval('_Video.prototype.Embed' + mainVideoId + ' = new _Embed(mainVideoId, videoIdList, options);');
	eval('this.Embed' + mainVideoId + '.initialize();');

};

/*
 * Embed
 */

_Embed.prototype.initialize = function () {

	this.headElement = document.getElementsByTagName('HEAD')[0];

	if (this.setBox()) {
		this.setCss();
		this.setJson();
	}

};
_Embed.prototype.setBox = function () {

	this.box = document.getElementById(this.boxId);

	var _boxId = (this.box) ? '' : this.boxId;
	var _title = (this.box) ? '同じ動画を貼り付けることは出来ません。' : '';
	var _detailURI = this.baseURI + '/article/detail.do?media=video&id=' + this.videoIdList[0];
	var _smallClassName = (this.sizeSmall) ? 'taggy_videoembed_small' : '';

	document.write(
		'<div id="' + _boxId + '" class="taggy_videoembed ' + _smallClassName + '">' +
			'<div class="taggy_videoembed_title">' + _title + '</div>' +
			'<div class="taggy_videoembed_source">' +
				//'<embed src="http://vm2-2.vision.ameba.jp/mcb.swf?id=XUs52qg75nW:bhac:No:bf:x67l5txc/A1BIf7/xmLekgcTNrEKPL:4BxF:35:5S:eZIBwBTEVi9C4sWlJUdA7x" quality="high" bgcolor="#FFFFFF" menu="false" width="328" height="298" name="root" id="root" scale="showall" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="" style="display: none;" />' +
			'</div>' +
			'<div class="taggy_videoembed_relation_title"></div>' +
			'<ul class="taggy_videoembed_relation_video"></ul>' +
			'<div class="taggy_videoembed_footer">' +
				'<a href="' + _detailURI + '&from=videoembed" class="taggy_videoembed_relation_more">関連する動画やブログをもっと見る</a>' +
				'<a href="' + this.baseURI + '?from=videoembed" target="_blank">' + 
					'<img src="' + this.baseURI + this.imagesPathName + '/logo_videoembed.gif" width="107" height="16" alt="generated by TAGGY" title="generated by TAGGY" />' +
				'</a>' +
			'</div>' +
		'</div>'
	);

	return (this.box) ? false : true;

};
_Embed.prototype.setCss = function () {

	var _href = this.baseURI + this.cssPathName + '/' + this.cssFileName;
	var _links = this.headElement.getElementsByTagName('LINK');

	for (var i = 0; i < _links.length; ++i) {
		if (_href == _links[i].href) return;
	}

	var _link = Taggy.Util.createLink(
		{
			rel : 'stylesheet',
			type : 'text/css',
			media : 'all',
			href : _href
		}
	);

	this.headElement.appendChild(_link);

};
_Embed.prototype.setJson = function () {

	var _script = Taggy.Util.createScript(
		{
			type : 'text/javascript',
			charset : 'utf-8',
			src : this.baseURI + this.jsonPathName + '?' + Taggy.Util.serialize(
				{
					id : this.videoIdList,
					callback : 'Taggy.Video.Embed' + this.mainVideoId +'.render',
					media : 'video',
					encoding : 'utf-8'
				}
			)
		}
	);

	this.headElement.appendChild(_script);

};
_Embed.prototype.setVideoParameters = function (parameters) {

	if (!this.videoParameters) this.videoParameters = {};

	for (var key in parameters) {
		if (!key) continue;
		this.videoParameters[key] = parameters[key];
	}

};
_Embed.prototype.render = function (parameters) {

	this.box = document.getElementById(this.boxId);
	this.setVideoParameters(parameters);

	var _titleBox = Taggy.Util.getElementsBySelector('div.taggy_videoembed_title', this.box)[0];
	var _sourceBox = Taggy.Util.getElementsBySelector('div.taggy_videoembed_source', this.box)[0];
	var _relationTitleBox = Taggy.Util.getElementsBySelector('div.taggy_videoembed_relation_title', this.box)[0];
	var _relationVideoBox = Taggy.Util.getElementsBySelector('ul.taggy_videoembed_relation_video', this.box)[0];

	_titleBox.innerHTML = this.videoParameters[this.mainVideoId].title;
	_relationTitleBox.innerHTML = this.videoParameters[this.mainVideoId].title + 'に関する動画';

	var _onerror = "this.src='" + this.baseURI + this.imagesPathName + '/noimage_videoembed_' + (this.sizeSmall ? 'small.gif' : 'medium.gif') + "';";
	var _imageHeight = (this.sizeSmall) ? '38' : '43';
	var _attr = this.videoParameters[this.mainVideoId];
	_sourceBox.innerHTML = (_attr.prevType == 'flash') ?
	Taggy.Util.makeEmbed(
		{
			rel : this.mainVideoId,
			src : _attr.url,
			type : _attr.type,
			width : (this.sizeSmall) ? 328 : _attr.width,
			height : (this.sizeSmall) ? 328 * _attr.height / _attr.width : _attr.height,
			flashvars : _attr.item,
			pluginspage : _attr.plugin
		}
	) :
	'<a href="' + _attr.link + '" target="_blank" title="' + _attr.title + 'を見る"><img src="' + _attr.relation + '" /></a>';

	for (var i = 0; i < this.videoIdList.length; ++i) {
		if (!this.videoIdList[i]) continue;
		var key = this.videoIdList[i];
		var _param = this.videoParameters[key];
		var _src = _param.relation;
		var _alt = _param.title;
		var _thumbnail = '<img src="' + _src + '" alt="' + _alt + '" title="' + _alt + '" height="' + _imageHeight + '" onerror="' + _onerror + '" />';
		_relationVideoBox.innerHTML +=
			'<li class="' + (i == 0 ? 'onplay' : '') + '">' +
			'<a href="javascript: void(1);" onclick="Taggy.Video.Embed' + this.mainVideoId + '.visible(' + key + ');">' + _thumbnail + '</a></li>';
	}

};
_Embed.prototype.visible = function (videoId) {

	var _target = window.event.srcElement || window.event.target;
	var _current = _target.parentNode.parentNode;

	var _titleBox = Taggy.Util.getElementsBySelector('div.taggy_videoembed_title', this.box)[0];
	var _sourceBox = Taggy.Util.getElementsBySelector('div.taggy_videoembed_source', this.box)[0];
	var _relationVideoBox = Taggy.Util.getElementsBySelector('ul.taggy_videoembed_relation_video', this.box)[0];

	var _embedObj = null;
	var _embedAttr = this.videoParameters[videoId];

	for (var i = 0; i < _relationVideoBox.childNodes.length; ++i) {
		var _li = _relationVideoBox.childNodes[i];
		_li.className = '';
	}

	_titleBox.innerHTML = _embedAttr.title;
	_sourceBox.innerHTML = (_embedAttr.prevType == 'flash') ?
	Taggy.Util.makeEmbed(
		{
			rel : videoId,
			src : _embedAttr.url,
			type : _embedAttr.type,
			width : (this.sizeSmall) ? 328 : _embedAttr.width,
			height : (this.sizeSmall) ? 328 * _embedAttr.height / _embedAttr.width : _embedAttr.height,
			flashvars : _embedAttr.item,
			pluginspage : _embedAttr.plugin
		}
	) :
	'<a href="' + _embedAttr.link + '" target="_blank" title="' + _embedAttr.title + 'を見る"><img src="' + _embedAttr.relation + '" /></a>';

	_current.className = 'onplay';

};

/*
 * Taggy
 */

var Taggy = Taggy || new _Taggy();

_Taggy.prototype.Util    = _Taggy.prototype.Util    || new _Util();
_Taggy.prototype.Video   = _Taggy.prototype.Video   || new _Video();
_Taggy.prototype.Event   = _Taggy.prototype.Event   || new _Event();
_Taggy.prototype.Browser = _Taggy.prototype.Browser || _Browser;
_Video.prototype.Embed   = _Video.prototype.Embed   || new _Embed();

if (Taggy.Browser.Gecko) Taggy.Event.observe(document, 'click', function (e) { window.event = e; }, true);
