function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

function loadScript(src) {
    var head = document.getElementsByTagName('head').item(0);
    var newScript = document.createElement('script');
        newScript.src = src;
        newScript.type = 'text/javascript';
    head.appendChild(newScript);
}

function toggleSubmenu(e) {
    //var submenu = e.parentNode.parentNode.getElementsByTagName('ul');
    var submenu = e.parentNode.parentNode.getElementsByTagName('ul').item(0);
    if (submenu) {
        submenu.style.display = submenu.style.display == 'none' ? 'block' : 'none';
    }
}

var timer = null;
var IMG = null;
var NewWindow = null;
function ViewImage(Isrc,title) {
	IMG = new Image;
	IMG.src = Isrc;
	if (typeof(title)=="undefined" || title=="") title="Gallery";
	if (NewWindow) { NewWindow.close(); }
	if (NewWindow==null || NewWindow.closed) {
		settings=
		 "left="+50+","
		 +"top="+50+","
		 +"width="+640+","
		 +"height="+480+","
		 +"toolbar=no,"
		 +"location=no,"
		 +"directories=no,"
		 +"status=no,"
		 +"menubar=no,"
		 +"scrollbars=yes,"
		 +"resizable=yes"
		 NewWindow = window.open("",'Gallery',settings);
	}
	NewWindow.document.open();
	//NewWindow.document.clear();
	NewWindow.document.write(
         "<html><head><title>"+ title +"</title>"
        +"</head>\n"
        +"<body style=\"text-align:left;margin:15px;\">\n"
        +"<img onclick=\"window.close();\" src=\"" + IMG.src + "\" border=\"0\" alt=\""+title+"\" />\n"
        +"</body>\n"
        +"</html>"
	);
	NewWindow.document.close();
	NewWindow.focus();
	timer = setInterval("resize()",250);
}

function resize(plusW,plusH) {
    if(!IMG.complete){ return; }
    clearInterval(timer);
    var windowW = IMG.width+(plusW ? plusW : 50);
    var windowH = IMG.height+(plusH ? plusH : 60);
    if (windowW >= screen.width - 100)
    {
        windowW = screen.width - 100;
    }
    if (windowH >= screen.height - 100)
    {
        windowH = screen.height - 100;
    }
	resizeWindowTo(NewWindow,windowW,windowH);
	IMG = null;
    clearInterval(timer);
}

function resizeWindowTo(WindowObject,w,h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			WindowObject.outerWidth=w;
			WindowObject.outerHeight=h;
		}
		else WindowObject.resizeTo(w,h);
	}
}

//otvaranie dokumentov v okne
function Proceed_okno_anchors() {
    var aa = document.getElementsByTagName('A');
    for (var a = 0 ; a < aa.length ; a++) {
        if (aa[a].className.indexOf('okno') != -1) {
            aa[a].onclick = function () {
                var w = screen.width - 80;
                var h = screen.height - 220;
                var settings =
                     "left="+Math.round((screen.width - w) / 2)+","
                    +"top=47,"
                    +"width="+w+","
                    +"height="+h+","
                    +"toolbar=yes,"
                    +"location=yes,"
                    +"directories=no,"
                    +"status=no,"
                    +"menubar=no,"
                    +"scrollbars=yes,"
                    +"resizable=yes";
                var NewWindow = window.open(this.href,'Document',settings);
                NewWindow.focus();
                return false;
            }
        }
    }
}
addEvent(window,'load',Proceed_okno_anchors);

/*==================================================
  Cookie functions
  ==================================================*/
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// confirmation alert po kliknuti na "odborna zona"
function OdbornaZonaConfirm(a) {
    var text = 'Nasledujúce informácie sú určené len pre odborných pracovníkov v zdravotníctve. Potrvrdzujem, že som zdravotník.';
    var confirmed_cookie = getCookie('OdbornaZonaConfirm');
    if (confirmed_cookie || confirm(text)) {
        //nastavime cookie - ? dni dopredu
        var expDate = new Date();
        expDate.setDate(expDate.getDate() + 10);
        setCookie('OdbornaZonaConfirm', 1, expDate, '/');
        return true;
        
    }
    else {
        location.href = '/pacientom/';
    }
    return false;
}

// externe linky
function tagExternalLinks() {
	var aTags = document.getElementsByTagName("A");
	var URLparts = new Array();
	for (var i=0; i < aTags.length; i++) {
		URLparts = aTags[i].href.split('/');
		if (
			URLparts[2] != window.location.host && 
			    aTags[i].href.substring(0,11) != 'javascript:' && 
                aTags[i].href.substring(0,7) != 'mailto:'
			) {
			aTags[i].onclick = doExternalClick;
            aTags[i].className += ' externalLink';
		}
	}
}
function doExternalClick() {
	window.open(this.href);
	return false;
}
addEvent(window,'load',tagExternalLinks);
