// Functions for CrossBrowser-----------------------------------------
// DOM: 1=NN4, 2=IE4, 3=IE5+, 4=NN6+, 0=Others
DOM = document.all?(document.getElementById?3:2)
                  :(document.getElementById?4
                  :(document.layers?1:0));

var BR_TYPE_NN = "NETSCAPE";
var BR_TYPE_NN4 = "NN4";
var BR_TYPE_IE = "MSIE";
var BR_TYPE_OPERA = "opera";
var BR_TYPE_FF = "FIREFOX";
var BR_TYPE_SAFARI = "SAFARI";
var BR_TYPE_MOZILLA = "MOZILLA";

var _loadNum = 0;
var _fadeinstart = false;
var _img2 = null;
var _start = true;

function getBrowserType() {
  var brtype = "";
  var uName = navigator.userAgent.toUpperCase();
  var aName = navigator.appName.toUpperCase();

  if (window.opera) {
    brtype = BR_TYPE_OPERA;
  } else if (uName.indexOf(BR_TYPE_NN) >=0) {
    brtype = BR_TYPE_NN;
  } else if (uName.indexOf(BR_TYPE_FF) >=0) {
    brtype = BR_TYPE_FF;
  } else if (uName.indexOf(BR_TYPE_IE) >=0) {
    brtype = BR_TYPE_IE;
  } else if (uName.indexOf(BR_TYPE_SAFARI) >=0) {
    brtype = BR_TYPE_SAFARI;
  } else if (document.layers) {
    brtype = BR_TYPE_NN4;
  } else if (uName.indexOf(BR_TYPE_MOZILLA) == 0) {
    brtype = BR_TYPE_MOZILLA;
  }

  return brtype;
}

function getBrowserVer() {
  var brtype = getBrowserType();
  var version = 0;
  var s = 0;
  var e = 0;
  var appVer = navigator.appVersion;
  var appUA = navigator.userAgent.toUpperCase();

  if (brtype == BR_TYPE_OPERA) {
    version = appVer;
  } else if (brtype == BR_TYPE_NN4) {
    s = 0;
    e = appVer.indexOf(" ");
    if (e == -1) e = 999;
    version = appVer.substring(s,e);
  } else if (brtype == BR_TYPE_IE) {
    s = appUA.indexOf("MSIE ",0) + 5;
    e = appUA.indexOf(";",s);
    if (e == -1) e = 999;
    version = appUA.substring(s,e);
  } else {
    s = appUA.lastIndexOf(brtype + '/') + brtype.length + 1;
    e = appUA.indexOf(brtype + ' ',s);
    if (e == -1) e = 999;
    version = appUA.substring(s,e);
  }

  return version;
}

function getOSType()
{
  var ostype = "";
  var uAgent  = navigator.userAgent.toUpperCase();
  if (uAgent.indexOf("MAC") >= 0) ostype = "MacOS";
  if (uAgent.indexOf("WIN") >= 0) ostype = "Windows";
  if (uAgent.indexOf("X11") >= 0){
    if(uAgent.indexOf("LINUX") >=0) {
      ostype = "LINUX";
    } else {
      ostype = "UNIX";
    }
  }

  return ostype;
}

function getKEYCODE(e) {
	var ua = navigator.userAgent;
//	if (ua.indexOf("Safari" != -1 || ua.serach('opera(\ |\/)6') != -1) {
	if (ua.search('opera(\ |\/)6') != -1) {
		return null;
	} else if (document.layers) {
		return e.which;
	} else if (document.all) {
		return event.keyCode;
	} else if (document.getElementById) {
		return e.keyCode;
	} else {
		return null;
	}
}

function getDivFromName(idName){
  if(DOM>=3)
    return document.getElementById(idName);
  if(DOM==2)
    return document.all(idName);
  if(DOM==1){
    var s = '';
    for(var i=1; i<arguments.length; i++)
      s += 'document.layers.'+arguments[i]+'.';
    return eval(s+'document.layers.'+idName);
  }
  return null;
}

function getDivLeft(div){
  if(DOM>=3) return div.offsetLeft;
  if(DOM==2) return div.style.pixelLeft;
  if(DOM==1) return div.left;
  return 0;
}

function getDivTop(div){
  if(DOM>=3) return div.offsetTop;
  if(DOM==2) return div.style.pixelTop;
  if(DOM==1) return div.top;
  return 0;
}

