﻿var Popup = {
	
	linkCounter:0,
	
	links:null,
	
	images:[],
	
	currentLink:null,
	
	closebarHeight:27,
	
	//does the browser support position:fixed?
	hasFixedSupport:true,
	
	initialize:function(){
		if(typeof isIE6 != 'undefined')
			this.hasFixedSupport = false;
		
		var hasPopups = false;
				
		this.linkCounter = 0;
		this.links = [];
		
		var re_popup = /^popup/;
		//var re_popup_params = /^popup\[.*]$/gi;
		
		var anchors = document.getElementsByTagName('a');

		for(var i=0,len=anchors.length;i<len;i++){
			var hasParams = false;
			
			var a = anchors[i];
			var rel = a.rel;
			
			if(re_popup.exec(rel))
			{
				var params = [	'640',	//0 - width
								'480'];	//1 - height
				
				hasPopups = true;
				
				var id = this.getNextId();
				var url = a.href;				
				
				// Check to see if parameters are specified
				// if so, change the defaults
				var rel_length = rel.length;
				if(rel_length>7){
					var str = rel.substr(6,rel_length-7);
					var splt = str.split(',');
					for(var j=0,k=splt.length;j<k;j++){
						if(splt[j]!=''){
							params[j] = splt[j];
						}
					}
				}
				
				//check to see if the url is a direct link to an image
				var re = /\.gif$|\.jpg$|\.png$/gi;
				var image = re.exec(url)?true:false;
				this.images[id] = new Image();
				this.images[id].src = url;
				
				a.onclick =　new Function("Popup.open('"+id+"')");
				a.href = "javascript:void(0)";
				//a.removeAttribute("href");
				a.removeAttribute("target");
					
				this.links[id] = {url:url,width:params[0],height:params[1],image:image};
			}
		}
		// If the page has popups, inject popup HTML
		if(hasPopups){
			var sb = [];
			sb[sb.length] = '<div onclick="Popup.close()" id="popup_mask"></div>';
			sb[sb.length] = '<div id="popup_div">';
			sb[sb.length] = 	'<img id="popup_image" src="" border="0" >';		
			sb[sb.length] = 	'<iframe id="popup_frame" border="0"></iframe>';
			sb[sb.length] = 	'<div id="popup_closebar">';
			sb[sb.length] = 		'<span onclick="Popup.close()" style="float:right">&nbsp;&nbsp;閉じる&nbsp;&nbsp;</span>';
			sb[sb.length] = 		'<span onclick="Popup.newWindow()">&nbsp;&nbsp;新しいウィンドウで開く&nbsp;&nbsp;</span>';
			sb[sb.length] = 	'</div>';
			sb[sb.length] = '</div>';
			
			document.body.insertAdjacentHTML("beforeEnd",sb.join(''))
		}	
	},
	hideFlash:function(){
		ECC.flashControl.hide();
	},
	showFlash:function(){
		ECC.flashControl.show();
	},
	getNextId:function(){
		var id = "link"+this.linkCounter;
		this.linkCounter++;
		return id;
	},
	closeMask:function(){

	},	
	open:function(id){
		if(typeof this.links[id] == 'undefined')
			return;
		
		this.hideFlash();
		
		var link = this.links[id];
		this.currentLink = id;
		var ref_ms = $('popup_mask');
		var ref_dv = $('popup_div');
		var ref_im = $('popup_image');
		var ref_fr = $('popup_frame');

		ref_ms.style.display = "block";
		ref_dv.style.display = "block";
		
		if(link.image){
			ref_im.style.display = "block";	
			ref_im.src = link.url;			
		}else{
			ref_fr.style.display = "block";	
			ref_fr.src = link.url;
		}
		
		//popup_closebar.innerHTML = link.url + ">" + link.width + ":" + link.height;
		
		this.resize();
	},
	close:function(){
		this.showFlash();
		
		var ref_ms = $('popup_mask');
		var ref_dv = $('popup_div');
		var ref_im = $('popup_image');
		var ref_fr = $('popup_frame');
		
		ref_ms.style.display = "none";
		ref_im.style.display = "none";
		ref_fr.style.display = "none";
		ref_fr.src = "site/pop/blank.html";
		ref_dv.style.display = "none";
		ref_dv.style.overflow = "auto";
	},
	
	newWindow:function(){
		this.close();
		if(!this.currentLink)
			return;
		
		var link = this.links[this.currentLink];
		
		window.open(link.url,'_blank','width='+link.width+',height='+link.height+',resizeable=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes, resizable=yes')
	},
	
	resize:function(){
		if(!this.currentLink)
			return;
		
		var link = this.links[this.currentLink];

		var width = link.width;
		var height = link.height				
		
		var browser = getBrowserSize();
		
		if(height > browser.height || width > browser.width){
			this.newWindow();
			return;
		}
		
		var ref_dv = $('popup_div');
		var ref_fr = $('popup_frame');
		var ref_im = $('popup_image');		

		if(link.image){
			width = this.images[this.currentLink].width;
			height = this.images[this.currentLink].height+this.closebarHeight;
		}
		
		ref_fr.style.height = (height-this.closebarHeight)+"px";
		
		if(this.hasFixedSupport){
			var top = (browser.height - height)/2;
			var left = (browser.width - width)/2;
		}else{
			var top = (browser.height - height)/2 + browser.top;
			var left = (browser.width - width)/2 + browser.left;			
		}
		
		//alert(top + ':'+left)
		ref_dv.style.top = top+"px";
		ref_dv.style.left = left+"px"
		ref_dv.style.width = width+"px";
		ref_dv.style.height = height+"px";
	}
}

	function setPopups(){
		Popup.initialize();
	}
	
	function getBrowserSize(){
		var ret = {	width: document.documentElement.clientWidth,
					height:document.documentElement.clientHeight,
					top:   document.documentElement.scrollTop,
					left:  document.documentElement.scrollLeft
					}
		return ret;
	}
	