var activeMenus = new Array();

function getPosition(node) {
        var pos = Array();
        pos['x']= 0;
        pos['y']= 0;
        do {
                if ((node.tagName!="TR") && (node.tagName!="TBODY") && (node.tagName!="COMPONENT")) { // mivel a TR ugyanolyan offsettel rendelkezik, mint a
                        if (node.offsetLeft) {
                                pos['x']+= node.offsetLeft;
                        }
                        if (node.offsetTop) {
                                pos['y']+= node.offsetTop;
                        }
                }
                node = node.offsetParent;
        } while (node);
        return pos;
}

var menuLeftOffset = 50;
var menuTop = 350;
function showMenu(id, keep, num, event) {
    var target = window.event ? window.event.srcElement : event.target;
    var pos = getPosition(target);
    
    if (window.event) {
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
    } else {
        xMousePos = event.pageX;
        yMousePos = event.pageY;
    }

    var topPx = num * 18;
    var menuDiv = document.getElementById('menu_'+id);
    if (menuDiv && menuDiv.style.display != 'block') {
	menuDiv.style.display = 'block';
	menuDiv.style.left = 175+"px"; //pos['x'] + target.offsetWidth;
	menuDiv.style.top = topPx+"px";

	activeMenus[activeMenus.length] = id;

	keep[keep.length] = id;
    }
    hideUnkept([id]);
}

function hideUnkept(keep) {
    for (var i = 0; i < activeMenus.length; i++) {
	if (keep.length != 0) {
	    for(var j = 0; j < keep.length; j++) {
		if (keep[j] != activeMenus[i]) {
		    document.getElementById('menu_'+activeMenus[i]).style.display = 'none';
		}
	    }
	} else {
	    document.getElementById('menu_'+activeMenus[i]).style.display = 'none';
	}
    }
}

function hideAll(event) {
    var target = window.event ? window.event.srcElement : event.target;

    if (target.getAttribute("isMenu") == 1) { return; }

    for (var i = 0; i < activeMenus.length; i++) {
	document.getElementById('menu_'+activeMenus[i]).style.display = 'none';
    }
}

var BrowserDetect = {
    init: function () {
	this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	this.version = this.searchVersion(navigator.userAgent)
	    || this.searchVersion(navigator.appVersion)
	    || "an unknown version";
	this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
	for (var i=0;i<data.length;i++)	{
	    var dataString = data[i].string;
	    var dataProp = data[i].prop;
	    this.versionSearchString = data[i].versionSearch || data[i].identity;
	    if (dataString) {
		if (dataString.indexOf(data[i].subString) != -1)
		    return data[i].identity;
	    }
	    else if (dataProp)
		return data[i].identity;
	}
    },
    searchVersion: function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
	{ 	string: navigator.userAgent,
	    subString: "OmniWeb",
	    versionSearch: "OmniWeb/",
	    identity: "OmniWeb"
	},
	{
	    string: navigator.vendor,
	    subString: "Apple",
	    identity: "Safari"
	},
	{
	    prop: window.opera,
	    identity: "Opera"
	},
	{
	    string: navigator.vendor,
	    subString: "iCab",
	    identity: "iCab"
	},
	{
	    string: navigator.vendor,
	    subString: "KDE",
	    identity: "Konqueror"
	},
	{
	    string: navigator.userAgent,
	    subString: "Firefox",
	    identity: "Firefox"
	},
	{
	    string: navigator.vendor,
	    subString: "Camino",
	    identity: "Camino"
	},
	{		// for newer Netscapes (6+)
	    string: navigator.userAgent,
	    subString: "Netscape",
	    identity: "Netscape"
	},
	{
	    string: navigator.userAgent,
	    subString: "MSIE",
	    identity: "Explorer",
	    versionSearch: "MSIE"
	},
	{
	    string: navigator.userAgent,
	    subString: "Gecko",
	    identity: "Mozilla",
	    versionSearch: "rv"
	},
	{ 		// for older Netscapes (4-)
	    string: navigator.userAgent,
	    subString: "Mozilla",
	    identity: "Netscape",
	    versionSearch: "Mozilla"
	}
    ],
    dataOS : [
	{
	    string: navigator.platform,
	    subString: "Win",
	    identity: "Windows"
	},
	{
	    string: navigator.platform,
	    subString: "Mac",
	    identity: "Mac"
	},
	{
	    string: navigator.platform,
	    subString: "Linux",
	    identity: "Linux"
	}
    ]

};

BrowserDetect.init();

