var stackstring = new Array();

function init() {
	var entrys = document.getElementsByClassName('entryContents');
	entrys.each(function(v){
		var tmp = new Array();
		
		$A(v.getElementsByTagName('p')).each(function(vi){
			var childs = vi.childNodes;
			var startelem = vi;
			$A(childs).each(function(pi){
				if(pi.nodeType==8) {
					startelem.removeChild(pi);
				}else if(pi.nodeType!=3 && pi.nodeName=='IMG') {
					vi.removeChild(pi);
					startelem.parentNode.insertBefore(pi,startelem.nextSibling);
					startelem = pi;
					$(pi).cssFloat('left');
				}else if(vi!=startelem){
					if(!(/[^\t\n\r ]/.test(pi.data))) {
						vi.removeChild(pi);
					}/*else if(/^(&nbsp;)+$/.test(pi.data)){
						vi.removeChild(pi);
					}*/else{
						vi.removeChild(pi);
						
						if(startelem.nodeName=='IMG') {
							if(pi.nodeType!=3 && pi.nodeName=='BR') {
								//startelem.parentNode.insertBefore(pi,startelem.nextSibling);
							}else{
								var ptmp = document.createElement('p');
								startelem.parentNode.insertBefore(ptmp,startelem.nextSibling);
								startelem = ptmp;
								startelem.appendChild(pi);
							}
						}else{
							startelem.appendChild(pi);
						}
					}
				}
			});
		});
		
		$A(v.getElementsByTagName('p')).each(function(vi){
			var inn = vi.innerHTML;
			tmp.push(inn)
		});
		stackstring.push(tmp);
	});
	
	resizeParam();
	attachOnFontResize(resizeParam);
}

function resizeParam() {
	startOverlay();

	resetWidth();
	var defaultPlusCount = 20;
	var defaultPlusCountSecond = 5;
	var entrys = document.getElementsByClassName('entryContents');
	entrys.each(function(entry,ei){
		var t = entry.getElementsByTagName('p');
		if(document.getElementsByClassName('scroll-fix-attach-point',entry).length>0) {
			$A(t).each(function(v){
				if(v.className=='scroll-fix-attach-point') return;
				v.parentNode.removeChild(v);
			});
			t = entry.getElementsByTagName('p');
		}
		
		
		$A(t).each(function(v,vi){
			var inn = stackstring[ei][vi];
			v.innerHTML = inn;
			v.style.width = '';
			if(v.offsetHeight<=330) {
				v.className = 'scroll-fix-attach-point';
				return;
			}
			var first = true;
			do {
				var attachpoint = document.createElement('p');
				if(first) {
					first = false;
					attachpoint.className = 'scroll-fix-attach-point';
				}
				v.parentNode.insertBefore(attachpoint,v);
				var i = Math.floor(inn.length * (330/v.offsetHeight*0.8)) || 0;
				do {
					attachpoint.innerHTML = inn.substr(0,i+defaultPlusCount);
					i+=defaultPlusCount;
					if(i>inn.length) break;
				}while(attachpoint.offsetHeight<330);
				
				if(i<=inn.length) {
					i-=defaultPlusCount;
					do {
						attachpoint.innerHTML = inn.substr(0,i+defaultPlusCountSecond);
						i+=defaultPlusCountSecond;
						if(i>inn.length) break;
					}while(attachpoint.offsetHeight<330);
					
					i-=defaultPlusCountSecond;
					do {
						attachpoint.innerHTML = inn.substr(0,i+1);
						i++;
						if(i>inn.length) break;
					}while(attachpoint.offsetHeight<330);
					
					attachpoint.innerHTML = inn.substr(0,i-1);
					inn = inn.substr(i-1);
					v.innerHTML = inn;
				}else{
					break;
				}
			}while(inn.length>0);
			v.parentNode.removeChild(v);
		});
	});
	getWidth();
	
	endOverlay();
}

Event.observe(window,'load',init, false);

function attachOnFontResize(func) {
	if($('attachonfontresizeiframe')==null) {
		var iframe = $(document.createElement('iframe'));
		iframe.style.position = 'absolute';
		iframe.style.top = '-50000px';
		iframe.style.width = '100em';
		iframe.id = 'attachonfontresizeiframe';
		(document.getElementsByTagName('body')[0]).appendChild(iframe);
		
		if(!document.uniqueID)
		{
			var iframe = $('attachonfontresizeiframe');
			try{
			//see how Safari fails on Iframe:
			//http://xkr.us/articles/dom/iframe-document/
		
			var doc = iframe.contentWindow || iframe.contentDocument || iframe.document;
			doc = doc.document || doc; 

			var dWin = iframe.contentWindow; 
			
			var s = 'style="width:100%;height:100%;padding:0;margin:0;overflow:hidden;"';
			
			doc.open();			
			doc.write('<html '+s+'><body '+s+'></body></html>');
			doc.close();
			
			Event.observe(dWin,'resize', func, false);
			}catch(ah){ /*Onlu Safari sucks here */};
		}
		else
		{
			Event.observe($('attachonfontresizeiframe'),'resize', func, false);
		}
	}
}

Element.addMethods( {
	cssFloat: function(element, style) {
		if(navigator.appName == "Microsoft Internet Explorer") element.style.styleFloat = style;
		else element.style.cssFloat = style;
	}
});

initOverlayflag = false;
function initOverlay() {
	if(initOverlayflag) return;
	var body = document.getElementsByTagName("body")[0];
	var overlay = document.createElement('div');
	overlay.id = 'xtoverlay';
	overlay.style.display = 'none';
	body.appendChild(overlay);
	initOverlayflag = true;
}

Event.observe(window, 'load', initOverlay, true);

function startOverlay() {
	//initOverlay();
	var arrayPageSize = getPageSize();
	var hoge = $('mcoverlay');
	$('xtoverlay').style.top = "0px";
	$('xtoverlay').style.left = "0px";
	$('xtoverlay').style.width = '100%';
	$('xtoverlay').style.height =  "100%";
	$('xtoverlay').show();
}

function endOverlay() {
	$('xtoverlay').style.display = 'none';
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
// by lightbox
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}