Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

/////////////////////////////
PhotoFrame = Class.create();
PhotoFrame.prototype = {
initialize: function(fname, width, height, large) {
    this.id = 'image-base';
    this.fname = fname;
    this.width = width;
    this.large = large;
    this.height = height;
    
    
    this.setSize(width, height);
    new Effect.Appear(this.id, {duration: 0.2 });
    
    this.loadimg = new Image();
    this.loadimg.src = fname;

    if(this.loadimg.complete) {
        this.showImage();
    } else {
        if(navigator.userAgent.indexOf("Safari") != -1) {
            // Safariでは画像のonloadイベントが立たないので
            this.safariOnLoad = function() {
                if(this.loadimg.complete) {
                    clearTimeout(this.timer);
                    this.showImage();
                } else 
                    this.timer = setTimeout(this.safariOnLoad.bind(this), 100);
                };
            this.safariOnLoad();
        } else {
            Event.observe(this.loadimg, 'load', this.showImage.bind(this), false);
        }
    }
},
showImage: function() {
    this.show = true;
    var image = $('showimg');
    var spinner = $('spinner');
    Element.hide(spinner);

    new Effect.Appear($('comment-space'), {duration: 0.4, queue: 'end'});
    image.src = this.loadimg.src;
    new Effect.Appear(image, {from: 0, duration: 0.2, queue: 'end'});
    $('overlay').onclick=function(){
		remove_show();
	}
},
windOpen:function(){
	window.open(this.large);
},
setSize: function(w, h) {
    var frame = $(this.id);
    var image = $('showimg');
    var spinner = $('spinner');
    
    var fw = w;
    var fh = h;
    Element.hide(image);
    
    frame.style.textAlign="left";
    /////////////////////////////////////
    var wCur = Element.getWidth($(this.id));
    var hCur = Element.getHeight($(this.id));
    var xScale = ((fw  + (5 * 2)) / wCur) * 100;
    var yScale = ((fh  + (5 * 2)) / hCur) * 100;
    new Effect.Scale(frame,yScale, {scaleX:false, duration: 0.4, queue: 'front',scaleFromCenter:true});
    new Effect.Scale(frame,xScale, {scaleY:false, delay: 0.2, duration: 0.4,scaleFromCenter:true,afterFinish:setComm});
    //////////////////////////////////
    image.style.width = '180px';
    image.style.height = '324px';
    TB_position($('image-base'));
}
};

function setComm(){
	TB_position($('image-base'));
}
/////////////////////////
function ovl(urls){
	if($('kdomain')) $('kdomain').style.visibility = "hidden";
	if(document.getElementById('overlay')==null){
		var waku = document.createElement('div');
		var base = document.createElement('div');
		var th_comm = document.createElement('div');
		waku.setAttribute("id", "overlay");
		waku.style.position = 'absolute';
		waku.style.zIndex = '1000';
		//var Ps = TB_getPageSize();
		var Ps = TB_getPageScrollTop();
		waku.style.width = '100%';
		waku.style.height= Ps[1]+'px';
		document.body.innerHTML+='<img id="spinner" src="/img/parts/load.gif" alt="loading" style="position:absolute;z-index:1200;" />';
		base.setAttribute("id", "image-base");
		base.style.position = 'absolute';
		base.style.zIndex = '1005';
		base.style.width = '200px';
		base.style.height= '200px';
		base.innerHTML='<div id="show-space"><img src="/img/parts/dummy.gif" alt="" id="showimg" /></div>';
		th_comm.setAttribute("id", "comment-space");
		document.body.appendChild(waku);
		TB_overlaySize($('overlay'));
		document.body.appendChild(base);
		$('image-base').appendChild(th_comm);
		$('show-space').style.position='absolute';
		$('show-space').style.top='10px';
		$('show-space').style.left='10px';
		$('show-space').style.height='324px';
		//$('showimg').style.height='324px';
		//$('overlay').onclick=function(){
		//	remove_show();
		//}
	}else{
		$('overlay').onclick=null;
	}
	if($('spinner').style.display == 'none'){
		Element.show($('spinner'));
	}


		$('showimg').src="/img/parts/dummy.gif";
		Element.show($('spinner'));
		Element.hide($('comment-space'));
		if(TB_position($('image-base'))){
			show_illust('http://foz.jp/z/'+urls+'/');
		}

		window.onscroll=function(){
			TB_position($('image-base'));
		}
		window.onresize=function(){
			TB_position($('image-base'));
		}
		window.oncontextmenu=function(){
		}
	return false;
}

