var isIE = (document.documentElement.getAttribute("style") == document.documentElement.style);
var commonPath = document.getElementsByTagName('head')[0].getElementsByTagName('script')[0].src.split('js/common.js')[0];
/*============================================================**
		changeImg
**============================================================*/
var preloadImg = new Object();
var preNum = 0;
function btnOverEvent(){
	var images = document.getElementsByTagName('img');
	for(var i = 0,num = images.length;i < num; i++){
		var img = images[i];
		var imgSrc = img.src;
		if(imgSrc.indexOf('_f.') > -1 && img.parentNode.href){
			var newImgSrc = imgSrc.split('_f.')[0] + '_h.' + imgSrc.split('_f.')[1];
			preloadImg[preNum] = new Image();
			preloadImg[preNum].src = newImgSrc;
			var funcOnImg = 'changeImg(this,\'' + newImgSrc + '\')\;return false\;';
			var funcOutImg = 'changeImg(this,\'' + imgSrc + '\')\;return false\;';
			setEvent(img.parentNode,'onmouseover',funcOnImg);
			setEvent(img.parentNode,'onmouseout',funcOutImg);
			preNum++
		}
	}
}
function changeImg(eventAnchor,imgSrc){
	eventAnchor.getElementsByTagName('img')[0].src = imgSrc;
}
/*============================================================**
		Width Resize
**============================================================*/
var resizeWidth = {
	'maxWidth': '1200',
	'minWidth': '1000',
	'autoWidth': '100%'
}
function widthResize(){
	if(document.getElementById('MAIN_INDEX_BODY')){
		var viewportWidth = document.body.clientWidth;
		if(viewportWidth < resizeWidth.minWidth){
			document.getElementById('MAIN_INDEX_BODY').style.width = resizeWidth.minWidth + 'px';
		}else {
			document.getElementById('MAIN_INDEX_BODY').style.width = resizeWidth.autoWidth;
		}
	}else if(document.getElementById('BODY')){
		var img = document.getElementById('MAIN_IMAGE');
		if(img && (img.width + 36 * 2 > resizeWidth.minWidth)){
			document.getElementById('BODY').style.width = img.width + 36 * 2 + 'px';
			document.getElementById('BODY').style.maxWidth = 'none';
		}else{
			var viewportWidth = document.body.clientWidth;
			if(viewportWidth > resizeWidth.maxWidth){
				document.getElementById('BODY').style.width = resizeWidth.maxWidth + 'px';
			}else if(viewportWidth < resizeWidth.minWidth){
				document.getElementById('BODY').style.width = resizeWidth.minWidth + 'px';
			}else {
				document.getElementById('BODY').style.width = resizeWidth.autoWidth;
			}
		}
	}
}
window.onresize = function(){
	setTimeout("widthResize()",400);
}
/*============================================================**
		win
**============================================================*/
var win = {
	newImgWin: function(linkUrl){
		if(window.opener && !window.opener.closed){
			window.opener.location.href = linkUrl;
			window.opener.focus();
		}else if(window.opener && window.opener.closed){
			window.name = 'newParentWin';
			var imgWin = window.open(linkUrl, "imgWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
			imgWin.focus();
		}else{
			var imgWin = window.open(linkUrl, "imgWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
			imgWin.focus();
		}
	},
	newOuterWin: function(linkUrl){
		/*
		if(window.opener && !window.opener.closed){
			window.opener.location.href = linkUrl;
			window.opener.focus();
		}else if(window.opener && window.opener.closed){
			window.name = 'newParentWin';
			var outerWin = window.open(linkUrl, "outerWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
			outerWin.focus();
		}else{
			var outerWin = window.open(linkUrl, "outerWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
			outerWin.focus();
		}
		*/
		var outerWin = window.open(linkUrl, "outerWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
		outerWin.focus();
	},
	linkParent: function(linkUrl){
		if(window.opener && !window.opener.closed){
			window.opener.location.href = linkUrl;
			window.opener.focus();
		}else{
			var parentWin = window.open(linkUrl,"parentWin","toolbar=yes,scrollbars=yes,menubar=yes,location=yes,status=yes,directories=yes,resizable=yes");
			parentWin.focus();
		}
	}
}
/*============================================================**
		crawl
**============================================================*/
var crawl = {
	init: {
		targetClass: 'newImgWin,newOuterWin,linkParent'
	},
	start: function(){
		var as = document.getElementsByTagName('a');
		var targets = this.init.targetClass.split(',');
		for(var i = 0; i < as.length; i++){
			var a = as[i];
			for(var j = 0; j < targets.length; j++){
				if(a.className == targets[j]){
					setEvent(a,'onclick','win.' + targets[j] + '(this.href);return false;');
				}
			}
		}
	}
}
var browser = {
	check: function(){
		var ua = navigator.userAgent;
		if(
			(ua.match('MSIE 5')) ||
			(ua.match('MSIE 4'))
		){
			this.valid = false;
		}
	},
	valid: true
}
browser.check();
/*============================================================**
		for viewer
**============================================================*/
var win_close = {
	write: function(t){
		var html = '<a href="#" onclick="window.close();return false;"><span>' + t + '</span></a>';
		document.write(html);
	}
}
var menu = {
	items: '',
	write: function(t){
		if(this.items == ''){
			return;
		}
		var current = location.href.split('/')[location.href.split('/').length - 2] + '/' + location.href.split('/')[location.href.split('/').length - 1];
		current = '../' + current;
		var opts = '';
		var items = this.items.split(',');
		for(var i = 0; i < items.length; i++){
			var count = (i + 1);
			/*
			if(count < 10){
				count = '00' + count;
			}else if(count < 100){
				count = '0' + count;
			}
			*/
			if(current == items[i]){
				opts += '<option value="' + items[i] + '" selected="selected">' + count + '</option>';
			}else{
				opts += '<option value="' + items[i] + '">' + count + '</option>';
			}
		}
		var select = '<select>' + opts + '</select>/' + items.length + ' <input type="button" value="' + t + '" onclick="menu.jump(this)" />';
		document.write(select);
	},
	jump: function(b){
		var select = b.parentNode.getElementsByTagName('select')[0];
		window.location.href = select.value;
	}
}
/*============================================================**
		etc
**============================================================*/
function setEvent(obj,eventType,func){
	if(isIE) {
		obj.setAttribute(eventType,new Function(func));
	} else {
		obj.setAttribute(eventType,func);
	}
}
window.onload = function(){
	btnOverEvent();
	if(browser.valid){
		crawl.start();
	}
}

