//　別ウィンドウを開く
new function() {
	/*	初期設定	*/
	function init() {
	  
	  //指定されている画像にクリックイベントを追加
	  if(!document.images){return;}
    var imgs = document.images;
    for(var i=0;i<imgs.length;i++){
			if(imgs[i].getAttribute("pddWindowOpen") != null) {
				//OPENイベントの追加
				addEvent(imgs[i],'click',function(){openWindow(this);});
				
			}
			if(imgs[i].getAttribute("pddWindowClose") != null) {
				//CLOSEイベントの追加
				//addEvent(imgs[i],'click',function(){closeWindowImg(this);});
				
			}
		}
		
		//不透明背景の追加
		var background = document.createElement('div');
    var alpha = 70;
    background.id = "pddWindowBackGround";
    background.currentAlpha = alpha;
    background.style.opacity = alpha/100;
    background.style.filter = 'alpha(opacity='+alpha+')';
    background.style.position = 'absolute';
    background.style.top = '0px';
    background.style.left = '0px';
    background.style.width = document.body.scrollWidth;
		//background.style.height = '100%';
    background.style.height = document.body.scrollHeight;
    background.style.zIndex = "900";
    background.style.cursor = 'pointer';
   
    background.style.background = '#000000';
    //background.style.visibility = 'hidden';
    background.style.display = 'none';
		//枠外をクリックしたときのCLOSEイベント
		addEvent(background,'click',function(){closeWindowBackGround();});

		document.body.insertBefore(background , document.body.firstChild);

    for(var i=0;i<imgs.length;i++){
			if(imgs[i].getAttribute("pddWindowOpen") != null) {
				var winid = imgs[i].getAttribute("pddWindowOpen");
				var elm = document.getElementById(winid);
				elm.style.display = "none";
			}
		}

	}
	
	//　ウィンドウを開く
	function openWindow(img) {

		var winid = img.getAttribute("pddWindowOpen");
		
		//詳細用ウィンドウの設定
		var elm = document.getElementById(winid);
    var alpha = 10;
		elm.currentAlpha = alpha;
    elm.style.opacity = alpha/100;
    elm.style.filter = 'alpha(opacity='+alpha+')';

		var win_width = 600;
		var win_height = 400;
		var winsize = img.getAttribute("pddWindowSize");
		if (winsize != null) {
      var temp = winsize.split(',');
			win_width = temp[0];
			win_height = temp[1];
		}
		
		elm.style.width = win_width;
		elm.style.height = win_height;

		var userAgent = window.navigator.userAgent;
		if (userAgent.indexOf("Firefox") >= 0) {
			elm.style.height = Number(win_height) + 0;
		}
    elm.style.border = '2px solid #333333';
    elm.style.position = 'absolute';
    elm.style.zIndex = "999";
		elm.style.left = (document.body.clientWidth - win_width) / 2;
		elm.style.top = document.body.scrollTop + ((document.body.clientHeight - win_height) / 2);
		
		//クローズボタンが無ければ追加
		var items = elm.childNodes;
		var button_flag = false;
    for(var i=0;i<items.length;i++){
    	if (items[i].tagName == "IMG") {
				if (items[i].getAttribute("pddWindowClose") != null) {
					button_flag = true;
					break;
				}
			}
		}

		if (!button_flag) {
			var close_image = document.createElement('img');
			close_image.src = "img/common/window_close.jpg";
			close_image.style.marginLeft = win_width - 90;
			close_image.setAttribute("pddWindowClose","close");
    	close_image.style.cursor = 'pointer';
    	close_image.style.marginTop = '10px';
			addEvent(close_image,'click',function(){closeWindowImg(this);});
			//elm.insertBefore(close_image , elm.lastChild);
			elm.appendChild(close_image);
		
		}
		
		//表示
		document.getElementById("pddWindowBackGround").style.display = 'inline';
		setTimeout(function() {
			document.getElementById(winid).style.display = "inline";
			setFader(document.getElementById(winid),100);
		}, 250);
		
	}
	
	//　ウィンドウを閉じる
	function closeWindowImg(img) {
		var par = img.parentNode;
		par.style.display = "none";
		document.getElementById("pddWindowBackGround").style.display = 'none';
	}
	function closeWindowBackGround() {
    var imgs = document.images;
    for(var i=0;i<imgs.length;i++){
			if(imgs[i].getAttribute("pddWindowOpen") != null) {
				var winid = imgs[i].getAttribute("pddWindowOpen");
				var elm = document.getElementById(winid);
				elm.style.display = "none";
			}
		}
		document.getElementById("pddWindowBackGround").style.display = 'none';
	}
	
  // 指定要素を指定透明度にするためのフェードアニメを設定する関数

  function setFader(targetObj,targetAlpha){
      targetObj.targetAlpha = targetAlpha;
      if(targetObj.currentAlpha==undefined){
          targetObj.currentAlpha = 100;
      }
      if(targetObj.currentAlpha==targetObj.targetAlpha){
          return;
      }
      if(!targetObj.fading){
          if(!targetObj.fader){
              targetObj.fader = fader;
          }
          targetObj.fading = true;
          targetObj.fader();
      }
  }

  // アルファ値をターゲット値に近づける関数
  // ターゲット値になったら終了

  function fader(){
      this.currentAlpha += (this.targetAlpha - this.currentAlpha)*0.2;
      if(Math.abs(this.currentAlpha-this.targetAlpha)<1){
          this.currentAlpha = this.targetAlpha;
          this.fading = false;
      }
      var alpha = parseInt(this.currentAlpha);
      this.style.opacity = alpha/100;
      this.style.filter = 'alpha(opacity='+alpha+')';
      if(this.fading){
          var scope = this;
          setTimeout(function(){fader.apply(scope)},30);
      }
  }

  // イベントを追加する関数
  function addEvent(eventTarget, eventName, func){
      if(eventTarget.addEventListener){
          // モダンブラウザ
          eventTarget.addEventListener(eventName, func, false);
      }else if(window.attachEvent){
          // IE
          eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
      }else if(eventTarget.attachEvent){
          // IE
          eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
			} else {
				eventTarget['on' + eventName] = func;
			}
  }

  addEvent(window,'load',init);

}