function getDivWidth(div){
  if(DOM>=3) return div.offsetWidth;
  if(DOM==2) return div.style.pixelWidth;
  if(DOM==1) return div.width;
  return 0;
}

function getDivHeight(div){
  if(DOM>=3) return div.offsetHeight;
  if(DOM==2) return div.style.pixelHeight;
  if(DOM==1) return div.height;
  return 0;
}

function getPageX(e){
  if(window.opera)
    return e.clientX;
  if(DOM==4)
    return e.clientX+window.scrollX;
  if(DOM==3 || DOM==2)
    return getEl().scrollLeft+window.event.clientX;
  if(DOM==1)
    return e.pageX;
  return 0;
}

function getPageY(e){
  if(window.opera)
    return e.clientY;
  if(DOM==4)
    return e.clientY+window.scrollY;
  if(DOM==3 || DOM==2)
    return getEl().scrollTop+window.event.clientY;
  if(DOM==1)
    return e.pageY;
  return 0;
}

function getEl() {
  return (document.compatMode == 'CSS1Compat')?document.documentElement:document.body;
}

function getClientWidth() {
  var pWidth = null;
  //NN用
  if(document.layers || document.getElementById) pWidth=window.innerWidth;
  //IE用
  if(document.all) pWidth=getEl().clientWidth;

  return pWidth;
}

function getClientHeight() {
  var pHeight = null;
  //NN用
  if(document.layers || document.getElementById) pHeight=window.innerHeight + window.scrollY;
  //IE用
  if(document.all) pHeight=getEl().clientHeight + getEl().scrollTop;

  return pHeight;
}
function getScrollTop() {
  var pTop = null;
  //NN用
  if(document.layers || document.getElementById) pTop= window.scrollY;
  //IE用
  if(document.all) pTop=getEl().scrollTop;

  return pTop;
	
}

function getStyle(aElement, aCssProperty)
{
  var tElementId = document.getElementById(aElement);
  if (!tElementId) return "";

  if(tElementId.currentStyle){
    switch(aCssProperty.toLowerCase()) {
      case 'height':
        return tElementId.offsetHeight; //IE
      case 'width':
        return tElementId.offsetWidth; //IE
      case 'font-size':
          return tElementId.currentStyle.fontSize.replace('px',''); //IE
      default:
        return tElementId.currentStyle[aCssProperty]; //IE
    }
  } else {
    var style = document.defaultView.getComputedStyle(tElementId, null); //firefox, Operaなど
    return parseInt(style.getPropertyValue(aCssProperty).replace('px',''));
  }
}

function getCSSvalue(id,property)
{
  for (var i in document.styleSheets) {
    var style = document.styleSheets[i].rules ? document.styleSheets[i].rules : document.styleSheets[i].cssRules;
    for (var j in style) {
      if (style[j].selectorText == id) {
        return style[j].style[property];
      }
    }
  }

  return "";
}

function setAlpha(div,a)
{
//alert(typeof div);
  var _div = (typeof div == 'object') ? div : document.getElementById(div);
  if (!_div) return;

  if (document.all) {
//    if (typeof div == 'object') {
//      _div.filters['alpha'].opacity = a;
//    } else {
      _div.style.filter = "alpha(opacity=" + a + ")";
//    }
  } else {
    var btype= getBrowserType();
    if (btype==BR_TYPE_FF || btype== BR_TYPE_NN) {
      _div.style.MozOpacity = a / 100;
    } else {
      _div.style.opacity = a / 100;
    }
  }
}

function setAlpha2(img,a) {
		if (document.all) {
			img.filters['alpha'].opacity = a;
	} else {
		var btype= getBrowserType();
		if (btype==BR_TYPE_FF || btype== BR_TYPE_NN) {
			img.style.MozOpacity = a / 100;
		} else {
			img.style.opacity = a / 100;
		}
	}
}

function dispImage(img,w,h) {
  var iw = img.width;
  var ih = img.height;

  if (iw < w && ih < h) {
    w = iw;
    h = ih;
  } else {
    if (w <= h) {
      if (iw >= ih) {
        h = ih * w / iw;
      } else {
        if ((w / iw) >= (h / ih)) {
          h = ih * w / iw;
        } else {
          w = iw * h / ih;
        }
      }
    } else {
      if (iw <= ih) {
        w = iw * h / ih;
      } else {
        if ((w / iw) >= (h / ih)) {
          w = iw * h / ih;
        } else {
          h = ih * w / iw;
        }
      }
    }
  }
  img.width = Math.floor(w);
  img.height = Math.floor(h);
  img.style.visibility="visible";
}

