//名前空間の登録
Ext.namespace("buyersnet.image");

/**
 * 画像クラス
 * 画像処理を行なうのに必要な関数を定義しています。
 */
buyersnet.image = function(){
	
	/**
	 * ウィンドウサイズ (幅)
	 */
	var window_width  = 480;
	
	/**
	 * ウィンドウサイズ (高さ)
	 */
	var window_height = 420;
	
	/**
	 * 画像オブジェクト
	 */
	var img;
	
	return {
		
		/**
		 * 画像を表示
		 * @param {string} src 画像パス
		 * @return 画像表示できたかどうか
		 */
		displayImage : function(src) {
			// 画像のパスが設定されていない。
			if (src == null || src == "") {
				return false;
			}
			
			if(img == undefined) {
				//
			} else {
				img = undefined;
			}
			
			img = new Image();
			img.src = src;
			
			if (img.complete) {
				if (img.src != src) {
					//
				} else {
					// 既に読み込み済み
					buyersnet.image.createWindow();
					return true;
				}
			}
			
			// 画像ロード
			img.onload = function(){
				buyersnet.image.createWindow();
			}
			
			return true;
		},
		
		/**
		 * 画像のリサイズ処理
		 * @param {Image} orgImg 元画像 
		 * @param {int} dstWidth 表示幅
		 * @param {int} dstHeight 表示高さ
		 * @return リサイズ後の画像
		 */
		resizeImage : function(orgImg, dstWidth, dstHeight) {
		
			// 元画像のサイズ
			var orgImgWidht  = orgImg.width;
			var orgImgHeight = orgImg.height;
			var scaleRatio;
			// 画像の幅・高さどちらを基準にするか
			if (orgImgWidht < orgImgHeight) {
				// 幅 < 高さ
				scaleRatio    = dstHeight / orgImgHeight;
			} else if (orgImgHeight < orgImgWidht) {
				// 高さ < 幅
				scaleRatio    = dstWidth / orgImgWidht;
			} else {
				// 高さ = 幅
				// 画面の表示高さが表示幅より小さいので強制的に高さにあわせる
				scaleRatio    = dstHeight / orgImg.height;
			}
			orgImg.width  = orgImgWidht * scaleRatio;
			orgImg.height = orgImgHeight * scaleRatio;
			return orgImg;
		},
		
		/**
		 * ウィンドウの作成、HTML書き出し
		 */
		createWindow : function() {
			var pop_win = window.open("../../../common/popupImage.html?newwindow=true", "", "width=" + window_width + ", height=" + window_height + ", scrollbars=no, resizable=no");
			var dstImg = buyersnet.image.resizeImage(img, window_width, window_height);
			
			pop_win.window.document.open();
			
			// HTMLを書き出す。
			var html = "";
			html += '<?xml version="1.0" encoding="UTF-8"?>';
			html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
			html += '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:te="http://www.seasar.org/teeda/extension" xml:lang="ja" lang="ja">';
			html += '<head>';
			html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			html += '<title>～TOCバイヤーズネット～</title>';
			html += '</head>';
			html += '<body style="margin:0;padding:0;border:0;">';
			
			html += '<table border="0" cellpadding="0" cellspacing="0" width="480" height="420">';
			html += '<tr valign="middle">';
			html += '<td align="center">';
			html += '<img src="' + dstImg.src + '" width="' + dstImg.width + '" height="' + dstImg.height + '" />';
			html += '</td>';
			html += '</tr>';
			html += '</table>';
			
			html += '</body>';
			html += '</html>';
			pop_win.window.document.write(html);
			pop_win.window.document.close();
		}
		
	};
}();

