function MShow() {
    var actualImage = 0;
    var imageDrawn = false;
    var imagesParsed = false;
    var elementsInitialized = false;
    var imageLoadTimer;
    
    var imagesLinks =  new Array();
    var imagesTitles =  new Array();
    var imagesElms =  new Array();
    var imagesWidth =  new Array();
    var imagesHeight =  new Array();
    var imagesStyle =  new Array();
    var imagesLoaded =  new Array();

    var loadingLetters = new Array();
    var loadingLetterAct = 0;
    var loadingLettersTimer;
    
    var windowWidth = 0;
    var windowHeight = 0;
    if (window.innerHeight) {
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    } else if ((document.documentElement.clientHeight)&&(document.documentElement.clientHeight > 0)) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body.clientHeight) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    
    //alert(windowWidth+' : '+windowHeight);
    //SetScrollTop();

    var bgElm;
    var msElm;
    var topElm;
    var closeElm;
    var nextElm;
    var prevElm;
    var imgContainerElm;

    ParseImages();
    InitElements();
    addEvent(window, 'resize', MShowResize);
    addEvent(window, 'scroll', MShowResize);
    
    function ParseImages() {
        var xanch=document.getElementsByTagName('a');
        var imgcount = 0;
        for(var i=0;i<xanch.length;i++) {
            if(xanch[i].className == 'mshowlink')  {
                imagesTitles[imgcount] = xCreateElement('p');
                var title = '';
                if (xanch[i].attributes.getNamedItem('title')) {
                    title = xanch[i].attributes.getNamedItem('title').nodeValue;
                }
                imagesTitles[imgcount].appendChild(document.createTextNode(title));
                imagesTitles[imgcount].style.position ='absolute';
                imagesTitles[imgcount].style.zIndex ='2500';
                imagesTitles[imgcount].style.background ='#FFFFFF';
                imagesTitles[imgcount].style.color ='#000000';
                imagesTitles[imgcount].style.padding = '0.4em 0';
                imagesTitles[imgcount].style.border='2px solid #FFFFFF';
                imagesLinks[imgcount] = xanch[i].attributes.getNamedItem('href').nodeValue;
                xanch[i].MShowIndex = imgcount;
                xanch[i].onclick = function() { DisplayShow(this); return false; }
                //xanch[i].onmouseover = function() { DisplayShow(this); return false; }
                //xanch[i].onmouseout = function() { HideShow(this); return false; }
                imgcount++;
            }
        }
        imagesParsed = true;
    }

    function InitElements() {
        
        var xbody = document.getElementsByTagName('body')[0];
        var xtop = GetScrollY();
        
        msElm = xCreateElement('div');
        msElm.style.width = '100%';
        msElm.style.height = windowHeight+'px';
        msElm.style.position = 'absolute';
        msElm.style.top = xtop+0+'px';
        msElm.style.left = '0px';
        msElm.style.zIndex = '2000';
        msElm.style.display ='none';
        xbody.insertBefore(msElm,xbody.firstChild);

        bgElm = xCreateElement('div');
        //bgElm.style.width = windowWidth+'px';
        bgElm.style.width = '100%';
        bgElm.style.height = windowHeight+'px';
        bgElm.style.background = '#515D87';
        bgElm.style.color = '#000000';
        bgElm.style.position = 'absolute';
        bgElm.style.top = xtop+0+'px';
        bgElm.style.left = '0px';
        bgElm.style.zIndex = '1000';
        bgElm.style.opacity = 50/100;
        bgElm.style.filter = 'alpha(opacity=50)';
        bgElm.style.display ='none';
        xbody.insertBefore(bgElm,xbody.firstChild);
        
        imgContainerElm = xCreateElement('div');
        imgContainerElm.style.textAlign = 'center';
        imgContainerElm.style.fontSize = '0.9em';
        imgContainerElm.style.fontWeight = 'normal';
        imgContainerElm.style.fontFamily = '"Arial CE", Arial, "Lucida Grande CE", Lucida, "Helvetica CE", Helvetica, sans-serif';
        imgContainerElm.style.display ='block';
        msElm.insertBefore(imgContainerElm,msElm.firstChild);

        loadingElm = xCreateElement('p');
        loadingElm.style.fontSize = '2em';
        loadingElm.style.fontWeight = 'normal';
        loadingElm.style.fontFamily = '"Arial CE", Arial, "Lucida Grande CE", Lucida, "Helvetica CE", Helvetica, sans-serif';
        loadingElm.style.display ='block';
        loadingElm.style.zIndex = '2001';
        loadingElm.style.position = 'absolute';
        loadingElm.style.backgroundColor = '#FFFFFF';
        loadingElm.style.color = '#999999';
        loadingElm.style.padding = '20px';
        loadingElm.style.margin = '0px';
        xtext = GetLoadingText()
        for(var i=0;i<xtext.length;i++) {
            loadingLetters[i] = xCreateElement('span');
            loadingLetters[i].appendChild(document.createTextNode(xtext.charAt(i)));
            loadingElm.appendChild(loadingLetters[i]);
        }
        loadingLetterAct = xtext.length-1;
        
        msElm.insertBefore(loadingElm,msElm.firstChild);

        topElm = xCreateElement('div');
        //topElm.style.width = windowWidth+'px';
        topElm.style.width = '100%';
        topElm.style.height = 'auto';
        topElm.style.background = '#515D87';
        topElm.style.color = '#FFFFFF';
        topElm.style.position = 'absolute';
        topElm.style.top = '0';
        topElm.style.left = '0';
        topElm.style.zIndex = '2100';
        topElm.style.borderTop = '2px solid #000000';
        topElm.style.borderBottom = '2px solid #000000';
        topElm.style.paddingTop = '10px';
        topElm.style.paddingBottom = '10px';
        topElm.style.textAlign = 'center';
        topElm.style.fontSize = '1em';
        topElm.style.fontWeight = 'normal';
        topElm.style.fontFamily = '"Arial CE", Arial, "Lucida Grande CE", Lucida, "Helvetica CE", Helvetica, sans-serif';
        topElm.style.display = 'block';
        msElm.insertBefore(topElm,msElm.firstChild);

        closeElm = xCreateElement('a');
        closeElm.style.display = 'none';
        closeElm.style.width = '24px';
        closeElm.style.height = '24px';
        closeElm.style.border = '1px solid #000000';
        closeElm.style.background = '#CC7A29 url(\'/jscripts/mshow-close00.png\') 50% 50% no-repeat';
        closeElm.style.padding = '0';
        closeElm.style.position = 'absolute';
        closeElm.style.top = '8px';
        closeElm.style.right = '6px';
        closeElm.style.zIndex = '2200';
        closeElm.onclick = function() { HideShow(); return false; }
        closeElm.onmouseover = function() { closeElm.style.background = '#B32424 url(\'/jscripts/mshow-close01.png\') 50% 50% no-repeat'; return false; }
        closeElm.onmouseout = function() { closeElm.style.background = '#CC7A29 url(\'/jscripts/mshow-close00.png\') 50% 50% no-repeat'; return false; }
        closeElm.style.cursor = 'pointer';
        closeElm.style.display = 'block';
        msElm.insertBefore(closeElm,msElm.firstChild);

        nextElm = xCreateElement('a');
        nextElm.style.display = 'none';
        nextElm.style.width = '24px';
        nextElm.style.height = '24px';
        nextElm.style.border = '1px solid #000000';
        nextElm.style.background = '#CC7A29 url(\'/jscripts/mshow-next00.png\') 50% 50% no-repeat';
        nextElm.style.padding = '0';
        nextElm.style.position = 'absolute';
        nextElm.style.top = '8px';
        nextElm.style.right = Math.round(windowWidth/2)-96+'px';
        nextElm.style.zIndex = '2200';
        nextElm.style.cursor = 'pointer';
        nextElm.onmouseover = function() { nextElm.style.background = '#919499 url(\'/jscripts/mshow-next01.png\') 50% 50% no-repeat'; return false; }
        nextElm.onmouseout = function() { nextElm.style.background = '#CC7A29 url(\'/jscripts/mshow-next00.png\') 50% 50% no-repeat'; return false; }
        nextElm.onclick = function() { DisplayImageNext(); return false; }
        nextElm.style.display = 'block';
        msElm.insertBefore(nextElm,msElm.firstChild);

        prevElm = xCreateElement('a');
        prevElm.style.display = 'none';
        prevElm.style.width = '24px';
        prevElm.style.height = '24px';
        prevElm.style.border = '1px solid #000000';
        prevElm.style.background = '#CC7A29 url(\'/jscripts/mshow-prev00.png\') 50% 50% no-repeat';
        prevElm.style.padding = '0';
        prevElm.style.position = 'absolute';
        prevElm.style.top = '8px';
        prevElm.style.left = Math.round(windowWidth/2)-96+'px';
        prevElm.style.zIndex = '2200';
        prevElm.style.cursor = 'pointer';
        prevElm.onmouseover = function() { prevElm.style.background = '#919499 url(\'/jscripts/mshow-prev01.png\') 50% 50% no-repeat'; return false; }
        prevElm.onmouseout = function() { prevElm.style.background = '#CC7A29 url(\'/jscripts/mshow-prev00.png\') 50% 50% no-repeat'; return false; }
        prevElm.onclick = function() { DisplayImagePrev(); return false; }
        prevElm.style.display = 'block';
        msElm.insertBefore(prevElm,msElm.firstChild);

        elementsInitialized = true;
    }
    
    function PositionElements() {
        var xtop = GetScrollY();
        bgElm.style.top = xtop+0+'px';
        bgElm.style.width = windowWidth+'px';
        bgElm.style.height = windowHeight+'px';
        msElm.style.top = xtop+0+'px';
        msElm.style.width = windowWidth+'px';
        msElm.style.height = windowHeight+'px';
        nextElm.style.right = Math.round(windowWidth/2)-96+'px';
        prevElm.style.left = Math.round(windowWidth/2)-96+'px';
        loadingElm.style.left = Math.round((windowWidth-loadingElm.offsetWidth)/2)+'px';
        loadingElm.style.top = Math.round((windowHeight-loadingElm.offsetHeight)/2)+'px';
    }
    
    function DisplayShow(obj) {
        actualImage = obj.MShowIndex;
        window.clearInterval(imageLoadTimer);
        DisplayImage();
        bgElm.style.display ='block';
        msElm.style.display ='block';
        loadingElm.style.left = Math.round((windowWidth-loadingElm.offsetWidth)/2)+'px';
        loadingElm.style.top = Math.round((windowHeight-loadingElm.offsetHeight)/2)+'px';
        loadingLettersTimer = window.setInterval(HighlightLoadingLetters,500);
    }
    
    function HideShow() {
        window.clearInterval(loadingLettersTimer);
        bgElm.style.display = 'none';
        msElm.style.display = 'none';
        imageDrawn = false;
    }
    
    function DisplayImageNext() {
        if (actualImage < imagesLinks.length-1) {
            actualImage++;
        } else {
            actualImage = 0
        }
        window.clearInterval(imageLoadTimer);
        DisplayImage();
    }
    
    function DisplayImagePrev() {
        if (actualImage > 0) {
            actualImage--;
        } else {
            actualImage = imagesLinks.length-1
        }
        window.clearInterval(imageLoadTimer);
        DisplayImage();
    }
    
    function DisplayImage() {
        if (imagesParsed && elementsInitialized) {
            
            if(topElm.firstChild) {
                topElm.removeChild(topElm.firstChild);
            }
            topElm.appendChild(document.createTextNode((actualImage+1)+' / '+imagesLinks.length));

            while (imgContainerElm.firstChild) {
		imgContainerElm.removeChild(imgContainerElm.firstChild);
	    }

            if(!imagesElms[actualImage]) {
                imagesElms[actualImage] = xCreateElement('img');
                imagesElms[actualImage].src=imagesLinks[actualImage];
                imagesElms[actualImage].alt=imagesTitles[actualImage];
                imagesElms[actualImage].style.display='block';
                imagesElms[actualImage].style.position='absolute';
                imagesElms[actualImage].style.zIndex='2500';
                imagesElms[actualImage].style.border='2px solid #FFFFFF';
                //imagesElms[actualImage].style.opacity = 50/100;
                imagesElms[actualImage].MShowIndex = actualImage;
                imagesElms[actualImage].onload = function() { ImageLoadComplete(this); }
                imagesStyle[actualImage] = false;
                imagesLoaded[actualImage] = false;
            }
            if (imagesLoaded[actualImage]) {
                DrawImage();
            } else {
                imageDrawn = false;
                imageLoadTimer = window.setInterval(ImageLoader,200);
            }
        }
    }
    
    function DrawImage() {
        if (!imagesStyle[actualImage]) {
            var winx = windowWidth-16;
            var winy = windowHeight-100;
            if (!imagesWidth[actualImage]) {
                imagesWidth[actualImage] = imagesElms[actualImage].width;
                imagesHeight[actualImage] = imagesElms[actualImage].height;
            }
            var imgwidth = imagesWidth[actualImage];
            var imgheight = imagesHeight[actualImage];
            var winr = winx/winy;
            var imgr = imgwidth/imgheight;
            if (winr < imgr) {  //land2port
                if (imgwidth > winx) {
                    imgwidth = winx;
                    imgheight = Math.round(imagesElms[actualImage].height*winx/imagesElms[actualImage].width);
                }
            } else { //port2land
                if (imgheight > winy) {
                    imgwidth = Math.round(imagesElms[actualImage].width*winy/imagesElms[actualImage].height);
                    imgheight = winy;
                }
            }
            imagesElms[actualImage].width=imgwidth;
            imagesElms[actualImage].height=imgheight;
            var x = Math.round((winx-imgwidth)/2)+6;
            var y = Math.round((winy-imgheight)/2)+44;
            imagesElms[actualImage].style.left=x+'px';
            imagesElms[actualImage].style.top=y+'px';
            imagesElms[actualImage].onclick = function() { HideShow(); return false; }
            imagesTitles[actualImage].style.width=imgwidth+'px';
            if (isIEBug()) {
                imagesTitles[actualImage].style.width=imgwidth+4+'px';
            }
            imagesTitles[actualImage].style.left=x+'px';
            imagesTitles[actualImage].style.top=y+imgheight-10+'px';
            imagesStyle[actualImage] = true;
            //alert(imgwidth+' '+imgheight);
        }
        imgContainerElm.appendChild(imagesElms[actualImage]);
        imgContainerElm.appendChild(imagesTitles[actualImage]);
    }
    
    function ImageLoader() {
        if (!imageDrawn) {
            if ((imagesLoaded[actualImage])||(imagesElms[actualImage].complete)) {
                imageDrawn = true;
                window.clearInterval(imageLoadTimer);
                DrawImage();
            }
        }
    }
    
    function ImageLoadComplete(obj) {
        imagesLoaded[obj.MShowIndex] = true;
    }

    function MShowResize() {
        if (window.innerHeight) {
	    windowWidth = window.innerWidth;
    	    windowHeight = window.innerHeight;
        } else if ((document.documentElement.clientHeight)&&(document.documentElement.clientHeight > 0)) {
	    windowWidth = document.documentElement.clientWidth;
    	    windowHeight = document.documentElement.clientHeight;
        } else if (document.body.clientHeight) {
	    windowWidth = document.body.clientWidth;
    	    windowHeight = document.body.clientHeight;
        }   
        if (imagesParsed) {
            PositionElements();
            for (i=0;i<imagesStyle.length;i++) {
                imagesStyle[i] = false;
            }
            DisplayImage();
        }
    }
    
    function GetScrollX() {
        var result = 0;
        if ( typeof( window.pageXOffset ) == 'number' ) {
            result = window.pageXOffset;
        } else if ( document.body && document.body.scrollLeft ) {
            result = document.body.scrollLeft;
        } else if( document.documentElement && document.documentElement.scrollLeft ) {
            //IE6 standards compliant mode
            result = document.documentElement.scrollLeft;
        }
        return result;
    }
    
    function GetScrollY() {
        var result = 0;
        if ( typeof( window.pageYOffset ) == 'number' ) {
            result = window.pageYOffset;
        } else if ( document.body && document.body.scrollTop ) {
            result = document.body.scrollTop;
        } else if( document.documentElement && document.documentElement.scrollTop ) {
            //IE6 standards compliant mode
            result = document.documentElement.scrollTop;
        }
        return result;
    }
    
    function GetLoadingText() {
        var result = 'Loading...';
        var xhtml = document.getElementsByTagName('html')[0];
        if (xhtml.attributes.getNamedItem('lang')) {
            if (xhtml.attributes.getNamedItem('lang').nodeValue == 'cs') {
                var result = 'Nahrávám...';
            }
        }
        return result;
    }
    
    function HighlightLoadingLetters() {
        loadingLetters[loadingLetterAct].style.color = '';
        loadingLetterAct++;
        if (loadingLetterAct >= loadingLetters.length) {
            loadingLetterAct = 0;
        }
        loadingLetters[loadingLetterAct].style.color = '#666666';
    }
    
}

function SetScrollTop() {
    if ( document.body && document.body.scrollTop ) {
        document.body.scrollTop = 0;
    } else if( document.documentElement && document.documentElement.scrollTop ) {
        document.documentElement.scrollTop = 0;
    }
}

function xCreateElement(type) {
    var result = false;
    if (document.createElementNS) {
        result = document.createElementNS('http://www.w3.org/1999/xhtml','html:'+type);
    } else {
        result = document.createElement(type);
    }
    return result;
}

function isIEBug() {
    var agt=navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_ie = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1));
    var is_ie56 = (is_ie && ((agt.indexOf('msie 5') != -1)||(agt.indexOf('msie 6') != -1)));
    return is_ie56;
}

function addEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

addEvent(window,'load',MShow);
