/**
 * Popup window with features
 *
 * @author Marek Čevelíček
 * @email lix@quick.cz
 * @web www.liquiddesign.eu
 * @version 1.2
 * @date 25/04/2007
 *
 * Keep ethics, please!
 * You are free to use this function but keep this header unchanged
 * in the script with link to the author's web page.
 * I welcome any additional hints from you, so e-mail me if you want.
 */

var myTWin = window.myTWin; //objekt okna

//ARGUMENTS: (string) url, (string) name, (integer) window_width, (integer) window_height, (boolean) center
function popUpWin (link, winName, width, height) {
    var args = popUpWin.arguments;
    var argLen = args.length;
    
    var minSize = new Array(100,100);
    var maxSize = new Array(screen.availWidth-100, screen.availHeight-100);
    
    //configuration
    var resizable = "yes";
    var captionHeight = 0;
    var windowPadding = 1;
    
    //counting
    var cntWidth = width+windowPadding*2;
    var cntHeight = height+captionHeight+windowPadding*2;
    var left = 0;
    var top = 0;
    
    var retValue = true;
    
    //zjisteni pripony
    var fparts = link.toString().split(".");
    var ext = fparts[fparts.length-1];
    
    //nastaveni velikosti okna
    if (width > maxSize[0]){
        cntWidth = maxSize[0];
    }
    
    if (height > maxSize[1]){
        cntHeight = maxSize[1];
    }
    
    if (width < minSize[0]){
        cntWidth = minSize[0];
    }
    
    if (height < minSize[1]){
        cntHeight = minSize[1];
    }
    
    if ( (args[4] == undefined) || (args[4] == true) ){
        //nastaveni centrovane pozice
        left = (screen.availWidth - cntWidth) / 2;
        top = (screen.availHeight - cntHeight) / 2;
    }else{
        left = 100;
        top = 100;
    }
    
    //otevirani okna
    if (myTWin != null && !myTWin.closed){
        myTWin.close();
    }
    
    myTWin = window.open("", winName, 
        "location=no,menubar=no,scrollbars=yes,titlebar=no,toolbar=no,personalbar=no,status=no,"+
        "resizable="+resizable+",width="+cntWidth+",height="+cntHeight+",left="+left+",top="+top+
        ",screenX="+left+",screenY="+top);
    
    if (myTWin==null || typeof(myTWin)=="undefined"){
        retValue = false; //okno nelze otervit (javascript disabled)
    }else{
        link.target = winName;
    }
    
    if (retValue){
        //myTWin.resizeTo(cntWidth, cntHeight); //resize nepresna IE
        //myTWin.resizeBy(cntWidth, cntHeight); //not used
        myTWin.moveTo(left,top); //presun okna
        //myTWin.innerWidth = cntWidth; //resize FF
        //myTWin.innerHeight = cntHeight; //resize FF
        myTWin.focus();
        
        if (ext == 'gif' || ext == 'jpg' || ext == 'png'){
            writeContentImage(myTWin, link.href, width, height, "", captionHeight);
        }else{
            myTWin.location.href=link.href;
        }
    }
    
    return retValue;
}

/**
 * Content of the popup window / image
 */
function writeContentImage (win, src, width, height, alt, caps) {
    win.document.open();
    win.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
    win.document.writeln('<html lang="cs" xml:lang="cs" xmlns="http://www.w3.org/1999/xhtml">');
    win.document.writeln('<head>');
    win.document.writeln('<meta content="text/html; charset=windows-1250" http-equiv="Content-Type" />');
    win.document.writeln('<meta content="cs" http-equiv="Content-Language" />');
    win.document.writeln('<meta content="no-cache" http-equiv="Cache-control" />');
    win.document.writeln('<meta content="(c) 2006 Liquid Design" name="Copyright" />');
    win.document.writeln('<meta content="Marek Čevelíček" name="Author" />');
    win.document.writeln('<meta content="robots" name="noindex,nofollow" />');
    win.document.writeln('<title>Los Kachlos</title>');
    win.document.writeln('<link media="screen" type="text/css" href="style.css" rel="Stylesheet" />');
    win.document.writeln('<link media="print" type="text/css" href="style.css" rel="Stylesheet" />');
    win.document.writeln('</head>');
    win.document.writeln('<body style="margin: 0px; background-color: #e9e9e9;">');
    win.document.writeln('<div style="position: absolute; left: 50%; top: 50%; margin-left: -'+(width/2)+'px; margin-top: -'+(height/2+caps/2)+'px; width: '+width+'px; height: '+height+'px;">');
    win.document.writeln('<a href="javascript: self.close();"><img src="'+src+'" width="'+width+'" height="'+height+'" alt="'+alt+'" style="border: 0px; display: block;" /></a>');
    win.document.writeln('</div>');
    win.document.writeln('</body>');
    win.document.writeln('</html>');
    win.document.close();
}
