function popUpImg() {
	var popup = document.createElement('div');
	popup.setAttribute('id','popup');
	var zoom = document.createElement('img');
	zoom.setAttribute('id','ImgZoom');
	zoom.style.display = "none";
	var parag = document.createElement('p');
	parag.setAttribute('id','closeZoom');
	parag.style.display = "none";
	var close = document.createTextNode('Zamknij');
	parag.appendChild(close);
	popup.style.padding = "0";
	popup.style.margin  = "0";
	popup.appendChild(parag);
	popup.appendChild(zoom);
	popup.style.visibility = 'hidden';
	popup.onclick = function() {
		return Hide(this);
	}

	var content = document.getElementsByTagName("body").item(0);
	content.appendChild(popup);

	var lnks = content.getElementsByTagName('a');
	for (i=0;i<lnks.length;i++) {
		var lnkclass = lnks[i].className;
		if (lnkclass == "zoom") {
			lnks[i].onclick = function() {
				return Show(this);
			}
		}
	}
}

function Show(img) {
	if (document.getElementById && document.createElement) {
		var url = img.getAttribute('href');
		var zoom = document.getElementById('ImgZoom');
		zoom.setAttribute('src', url);
		zoom.style.display = "block";
		var popUp = document.getElementById('popup');
		popUp.style.visibility = 'visible';
		popUp.style.position   = "absolute";
		/* parse image href for dimensions
		based on http://www.themaninblue.com/writing/perspective/2004/08/05/ */

		var paramString = url.replace(/.*\?(.*)/, "$1");
 		var paramTokens = paramString.split("&");
 		var paramList = new Array();

 		for (i = 0; i < paramTokens.length; i++) {
			var paramName = paramTokens[i].replace(/(.*)=.*/, "$1");
			var paramValue = paramTokens[i].replace(/.*=(.*)/, "$1");
			paramList[paramName] = paramValue;
		}
 		imgWidth  = paramList["x"];
 		imgHeight = paramList["y"];

		if (imgWidth)  {zoom.style.width  = imgWidth  + 'px';}
 		if (imgHeight) {zoom.style.height = imgHeight + 'px';}

		popUp.style.width   = "auto";
		popUp.style.height  = "auto";
		popUp.style.padding = "5px";
		var popUpHeight  = document.getElementById('popup').offsetHeight;
		var screenHeight = getScreenHeight();
		var scrolled     = getScroll();
		var top = ((screenHeight - popUpHeight)/2) + scrolled;
		//alert("wysokosc popupa:" + popUpHeight + " wysokosc ekranu" + screenHeight + " ile przewiniete:" + scrolled + " poz:" + top);
	    if (top < 335)
			top = 335;
		popUp.style.top = top+"px";
		var ScreenWidth = getScreenWidth();
		var popUpWidth  = document.getElementById('popup').offsetWidth;
		var left = (ScreenWidth-popUpWidth)/2;
		popUp.style.left    = left+"px";
		var close = document.getElementById('closeZoom');
		close.style.display = "block";
		close.style.margin  = "0";
		close.style.padding = "0";
		close.style.color   = "#fff";
		popUp.style.cursor  = "pointer";
		return false;
	}
	else
		return true;
}

function Hide(popup) {
	document.getElementById('ImgZoom').src = 'images/blank.gif';
	popup.style.visibility = 'hidden';
}

function getScreenHeight() {
	var y;
	if (self.innerHeight) {
		// wszystko poza IE
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// IE 6 Strict
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// inne IE
		y = document.body.clientHeight;
	}
	return y;
}

function getScreenWidth() {
	var x;
	if (self.innerWidth) {
		// wszystko poza IE
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		// IE 6 Strict
		x = document.documentElement.clientWidth;
	}
	else if (document.body) {
		// inne IE
		x = document.body.clientWidth;
	}
	return x;
}

function getScroll() {
	var y;
	if (self.pageYOffset) {
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		y = document.documentElement.scrollTop;
	}
	else if (document.body) {
		y = document.body.scrollTop;
	}
	return y;
}

function szukajGab() {
    var frm = document.getElementById('szukajGab');
    var woj = frm.woj;
	var miasto  = frm.miasto.options[frm.miasto.selectedIndex].value;
    tabelaMiast = getMiasta(frm);
	zmienMiasta(frm, woj, miasto);
    woj.onchange = function() {
        zmienMiasta(frm, woj, null);
    }
}

