var popup_tmpPic = new Image;
var p_imgHeight;
var p_imgWidth;

var p_shadow;
var p_body;
var p_close;
var p_center;
var p_img;

popup_tmpPic.onload=function(){
	p_img.src=popup_tmpPic.src;
	p_img.style.width=p_imgWidth+'px';
	p_img.style.height=p_imgHeight+'px';
	p_center.style.padding='0px';
}

//Simple Popup
function create_popup(){
	p_shadow=document.body.appendChild(document.createElement('div'));
	p_shadow.className='popup_shadow';
	p_body=document.body.appendChild(document.createElement('div'));
	p_body.className='popup_body';
	p_close=p_body.appendChild(document.createElement('div'));
	p_close.className='popup_close';
	p_close.innerHTML='<span onClick="showimg(null);">&times</span>';
	p_center=p_body.appendChild(document.createElement('div'));
	p_center.className='popup_center';
	p_img=p_center.appendChild(document.createElement('img'));
	p_img.src='/img/loading.gif';
	p_img.onclick=function(){showimg(null)};
}

function showimg(Src,Width,Height){
	if(Src){
		if(!p_body)create_popup();
		var img=document.getElementById('p_image');
		p_img.src='/img/loading.gif';
		p_img.style.height='100px';
		p_img.style.width='100px';
		
		p_imgHeight=((Height+60)>document.body.clientHeight?document.body.clientHeight-60:Height);
		p_imgWidth=Math.round(p_imgHeight*Width/Height);
				
		p_center.style.padding=Math.ceil(p_imgHeight/2-50)+'px 0px '+(p_imgHeight-Math.ceil(p_imgHeight/2)-50)+'px 0px';
		p_body.style.width=(p_imgWidth+50)+'px';
		p_body.style.margin='-'+Math.ceil(p_imgHeight/2+25)+'px 0px 0px -'+Math.ceil(p_imgWidth/2+25)+'px';

		if(p_imgHeight<Height)popup_tmpPic.src='/resize_h'+p_imgHeight+Src;
		else popup_tmpPic.src=Src;

		p_shadow.style.visibility='visible';
		p_body.style.visibility='visible';
	} else {
		p_shadow.style.visibility='hidden';
		p_body.style.visibility='hidden';
	}
}

function preview_border(event,on){
	source=(event.srcElement?event.srcElement:event.target);
	source.style.borderColor=(on?'#009DDB':'#FFF');
}

//Gallery Popup
var p_left;
var p_right;
var p_comment;
var CurNum;

function create_gallery_popup(){
	if(!p_shadow){
		p_shadow=document.body.appendChild(document.createElement('div'));
		p_shadow.className='popup_shadow';
	}
	p_body=document.body.appendChild(document.createElement('div'));
	p_body.className='popup_body';
	p_close=p_body.appendChild(document.createElement('div'));
	p_close.className='popup_close';
	p_close.innerHTML='<span onClick="gallery_popup(null);">&times</span>';
	p_left=p_body.appendChild(document.createElement('div'));
	p_left.className ='gpopup_arrowleft';
	p_left.innerHTML ='<span onclick="eval(Photos[CurNum==1?CurNum=MaxNum:--CurNum]);">&lt;</span>';
	p_right=p_body.appendChild(document.createElement('div'));
	p_right.className='gpopup_arrowright';
	p_right.innerHTML='<span onclick="eval(Photos[CurNum==MaxNum?CurNum=1:++CurNum]);">&gt;</span>';
	p_center=p_body.appendChild(document.createElement('div'));
	p_center.className='gpopup_center';
	p_img=p_center.appendChild(document.createElement('img'));
	p_img.src='/img/loading.gif';
	p_img.onclick=function(){eval(Photos[CurNum==MaxNum?CurNum=1:++CurNum]);};
	p_comment=p_body.appendChild(document.createElement('div'));
	p_comment.className='gpopup_comment';
}

function gallery_popup(Src,Width,Height,Comment){
	if(Src){
		if(!p_body)create_gallery_popup();
		var img=document.getElementById('p_image');
		p_img.src='/img/loading.gif';
		p_img.style.height='100px';
		p_img.style.width='100px';
		
		p_imgHeight=((Height+100)>document.body.clientHeight?document.body.clientHeight-100:Height);
		p_imgWidth=Math.round(p_imgHeight*Width/Height);
				
		p_center.style.padding=Math.ceil(p_imgHeight/2-50)+'px 0px '+(p_imgHeight-Math.ceil(p_imgHeight/2)-50)+'px 0px';
		p_body.style.width=(p_imgWidth+100)+'px';
		p_body.style.margin='-'+Math.ceil(p_imgHeight/2+38)+'px 0px 0px -'+Math.ceil(p_imgWidth/2+75)+'px';

		arrowPadding=(Math.ceil(p_imgHeight/2)-15)+'px ';
		p_left.style.paddingTop=arrowPadding;
		p_right.style.paddingTop=arrowPadding;
		p_comment.innerHTML=Comment;
		
		if(p_imgHeight<Height)popup_tmpPic.src='/resize_h'+p_imgHeight+Src;
		else popup_tmpPic.src=Src;
		
		p_shadow.style.visibility='visible';
		p_body.style.visibility='visible';
	} else {
		p_shadow.style.visibility='hidden';
		p_body.style.visibility='hidden';
	}
}

function gallery(Num){
	CurNum=Num;
	eval(Photos[Num]);
}

//Video Popup
var v_body;
var v_close;
var v_center;
var v_left;
var v_right;

function create_video_popup(){
	if(!p_shadow){
		p_shadow=document.body.appendChild(document.createElement('div'));
		p_shadow.className='popup_shadow';
	}
	v_body=document.body.appendChild(document.createElement('div'));
	v_body.className='popup_body';
	v_close=v_body.appendChild(document.createElement('div'));
	v_close.className='popup_close';
	v_close.innerHTML='<span onClick="video_popup(null);">&times</span>';
	v_center=v_body.appendChild(document.createElement('div'));
	v_center.className='popup_center';
	
	v_left=v_center.appendChild(document.createElement('div'));
	v_left.className ='vpopup_left';
	v_right=v_center.appendChild(document.createElement('div'));
	v_right.className ='vpopup_right';
}

function video_popup(Src,preview,comment){
	if(Src){
		if(!v_body)create_video_popup();
		v_left.innerHTML='<div id="mediaspace">You need <a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank">Flash Player 9</a> and a browser with javascript!</div>';
		var so = new SWFObject('/player.swf','','360','290','9');
		so.addVariable("width","360");
		so.addVariable("height","290");
		so.addParam('allowfullscreen','true');
		so.addParam('allowscriptaccess','always');
		so.addParam('wmode','opaque');
		so.addVariable('file',Src);
		so.addVariable('backcolor','FFFFFF');
		so.addVariable('frontcolor','000000');
		so.addVariable('lightcolor','000000');
		so.addVariable('screencolor','000000');
		so.addVariable('autostart','true');
		so.write('mediaspace');
		
		v_right.innerHTML=comment;
		
		v_body.style.width='760px';
		v_body.style.height='340px';
		v_body.style.margin='-170px 0px 0px -380px';

		p_shadow.style.visibility='visible';
		v_body.style.visibility='visible';
	} else {
		v_left.innerHTML='';
		p_shadow.style.visibility='hidden';
		v_body.style.visibility='hidden';
	}
}

function videogallery(Num){
	eval(Videos[Num]);
}
