﻿/*
-------------------------------------
	marutenten.jp Hoshizora
	:marutenten project vol.1
	:produced by marutenten project
-------------------------------------
*/


var html = '<div id="STAR_BANNER"><object id="mttHoshizoraBanner" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="169" height="259" ALIGN=""><param name="movie" value="http://marutenten.jp/mttstar/banner.swf"><param name="quality" value="high"><param name="allowScriptAccess" value="always"><param name="flashvars" value=""><embed name="mttHoshizoraBanner" src="http://marutenten.jp/mttstar/banner.swf" quality="high" bgcolor="#ffffff" width="169" height="259" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" flashvars=""></embed></object></div>';
document.write('<link rel="stylesheet" type="text/css" href="http://marutenten.jp/mttstar/hoshizora.css"/>');
document.write('<script language="javascript" src="http://marutenten.jp/js/jquery.js"></script>');
document.write(html);
//document.write('<iframe style="position:absolute;top:-500px;" name="hoshizoraHistoryFrame" id="hoshizoraHistoryFrame" src="http://marutenten.jp/mttstar/hoshizoraHistory.php" width="300" height="300"></iframe>');



function mttHoshizoraInit(){
	var m = new mttHoshizora();
	var ps = m.getPageSize();
	var scend = document.body.scrollHeight-ps[1];
	m.pageScroll(scend,6,'init2');
}
function mttHoshizoraRemove(){
	location.href = location.href;
}
function mttHoshizora(){
	this.convertTarget = new Array("。",'\\.','・');
	this.convertFontColor = 255;
	this.baseFontColor = 20;
	this.hiliteColor = '#ffffff';
	this.bgColor = '#000000';
	this.totalCount = 0;
	this.arr = new Array();
	this.op = 256;
	this.orgHTML = '';
	this.reg = new RegExp(this.convertTarget.join("|"),"i");
	this.textColorClass = new Array(document.body);

	mttHoshizora.prototype.init2=function(){
		this.parseCSS();
		this.parseHTML(document.body, 1);

		document.body.style.color = '';
		this.showBackground();
	}


	mttHoshizora.prototype.parseCSS=function(){
		if(!document.all){
			var css = document.createElement('style');
			css.type = "text/css";

			//css.appendChild(document.createTextNode("a:link{color:#fff;text-decoration:none;background:none} a:visited{color:#fff;text-decoration:none;background:none} .mttHoshizoraBlind{color:#fff;text-decoration:none ! important} .mttHoshizoraHilite{color:#fff;}"));
			css.appendChild(document.createTextNode(".mttHoshizoraBlind{color:#fff;text-decoration:none ! important} .mttHoshizoraHilite{color:#fff;}"));
			var head = document.getElementsByTagName("head");
			head[0].appendChild(css);
			var st = document.styleSheets[document.styleSheets.length-1];

			cssrule = st.rules || st.cssRules;
			for(var i=0; i<cssrule.length; i++){
				if(cssrule[i].selectorText=='.mttHoshizoraBlind'){
					this.textColorClass.push(cssrule[i]);
				}
			}
		}else{
			var st = document.styleSheets[0];
			st.addRule(".mttHoshizoraBlind","{color:#fff}");
			this.textColorClass.push(st.rules[st.rules.length-1]);
			//st.addRule("a:link", "{text-decoration:none;background:none}");
			//st.addRule("a:visited", "{text-decoration:none;background:none}");
			st.addRule(".mttHoshizoraHilite","{color:#fff}");
		}
	}

	mttHoshizora.prototype.parseHTML=function(node, depth){
		var i=0;
		var tagreg = /^(script|style|textarea|iframe|noscript)$/i;
		var tagreg2 = /^(iframe|img|input|textarea|embed|object)$/i;

		var tmpnodes = new Array();

		while(node && depth>0){
			var nodename = node.nodeName;
			var nstyle = node.style;
			if(node.nodeType==1){
				if(!tagreg.test(nodename) && nstyle){
					if(nodename.toLowerCase()=='a'){
						//if(node.childNodes.length == 1 && node.firstChild.nodeType==3){
							/*
							var span = this.setSpan(node);
							span.innerHTML = node.innerHTML;
							node.parentNode.replaceChild(span,node);
							node = span;
							tmpnodes.push(node.firstChild);
							*/
						var span = document.createElement("SPAN");
						span.innerHTML = node.innerHTML;
						node.parentNode.replaceChild(span, node);
						node = span;

						/*
						var span = document.createElement('span');
						span.setAttribute("class", "mttHoshizoraBlind");
						span.setAttribute("className", "mttHoshizoraBlind");
						span.innerHTML = node.innerHTML;
						node.parentNode.replaceChild(span, node);
						node=span;
						tmpnodes.push(node.firstChild);
						*/
						//}
					}else{
						nstyle.backgroundColor = 'transparent';
						nstyle.color = '';
						nstyle.backgroundImage = 'none';
						nstyle.border = '0px';
					}
				}
				if(tagreg2.test(nodename)){
					nstyle.visibility = 'hidden';
				}else if(nodename=='MARQUEE'){
					nstyle.display = 'none';
				}

				if(node.hasChildNodes()){
					node = node.childNodes[0];
					depth++;
					continue;
				}
			}else if(node.nodeType==3 && !tagreg.test(node.parentNode.nodeName)){
				if(!node.nodeValue.match(/^[\s\t]*$/gi)){
					var attr = node.parentNode.getAttribute('class')||node.parentNode.getAttribute('className');
					if(attr!='mttHoshizoraBlind'){
						var span = this.setSpan(node);
						span.innerHTML = node.data;
						node.parentNode.replaceChild(span, node);
						node = span;
						tmpnodes.push(node.firstChild);
					}
				}
			}

			if(node.nextSibling){
				node = node.nextSibling;
			}else{
				while (depth > 0) {
					node = node.parentNode;
					depth --;


					if (node && node.nextSibling) {
						node = node.nextSibling;
						break;
					}
                }
			}
		}
		if(tmpnodes.length==0) return;
		node = tmpnodes[0];
		while(node){
			node = this.convertText(node);
			if(node.nextSibling){
				node = node.nextSibling;
			}else if(tmpnodes.length > 0){
				node = tmpnodes.shift();
			}else{
				node = false;
			}
		}
	}

	mttHoshizora.prototype.setSpan=function(n){
		var span = document.createElement("SPAN");
		if(document.all) span.setAttribute("className","mttHoshizoraBlind");
		else span.setAttribute("class","mttHoshizoraBlind");
		return span;
	}
	mttHoshizora.prototype.showBackground=function(){
		this.op -= 15;
		this.op = Math.max(this.op, 0);
		document.body.style.backgroundColor = "rgb("+this.op+","+this.op+","+this.op+")";

		if(this.op <= 2){
			this.convert();
		}else{
			var self = this;
			setTimeout(function(){self.showBackground()},10);
		}
	}
	mttHoshizora.prototype.convert=function(){
		var c = Math.floor((this.baseFontColor-this.convertFontColor)/15);
		this.convertFontColor = (Math.abs(c) < 1) ? this.baseFontColor : this.convertFontColor += c;

		for(var i in this.textColorClass){
			if(this.textColorClass[i].style){
			this.textColorClass[i].style.color = "rgb(" + this.convertFontColor+","+this.convertFontColor+","+this.convertFontColor+")";
			}
		}
		if(this.convertFontColor > this.baseFontColor){
			var self = this;
			setTimeout(function(){self.convert()}, 16);
		}else{
			var self = this;
			setTimeout(function(){self.pageScroll(0, 15,'showFlash')}, 1000);
		}
	}
    mttHoshizora.prototype.convertText = function(node) {
        var match = this.reg.exec(node.data);
        if (match) {
        	this.totalCount++;
            var val = match[0];
            var k = '';

            var node2 = node.splitText(match.index);
            var node3 = node2.splitText(val.length);
            var span = node.ownerDocument.createElement('SPAN');
            node.parentNode.replaceChild(span, node2);
            span.setAttribute("class","mttHoshizoraHilite");
            span.setAttribute("className","mttHoshizoraHilite");
            span.appendChild(node2);
            return span;
        } else {
            return node;
        }
    };
	mttHoshizora.prototype.getScrollTop=function() { // 020225
		if (window.pageYOffset) {
			return window.pageYOffset;
		} else {
			return document.body.scrollTop || document.documentElement.scrollTop;
		}
	}

	mttHoshizora.prototype.pageScroll=function(toY,frms,callback,keep) { // 020314
		if (!keep) keep = false;
		//if (pageScrollTimer) clearTimeout(pageScrollTimer);
		if (!toY || toY < 0) toY = 0;
		var cuY = this.getScrollTop();
		if (!frms) frms = 6;

		cuY += (toY-cuY) > 0 ? Math.max(1, (toY - cuY) / frms) : (toY-cuY)/frms;if (cuY < 0) cuY = 0;
		var posY = Math.floor(cuY);
		window.scrollTo(0, posY);
		if(keep) eval(callback);
		if (posY != toY) {
			var self = this;
			pageScrollTimer = setTimeout(function(){self.pageScroll(toY,frms,callback,keep)},16);
		}else{
			if(callback=='init2'){
				this.init2();
			}else if(callback=='showFlash'){
				this.showFlash();
			}
		}
	}
	mttHoshizora.prototype.getPageSize=function(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		arrayPageSize = new Array(w,h) 
		return arrayPageSize;
	}
	mttHoshizora.prototype.showFlash=function(){
		$('body').append('<div id="STAR_FLASH"><object id="star" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" ALIGN=""><param name="movie" value="http://marutenten.jp/mttstar/star.swf"><param name="quality" value="high"><param name="scale" value="noscale" /><param name="wmode" value="transparent" /><param name="allowScriptAccess" value="always"><param name="flashvars" value="totalCount='+this.totalCount+'"><embed name="star" src="http://marutenten.jp/mttstar/star.swf" quality="high" bgcolor="#000000" scale="noscale" width="100%" height="100%" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" wmode="transparent" flashvars="totalCount='+this.totalCount+'"></embed></object></div>');
		this.TB_Position();
	}
	mttHoshizora.prototype.TB_Position=function() {
		var pagesize = this.getPageSize();

		if (window.innerHeight && window.scrollMaxY) {	
			var yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			var yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			var yScroll = document.body.offsetHeight;
		}
		
		var scrollTop = this.getScrollTop();
		$("#STAR_FLASH").css({top:scrollTop+"px" });
	}
}
