﻿/**
 * SUBMODAL v1.6
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Subimage LLC
 * http://www.subimage.com
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://submodal.googlecode.com
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
//var gDefaultPage = "propHighlight/loading.html";
var gHideSelects = false;
var gReturnVal = null;

var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
}

/**
 * Initializes popup code on load.	
 */
function initPopUp() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];

	if (document.getElementById('popupMask'))
		theBody.removeChild(document.getElementById('popupMask'));
	if (document.getElementById('popupContainer'))
		theBody.removeChild(document.getElementById('popupContainer'));

	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popmask.onclick = new Function("hidePopWin(false)");
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar"  style="visibility: hidden;background:url(propHighlight/image/web_images/title_bg.gif); background-position:bottom; background-repeat: repeat-x;"  >' +
				'<div id="popupTitle">&nbsp;</div>' +
				'<div id="popupControls">' +
					'<img src="propHighlight/image/web_images/buttom_close.gif" onclick="hidePopWin(false);" id="popCloseBox" />' +
				'</div>' +
			'</div>' +
			//'<iframe src="'+ gDefaultPage +'" fgfgf="jkjkjkj" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
			'<div id="newPopupFrame" style="width:100%;height:100%;background-color:white;text-align:left; float: left" scrolling="auto"  allowtransparency="true" width="90%" height="100%"> PLEASE WAIT ..... </div>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);

	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	//gPopFrame = document.getElementById("popupFrame");	
	gPopFrame2 = document.getElementById("newPopupFrame");
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	
	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
			// var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			// elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// default width and height
				var width = 400;
				var height = 200;
				// Parse out optional width and height from className
				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
addEvent(window, "load", initPopUp);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	* @argument showCloseBox - show the close box - default true
	*/
function showPopWin(id, width, height, returnFunc, showCloseBox, propName) {
	// show or hide the window close widget
	//voice edit
	width = 700;
	height = 630;
	//voice edit
	if (showCloseBox == null || showCloseBox == true) {
		document.getElementById("popCloseBox").style.display = "block";
	} else {
		document.getElementById("popCloseBox").style.display = "none";
	}
	
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(width, height);
	
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	
	setMaskSize();

	//voice edit

	//voice edit
	
	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame2.style.width = width + "px"; //parseInt(document.getElementById("popupTitleBar").offsetWidth, 10)  + "px" ;
	gPopFrame2.style.height = (height) + "px";

	
	// set the url
	//gPopFrame.src = url;   *************************** voice Edited
        
	//doc.body.innerHTML = "<table width=\"800px\"><tr><td>prev</td>";
	//doc.body.innerHTML += "<td align=\"center\"><img src=\"" + galleryConfig.images[id].biggest + "\" /></td>";
	//doc.body.innerHTML += "<td>next</td></tr></table>"

	
	
	var	onClickString_Prev = "";
	var	onClickString_Next = "";
	
	if  ( id < galleryConfig.images.length - 1)
	{
		onClickString_Next = "onclick='updatePhoto(" + (id + 1) + ");'";
	}
	if (id > 0)
	{
		onClickString_Prev = "onclick='updatePhoto(" + (id - 1)  + ");'";
	}	
	
	var curPageText = "第 "+ "<span id='pageNo' ></span> 	" + "  頁";
    var totalPages = "共 " + galleryConfig.images.length + " 頁";
	
	var HTMLString = " <table width='700px'  height='100%' valign='middle' align='center'>" +

					 "<tr><td  align='center' ><img src='propHighlight/image/web_images/buttom_previous.gif' id='prevButton'  " + onClickString_Prev + "  style='cursor: pointer'/> </td>" +
					 "<td align='center' width='600' height='600' border='0' cellpadding='0' cellspacing='0' bgcolor='#EEEEEE'>" + 
					 "<img id='photoFrame' src=\"" + galleryConfig.images[id].biggest + "\" onclick=\"showOrgImage('" + galleryConfig.images[id].orgSize + "')\"  style='cursor: pointer'/></td>" + 
					 "<td align='center' ><img src='propHighlight/image/web_images/buttom_next.gif' id='nextButton' " + onClickString_Next + "  style='cursor: pointer'  /></td></tr>" +
					 "<tr><td colspan='3'>" + 
					 "<div style='float: left; width:30%'>"+ propName +"</div>" +
					 "<div style='float: left; text-align:center;  width:40%'><span id='caption'>"+ galleryConfig.images[id].caption +"</span></div>" +
					 "<div style='float: right; text-align:right'> <img height='20px' src='propHighlight/image/web_images/buttom_close.gif' onclick='hidePopWin(false);' id='popCloseBox2' /></div>" +
					 "<div style='float: right;' >" + curPageText + " /  " + totalPages + "</div>" +
					 "</td></tr>" +
					 "</table>";
	//doc.write(scriptFunction + HTMLString);
	//gPopFrame.style.visibility = 'hidden';
	gPopFrame2.innerHTML = HTMLString;
	
	document.getElementById("pageNo").innerHTML = id + 1;
	
	
	
	
	hideScroll();
	
	//***************************END OF  voice Edited
	
	
	gReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	
	//window.setTimeout("setPopTitle();", 600);
}