function getMiasta(frm) {
    var miasta  = frm.miasto;
    var kids    = miasta.childNodes;
    var tabela  = new Array();
    var wojs = new Array('zachodniopomorskie','lubelskie','mazowieckie','dolnośląskie','śląskie','świętokrzyskie','małopolskie',
                          'podkarpackie','pomorskie','warmińsko_mazurskie','podlaskie','lubuskie','wielkopolskie','kujawsko_pomorskie',
                          'łódzkie','wielkopolskie','opolskie');
    for (var i = 0; i < kids.length; i++) {
        tabela[i] = new Array();
    }
    for (i = 0; i < kids.length; i++) {
        kids_of_kids = kids[i].childNodes;
        for(var j = 0; j < kids_of_kids.length; j++) {
            if (kids_of_kids[j].hasChildNodes() && kids_of_kids[j].childNodes[0].nodeValue != '') {
                tabela[i].push(kids_of_kids[j].childNodes[0].nodeValue);
            }
        }
    }
    if (navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1) {
        tabela.shift();
    }
    return tabela;
}

function zmienMiasta(frm, woj, miasto) {
    var miasta  = frm.miasto;
    var woj_akt = woj.options[woj.selectedIndex].childNodes[0].nodeValue;
    var wojs = new Array('zachodniopomorskie','lubelskie','mazowieckie','dolnośląskie','śląskie','świętokrzyskie','małopolskie',
                          'podkarpackie','pomorskie','warmińsko_mazurskie','podlaskie','lubuskie','wielkopolskie','kujawsko_pomorskie',
                          'łódzkie','wielkopolskie','opolskie');
    for (var i = 0; i < wojs.length; i++) {
        if (wojs[i] == woj_akt.replace('-','_')) {
            var indeks = i;
        }
    }
    var kids = miasta.childNodes;
    var j;
    var miasta_z_tego_woj = tabelaMiast[indeks];
    for (i = 0; i < kids.length; i++) {
       while (kids[i].hasChildNodes()) {
           kd_of_kd = kids[i].childNodes;
           for (var j = 0; j < kd_of_kd.length; j++) {
               while (kd_of_kd[j].hasChildNodes()) {
                   for (var k = 0; k < kd_of_kd[j].childNodes.length; k++) {
                        kd_of_kd[j].removeChild(kd_of_kd[j].childNodes[k]);
                   }
               }
               kids[i].removeChild(kd_of_kd[j]);
           }
       }
    }
    while(kids.length) {
        for (i = 0; i < kids.length; i++) {
            miasta.removeChild(kids[i]);
        }
    }
    if (miasta_z_tego_woj) {
        for (j = 0; j < miasta_z_tego_woj.length; j++) {
            var new_option = document.createElement('option');
            var new_text   = document.createTextNode(miasta_z_tego_woj[j]);
			if (miasto && miasta_z_tego_woj[j] == miasto) {
				new_option.setAttribute('selected', 'selected');
			}
            new_option.setAttribute('value', miasta_z_tego_woj[j]);
            new_option.appendChild(new_text);
			miasta.appendChild(new_option);
        }
    }
}