function showImage(id,id2,src,w,h) {
  var cw = getClientWidth();
  var ch = getClientHeight();
  var left = 0;
  var top = 0;
  var div = document.getElementById(id);
  var divc = document.getElementById(id2);
  var mask = document.getElementById('mask');
  mask.style.width = cw + 'px';
  mask.style.height = getEl().scrollHeight + 'px';
  setAlpha(mask,80);
//  mask.style.visibility = "visible";

  if (_img2) return;

  _img2 = new Image();
  _img2.style.visibility = 'hidden';
//  _img2.style.border = 'solid 1px gray';
//  _img2.style.padding = '4px';
  _img2.onload = function() {
    action = Math.floor(Math.random() * 3) + 1;
    setAlpha(_img2,0);
    dispImage(_img2,w,h);
    w2 = _img2.width + 30;
    h2 = _img2.height + 40;
/*    if (w2 > w) {
      w = w2;
    }
    if (h2 > h) {
      h = h2;
    }*/
    w = w2;
    if (w < 0) {
      w = 0;
    }
/*    h = h2 - 80;
    if (h < 0) {
      h = 0;
    }*/
    h = h2;
    if (cw > w) {
      left = (cw - w) / 2;
    }
    if (ch > h) {
      top = (ch + getScrollTop() - h) / 2;
    }
    div.style.width = w + 'px';
    div.style.height = h + 'px';
    div.style.top = top + 'px';
    div.style.left = left + 'px';
    div.style.visibility = 'visible';
    divc.appendChild(_img2);

    var imgs = new Array(_img2);
    setBox(div,w,h,w2,h2,left,imgs,action);
//    fadein2(50,imgs,action);
  }
  _img2.src = src;
}

function hideImage(id,id2) {
  if (!_img2) return;
  
  var div = document.getElementById(id);
  var divc = document.getElementById(id2);
  try {
	  divc.removeChild(_img2);
  } catch (e) {
  }
  _img2 = null;
  div.style.visibility="hidden";

  var mask = document.getElementById('mask');
  mask.style.visibility = "hidden";
}

function clipImage(w,h,name) {
  var imgs = document.getElementsByName(name);
  for (var i=0; i < imgs.length; i++) {
    imgPros(w,h,imgs[i]);
  }

  fadein(100,imgs);
}

function clipImage2(w,h,img) {
  var top = Math.floor((h - img.height) / 2);
  var left = Math.floor((w - img.width) / 2);
  var ctop = top;
  var cleft = left;
  var cbottom = "auto";
  var cright = "auto";
  if (top < 0) {
	ctop = (top * -1) + 'px';
	cbottom = (img.height + top) + 'px';
  } else {
	ctop = "auto";
  }
  if (left < 0) {
	cleft = (left * -1) + 'px';
	cright = (img.width + left) + 'px';
  } else {
	cleft = "auto";
  }

  img.style.clip = "rect(" + ctop + " " + cright + " " + cbottom + " " + cleft + ")";
  img.style.top = top + 'px';
  img.style.left = left + 'px';
  img.style.visibility = "visible";
}

function clipImage3(w,h,x,y,img) {
  var ih = img.height;
  var iw = img.width;
  var ctop = "auto";
  var cleft = "auto";
  var cbottom = "auto";
  var cright = "auto";
  if (x < 0) {
    cleft = (x * -1) + 'px';
  }
  if (x + iw > w) {
    cright = (w - x) + 'px';
  }

  img.style.clip = "rect(" + ctop + " " + cright + " " + cbottom + " " + cleft + ")";
}

function clipImage4(w,h,img) {
  var iw = img.width;
  var ih = img.height;
  var awh = calcWH3(w,h,iw,ih);
  img.width = awh[0];
  img.height = awh[1];
  var top = Math.floor((h - img.height) / 2);
  var left = Math.floor((w - img.width) / 2);
  var ctop = top;
  var cleft = left;
  var cbottom = "auto";
  var cright = "auto";
  if (top < 0) {
	ctop = (top * -1) + 'px';
	cbottom = (img.height + top) + 'px';
  } else {
	ctop = "auto";
  }
  if (left < 0) {
	cleft = (left * -1) + 'px';
	cright = (img.width + left) + 'px';
  } else {
	cleft = "auto";
  }

  img.parentNode.style.height = (img.height < h ? img.height : h) + 'px';
  img.style.clip = "rect(" + ctop + " " + cright + " " + cbottom + " " + cleft + ")";
  img.style.top = top + 'px';
  img.style.left = left + 'px';
  img.style.visibility = "visible";
}

