//isIE =(document.all)?1:0;

//判斷瀏覽器
isIE = 0;
isIE6 = 0;
isIE7 = 0;
isFF = 0;
if (window.XMLHttpRequest){ //Mozilla, Safari,...IE7
	if(!window.ActiveXObject){ // Mozilla, Safari,...
		isFF = 1;
	}else{   
		isIE = 1;
		isIE7 = 1;
	}
}else{   
	isIE = 1;
	isIE6 = 1;
}  

function AddFavority(title, url){ //加入最愛
	if (document.all) 
		window.external.AddFavorite(url, title); 
	else if (window.sidebar) 
		window.sidebar.addPanel(title, url, "") 
} 

function AddBrowerIndexPage(url, obj){ //設為首頁
	if( isIE ){
		obj.style.behavior = "url(#default#homepage)";
		obj.setHomePage(url);
	}else{
		var txt = "1. 將滑鼠移至網址列後，拖曳網址列上的圖片至瀏覽器\"房屋\"圖示上.\n";
		txt += "2. 從彈跳的視窗中，選擇 \"是\"\n";
		txt += "3. 恭喜，你已完成設定！";
		alert(txt);
	}
}  

function getFormObj(frm){ //取得表單物件
	var x = false;
	if( document.getElementById(frm) ){
		x = document.getElementById(frm);
	}else if( document.eval(frm) ){
		x = eval("document." + frm);
	}
	return x;
}

function SetFormSelectSelected(frm, obj_name, val){ //設定 Select-obj物件 selected 該值
	if( typeof( frm ) == "string" )
		var x = getFormObj(frm);
	else
		var x = frm;
	if( x ){
		//var obj = eval("x." + obj_name);
		var obj;
		for(var i=0; i < x.elements.length; i++ ){
			if( x.elements[i].name == obj_name ){
				obj = x.elements[i];
				break;
			}
		}

		if( obj ){
			if( obj.options && obj.options.length > 0 ){
				for(var i=0; i < obj.options.length; i++){	
					if( obj.options[i].value == val ){
						obj.options[i].selected = true;
						break;
					}
				}
			}
		}
	}
}
function SetFormRadioChecked(frm, obj_name, val){
	if( typeof( frm ) == "string" )
		var x = getFormObj(frm);
	else
		var x = frm;
	if( x ){
		var obj = eval("x." + obj_name);

		if( obj ){
			if( obj.length ){
				for(var i=0; i< obj.length; i++){
					if( obj[i].value == val ){
						obj[i].checked = true;
						break;
					}
				}
			}else{
				if( obj[i].value == val ){
					obj[i].checked = true;
				}
			}
		}
	}
}

//-----
function getAbsoluteOffsetLeft(obj) { //取得物件的絕對位置 X座標
	var x = obj.offsetLeft;
	while(obj = obj.offsetParent){
		x += obj.offsetLeft;
	}
	if(isIE){
		return x;
	}else{
		return x + "px";
	}
}
function getAbsoluteOffsetTop(obj) { //取得物件的絕對位置 Y座標
	var y = obj.offsetTop;
	while(obj = obj.offsetParent){
		y += obj.offsetTop;
	}
	if(isIE){
		return y;
	}else{
		return y + "px";
	}
}


/*** 按鈕 ***/
function changeBT(obj){
	var s = obj.src;
	var x = s.length - 4;

	var ns = s.substring(0, x);
	var nu = s.substring(x, s.length);

	obj.src = ns + "-1" + nu;
}

function recoveBT(obj){
	var s = obj.src;
	var x = s.length - 4;
	var y = s.length - 6;

	var ns = s.substring(0, y);
	var nu = s.substring(x, s.length);

	obj.src = ns + nu;
}

function showMenu(obj, n){
	var menu = document.getElementById("menu");
	var mdata = document.getElementById("mdata" + n);
	var firstM = document.getElementById("bt1");
	var endM = document.getElementById("bt8");

	if( mdata ){
		var mtxt = document.getElementById("menu_txt");
		var mup = document.getElementById("arrmenu");

		var x = parseInt( getAbsoluteOffsetLeft(obj) );
		var y = parseInt( getAbsoluteOffsetTop(obj) );
		
		var startX = parseInt( getAbsoluteOffsetLeft(firstM) ); //起啟點
		var endX = parseInt( getAbsoluteOffsetLeft(endM) ) + endM.offsetWidth; //終點

		if( isIE )
			menu.style.width = (mdata.offsetWidth + 15) + "px";
		else
			menu.style.width = (mdata.offsetWidth + 10) + "px";

		mtxt.innerHTML = mdata.innerHTML;

		/////// 設定選單位置 ////////
		var md = 40; //一開始扣除的位置
	
		var setL = ( (x - 40) > startX )? (x - 40) : startX; //小於起啟點時，則以起啟點為主
		var setL2 = ( (setL + menu.offsetWidth) < endX )? setL : (endX - menu.offsetWidth); //超過終點，則以 終點 - 長度
	
		menu.style.left = setL2 + "px";
		menu.style.top = (y + 50) + "px";

		/////// 設定選單上的箭頭位置 ////////
		var up = (obj.offsetWidth / 2);
		var setArrL = (x - setL2) + up;
		mup.style.left = setArrL + "px";

	}else{
		menu.style.left = "-1000px";
		menu.style.top = "0px";
	}
}

//////////////////////////////////////

