$(document).ready(function () {
	setPopup('#release_link', '#rlpopup');
	setPopup('#myblog_link', '#cwpopup');
	setPopup('#manage_link', '#mapopup');
});

function setPopup(linkID, popupID) {
	var popup = $(popupID);
	if (popup.length <= 0) return;

	var link = $(linkID);
	var pos = link.position();
	var h = link.height() - 1;

	var popupclose = function() {
		popup.countdown--;
		if (popup.countdown <= 0) {
			popup.slideUp(150);
		} else {
			setTimeout(popupclose, 350);
		}
	};

	link.mouseover(function() {
		$('.menupopup').slideUp(50);
		popup.css({'top': (pos.top + h) + 'px', 'left': (pos.left + 5) + 'px'}).slideDown('fast');
		popup.countdown = 3;
		popup.timer = setTimeout(popupclose, 350);
	});
	
	popup.mousemove(function() { popup.countdown = 3; });
}

function setInfolink() {
	$('.infolink').click(function() {
		var self = $(this);
		var page = self.attr('href').split('#')[1];
		showInfo(page);
		//self.click( (return function(p){showInfo(p);}(page)); );
	});
}

function getCover(asin, div) {
	if (div.html() == '') $.post('http://www.helloproject.tw/cal.php', {'mode': 'cover', 'asin': asin}, function(info) {
		if (info) {
			var str = '<td valign="top"><a href="'+info['url']+'" target="_blank">';
			str += info['imageM'] ? '<img src="' + info['imageM'] + '" alt="'+info['tag']+'" />' : '<img src="/pics/nowptn.png" alt="Now Printing" />';
			str += '</a></td>';
			str += '封面圖片等資訊均由 <a href="'+ info['url'] +'" target="_blank">';
			str += '<img src="/pics/amz.gif" alt="Amazon.co.jp" style="vertical-align: middle;" /></a> 提供。'
			div.html( str );
		} else {
			div.html('目前沒有封面。');
		}
	}, 'json');
	div.toggle();
}


//function openDialog( box_id, title, main, width, height, box_class, box_show_time, box_top ) {
var Dialog = {

	bound: function() {
		var viewWidth = document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth;
		var viewHeight = window.innerHeight || document.documentElement.clientHeight;
		var viewTop = window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
		var bodyHeight = document.body.clientHeight || document.documentElement.clientHeight;

		return {width: viewWidth, height: viewHeight, top: viewTop, docHeight: bodyHeight < viewHeight ? viewHeight : bodyHeight};
	},

	close: function() {
		$('#dialog_bg').remove();
		$('.dialog_box').remove();
	},

	open: function(title, main, width, height, boxClass, modal) {
		var bound = Dialog.bound();

		var top = (bound.height - height - 50) / 2;
		var left = ( bound.width / 2 ) - ( width / 2 );

		var html  = '<div id="dialog_bg" style="width: ' + bound.width + 'px; height: ' + bound.docHeight + 'px;"></div>';
//			if (! modal) html += 'onclick="Dialog.close();"';
//			html += '></div>';

			html += '<div id="dialog_box" class="dialog_box ' + boxClass +'" style="top:' + top + 'px;left:' + left + 'px;width:' + width + 'px; height: ' + height + 'px">';
			html += '<h4 style="">';
			if (! modal) html += ' <img src="http://images.gamebase.com.tw/etc/sys/xx.gif" class="close" onclick="Dialog.close();">';
			html += title + '</h4><div id="dialog_main" class="dialog_main" >' + main + '</div></div>';

		$(document.body).append(html);
		var obj = $('#dialog_box');
		obj.main = $('#dialog_main');

		obj.close = function (){ Dialog.close(); };
		return obj;
	}
}
