var imgLayout = '';
var setLayout = '';
var resizing = 0;
var cycle = 1;
var saf = (navigator.userAgent.indexOf('Safari') != -1) ? 1 : 0;

window.onresize = widthResize;

window.onload = function(){
	thumbnail();
	setSlideshowLink();
	setOpenNewWin();
	window.focus();
	wResize();
}

function setOpenNewWin(){
	var body=document.getElementsByTagName('body')[0];
	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
			var e = anchors[i];
			var newWinfunc = "openNewWin('" + e.href + "');";
			var newWinInnerfunc = "openNewWinInner('" + e.href + "');";
			if (e.className == "openNewWinO") {
					e.setAttribute("title","Opening new window");
					if(body.getAttribute("style") == body.style) {
							e.setAttribute("onclick",new Function(newWinfunc + "return false;"));
					} else {
							e.setAttribute("onclick",newWinfunc + "return false;");
					}
			} else if (e.className == "openNewWin") {
					e.setAttribute("title","Opening new window");
					if(body.getAttribute("style") == body.style) {
							e.setAttribute("onclick",new Function(newWinInnerfunc + "return false;"));
					} else {
							e.setAttribute("onclick",newWinInnerfunc + "return false;");
					}
			}
	}
}


function setSlideshowLink(){
	var body=document.getElementsByTagName('body')[0];
	subAnchors = document.getElementById('sub').getElementsByTagName('a');
	for (var i = 0; i < subAnchors.length; i++) {
		if(subAnchors[i].href.indexOf('slideshow',0) >= 0){
			subAnchors[i].setAttribute("title","Opening new window");
			if(body.getAttribute("style") == body.style) {
					subAnchors[i].setAttribute("onclick",new Function("slideShowWin('" + subAnchors[i].href + "');return false;"));
			} else {
					subAnchors[i].setAttribute("onclick","slideShowWin('" + subAnchors[i].href + "');return false;");
			}
		}
	}
}

function wResize(){
	var parentWidth = document.body.clientWidth;
	if (parentWidth > 1200){
			document.getElementById('wrap').style.width="1130px";
			if(document.getElementsByTagName('address')[0]){
				document.getElementsByTagName('address')[0].style.width="930px";
				document.getElementsByTagName('address')[0].style.marginRight="30px";
			}
	} else if(parentWidth < 730){
			document.getElementById('wrap').style.width="700px";
			if(document.getElementsByTagName('address')[0]){
				document.getElementsByTagName('address')[0].style.width="500px";
				document.getElementsByTagName('address')[0].style.marginRight="30px";
			}
	} else {
			document.getElementById('wrap').style.width="auto";
			if(document.getElementsByTagName('address')[0]){
				document.getElementsByTagName('address')[0].style.width="auto";
				document.getElementsByTagName('address')[0].style.marginRight="30px";
			}
	}
	clearTimeout(setLayout);
}


function resizeEv(){
		if (resizing == 0){
				imgLayout = setTimeout("thumbnail();", 75);
				resizing = 1;
		}
		setLayout = setTimeout("wResize();", 500);
}

function widthResize(){
		resizeEv();
}

function openNewWin(url){
		var theWin = window.open(url, "other","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
		theWin.focus();
		targetWin = theWin;
		focusWin = setTimeout('activeWin()',1000);
		othrWin = theWin;
}

function openNewWinInner(url){
		var theWinInner = window.open(url,"Inner","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
		theWinInner.focus();
		targetWin = theWinInner;
		focusWin = setTimeout('activeWin()',1000);
		othrWin = theWinInner;
}

function slideShowWin(url){
		var theWinInner = window.open(url,"Inner","width=800,height=600,toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
		theWinInner.focus();
		targetWin = theWinInner;
		focusWin = setTimeout('activeWin()',1000);
		othrWin = theWinInner;
}

function activeWin(){
		clearTimeout(focusWin);
		targetWin.focus();
}

function removeBrTags(){
		if (saf){
				var divisions = document.getElementById('thumbsList').getElementsByTagName('div');
				for(var i=0;i<divisions.length;i++){
						if (divisions[i].className == 'colThumb'){
								divisions[i].style.clear = 'none';
						}
				}
		} else {
				var brElems = document.getElementById('thumbsList').getElementsByTagName('br');
				if (brElems.length > 0){
						var brElemLen = brElems.length;
						//alert(brElemLen);
						var brElemLen2 = 0;
						for (var i = brElemLen - 1;i >= 0;i--){
							if(brElems[i].parentNode.className != 'fl'){
								brElems[i].parentNode.removeChild(brElems[i]);
								brElemLen2++;
							}
						}
						//alert('br is counted by ' + brElemLen2);
				} else {
						//alert('br is nothing.');
				}
		}
}

function thumbnail(){
		var parentWidth = document.body.clientWidth;
		var num;
		var checkBr = 0;
		var pageID = document.getElementsByTagName('body')[0].id;
		if((pageID == 'cha1' | pageID == 'cha2' | pageID == 'cha3' | pageID == 'cha4' | pageID == 'cha5') && document.getElementById('wrap').className == 'c0'){
				var divisions2 = document.getElementById('content');
				var divisions = document.getElementsByTagName('div');
				var divisionCols, thumbNum;
				for(var i = 0; i < divisions.length; i++) {
						if(divisions[i].className == 'col1'){
								if (parentWidth > 1200){
										num = 3;
								} else {
										num = 2;
								}
								divisionCols = divisions[i].getElementsByTagName('div');
								thumbNum = num;
								for(var j = 0; j < divisionCols.length; j++){
										if(divisionCols[j].className == 'colThumb'){
												thumbNum++;
												if(thumbNum % num == 0){
														if (checkBr == 0){
																//alert(num + ' : ' + parentWidth);
																removeBrTags();
																var newElementOriginal = document.createElement('br');
																newElementOriginal.className = 'clearFloat';
																var newElement;
																checkBr = 1;
														}
														if (saf){
																if (j+1 != divisionCols.length){
																		divisionCols[j+1].style.clear = 'both';
																}
														} else {
																newElement = newElementOriginal.cloneNode(true);
																//newElement.id = cycle + '-' + thumbNum + '-' + num;
																if (j+1 != divisionCols.length){
																		divisionCols[j+1].parentNode.insertBefore(newElement, divisionCols[j+1]);
																}
														}
												}
										}
								}
						}
				}
		}
		clearTimeout(imgLayout);
		resizing = 0;
		cycle++;
}
