// ================================================
//
// JSLIB / Knihovna obecných a užitečných JS funkcí
//
// verze                                       1.0
//
// ================================================


// ================================================
// Otevře nové okno                                
// ================================================
function OpenPopupWindow(PopupWindowUrl, PopupWindowWidth, PopupWindowHeight, ShowLocation) {
    
    if (PopupWindowUrl.length > 0) {
    
        if (!PopupWindowWidth)
            PopupWindowWidth = 700;
            
        if (!PopupWindowHeight)
            PopupWindowHeight = 400;
            
        if (!ShowLocation)
            ShowLocation = false;
    
        var centerPoint = window.center({width:PopupWindowWidth,height:PopupWindowHeight});
        var random = Math.round(Math.random() * 100);
        var design = 'width=' + PopupWindowWidth + ', height=' + PopupWindowHeight + ', left=' + centerPoint.x + ', top=' + centerPoint.y + ', location=' + (ShowLocation ? '1' : '0') + ',toolbar=0, menubar=0, statusbar=0, scrollbars=1, resizable=no';
        
        window.open(PopupWindowUrl, 'PopupWin' + random, design);
    }
    return false;
    
}


// ================================================
// Otevře nové okno s obrázkem                               
// ================================================
function OpenPopupImage(ImageUrl) {

    if (ImageUrl.length > 0) {
        var random = Math.round(Math.random() * 100);
        var win = window.open('', 'PopupWin' + random);
        win.document.write("<html><head><title>Picture</title></head><body onclick=\"window.close();\">");
        win.document.write("<img src=\"" + ImageUrl + "\" alt=\"\" />");
        win.document.write("</body></html>");
    }
    return false;
    
}



// ================================================
// Zjistí velikost okna, vrací objekt dvou
// proměnných: width, height
// ================================================
window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}



// ================================================
// Zjistí souřadnice bodu, na které je potřeba
// posunout okno, aby bylo ve středu obrazovky                               
// ================================================
window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}


// ================================================
// Vloží flash animaci                             
// ================================================
var FlashReplace = {
    elmToReplace: null,
    flashIsInstalled: null,
    defaultFlashVersion: 8,
    replace: function(elmToReplace, src, id, width, height, version, params) {
        this.elmToReplace = document.getElementById(elmToReplace);
        this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
        if (this.elmToReplace /* && this.flashIsInstalled */) {
            var obj = '<object' + ((window.ActiveXObject) ? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
            obj += ' width="' + width + '"';
            obj += ' height="' + height + '"';
            obj += '>';
            var param = '<param';
            param += ' name="movie"';
            param += ' value="' + src + '"';
            param += '>';
            param += '';
            var extraParams = '';
            var extraAttributes = '';
            for (var i in params) {
                extraParams += '<param name="' + i + '" value="' + params[i] + '">';
                extraAttributes += ' ' + i + '="' + params[i] + '"';
            }
            var embed = '<embed id="' + id + '" src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
            var embedEnd = extraAttributes + '></embed>';
            var objEnd = '</object>';
            this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;
        }
    },

    checkForFlash: function(version) {
        this.flashIsInstalled = false;
        var flash;
        if (window.ActiveXObject) {
            try {
                flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
                this.flashIsInstalled = true;
            }
            catch (e) {
                // Throws an error if the version isn't available			
            }
        }
        else if (navigator.plugins && navigator.mimeTypes.length > 0) {
            flash = navigator.plugins["Shockwave Flash"];
            if (flash) {
                var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*(\d+\.\d+).*/, "$1");
                if (flashVersion >= version) {
                    this.flashIsInstalled = true;
                }
            }
        }
        return this.flashIsInstalled;
    }
}; 
