/*
 * スキンサムネイル表示 JavaScript
 *
 * Copyright (c) 2003-2005 DRECOM CO.,LTD. All rights reserved.
 * 
 * info@drecom.co.jp
 * http://www.drecom.co.jp/
 */

/**
 * Namespace object.
 */
function DesignPreview() {
	this.$layer = null;
	this.$cache = null;
}

/**
 * プレビューを表示するレイヤーの取得.
 */
DesignPreview.prototype.getLayer = function() {
	if (this.$layer == null) {
		this.$layer = XBSLayer.makeLayer('blog-design-preview');
	}
	return this.$layer;
}

/**
 * サムネイル画像を読み込む
 */
DesignPreview.prototype.load = function(imgPath) {
	if (this.$cache == null) {
		this.$cache = new Object();
	}
	if (this.$cache[imgPath] != null) {
		return this.$cache[imgPath];
	}
	// preload ...
	this.$cache[imgPath] = new Image();
	this.$cache[imgPath].src = imgPath;
}

/**
 * サムネイル表示の際にサンプル画像をレイヤー表示するための関数
 * 
 * @param aFile ファイルの場所（パス）
 * @param aName スキンの名前
 * @param theEvent イベントオブジェクト
 */
DesignPreview.prototype.preview = function(imgPath, tmplName, theEvent) {
	var x, y;
	var layer = this.getLayer();
	
	if (theEvent == null) {
		theEvent = window.event;
	}
	x = XBSEvent.getMouseX(theEvent);
	y = XBSEvent.getMouseY(theEvent);
	
	layer.setInnerHTML(this.makeHtml(imgPath, tmplName));

	var preview_img = document.getElementsByName("preview_img");
	var img_width;
	
	try {
		img_width = preview_img[0].offsetWidth;
	} catch(e) {
		img_width = 250;
	}
	
	x = (x < 380) ? x + 65 : x - img_width - 100;
	y -= (y < 380) ? 70 : 150;
	
	layer.setLeftTopPosition(x, y);
	layer.setVisible(true);
}

/**
 * プレビューを閉じる。
 * 
 */
DesignPreview.prototype.close = function() {
	if (this.$layer != null) {
		this.$layer.setVisible(false);
	}
}

DesignPreview.prototype.makeHtml = function(imgPath, tmplName) {
	var str = '';
	
	// preload
	this.load(imgPath);
	
	return '<table border="1" cellspacing="0" cellpadding="10" width="150">' + 
	       '<tr><td bgcolor="#ffffff" align="center">'+ 
	       sanitize_msg(tmplName) + '<br><img name="preview_img" src="'+imgPath +
	       '" alt="Now Loading..."><br>' +
	       '</td></tr></table>';

	return str;
}

// -----------------------------------------------------------
// P U B L I C 
// -----------------------------------------------------------
var designPreview = new DesignPreview();

// For compatibility
function preView(imgPath, aName, theEvent) {
	designPreview.preview(imgPath, aName, theEvent);
}
function closepreView() {
	designPreview.close();
}