function show_illust(urls){
	var pars = 'parsexml';
	var myAjax = new Ajax.Request(
			urls, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: showResponsei
			});

}
function showResponsei(originalRequest){
		var items = eval( '(' + originalRequest.responseText + ')' );
		//alert(items.title+items.illust+items.time+items.bookmark+items.trimt+items.trimtext+items.postname+items.lprev+items.lnext+items.screenI+items.screenEz+items.screenYh);
		$('showimg').src=items.illust;
		$('comment-space').innerHTML='<h2>'+items.title+'</h2>';

		var tempsrc = '<p>縦サイズ（320～432）と画像形式（jpg or gif）を選択できます。<br />';
		tempsrc += '必要項目を入力選択してボタンを押すとQRコードが生成されます。</p>';
		tempsrc += '<form method="" action="">';
		tempsrc += '横240px×縦<input type="text" name="ht" id="ht" value="320" size="5" />px ';
		tempsrc += '<input type="radio" name="ftype" id="" value="jpeg" checked="checked" />jpg ';
		tempsrc += '<input type="radio" name="ftype" id="" value="gif" />gif ';
		tempsrc += '<input type="hidden" name="fn" id="fn" value="'+items.postname+'" />';
		tempsrc += '<input type="hidden" name="tr" id="tr" value="'+items.trimt+'" />';
		tempsrc += '<input type="button" name="" id="" value="QR生成" onclick="changeimg(this.form.ftype)" />';
		tempsrc += '</form>';
		tempsrc += '<p>※この画像の縦サイズは <strong>'+items.trimtext+'</strong> を基準に変更されます。</p>';
		tempsrc += '<img src="/img/dummy.gif" id="qrimg" alt="qrcode" class="left" />';
		tempsrc+='<p><a href="'+items.bookmark+'#commentform" title="コメント" class="comcnt"><img src="/img/com.gif" alt="" /> コメント('+items.comcnt+')</a></p>';
		tempsrc+='<p id="readQr">　</p>';
		tempsrc+='<div id="screenInfoLinks">';
		tempsrc+='<h4>機種別画面サイズ情報</h4>';
		tempsrc+='<p>各公式ページに移動します。</p>';
		tempsrc+='<ul>';
		tempsrc+='<li><a href="'+items.screenI+'" target="_blank">i mode</a></li>';
		tempsrc+='<li><a href="'+items.screenEz+'" target="_blank">ez web</a></li>';
		tempsrc+='<li><a href="'+items.screenYh+'" target="_blank">softbank</a></li>';
		tempsrc+='</ul>';
		tempsrc+='</div>';
		tempsrc+='<div id="view-btn-sets">';
		if(items.lnext){
			tempsrc+='<a href="javascript:;" onclick="ovl(\''+items.lnext+'\');return false" title="new"><img src="/img/new.gif" alt="new" /></a>';
		}else{
			tempsrc+='<img src="/img/new2.gif" alt="new" />';
		}
		tempsrc+='<a href="javascript:;" onclick="remove_show()" title="閉じる"><img src="/img/close.gif" alt="閉じる" class="close" /></a>';
		if(items.lprev){
			tempsrc+='<a href="javascript:;" onclick="ovl(\''+items.lprev+'\');return false" title="old"><img src="/img/old.gif" alt="old" /></a>';
		}else{
			tempsrc+='<img src="/img/old2.gif" alt="old" />';
		}
		tempsrc+='</div>';
		$('comment-space').innerHTML+=tempsrc;

		new PhotoFrame(items.illust,600,370);
		nowitem=items.bookmark;

}
function remove_show(){
	Element.remove($('spinner'));
	new Effect.Fade($('image-base'),{duration: 0.4,delay: 0,afterFinishInternal: function(effect) {Element.remove($('image-base'));}});
	//new Effect.Fade($('comment-space'),{duration: 0.4,delay: 0,afterFinishInternal: function(effect) {Element.remove($('comment-space'));}});
	new Effect.Fade($('overlay'),{duration: 0.4,delay: 0,afterFinishInternal: function(effect) {Element.remove($('overlay'));}});
	if($('kdomain')) $('kdomain').style.visibility = "visible";
}
function TB_overlaySize(a){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
  	a.style.height= yScroll +"px";
}
function TB_position(a) {
	var pagesize = TB_getPageSize();
	var arrayPageScroll = TB_getPageScrollTop();
	a.style.left=(arrayPageScroll[0] + (pagesize[0] - parseInt(a.style.width.replace(/px/,"")))/2)+"px";
	a.style.top=(arrayPageScroll[1] + (pagesize[1]-50-parseInt(a.style.height.replace(/px/,"")))/2)+"px";
	$('spinner').style.left=(arrayPageScroll[0] + (pagesize[0])/2)-12+"px";
	$('spinner').style.top=(arrayPageScroll[1] + (pagesize[1]-50)/2)-12+"px";
	/*$('comment-space').style.top=parseInt(a.style.top.replace(/px/,""))+parseInt(a.style.height.replace(/px/,""))+'px';
	$('comment-space').style.left=a.style.left;
	$('comment-space').style.width=a.style.width;*/
	
	return true;
}
function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}

