var im_width=200;
var im_height=100;

function pageWidth() {
	return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}
function pageHeight() {
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
} 
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
} 
function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}
function scrollFix(){
	var obol=document.getElementById('ol');
	obol.style.top=posTop()+'px';
	obol.style.left=posLeft()+'px'
}
function sizeFix(){
	var obol=document.getElementById('ol');
	obol.style.height=pageHeight()+'px';
	obol.style.width=pageWidth()+'px';
	if(document.getElementById('ol').style.display=='block' && document.getElementById('mbox').style.display=='block') picFix();
}
function picFix(){

		if((im_width>(pageWidth()-100)) && (im_height>(pageHeight()-80))) {
			if((im_width/im_height)>(pageWidth()/pageHeight())) {
				var mywidth=(pageWidth()-100);
				var myheight=Math.round(mywidth*(im_height/im_width));
			} else {
				var myheight=(pageHeight()-80);
				var mywidth=Math.round(myheight*(im_width/im_height));
			}
		} else if(im_width>(pageWidth()-100)) {
			var mywidth=(pageWidth()-100);
			var myheight=Math.round(mywidth*(im_height/im_width));
		} else if(im_height>(pageHeight()-80)) {
			var myheight=(pageHeight()-80);
			var mywidth=Math.round(myheight*(im_width/im_height));
		} else {
			var mywidth=im_width;
			var myheight=im_height;
		}

		document.getElementById('mbi').style.width=mywidth+'px';
		document.getElementById('mbi').style.height=myheight+'px';
		var tp=posTop()+((pageHeight()-myheight)/2)-12;
		var lt=posLeft()+((pageWidth()-mywidth)/2)-12;

		var obbx=document.getElementById('mbox');
		obbx.style.top=(tp<0?0:tp)+'px';
		obbx.style.left=(lt<0?0:lt)+'px';
		obbx.style.width=mywidth+'px';
		inf('hidden');
		obbx.style.display='block';
}

function kp(e){
	ky=e?e.which:event.keyCode;if(ky==88||ky==120)hm();return false
}

function inf(h){
	tag=document.getElementsByTagName('select');
	for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
	tag=document.getElementsByTagName('iframe');
	for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
	tag=document.getElementsByTagName('object');
	for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
}


function sm(obl){
	var obol=document.getElementById('ol');
	obol.style.height=pageHeight()+'px';
	obol.style.width=pageWidth()+'px';
	obol.style.top=posTop()+'px';
	obol.style.left=posLeft()+'px';
	obol.style.display='block';
	im=new Image();
	im.onload=function(){
		im_width=im.width;
		im_height=im.height;
		if((im_width>(pageWidth()-100)) && (im_height>(pageHeight()-80))) {
			if((im_width/im_height)>(pageWidth()/pageHeight())) {
				var mywidth=(pageWidth()-100);
				var myheight=Math.round(mywidth*(im_height/im_width));
			} else {
				var myheight=(pageHeight()-80);
				var mywidth=Math.round(myheight*(im_width/im_height));
			}
		} else if(im_width>(pageWidth()-100)) {
			var mywidth=(pageWidth()-100);
			var myheight=Math.round(mywidth*(im_height/im_width));
		} else if(im_height>(pageHeight()-80)) {
			var myheight=(pageHeight()-80);
			var mywidth=Math.round(myheight*(im_width/im_height));
		} else {
			var mywidth=im_width;
			var myheight=im_height;
		}
		document.getElementById('mbi').src=obl.href;
		document.getElementById('mbi').style.width=mywidth+'px';
		document.getElementById('mbi').style.height=myheight+'px';
		var tp=posTop()+((pageHeight()-myheight)/2)-12;
		var lt=posLeft()+((pageWidth()-mywidth)/2)-12;
		var obbx=document.getElementById('mbox');
		obbx.style.top=(tp<0?0:tp)+'px';
		obbx.style.left=(lt<0?0:lt)+'px';
		obbx.style.width=im.style.width;
		inf('hidden');
		obbx.style.display='block';
		document.onkeypress=kp;
		return false
	};
	im.src=obl.href
}


function hm(){
	document.getElementById('ol').style.display='none';
	document.getElementById('mbox').style.display='none';
	inf('visible');
	document.onkeypress=''
}


function initmb(){
	if(!document.getElementsByTagName)return;

	var anchors=document.getElementsByTagName('a');
	for(var i=0;i<anchors.length;i++){
		var an=anchors[i];
		if(an.getAttribute('href')&&/gif|jpe?g|png$/.test(an.getAttribute('href'))){
			an.onclick=function(){sm(this);return false}
		}
	}

	var obody=document.getElementsByTagName('body')[0];
	var frag=document.createDocumentFragment();

	var obol=document.createElement('div');
	obol.setAttribute('id','ol');
	obol.style.display='none';
	obol.style.position='absolute';
	obol.style.top=0;
	obol.style.left=0;
	obol.style.zIndex=998;
	//obol.style.width='100%';
	frag.appendChild(obol);

	var obbx=document.createElement('div');
	obbx.setAttribute('id','mbox');
	obbx.onclick=function(){hm();return false};
	obbx.style.display='none';
	obbx.style.position='absolute';
	obbx.style.zIndex=999;

	var obim=document.createElement('img');
	obim.setAttribute('id','mbi');

	obbx.appendChild(obim);

	frag.insertBefore(obbx,obol.nextSibling);
	obody.insertBefore(frag,obody.firstChild);

	window.onscroll = scrollFix;
	window.onresize = sizeFix;
}