function StartRoomPic(n){

	var doc = document.getElementById("doc");
	var doc_x = getAbsoluteOffsetLeft(doc);
	var doc_y = getAbsoluteOffsetTop(doc);
	var doc_w = doc.offsetWidth;
	var doc_h = doc.offsetHeight;

	middle_h = (doc_h > screen.height)? screen.height : doc_h;
	//-----------------------------------------------------------------------------
	var obj = document.getElementById("subPic");

	obj.style.display = "block";
	obj.innerHTML = "";
	obj.style.width = "0px";
	obj.style.height = "0px";
	obj.style.minHeight = "0px";
	obj.style.left = "-1000px";
	obj.style.top = "-1000px";

	setResizePic(n);
}

function setResizePic(n){
	var nowPic = sPic[n];
	var addW = nowPic[1] * 0.05;
	var addH = nowPic[2] * 0.05;
	var next = false;

	var obj = document.getElementById("subPic");
	
	if( (obj.offsetWidth + addW) < nowPic[1] || (obj.offsetHeight + addH) < nowPic[2] ){
		obj.style.width = (obj.offsetWidth + addW) + "px";
		obj.style.minHeight = (obj.offsetHeight + addH) + "px";
		if( isIE6 ){
			obj.style.height = (obj.offsetHeight + addH) + "px";
		}
		next = true;
	}else{
		obj.style.width = (nowPic[1] + 28) + "px";
		obj.style.minHeight = (nowPic[2] + 28 + 40) + "px";
		if( isIE6 ){
			obj.style.height = (nowPic[2] + 28 + 40) + "px";
		}
	}

	var nowW = obj.offsetWidth;
	var nowH = obj.offsetHeight;

	if( screen.width > nowW )
		obj.style.left = ( (screen.width - nowW) /2 ) + "px";
	else
		obj.style.left = "0px";

	if( middle_h > nowH )
		obj.style.top = ( (middle_h - nowH) /2 ) + "px";
	else
		obj.style.top = "0px";

	if( next )
		window.setTimeout( function(){ setResizePic(n); }, 10);
	else
		showCurrectPic(n);
}

function showCurrectPic(n){
	var obj = document.getElementById("subPic");
	var nowPic = sPic[n];

	var areaW = (nowPic[1] / 2);
	var areaH = (nowPic[2] / 2) + 20;

	var txt = "<div class='roompic' style=''>";
	txt += "<div class='pic' style='width:" + nowPic[1] + "px; height:" + nowPic[2] + "px; margin-top:-" + areaH + "px; margin-left:-" + areaW + "px;'>";
	txt += "<div class='main'>\n";
		txt += "<div class='bt'><img src='images/room/subbutton.gif' usemap='#m_subbutton' /></div>\n";
		txt += "<div class='mp'><img src='" + nowPic[0] + "' width='" + nowPic[1] + "' height='" + nowPic[2] + "' /></div>";
		txt += "<div class='txt'>" + nowPic[3] + "</div>";
	txt += "</div>\n";
	txt += "</div>";
	txt += "</div>";

	obj.innerHTML = txt;

	NowRoomNum = n;
}

function previousRoomPic(){
	var g = NowRoomNum - 1;
	var find = false;
	for(var i in sPic){
		i = parseInt(i);
		if( i == g ){
			find = true;
		}
	}
	if( !find ){
		g = i;
	}
	StartRoomPic(g);
}

function nextRoomPic(){
	var g = NowRoomNum + 1;

	var find = false;
	var first = false;
	for(var i in sPic){
		i = parseInt(i);
		first = (first == false)? i : first;
		if( i == g ){
			find = true;
		}
	}
	if( !find ){
		g = first;
	}
	StartRoomPic(g);
}

function closeRoomPic(){
	var obj = document.getElementById("subPic");
	obj.innerHTML = "";
	obj.style.width = "0px";
	obj.style.height = "0px";
	obj.style.minHeight = "0px";
	obj.style.left = "-1000px";
	obj.style.top = "-1000px";
	obj.style.display = "none";
	
}

//////////////////////////////////////

function ShowTipWin(obj, n){
	var data = cinfo[n];
	var tip = document.getElementById("ctime_txt");
	var txt = "";
	if( data ){
		txt += "<div><b>" + data[2] + "</b></div>\n";
		txt += "<div class='mh5'></div>\n";
		if( data[0] ){
			txt += "<div>" + data[0] + "</div>";
			txt += "<div class='mh5'></div>\n";
		}
		txt += "<div style='color:#999;'>" + data[1] + "</div>\n";

		tip.innerHTML = txt;
	}

	VisibleTipData(obj, n);
	//window.setTimeout( function(){ VisibleTipData(obj, n); }, 300);
}

function CloseTipWin(){
	var tip = document.getElementById("ctime");
	tip.style.display = "none";
	tip.style.left = "-1000px";
	tip.style.top = "-1000px";
}

function VisibleTipData(obj, n){

	var now_x = getAbsoluteOffsetLeft(obj);
	var now_y = getAbsoluteOffsetTop(obj);
	now_x = parseInt(now_x);
	now_y = parseInt(now_y);


	var tip = document.getElementById("ctime");
	tip.style.display = "";

	tip.style.left = (now_x - 60) + "px";
	tip.style.top = (now_y - tip.offsetHeight) + "px";
}

/**
 * sprintf() for JavaScript v.0.4
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
 * Thanks to David Baird (unit test and patch).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