function calcWH3(w,h,iw,ih){
  var th;
  var tw;
  if (w>=iw || h >=ih) {
    return new Array(iw,ih);
  }
  if (iw < ih) {
    tw = w;
    th = Math.floor((ih * w) / iw);
  } else {
    th = h;
    tw = Math.floor((iw * h) / ih);
  }

  if (th < h) {
    tw = Math.floor(tw * (h / th));
    th = h;
  }
  if (tw < w) {
    th = Math.floor(th * (w /tw));
    tw = w;
  }

  return new Array(tw,th);
}

function loaddone(w,h,name) {
  var imgs = document.getElementsByName(name);
  _loadNum++;
  if (_loadNum == imgs.length) {
    clipImage(w,h,name);
  }
}

function imgPros(w,h,_img) {
	var _n = 0;
	var img = _img;
	var top = 0;
	var left = 0;
	var clipTop = 0;
	var clipLeft = 0;
	var clipBottom =0;
	var clopRight = 0;
	var boxWidth = w;
	var boxHeight = h;
	function _initImgPros() {
		top = Math.floor((boxHeight - img.height) / 2);
		left = Math.floor((boxWidth - img.width) / 2);
		clipTop = top;
		clipLeft =left;
		clipBottom = "auto";
		clipRight = "auto";
		if (top < 0) {
			clipTop = (top * -1) + 'px';
			clipBottom = (img.height + top) + 'px';
		} else {
			clipTop = "auto";
		}
		if (left < 0) {
			clipLeft = (left * -1) + 'px';
			clipRight = (img.width + left) + 'px'
		} else {
			clipLeft = "auto";
		}
		setAlpha2(img,0);
		img.style.clip = "rect(" + clipTop + " " + clipRight + " " + clipBottom + " " + clipLeft + ")";
		img.style.top = top + 'px';
		img.style.left = left + 'px';
		img.style.visibility = "visible";
	}
	_initImgPros();
}

function setBox(div,w,h,w2,h2,left,imgs,action) {
  var tm = 30;
  var dw = Math.floor((w2 - w)/20);
  var dh = Math.floor((h2 - h)/20);
  var _w = w;
  var _left = left;
  var timerID = setTimeout(function _setBox() {
    if (w == w2 && h == h2) {
      clearTimeout(timerID);
      var mask = document.getElementById('mask');
      mask.style.visibility = "visible";
      fadein2(50,imgs,action);

      return;
    }
    if (w < w2) {
      w += dw;
      if (w > w2) {
        w = w2;
      }
    }
    left = _left - Math.floor((w - _w) /2);
/*    if (h < h2) {
      h += dh;
      if (h > h2) {
        h = h2;
      }
    }*/

    div.style.width = w + 'px';
    div.style.height = h + 'px';
    div.style.left = left + 'px';

    timerID = setTimeout(_setBox,tm);
  },tm);
}

function fadein(_tm,_imgs)
{
	var tm = _tm;
	var _n = 0;
	var imgs = _imgs;
	var img = imgs[0];
	var nc = 1;

	setTimeout(function _fadein() {
		if (_n >= 100) {
			nc++;
			if (nc > imgs.length) { _start = false; return; }
			_n = 0;
			img = imgs[nc-1];
		}
		_n += 10;
		setAlpha(img,_n);
		setTimeout(_fadein,tm);
	},tm);
}

function fadein2(_tm,_imgs,_action)
{
	var tm = _tm;
	var _n = 0;
	var imgs = _imgs;
	var img = imgs[0];
	var nc = 1;
	var w = img.width;
	var h = img.height;
	var dw = 0;
	var dh = 0;
	var _timerID = 0;

	_timerID = setTimeout(function _fadein2() {
		if (_n >= 100) {
          nc++;
          if (nc > imgs.length) { clearTimeout(_timerID); return; }
          _n = 0;
          img = imgs[nc-1];
		}
		if (_n == 0) {
		  w = img.width;
		  h = img.height;
		  dw = w/100;
		  dh = h/100;
//		  img.style.visibility = 'visible';
		}
		_n += 5;
		if (_n < 100) {
          if (_action != 2){			
		    img.width = Math.floor(dw * _n);
          }
          if (_action !=1) {
		    img.height = Math.floor(dh * _n);
          }
		} else {
          if (_action != 2){			
  		    img.width = w;
          }
          if (_action !=1) {
  		    img.height = h;
          }
		}

		setAlpha(img,_n);
		_timerID = setTimeout(_fadein2,tm);
	},tm);
}