function TB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

function getMouseX(e){
  if(window.opera)   
          return e.clientX
  else if(document.all)   
          return document.body.scrollLeft+event.clientX
  else if(document.layers||document.getElementById)
          return e.pageX
}
function getMouseY(e){
  if(window.opera)   
          return e.clientY
  else if(document.all)   
          return document.body.scrollTop+event.clientY
  else if(document.layers||document.getElementById)
          return e.pageY
}




//----------------------------
function fozmailer(){
	if($F('mname') == ''){
		$('errorsp').innerHTML = '<em>メールネームをご記入ください。</em>';
		$('errorsp').style.display = '';
	}else if($F('kdomain') == '---' || $F('kdomain') == ''){
		$('errorsp').innerHTML = '<em>ドメインを選択してください。</em>';
		$('errorsp').style.display = '';
	}else{
		$('errorsp').style.display = 'none';
		var mn = $F('mname');
		var kd = $F('kdomain');
		var url = 'http://foz.jp/lib/mail.php';
		var pars = 'mode=send2&mname=' + mn + '&kdomain=' + kd;
		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				postBody: pars, 
				onLoading: showLoading,
				onComplete: showResponse
			});
	}
	return false;
}
////loading
function showLoading(originalRequest){
	$('errorsp').innerHTML = '<img src="/img/loading.gif" width="62" height="10" alt="sending..." />';
	$('errorsp').style.display = '';
}
////complete
function showResponse(originalRequest){
	//var thumbs = eval( '(' + originalRequest.responseText + ')' );
	var res = originalRequest.responseText;
	if(res == 'success'){
		$('mailform').innerHTML = '<p><strong>送信完了！</strong><br />ご記入されたメールアドレスにURLを送信しました。</p>';
	}else if(res == 'inputError'){
		$('errorsp').innerHTML = '<em>必要項目をご記入ください。</em>';
		$('errorsp').style.display = '';
	}else{
		$('errorsp').innerHTML = '<em>エラー</em>';
		$('errorsp').style.display = '';
	}
}