var ImageoldLoad = window.onload;
window.onload=function(){
	if (typeof ImageoldLoad == 'function') {ImageoldLoad();}
	var imgTagList=document.getElementsByTagName("*");
	var s_pattern = new RegExp('(^|\\\\s)rcsb_image_small(\\\\s|$)');
	var m_pattern = new RegExp('(^|\\\\s)rcsb_image_medium(\\\\s|$)');
	var l_pattern = new RegExp('(^|\\\\s)rcsb_image_large(\\\\s|$)');
	if ( imgTagList.length > 0) {0.
		for (i = 0, j = 0; i < imgTagList.length; i++) {
			if ( ( s_pattern.test(imgTagList[i].className) ) || ( m_pattern.test(imgTagList[i].className) ) || ( l_pattern.test(imgTagList[i].className) ) ) {
				imgTagList[i].style.cursor = 'pointer';
				imgTagList[i].onclick = function() {
					var lg = rcsb_sb.show(this.getAttribute('rel'), this.getAttribute('title'), rcsb_image.get_img_src(this.getAttribute('rel'),'500',this.getAttribute('rev')));
				}
			}
			if ( s_pattern.test(imgTagList[i].className) ) {
				imgTagList[i].appendChild(rcsb_image.create(imgTagList[i].getAttribute('rel'), rcsb_image.get_img_src(imgTagList[i].getAttribute('rel'),'80',imgTagList[i].getAttribute('rev'))));
			}
			if ( m_pattern.test(imgTagList[i].className) ) {
				imgTagList[i].appendChild(rcsb_image.create(imgTagList[i].getAttribute('rel'), rcsb_image.get_img_src(imgTagList[i].getAttribute('rel'),'250',imgTagList[i].getAttribute('rev'))));
			}
			if ( l_pattern.test(imgTagList[i].className) ) {
				imgTagList[i].appendChild(rcsb_image.create(imgTagList[i].getAttribute('rel'), rcsb_image.get_img_src(imgTagList[i].getAttribute('rel'),'500',imgTagList[i].getAttribute('rev'))));
			}
		}
	}
}

var rcsb_image=function(){
	return {
		create:function(id, imgSrc) {
			var img = document.createElement('img');
			img.src = imgSrc;
			img.title = id;
			return img;
		}, 
		get_img_src:function(id, size, type) {
			var imgSrc
			if ( type == null ) {
				type = 'bio_1';
			}
			if ( type == 'asr' ) {
				imgSrc = "http://www.rcsb.org/pdb/images/"+id+"_asr_r_"+size+".jpg";
			} else {
				var typeArr = type.split("_");
				if (typeArr.length == 1) {
					typeArr[1] = '1';
				}
				imgSrc = "http://www.rcsb.org/pdb/images/"+id+"_"+typeArr[0]+"_r_"+size+".jpg?bioNum="+typeArr[1];
			}		
			return imgSrc;
		}
	};
}();


var rcsb_sb=function(){
	return {
		show:function(id, title, imgSrc) {
			var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
			var imgURL = "http://www.rcsb.org/pdb/explore/images.do?structureId=" + id;
			if ( title == null ) {
				title ="";
			} else {
				title = title + "<br />";
			}
			if (isIE6) {
				window.open(imgURL);
			} else {
				var bg = document.createElement('div');
				bg.setAttribute('id','shadow_bg');
				bg.style.cursor='pointer';
				bg.style.top='0';
				bg.style.left='0';
				bg.style.backgroundColor='#333333';
				bg.style.position='fixed';
				bg.style.width='100%';
				bg.style.height='100%';
				bg.style.filter='alpha(opacity=90)';
				bg.style.opacity='0.9';
				bg.style.zIndex='1001';
				bg.onclick = function() {
					img_done = document.getElementById('imgBox');
					document.body.removeChild(img_done);
					bg_done = document.getElementById('shadow_bg');
					document.body.removeChild(bg_done);
				}
				var imgBox = document.createElement('div');
				imgBox.setAttribute('id','imgBox');
				imgBox.style.cursor='pointer';
				imgBox.style.top='50%';
				imgBox.style.left='50%';
				imgBox.style.marginTop='-265px';
				imgBox.style.marginLeft='-250px';
				imgBox.style.background='#ffffff url('+imgSrc+') top center no-repeat';
				imgBox.style.position='fixed';
				imgBox.style.width='500px';
				imgBox.style.height='530px';
				imgBox.style.filter='alpha(opacity=100)';
				imgBox.style.opacity='1';
				imgBox.style.zIndex='1002';
				imgBox.onclick = function() {
					img_done = document.getElementById('imgBox');
					document.body.removeChild(img_done);
					bg_done = document.getElementById('shadow_bg');
					document.body.removeChild(bg_done);
					window.open(imgURL);
				}
				var closeBox = document.createElement('div');
				closeBox.style.cursor='pointer';
				closeBox.style.bottom='0';
				closeBox.style.right='0';
				closeBox.style.color='#333333';
				closeBox.style.position='absolute';
				closeBox.style.padding='10px';
				closeBox.style.fontFamily = 'Verdana, Geneva, sans-serif';
				closeBox.innerHTML = "CLOSE";
				closeBox.onclick = function() {
					img_done = document.getElementById('imgBox');
					document.body.removeChild(img_done);
					bg_done = document.getElementById('shadow_bg');
					document.body.removeChild(bg_done);
				}
				var idBox = document.createElement('div');
				idBox.style.cursor='pointer';
				idBox.style.bottom='0';
				idBox.style.left='0';
				idBox.style.color='#333333';
				idBox.style.position='absolute';
				idBox.style.padding='10px';
				idBox.style.marginRight='100px';
				idBox.style.fontSize='70%';
				idBox.style.fontFamily='Verdana, Geneva, sans-serif';
				idBox.innerHTML="<h2 style='margin:0;padding:0;'>"+id+"</h2>"+title+"(click for more images)";
				imgBox.appendChild(idBox);
				imgBox.appendChild(closeBox);
				document.body.appendChild(bg);
				document.body.appendChild(imgBox);
			}
		}
	};
}();