var likes = [];

function addLike (shareUrl, buttonId, x, y, container) {
	likes[likes.length] = new Like(shareUrl, buttonId, y, x, document.getElementById("content"));
}

/**
 * likeボタンを配置
 *
 * @param shareUrl {String} - 対象のurl
 * @param buttonId {String} - likeボタンのid
 * @param top {Number} - topの座標
 * @param left {Number} - leftの座標
 * @param container {Object} - 配置するオブジェクト
 */
function Like(shareUrl, buttonId, top, left, container) {
	var me = this,
		old = document.getElementById(buttonId);
	
	//同じidのiframeが存在した場合は消す
	if(old && old.tagName.match(/iframe/i)) {
		$(old).remove();
	}

	this.button = this.createButton(shareUrl || document.location.href, container || document.body);	//配置されたボタン。実態はLikeボタンのiframe

	//styleの設定
	this.button.style.position = "absolute";
	this.button.style.top = top + "px";
	this.button.style.left = left + "px";
	this.button.id = buttonId;	//idを付加

	this.disappear(0);
	setTimeout(function () {
		me.appear(1000);
	}, 0);
}

Like.prototype = {

	isAppear: false,	//表示中か

	
	/**
	 * ボタンを生成する
	 *
	 * @param url {String} - 共有するurl
	 * @return 配置されたlikeボタン
	 */
	createButton: function(url, container) {
		var button = document.createElement("iframe");
		button.src = "http://www.facebook.com/plugins/like.php?href=" + encodeURIComponent(url || document.location.href) + "&layout=button_count&show_faces=false&width=110&height=20&action=like";
		button.allowtransparency = true;
		button.style.border = "medium none";
		button.style.overflow = "hidden";
		button.style.width = "110px";
		button.style.height = "20px";
		button.frameBorder = "0";
		button.scrolling = "no";

		//divで囲ってもいいかも
//		var button = createElement("<iframe src=\"http://www.facebook.com/plugins/like.php?href=" + encodeURIComponent(url || document.location.href) + "&layout=button_count&show_faces=false&width=110&height=20&action=like\" allowtransparency=\"true\" style=\"border: medium none ; overflow: hidden; width: 110px; height:20px;\" frameborder=\"0\" scrolling=\"no\"></iframe>");
		container.appendChild(button);
		return button;	//オブジェクトを返す
	},

	/**
	 * 登場
	 *
	 * @param time {Number} - 効果時間
	 * @param callback {Function} - コールバック
	 */
	appear: function (time, callback) {
		this.isAppear = true;

		$(this.button)
			.stop(true, true)
			.fadeTo(time || 1000, 1, function () {
				if(callback) {
					callback.apply(this);
				}
			});
	},

	/**
	 * 退場
	 *
	 * @param time {Number} - 効果時間
	 * @param callback {Function} - コールバック
	 */
	disappear: function (time, callback) {
		this.isAppear = false;
		
		$(this.button)
			.stop(true, true)
			.fadeTo(time || 1000, 0, function () {
				if(callback) {
					callback.apply(this);
				}
			});
	},

	/**
	 * 退場後に要素を削除
	 *
	 * @param time {Number} - 効果時間
	 * @param callback {Function} - コールバック
	 */
	removeTo: function (time, callback) {
		if(this.isAppear === true) {
			this.isAppear = false;
			this.disappear(time, function () {
				if(callback) {
					callback.apply(this);
				}
				$(this).remove();
			});
		}
	}
};


//まとめてlikeボタンを消すような関数を用意しておく

/**
 * likeボタンをすべて消す
 *
 * @param time {Number} - 効果時間
 */
function removeLikeAll (time) {
	for(var i = 0, len = likes.length; i < len; i++) {
		var btn = likes[i];

		if(btn.removeTo) {
			btn.removeTo(time);
		}
	}
}