/*
function showLegend(legend) {
	
    var tds = document.getElementsByTagName("td");    
    var regex  = new RegExp(/^([0-9]{1,2}:[0-9]{1,2})|([0-9]{1,2}\.[0-9]{1,2})/g);
    
    if (document.all && !window.opera) {
        var regex2 = new RegExp("<span_class=.*>(.*)</span>", "gi");
        var regex3 = new RegExp("<span class=(.*)>", "gi");
        var regex4 = new RegExp("<span_class=(.*)>", "gi");
        var rep1 = "<span_class=$1>";
        var rep2 = "<span class=$1>";
    } else {
        var regex2 = new RegExp("<span_class=\".*\">(.*)</span>", "gi");
        var regex3 = new RegExp("<span class=\"(.*)\">", "gi");
        var regex4 = new RegExp("<span_class=\"(.*)\">", "gi");
        var rep1 = "<span_class=\"$1\">";
        var rep2 = "<span class=\"$1\">";
    }
    for (var j = 0; j < legend.length; j++) {
        if (legend[j][0] == "+" || legend[j][0] == "$" || legend[j][0] == "^" || legend[j][0] == "*") {
            legend[j][0] = "\\" + legend[j][0];
        }
        legend[j][0] = legend[j][0].replace("&amp;", "&");
        legend[j][0] = legend[j][0].replace("&gt;", ">");
        legend[j][0] = legend[j][0].replace("&lt;", "<");
    }
    for (var i = 0; i < tds.length; i++) {
        if (tds[i].className.match("godziny")) {
            var content = tds[i].innerHTML;
            var changed = false;
            content = content.replace(regex3, rep1);
            content = content.split(" ");
            for (var k = 0; k < content.length; k++) {
                var matched = new Array();
                var index = 0;
                var tmp   = content[k];
                tmp = tmp.replace(regex2, "$1");
                tmp = tmp.replace("&amp;", "&");
                tmp = tmp.replace("&lt;", "<");
                tmp = tmp.replace("&gt;", ">");
                tmp = tmp.replace(regex, "");
                for (var j = 0; j < legend.length; j++) {
                    var tmp2 = legend[j][0];
                    if (tmp.match(tmp2) != null) {
                        matched[index] = j;
                        index += 1;
                    }
                }
                if (matched.length) {                     
                    var className = "legend";
                    for (j = 0; j < matched.length; j++) {
                        className += "-" + matched[j]; 
                    }
                    content[k] = '<span class="' + className + '">' + content[k] + "</span>";
                }
            }
            content = content.join(" ");
            content = content.replace(regex4, rep2);
            tds[i].innerHTML = content;
        }
    }
    tds = null;
    var spans = document.getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].className.substr(0, 6) == "legend") {
            spans[i].style.cursor = "default";
            spans[i].onmouseover = function(e) {
                this.style.borderBottom = "1px solid #060";
                if (!e) var e = window.event;
				x = mouseX(e);
				y = mouseY(e);
				className = this.className.split("-");
				var message = "<table>";
				for (var j = 0; j < className.length; j++) {
                    if (legend[className[j]] && legend[className[j]][0] && legend[className[j]][1]) {
                        if (legend[className[j]][0] == "\\+" || legend[className[j]][0] == "\\*" || legend[className[j]][0] == "\\^" || legend[className[j]][0] == "\\$") {
                            legend[className[j]][0] = legend[className[j]][0].substr(1,1);
                        }
                        message += '<tr><td valign="top">'+legend[className[j]][0]+"</td><td>"+legend[className[j]][1]+"</td></tr>";
                    }
				}
				message += "</table>";
				return ShowMsg(message, x, y);
            }
            spans[i].onmouseout = function() {
				//pausecomp(250);
				this.style.borderBottom = "1px solid #fff";
				document.getElementById('linkMsg').style.display = 'none';
			}
        }
    }
    spans = null;
    linkMsg = null;
}
*/

function showLegend(legend) {
	
    var tds = document.getElementsByTagName("td");
    // budujemy regexa
    var tmp = " \(\)A-Za-zĄĆĘŃŁŃÓŚÓŻŹąćęńłóśóżź";
    var tmpRegExp = new RegExp("[^ " + tmp + "]");
    var special = "()\\/+?$^-{},.:";
    for (var i = 0; i < legend.length; i++) {
        var symbol = legend[i][0];
        for (var j = 0; j < symbol.length; j++) {
            var tmpSymbol = symbol.charAt(j);
            if (special.indexOf(tmpSymbol) != -1) {
                tmpSymbol = "\\" + tmpSymbol;
            }
            if (typeof tmpSymbol == "undefined") {
                throw "tmpSymbol is undefined";
            }          
            if (tmpSymbol.match(tmpRegExp)) {
                tmp += tmpSymbol;
            }
        }
    }
    var hourAndLegendRegex = new RegExp("([0-9]{1,2}(?:\\:|\\.)[0-9]{1,2})\\s?([" + tmp + "]+)\\s", "gm");
    for (var i = 0; i < tds.length; i++) {
        if (tds.item(i).className.match("godziny")) {
            var html = tds.item(i).innerHTML;
            // dodajemy spacje na koniec htmla, zeby dzialal regex
            html += " ";
            html = html.replace(hourAndLegendRegex, '$1 <span class="legend">$2</span> ');
            // pozbywamy sie spacji z konca, ktora wczesniej dodalismy i wstawiamy do htmla
            tds.item(i).innerHTML = html.replace(/\s$/, '');
        }
    }
    tds = null;
    
    var spans = document.getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans.item(i).className == "legend") {
            spans.item(i).style.cursor = "default";
            spans.item(i).onmouseover = legendOnMouseOver;
            spans.item(i).onmouseout = function() {
                //pausecomp(250);
                this.style.borderBottom = "1px solid #fff";
                document.getElementById('linkMsg').style.display = 'none';
            }
        }
    }
    spans = null;
}


