var bRewrite = false;

var preloadImages = new Array();

if(window.attachEvent) {
	window.attachEvent('onload', rewrite_document);
} else if(window.addEventListener) {
	window.addEventListener('load', rewrite_document, false);
	window.addEventListener('pageshow', rewrite_document, false);
	
	if(isSafari()) {
		window.addEventListener('unload', function(){}, false);
	}
}



function get_browser()
{
    var checkOP = window.opera;            // OP
    var checkN6 = document.getElementById; // N6
    var checkIE = document.all;            // IE
    var checkN4 = document.layers;         // N4

    if(checkOP) {
        return "OP6";  // Opera 6 or above
    } else if(checkIE) {
		if(typeof document.body.style.maxHeight != "undefined") {
			return "IE7";  //  IE 7 or above
		}
        return "IE4";  // Internet Explorer 4.0 or above
    } else if(checkN6) {
		var userAgent = navigator.userAgent;
		if(userAgent.indexOf("Chrome") >= 0) {
			return "CH";  // Google Chrome
		}
		if(userAgent.indexOf("Safari") >= 0) {
			return "SA1";  // Safari 1.X
		}
		if(userAgent.indexOf("Firefox") >= 0) {
			return "FF";  // Firefox
		}
        return "NS6";  // Netscape 6 or above
    } else if(checkN4) {
        return "NN4";  // Netscape Navigator 4 to 5
    }

    return "UNKNOWN";
}



function isMac()
{
	if(navigator.platform) {
		var platform = navigator.platform.toLowerCase();
		
		if(platform.indexOf("mac") >= 0) {
			return true;
		}
	}
	
	return false;
}



function isSafari()
{
	if(navigator.userAgent) {
		var userAgent = navigator.userAgent.toLowerCase();
		
		if(userAgent.indexOf("safari") >= 0) {
			return true;
		}
    }

    return false;
}



function popupImage(imgURL, imgWidth, imgHeight, imgTitle)
{
	var winWidth = imgWidth + 20;
	var winHeight = imgHeight + 20;
	var title = unescape(imgTitle);

	var br = get_browser();
	
	if(br=="SA1" || br=="CH") {
		var newWin = window.open("", "", "toolbar=no,location=no,directoryies=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width="+winWidth+",height="+winHeight);
		if(br != "CH") {
			newWin.moveTo(10, 10);
		}
	} else {
		var newWin = window.open("./", "", "toolbar=no,location=no,directoryies=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width="+winWidth+",height="+winHeight);
		newWin.moveTo(10, 10);
	}

	newWin.document.open();
	
	newWin.document.writeln('<?xml version="1.0" encoding="utf-8"?>');
	newWin.document.writeln('');
	newWin.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	newWin.document.writeln('');
	newWin.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">');
	newWin.document.writeln('');
	newWin.document.writeln('<head profile="http://gmpg.org/xfn/11">');
	newWin.document.writeln('    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />');
	newWin.document.writeln('    <meta http-equiv="content-language" content="ja" />');
	newWin.document.writeln('    <meta http-equiv="content-style-type" content="text/css" />');
	newWin.document.writeln('    <meta http-equiv="imagetoolbar" content="no" />');
	newWin.document.writeln('');	
	newWin.document.writeln('    <style type="text/css">/*<![CDATA[*/');
	newWin.document.writeln('        * { margin:0px; paddingt:0px; border-style:none; border-width:0px; }');
	newWin.document.writeln('        html { overflow-y:hidden; overflow-x:hidden; }');
	newWin.document.writeln('        body { margin:0px; padding:10px; }');
	newWin.document.writeln('    /*]]>*/</style>');
	newWin.document.writeln('');
	newWin.document.writeln('    <title>'+title+'</title>');
	newWin.document.writeln('</head>');
	newWin.document.writeln('');
	newWin.document.writeln('<body>');
	newWin.document.writeln('<div><img src="'+imgURL+'" width="'+imgWidth+'" height="'+imgHeight+'" title="'+title+'" alt="'+title+'" /></div>');
	newWin.document.writeln('</body>');
	newWin.document.writeln('');
	newWin.document.writeln('</html>');
	
	newWin.document.close();
}




function rewrite_document()
{
	if(bRewrite) {
		return;
	}
	bRewrite = true;
	
	if(!document.getElementsByTagName) {
		return;
	}
	
	
	
	var spans = document.getElementsByTagName('span');
	
	for(var i=0; i<spans.length; i++) {
		var span = spans[i];
		var classes = span.className.split(" ");
		var bContactEMail = false;
		
		for(j=0; j<classes.length; j++) {				
			if(classes[j] == 'contactEMail') {
				bContactEMail = true;
				break;
			}
		}
		
		if(bContactEMail) {
			span.innerHTML = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,115,105,103,97,105,99,104,105,64,105,109,97,98,97,114,105,45,99,105,116,121,46,106,112,34,32,62,115,105,103,97,105,99,104,105,64,105,109,97,98,97,114,105,45,99,105,116,121,46,106,112,60,47,97,62);
		}
	}
	
	
	
	var anchors = document.getElementsByTagName('a');
	
	for(var i=0; i<anchors.length; i++) {
		var a = anchors[i];
		var classes = a.className.split(" ");
	
		if(a.href) {
			var bTargetBlank = false;
			var bPopupImage = false;
			var imgWidth = 0;
			var imgHeight = 0;
			
			for(var j=0; j<classes.length; j++) {				
				if(classes[j] == 'popupNewWindow') {
					bTargetBlank = true;
				} else if(classes[j] == "popupImage") {
					bPopupImage = true;
				} else if(classes[j].substr(0, 1) == "w") {
					imgWidth = classes[j].substr(1, 9999);
				} else if(classes[j].substr(0, 1) == "h") {
					imgHeight = classes[j].substr(1, 9999);
				}
			}
			
			if(bTargetBlank) {
				a.target = '_blank';
			}
			
			if(bPopupImage && imgWidth!=0 && imgHeight!=0) {
				a.href = 'javascript:popupImage("'+a.href+'", '+imgWidth+', '+imgHeight+', "'+escape(a.title)+'");';
			}
		}
	}
	
	
	
	if(typeof localRewriteFunc == "function") {
		localRewriteFunc();
	}
}
