
/*	画像ズームクラス	*/

var setAlpha = function(id, opacity){
	var obj = document.getElementById(id);
	if(!obj)
		return;
	obj.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
	obj.style.MozOpacity = opacity;
	obj.style.opacity = opacity;
};

//
//  getPageSize()
//
var getPageSize= function() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

var zoomerInstance = null;
var showZoom = function(url,text,width,height) {
	zoomerInstance = new Zoomer();
	zoomerInstance.setup();
	var imgPreloader = new Image();
/*	imgPreloader.onload = (function(){
		if((!width) || (!height))
		{
			width = imgPreloader.width;
			height = imgPreloader.height;
		}
		zoomerInstance.setPhoto(url,text,width,height);
		zoomerInstance.show();
	}).bind(this);
*/
	Event.observe(imgPreloader, 'load', function(){
		if((!width) || (!height))
		{
			width = imgPreloader.width;
			height = imgPreloader.height;
		}
		zoomerInstance.setPhoto(url,text,width,height);
		zoomerInstance.show();
	}).bind(this);
	imgPreloader.src = url;
}

var closeZoom = function() {
	zoomerInstance.hide();
	delete zoomerInstance;
	zoomerInstance = null;
}

var Zoomer = Class.create({
	id_background:'zoomerbg',
	id_box: 'zoomerbox',
	id_frame: 'zoomerframe',
	id_body: 'zoomerbody',
	id_panelframe: 'zoomerpanelframe',
	id_panel: 'zoomerpanel',
	panelHtml: '<div style="float:left;"><a href="javascript:;" onclick="window.print();" />印刷</a></div><div style="float:right;"><a href="javascript:;" onclick="{0}">閉じる</a></div>',
	closure:'closeZoom',
	zoomx: 700,
	zoomy: 600,
	dur: 0.5,
	framepadding: 10,
	//	コンストラクタから呼ばれる初期化処理
	initialize: function() {
	},
	//	初期化処理
	setup: function() {
		var bg = this.insertElement(this.id_background, null, false);
		
		var box = this.insertElement(this.id_box, null, false);
		var frame = this.insertElement(this.id_frame, box, true);
		var body = this.insertElement(this.id_body, frame, true);
		
		var pframe = this.insertElement(this.id_panelframe, box, false);
		var panel = this.insertElement(this.id_panel, pframe, true);
		panel.innerHTML = this.getClosure();
	},
	//	要素を追加
	insertElement: function(id,parent,show,zorder) {
		if(!parent)
			parent = document.body;
		var element = document.createElement('div');
		element.id = id;
		if(show)
			element.style.display = 'block';
		else
			element.style.display = 'none';
		if(!zorder)
			element.style.zorder = zorder;
		parent.appendChild(element);
		return element;
	},
	getClosure: function() {
		 return this.panelHtml.replace('{0}',this.closure+'()');
	},
	setBodySize: function(cx,cy) {
		this.zoomx = cx;
		this.zoomy = cy;
	},
	setPhoto: function(url,alt,cx,cy) {
		if((cx>0) && (cy>0))
			this.setBodySize(cx,cy);
		var element = document.createElement('img');
		element.src = url;
		element.width = this.zoomx;
		element.height = this.zoomy;
		if(alt)
			element.alt = alt;
		$(this.id_body).appendChild(element);
	},
	//	表示
	show: function(fixed) {
		//	要素を表示
		var pad = this.framepadding*2;
		var size = getPageSize();
		if(size[0]<this.zoomx)
			size[0] = this.zoomx + pad;
		if(size[1]<this.zoomy)
			size[1] = this.zoomy + pad;
		$(this.id_body).hide();
		$(this.id_background).show();
		$(this.id_background).style.width = size[0]+'px';
		$(this.id_background).style.height = size[1]+'px';
		setAlpha(this.id_background,0.75);
		$(this.id_box).show();
	//	$(this.id_frame).hide();
		$(this.id_panel).hide();
		var margin = ((size[0]-this.zoomx)/2);
		$(this.id_frame).style.padding = '0px';
		$(this.id_frame).style.width = '10px';
		$(this.id_frame).style.height = '1px';
		$(this.id_panelframe).style.width = this.zoomx+'px';
		$(this.id_frame).style.lineHeight = $(this.id_background).style.lineHeight;
		$(this.id_panelframe).style.lineHeight = $(this.id_background).style.lineHeight;
		$(this.id_body).style.padding = '0px';
		if(fixed==true)
		{
			var offset = Position.realOffset(window.document.body);
			$(this.id_box).style.top = offset[1] + this.framepadding +'px';
			$(this.id_box).style.position = 'absolute';	//	IE6対応
		}
		new Effect.Scale(this.id_frame, this.zoomy*100, {scaleX: false, duration: this.dur, queue: 'front'});
		new Effect.Scale(this.id_frame, this.zoomx*10, {scaleY: false, duration: this.dur, delay: this.dur, afterFinishInternal: function(effect){
				$(this.id_frame).style.padding = this.framepadding+'px';
				$(this.id_panelframe).style.padding = this.framepadding+'px';
				$(this.id_frame).style.fontSize = $(this.id_background).style.font;
				$(this.id_panelframe).style.fontSize = $(this.id_background).style.font;
			}.bind(this)
		});
		new Effect.Appear(this.id_body, {from:0.1,to:1.0,fps:20,duration:this.dur, delay: this.dur*2});
		new Effect.SlideDown(this.id_panelframe,{from:0.0,to:1.0,fps:20,duration:this.dur, delay: this.dur*3});
		new Effect.Appear(this.id_panel, {from:0.1,to:1.0,fps:20,duration:this.dur, delay: this.dur*4, afterFinishInternal: function(effect){
				var size = getPageSize();
				var bg = $(this.id_background);
				if(bg)
					bg.style.height = size[1]+pad+'px';
			}.bind(this)
		});
	},
	//	非表示
	hide: function() {
		Effect.Fade(this.id_box,{duration:this.dur});
		Effect.Fade(this.id_background,{duration:this.dur});
	
		$(this.id_background).remove();
		$(this.id_box).remove();
	}
});
