/*
Copyrighted By bear_form@yahoo.com

Disclaimer:
you may not alter or make an illegal copy of these script below, or even gain knowledge from it, just use it at your own risk

how to use:
wpopup(url); or wpopup(url,options);

url => contain a local url or some internet site. ex: "http://someurl.com"
options => can be included or just left it empty, may contains:
name = unique name or id of the popup, remember if the name is blank or you define the same name for different popup then it will only open one window and close the other
width,height,left,top = the size of the popup
title = some text to be put on top of the popup
unclosable = hide close button if set
onclose = the name of the function that is triggered when the popup is closed
unblock = unblock the layer so that you can clearly view the background below the popup, default set to empty
resizable,minimize,fullscreen = under construction

wpopup_close(popup_name,onclose) => to close the popup via script, the variable 'popup_name' is the unique identifier name that is defined when the popup is opened, onclose is the function that is called when the popup is closed
*/

var imgPath = "images/";

/*popup function*/
function wpopup(url,option){
var p=parseProp(option);
if(p['width']==null || p['width']=="") p['width']="480";
p['left']=(screen.width-p['width'])/2;
if(p['left']<0) p['left']=0;
if(p['height']==null || p['height']=="") p['height']="320";
p['top']=(screen.height-p['height']-200)/2;
if(p['top']<0) p['top']=0;
if(p['name']=="" || p['name']==null) p['name']="wname";
if(p['title']=="" || p['title']==null) p['title']="&nbsp;";
if(p['bgcolor']=="" || p['bgcolor']==null) p['bgcolor']="#F1F1F1";
if(p['onclose']=="" || p['onclose']==null) p['onclose']="";

try{
document.body.removeChild(document.getElementById('wpopupid_'+p['name']));
}catch(e){}
try{
document.body.removeChild(document.getElementById('wpopupid_block_'+p['name']));
}catch(e){}
var otab = document.createElement("table");
otab.id='wpopupid_'+p['name'];
otab.style.position="fixed";
otab.style.left=p['left']+'px';
otab.style.top=p['top']+'px';
otab.style.width=p['width']+'px';
otab.style.height=p['height']+'px';
otab.cellPadding="0px";
otab.cellSpacing = "0px";
otab.style.border = "1px solid #848586";
otab.style.cursor='default';
var otbody = document.createElement("tBody");
var otr = document.createElement("tr");
var otd = document.createElement("td");
otd.noWrap=true;
otd.style.height="37px";
otd.style.paddingLeft="5px";
otd.style.backgroundColor = "#E6E6E6";
otd.style.backgroundImage = "url('" + imgPath + "popup_header_bg.jpg" + "')";
otd.align="left";
otd.innerHTML = '<font style="color:#777777;font-family:Tahoma;font-size:14px" ><b>'+p['title']+'</b></font>';
otr.appendChild(otd);

var otd = document.createElement("td");
otd.style.width='1%';
otd.style.paddingRight="5px";
otd.style.backgroundColor = "#E6E6E6";
otd.style.backgroundImage = "url('" + imgPath + "popup_header_bg.jpg" + "')";
if(p['unclosable']==null || p['unclosable']==''){
otd.innerHTML = '<a href="#" onclick=wpopup_close("'+p['name']+'","'+p['onclose']+'")><img align="right" alt="X" src="'+imgPath+'popup_close.jpg" border="0"></a>';
}
otr.appendChild(otd);
otbody.appendChild(otr);
otab.appendChild(otbody);

var otr = document.createElement("tr");
var otd = document.createElement("td");
otd.colSpan='2';
otd.style.padding='0px';
otd.style.margin='0px';
otd.style.height=(parseInt(p['height'])-'37')+'px';
otd.style.backgroundColor=p['bgcolor'];

var ifr= document.createElement('iframe');
ifr.id='wpopupframe_'+p['name'];
ifr.name='wpopupframe_'+p['name'];
ifr.frameBorder='0px';
ifr.style.border='0px';
ifr.style.height='100%';
ifr.style.width='100%';

otd.appendChild(ifr);
otr.appendChild(otd);
otbody.appendChild(otr);

//footer section
var otr = document.createElement("tr");
var otd_footer = document.createElement("td");
otd_footer.colSpan='2';
otd_footer.style.padding='0px';
otd_footer.style.margin='0px';
otd_footer.style.height='34px';
otd_footer.style.width='100%';
otd_footer.style.borderTop='1px solid #DADADA';
otd_footer.style.backgroundColor="#fff";
otd_footer.align='right';
otd_footer.innerHTML="<img src='"+imgPath+"popup_footer_logo.jpg'><br><div style='background-color:#848485;width:100%'>&nbsp;</div>";
otr.appendChild(otd_footer);
otbody.appendChild(otr);
//footer section

otab.appendChild(otbody);

var zi=getLastIndex();
if(otab.style.zIndex<zi) otab.style.zIndex=zi+1;

if(p['unblock']!='1'){
var bdiv=document.createElement('div');
bdiv.id='wpopupid_block_'+p['name'];
bdiv.style.position='fixed';
bdiv.style.left='0px';
bdiv.style.top='0px';
bdiv.style.width=screen.width+'px';
bdiv.style.height=screen.height+'px';
bdiv.style.opacity='0.6';
bdiv.style.filter='alpha(opacity=60)';
bdiv.style.backgroundColor='#000';
document.body.appendChild(bdiv);
}

document.body.appendChild(otab);

otab.onmousedown=dragable_start;
ifr.src=url;

if(document.all){
ifr.onfocus=function(){
var zi=getLastIndex();
if(document.getElementById('wpopupid_'+p['name']).style.zIndex<zi) document.getElementById('wpopupid_'+p['name']).style.zIndex=zi+1;
}
}else{
try{
window['wpopupframe_'+p['name']].onfocus=function(){
var zi=getLastIndex();
if(document.getElementById('wpopupid_'+p['name']).style.zIndex<zi) document.getElementById('wpopupid_'+p['name']).style.zIndex=zi+1;
}
}catch(e){}
}

/*dragable function*/
function dragable_start(e){
document.getElementById('wpopupframe_'+p['name']).style.display="none";;
dragee = this;
var zi=getLastIndex();
if(dragee.style.zIndex<zi) dragee.style.zIndex=zi+1;
e = e || window.event;
document.onmousemove = dragable_move;
document.onmouseup = dragable_stop;
this.startMouseX = e.clientX;
this.startMouseY = e.clientY;
this.startX = parseInt(this.style.left,10);
this.startY = parseInt(this.style.top,10);
return false;
}
function dragable_move(e){
e = e || window.event; 
var l=e.clientX - dragee.startMouseX + dragee.startX;
if(l<0) l=0;
dragee.style.left = l+'px';
var t=e.clientY - dragee.startMouseY + dragee.startY;
if(t<0) t=0;
dragee.style.top = t+'px';
return false;
}
function dragable_stop(e){
document.getElementById('wpopupframe_'+p['name']).style.display="";
document.onmousemove = null;
document.onmouseup = null;
}
/*dragable function*/
}
/*popup function*/



function getLastIndex(){
var m=1;
var all=(document.all)?document.all:document.getElementsByTagName('*');
for(var i in all){
try{
if(parseInt(all[i].style.zIndex)>m) m=parseInt(all[i].style.zIndex);
}catch(e){}
}
return m;
}
function parseProp(str){
var arrProp,ret=new Array();
if(str==null || str=="") return ret;
var ret=new Array();
var arrStr=str.split(",");
for(var i=0;i<arrStr.length;i++){
arrProp=arrStr[i].split("=");
ret[arrProp[0]]=arrProp[1];
}
return ret;
}

function wpopup_close(popup_name,onclose){
if(onclose!="" && onclose!=null) eval(onclose+"();");
document.body.removeChild(document.getElementById('wpopupid_'+popup_name));
try{
document.body.removeChild(document.getElementById('wpopupid_block_'+popup_name));
}catch(e){}
}

function findPos(obj){
var curleft = curtop = 0;
if(obj.offsetParent){
do{
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}while (obj = obj.offsetParent);
}
return [curleft,curtop];
}