function hextodec(v) {
	switch(v) {
		case 'a':
			return 10;
		case 'b':
			return 11;
		case 'c':
			return 12;
		case 'd':
			return 13;
		case 'e':
			return 14;
		case 'f':
			return 15;
		default:
			return parseInt(v);
	}
}

function dectohex(v) {
	switch(v) {
		case 10:
			return 'a';
		case 11:
			return 'b';
		case 12:
			return 'c';
		case 13:
			return 'd';
		case 14:
			return 'e';
		case 15:
			return 'f';
		default:
			return new String(v);
	}
}

function convHex(h) {
	return dectohex(Math.floor(h / 16)) + dectohex(h % 16);
}

function convColor(color) {
	var rgb = color.substr(4, color.length - 5).split(', ');

	return "#" + convHex(rgb[0])+convHex(rgb[1])+convHex(rgb[2]);
}

function rgbTohsv(r,g,b) {
	var h = s = v = cmax = cmin = c = 0;
	if ( r >= g)   cmax = r; else cmax = g;
	if ( b > cmax) cmax = b;
	if ( r <= g)   cmin = r; else cmin = g;
	if ( b < cmin) cmin = b;
	v = cmax;
	c = cmax - cmin;
	if (cmax == 0) s = 0; else s = c/cmax;
	if (s != 0)
	{
		if (r == cmax)
		{
			h = (g - b)/c;
		}else{
			if (g == cmax)
			{
				h = 2 + (b - r)/c;
			}else{
				if (b == cmax) h = 4 + ( r - g)/c;
			}
		}
		h = h * 60;
		if (h < 0) h = h + 360;
	}

	return new Array(h,s,v);
}

function hsvTorgb(h,s,v) {
	var r = g = b = 0;
	if (s < 0) s = 0;
	if (s > 1) s = 1;
	if (v < 0) v = 0;
	if (v > 1) v = 1;
	h = h % 360;
	if (h < 0) h = h + 360;
	h = h / 60;
	var i = Math.floor(h);
	var f = h - i;
	var p1 = v * (1 - s);
	var p2 = v * (1 - s*f);
	var p3 = v * (1 - s*(1 - f));
	if (i == 0) { r = v; g = p3; b = p1; }
	if (i == 1) { r = p2;g = v;  b = p1; }
	if (i == 2) { r = p1;g = v;  b = p3; }
	if (i == 3) { r = p1;g = p2; b = v;  }
	if (i == 4) { r = p3;g = p1; b = v;  }
	if (i == 5) { r = v; g = p1; b = p2; }

	
}

function changeTab(id, tab) {
	var div = null;
	var divTab = null;
	var tmp = "centerDiv";
	var tmpTab = "centerTab";

	if (id == _currentId) return;

	div = document.getElementById(id);
	divTab = document.getElementById(tab);
	if (id == "centerDiv") {
		tmp = "fieldDiv";
		tmpTab = "fieldTab";
	}

	div.style.visibility = "visible";
	divTab.style.backgroundColor = "white";
	divTab.style.cursor = "default";
	
	document.getElementById(tmp).style.visibility = "hidden";
	document.getElementById(tmpTab).style.backgroundColor = "gray";
	document.getElementById(tmpTab).style.cursor = "pointer";
	
	_currentId = id;
}

function send(name,page) {
	if (page) {
		document.getElementsByName('current_page_num')[0].value = page;
	}
	document.forms[name].submit();

	return false;
}

function setdisable() {
	var radios = document.getElementsByName('category');
	for (var i = 0; i < radios.length; i++) {
		if (!radios[i].value) {
			radios[i].disabled = true;
		}
	}
}

function clearCheck(name,page) {
	var checks = document.getElementsByName('keyword[]');
	for (var i = 0; i < checks.length; i++) {
		checks[i].checked = false;
	}

	send(name,page);
}

function moveTop() {
  var ch = getClientHeight();
  var sh = getEl().scrollHeight;
  var box = document.getElementById('mc_box');
  if (!box) {
	window.scrollTo(0,180);
	return;
  }
  
  var top = sh - ch - 50;
  window.scrollTo(0,top);
}


