// Requires infobox.css in /css/ and style images in /css/images/infobox/


function createinfobox(){
	
	tt = document.getElementById('infobox')

	if(!tt){	
		infbox = document.createElement("div");	
		infbox.id = "infobox";
//		infbox.style.width="400px"
		infbox.style.width="auto"
		infbox.style.height="auto"
		infbox.style.margin="auto"
			
		document.body.appendChild(infbox);
		var ht=''
		ht+='<table id="infobox_table" cellpadding=0 cellspacing=0 border=0>'
		ht+='<tr><td class="box_tl"><img border=0 width=25 height=25 src="/css/images/infobox/pixel.gif"></td><td class="box_t"></td><td class="box_tr"><img border=0 width=25 height=25 src="/css/images/infobox/pixel.gif"></td></tr>'
		ht+='<tr><td rowspan=2 class="box_l"></td><td valign=middle class="box_cont"><table class="infoboxtitle_bg" cellpadding=0 cellspacing=0 border=0><tr><td width=99%><div id="infoboxtitle"></div></td><td width=1%><img alt="close" vspace=4 hspace=4 align=right src="/css/images/infobox/icon_close.jpeg" onclick="infoboxclose()"></td></tr></table>'
		ht+='<div id="infoboxmsg"></div>'
		ht+='<div id="infoboxstat"></div>'		
		ht+='</td><td rowspan=2 class="box_r"></td></tr>'
		ht+='<tr><td align=right class="box_cont">'
//		ht+='<hr size=1><input type=button value="Close" onclick="infoboxclose()">'
		ht+='</td></tr>'
		ht+='<tr width=300><td class="box_bl"></td><td  class="box_b"></td><td class="box_br"></td></tr></table>'
		infbox.innerHTML=ht

	//make dragable 
		new Draggable( 'infobox', {handle: 'infoboxtitle'});
 

	}

}

function infoboxclose(){
	infboxmsg.innerHTML=''
	infbox.style.display="none"
}

function infoboxstat(msg,adto){
	if(!document.getElementById('infobox')){return}
	infboxstat = document.getElementById('infoboxstat')
	if(adto){infboxstat.innerHTML+= msg}
	else{infboxstat.innerHTML= msg}
}

function handler_youtube(msg,w,h){
	if(!w){w=320}
	if(!h){h=Math.round(w*.63)}
	var htm=''
	htm+='<object id="videoplayer" width="'+w+'" height="'+h+'"><param name="movie" value="'+msg+'"></param><param name="play" value="true"></param>'
	htm+='<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
	htm+='<embed id="vidplay" name="videoplayer" src="'+msg+'" type="application/x-shockwave-flash" allowscriptaccess="always" play="true" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed></object>'
	return(htm)	
}


function handler_htm(msg,w){
	var htm=''
	htm+='<iframe id="htm" name="htm" width=400 height=400 frameborder=0 class="infobox_frame" scrolling=auto src="'+msg+'"></iframe>'
	return(htm)	
}

function handler_img(msg,w){
	var htm=''
	ww = ''
	if(w){ww='width="'+w+'"'}
	
	
//	htm+='<iframe id="htm" name="htm" width=400 height=400 frameborder=0 class="infobox_frame" scrolling=auto src="'+msg+'"></iframe>'
	htm+='<img '+ww+' src="'+msg+'">'

	return(htm)	
}

function handlers(msg,w){
	htmext = '.php,.htm,.html'
	imgext = '.jpeg,.jpg,.gif,.png'
	if(msg.indexOf(' ')!=-1){return(msg)}
	if(!msg.indexOf('http://www.youtube.com')){return(handler_youtube(msg,w))}
	if(htmext.indexOf(getext(msg))!=-1){return(handler_htm(msg,w))}
	if(imgext.indexOf(getext(msg))!=-1){return(handler_img(msg,w))}
	return(msg)	
}

function getexto(str){
	return(str.substr(str.lastIndexOf('.') + 1));	
}

function getext(str){   
	var ext = /^.+\.([^.]+)$/.exec(str);   return ext == null ? "" : ext[1]; 
} 

function RealTypeOf(v) {
  if (typeof(v) == "object") {
    if (v === null) return "null";
    if (v.constructor == (new Array).constructor) return "array";
    if (v.constructor == (new Date).constructor) return "date";
    if (v.constructor == (new RegExp).constructor) return "regex";
    return "object";
  }
  return typeof(v);
}