function showOrgImage(path)
{
window.open (path, '_blank', 'location=0, menubar=0,titlebar=0, scrollbars=1, status=0,toolbar=0');
}
//
var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		//theBody.style.overflow = "hidden";
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSize();
		
		//window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		var fullHeight = document.documentElement.clientHeight;
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight+50)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;


/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			

	var fullHeight = document.documentElement.scrollHeight;
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width

	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		// Set the return code to run in a timeout.
		// Was having issues using with an Ajax.Request();
		gReturnVal = window.frames["popupFrame"].returnVal;
		window.setTimeout('gReturnFunc(gReturnVal);', 1);
	}
	// ********************voice Edited
	displayScroll();
	// ********************END OF voice Edited
		
	//gPopFrame.src = gDefaultPage;
	gPopFrame2.innerHTML = "PLEASE WAIT .... ";
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		//document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;    voice
	}
}

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
 * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
 * IE has a problem with wanted select form tags to always be the topmost z-index or layer
 *
 * Thanks for the code Scott!
 */
function hideSelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++) {
    x[i].style.visibility = "hidden";
  }
}

/**
 * Makes all drop down form select boxes on the screen visible so they do not 
 * reappear after the dialog is closed.
 * 
 * IE has a problem with wanting select form tags to always be the 
 * topmost z-index or layer.
 */
function displaySelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++){
    x[i].style.visibility = "visible";
  }
}


var agt=navigator.userAgent.toLowerCase();

var is_ie=((agt.indexOf("msie")!=-1)&&(agt.indexOf("opera")==-1));

function hideScroll()
{
           if (is_ie)
           {
                     //document.body.scroll = "no";

		     document.body.style.overflow="hidden";
				
           }
           else
           {         this.current_scroll_top = document.body.scrollTop;

                     document.body.scrollTop = 0;                      

                     document.body.style.overflow="hidden";   
           }

}

function displayScroll()
{

           if(is_ie)

                     document.body.scroll = "yes";

           else

           {      document.body.style.overflow="visible";    

                     document.body.scrollTop = this.current_scroll_top;
	   }

}

function updatePhoto(index) {
	
	var prevButton = document.getElementById('prevButton');
	var nextButton = document.getElementById('nextButton');
	var photoFrame = document.getElementById('photoFrame');
	var caption = document.getElementById('caption');
	
	photoFrame.src = galleryConfig.images[index].biggest;
	caption.innerHTML = galleryConfig.images[index].caption;
	
	photoFrame.onclick = new Function('showOrgImage(\'' + galleryConfig.images[index].orgSize + '\')' );
		
		if (index > 0)
			prevButton.onclick = new Function('updatePhoto(' + (index - 1) + ')' );
		else
			prevButton.onclick = '';
			
		if (index < galleryConfig.images.length - 1)
			nextButton.onclick = new Function('updatePhoto(' + (index + 1) + ')' );
		else
			nextButton.onclick = '';
			
			document.getElementById("pageNo").innerHTML = index + 1;
			
}

ScrollFreeze = /*2843293230303620532E4368616C6D657273*/
{
propFlag : true,
Ydisp : 0,
Xdisp : 0,

on : function()
{
if(this.getProp())
window.onscroll=function(){ ScrollFreeze.setXY(); }
},

off : function(){window.onscroll=null;},

getProp : function()
{
if( typeof window.pageYOffset != 'undefined' )
{
this.Ydisp=window.pageYOffset;
this.Xdisp=window.pageXOffset;
}
else
if(document.documentElement)
{
this.Ydisp=document.documentElement.scrollTop;
this.Xdisp=document.documentElement.scrollLeft;
}
else
if(document.body && typeof document.body.scrollTop != 'undefined')
{
this.Ydisp=document.body.scrollTop;
this.Xdisp=document.body.scrollLeft;
}
else
this.propFlag=false;

return this.propFlag;
},

setXY : function()
{
window.scrollTo( this.Xdisp, this.Ydisp );
}
}