function resizeMe(mode) {
    var bodyDiv = document.getElementById('bodyDiv');
    var contentDiv = document.getElementById('contentDiv');

    if (bodyDiv.offsetHeight > 845) {
        if (mode == 1) bodyDiv.style.height = contentDiv.offsetHeight > 305 ? (bodyDiv.offsetHeight - (BrowserDetect.browser == "Safari"?50:330))+'px' : 845+'px';
        else if (mode == 2) bodyDiv.style.height = contentDiv.offsetHeight > 305 ? (bodyDiv.offsetHeight + 65)+'px' : 845+'px';
	else bodyDiv.style.height = contentDiv.offsetHeight > 305 ? (bodyDiv.offsetHeight - (BrowserDetect.browser == "Safari"?350:630))+'px' : 845+'px';
    } else {
//	bodyDiv.style.height = 845+'px';
    }
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) {
	contentDiv.style.top = (parseInt(contentDiv.style.top) - 55) + 'px';
    }
    if (BrowserDetect.browser == "Safari") {
	if (mode) {
	    var safariPos = {2:{'top':50,'left':50},3:{'top':75,'left':65},4:{'top':-80,'left':0},5:{'top':-60,'left':335},6:{'top':265,'left':155},7:{'top':-75,'left':35}};
	} else {
	    var safariPos = {2:{'top':50,'left':50},3:{'top':75,'left':65},4:{'top':-70,'left':0},5:{'top':-50,'left':335},6:{'top':265,'left':-565},7:{'top':-65,'left':755},8:{'top':-260,'left':75}};
	}

	for(var i = 2; i<bodyDiv.children.length; i++) {
	    bodyDiv.children[i].style.top = safariPos[i]['top']+'px';
	    bodyDiv.children[i].style.left = safariPos[i]['left']+'px';
	}
    }
}

var topOffset = 5;
var scrollStep = 1;
var notScrolling = null;
function startScrolling(id) {
    document.getElementById(id+"_2").style.top = (topOffset + document.getElementById(id+"_1").scrollHeight) + "px";
    if (document.getElementById(id+"_1").scrollHeight > 100) {
	setTimeout("scrollMe('"+id+"')", 1000);
	document.getElementById(id+"_2").style.display = "block";
    }
}

function scrollMe(id) {
    if (notScrolling != id) {
	var first = document.getElementById(id+"_1");
        var second = document.getElementById(id+"_2");

	first.style.top = (parseInt(first.style.top) - scrollStep) + 'px';
        second.style.top = (parseInt(second.style.top) - scrollStep) + 'px';

	if (parseInt(first.style.top)*(-1) > first.scrollHeight) first.style.top = (topOffset + parseInt(second.style.top) + first.scrollHeight) + 'px';
        if (parseInt(second.style.top)*(-1) > first.scrollHeight) second.style.top = (topOffset + parseInt(first.style.top) + first.scrollHeight) + 'px';
    }

    setTimeout("scrollMe('"+id+"')", 65);
}

function mouseX(evt, doc) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX) return evt.clientX + (doc.documentElement.scrollLeft ? doc.documentElement.scrollLeft : doc.body.scrollLeft);
    else return null;
}

function mouseY(evt, doc) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY) return evt.clientY + (doc.documentElement.scrollTop ? doc.documentElement.scrollTop : doc.body.scrollTop);
    else return null;
}

function showWeek(week, type, event) {
    var httpReq = null;
    var offset = -2;

    try {
        httpReq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            httpReq = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(sc) {
            httpReq = null;
        }
    }

    if (!httpReq && typeof XMLHttpRequest!="undefined") {
        httpReq = new XMLHttpRequest();
	offset = 0;
    }

    if (window.event) event = window.event;
//    x = mouseX(event, document) - document.getElementById("contentDiv").offsetLeft + offset;
//    y = mouseY(event, document) - document.getElementById("contentDiv").offsetTop + offset;
    x = mouseX(event, document) + offset;
    y = mouseY(event, document) + offset;

    var req = "event_week.php?week="+week+"&type="+type+"&ts="+(new Date().getTime().toString());
    httpReq.open("GET", req);
    httpReq.onreadystatechange=function() {
        if(httpReq.readyState==4&&httpReq.responseText) {
            document.getElementById("events").innerHTML = httpReq.responseText;
	    document.getElementById("events").style.left = x+'px';
	    document.getElementById("events").style.top = y+'px';
	    document.getElementById("events").style.display = "block";
        }
    };
    httpReq.send(null);
}

function hideWeek(event) {
    var src = window.event ? window.event.srcElement : event.target;

    if (src.tagName != "DIV") document.getElementById("events").style.display = "none";
}