function infobox_prompt(msgtitle,defmsg,callback){

	typ = RealTypeOf(infobox_var)
	
	var htm=''

	if(typ=='string'){
		htm+='<form id="infobox_form">'
		htm+='<input id="val" type="text" size=45 value="'+defmsg+'"><br><br>'
		htm+='<input type="button" class="button" value=" OK " onclick="'+callback+'(this.form.val.value);infoboxclose();"></form>'
	}
	else{
		alert('Error')
		return
	}

	infobox(msgtitle,htm,'',300)
	
}	

function infobox(msgtitle,msg,delayclose,w){

	if(msgtitle=='close' && !msg){infoboxclose();return}

	msg = handlers(msg,w)

	if(w){w=parseInt(w)+50}

	if(!document.getElementById('infobox')){createinfobox()}

	if(!isNaN(delayclose) && delayclose!=''){
		tt = setTimeout('infoboxclose()',delayclose);
	}

	if(msg=='password'){
		w=300
		msg='<form><input name="pw" type="password" size=30><input type="button" class="but" value="Ok" onclick="dopassword(this.form.pw.value)"></form>'
	}


	infbox.style.display="block"
	infbox.style.visibility="visible"

	if(w){document.getElementById('infobox_table').style.width= w +'px'}
	else{document.getElementById('infobox_table').style.width= 'auto'}

	if(msg.length <100){document.getElementById('infobox_table').style.width= '400px'}

	infboxmsg = document.getElementById('infoboxmsg')
	infboxtit = document.getElementById('infoboxtitle')
	infboxtit.innerHTML= msgtitle
	infboxmsg.innerHTML= msg

//	divcenter('infobox')
	centerWindow('infobox')
	$('infobox').setStyle({'top': 200 });	
}


function centerWindow(element) {     
	if($(element) != null) {          
		if(typeof window.innerHeight != 'undefined') {  
			
				ttop =  Math.round(document.viewport.getScrollOffsets().top +  ((window.innerHeight - $(element).getHeight()))/2)+'px';             
				if(ttop<0){ttop=0}
			$(element).style.top =  ttop             
			$(element).style.left =                     
				Math.round(document.viewport.getScrollOffsets().left +                     
				((window.innerWidth - $(element).getWidth()))/2)+'px';          
			} else {               
				ttop  = Math.round(document.body.scrollTop +  (($$('body')[0].clientHeight - $(element).getHeight()))/2)+'px';   
				if(ttop<0){ttop=0}
				$(element).style.top =  ttop                   
				            
			$(element).style.left =                     
				Math.round(document.body.scrollLeft +                     
				(($$('body')[0].clientWidth - $(element).getWidth()))/2)+'px';
 	}     }
}



function centerdiv(dobj,cx,cy){
	
	var w = parseInt(dobj.style.width)
	var h = parseInt(dobj.style.height)

	if(window.pageYOffset){yo=window.pageYOffset}else{yo=document.body.scrollTop}
	if(window.pageXOffset){xo=window.pageXOffset}else{xo=document.body.scrollLeft}

	getpagedims()
	
	if(cx){
	if(cx==1){dobj.style.left = ((winw/2) - (w/2)) +xo}
	else{dobj.style.left =cx+xo}
	}

	if(cy){
	if(cy==1){dobj.style.top = ((winh/2) - (h/2)) +yo}
	else{dobj.style.top =cy+yo}
	}
}

function getpagedims(){     

	windowsize()

	if (window.innerHeight && window.scrollMaxY) {
		// Firefox         
		pageh = window.innerHeight + window.scrollMaxY;         
		pagew = window.innerWidth + window.scrollMaxX;     

	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ 
		// all but Explorer Mac         
		pageh = document.body.scrollHeight;         
		pagew = document.body.scrollWidth;     } 
	else { 
	// works in Explorer 6 Strict, Mozilla (not FF) and Safari         
		pageh = document.body.offsetHeight;         
		pagew = document.body.offsetWidth;       }     

} 

function windowsize() {
  winw = 0, winh = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winw = window.innerWidth;
    winh = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winw = document.documentElement.clientWidth;
    winh = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winw = document.body.clientWidth;
    winh = document.body.clientHeight;
  }
//  window.alert( 'Width = ' + winw );
//  window.alert( 'Height = ' + winh );
}