function legendOnMouseOver(e) {
    this.style.borderBottom = "1px solid #060";
    if (!e) var e = window.event;
    x = mouseX(e);
    y = mouseY(e);
    
    // odczytujemy jakie symbole trzeba pobrac z legendy
    var symbols = new Array();
    var parenthesisOpen = false;
    var parenthesisCount  = 0;
    var multiLetterSymbol = "";
    var platform = -1;
    var text = this.innerHTML;
    for (var j = 0; j < text.length; j++) {
        if (parenthesisOpen == true) {
            if (text.charAt(j) == "(") {
                parenthesisCount  += 1;
                multiLetterSymbol += text.charAt(j);
            } else if (text.charAt(j) == ")") {
                parenthesisCount  -= 1;
                if (parenthesisCount == 0) {
                    symbols.push(multiLetterSymbol);
                    multiLetterSymbol = "";
                    parenthesisOpen   = false;
                } else {
                    multiLetterSymbol += text.charAt(j);
                }
            }
        } else {
            if (text.charAt(j) == "(") {
                parenthesisCount += 1;
                parenthesisOpen   = true;
            } else {
                if (j != text.length - 1 || isNaN(parseInt(text.charAt(j)))) {
                    symbols.push(text.charAt(j));
                } else {
                    platform = parseInt(text.charAt(j));
                }
            }
        }
    }
    message = "";
    if (symbols.length) {
        for (var i = 0; i < symbols.length; i++) {
            if (symbols[i] && symbols[i].length) {
                for (var j = 0; j < legend.length; j++) {
                    if (legend[j] && legend[j][0] == symbols[i]) {
                        message += '<tr><td valign="top">'+legend[j][0]+"</td><td>"+legend[j][1]+"</td></tr>";
                        break;
                    }
                }
            }
        }
    }
    if (platform != -1) {
        message += '<tr><td valign="top" colspan="2">Peron ' + platform + "</td></tr>";
    }
    if (message) {
        ShowMsg("<table>" + message + "</table>", x, y);
    }
}

function ShowMsg(msg, x, y) {
	if (document.getElementById && x && y) {
		var linkMsg = document.getElementById('linkMsg');
		linkMsg.innerHTML = msg;
		/*var title = document.createTextNode(msg);
		if (linkMsg.firstChild) {
			var old = linkMsg.firstChild;
			linkMsg.replaceChild(title, old);
		}
		else {
			linkMsg.appendChild(title);
		}*/
		linkMsg.style.display    = "block";
		linkMsg.style.visibility = 'visible';
		linkMsg.style.position   = 'absolute';
		linkMsg.style.top        = y+10+"px";
		linkMsg.style.left       = x+3+"px";
		linkMsg = null;
	}
}

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

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

function rozklad() {
    var odjazd = document.getElementById("przyjazd1");
    if (odjazd.checked) {
        document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Dokąd";
    } else {
        document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Skąd";
    }
    odjazd.onclick = function() {
        if (this.checked) {
            document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Dokąd";
        } else {
            document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Skąd";
        }        
    }
    var przyjazd = document.getElementById("przyjazd2");
    przyjazd.onclick = function() {
        if (this.checked) {
            document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Skąd";
        } else {
            document.rozklad.dokad.options.item(0).childNodes.item(0).nodeValue = "Dokąd";
        }        
    }
    przyjazd = null;
}

function menu() {
    var menuObrazkowe = document.getElementById('menuObrazkowe');
    var images = menuObrazkowe.getElementsByTagName('img');
    var doc    = document;
    if (doc.images && !doc.preloadedImages) {
        doc.preloadedImages = new Array(images.length);
        for (var i = 0; i < images.length; i++) {
            if (images.item(i).src.substr(images.item(i).src.length - 6, 6) == "00.jpg") {
                doc.preloadedImages[i]     = new Image;
                doc.preloadedImages[i].src = images.item(i).src.replace('00', '01');
            }
        }
    }
    for (var i = 0; i < images.length; i++) {
        if (images.item(i).src.substr(images.item(i).src.length - 6, 6) == "00.jpg") {
            images.item(i).onmouseover = function() {
                this.src = this.src.replace('00', '01');
            }
            images.item(i).onmouseout = function() {
                this.src = this.src.replace('01', '00');
            }
        }
    }
    doc    = null;
    images = null;
    menuObrazkowe = null;
}

function ofertaMenu() {
    var ofertaMenu = document.getElementById("ofertaMenu");
    ofertaMenu.childNodes.item(0).onclick = function() {
        var ul = document.getElementById("ofertaMenu").childNodes.item(2);
        if (ul.style.display == "block") {
            ul.style.display = "none";
        } else {
            ul.style.display = "block";
        }
        ul = null;
        return false;
    }
    ofertaMenu = null;
}

window.onload = function() {
	if (document.getElementById && document.createElement) {
		if (document.getElementById("images")) {
			popUpImg();
		}
		if (document.getElementById('marka') && document.getElementById('modele') && document.getElementById('rocznik'))
			changeAll();
        if (document.getElementById('rozklad')) {
            rozklad();
        }
        if (document.getElementById('menuObrazkowe')) {
            menu();
            ofertaMenu();
        }
        var linkMsg = document.createElement('div');
        linkMsg.setAttribute('id','linkMsg');
        linkMsg.style.display    = "none";
        linkMsg.style.visibility = 'hidden';
        document.getElementsByTagName("body").item(0).appendChild(linkMsg);
	}
}