function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



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 selectOddzial() {
    var Select = document.getElementById("selectOddzial");
    Select.onchange = function() {
        var Location = Select.options[Select.selectedIndex].value;
        if (Location)
            location.href = 'http://www.windicar.pl/index.php?id=5#' + Location;
    }
}

function setKontaktForm() {
    var url = location.href;
    var paramTokens = url.split("#");
    var miasto  = paramTokens[paramTokens.length-1];
    var selects = document.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++) {
        var Select = selects[i];
        for (var j = 0; j < Select.options.length; j++) {
            if (Select.options[j].hasChildNodes()) {
                var Opis = Select.options[j].value;
                Opis = Opis.substr(0,Opis.indexOf('@'));
                if (Opis == miasto) {
                    Select.selectedIndex = j;
                    break;
                }
            }
        }
    }
}

function ShowElement(x)
{
  var field = document.getElementById(x);
  field.style.display = 'block';
}

function HideElementAgain(x)
{
  var field = document.getElementById(x);
  field.style.display = 'none';
}

function ShowHideElement(x) {
    var field = document.getElementById(x);
    if (field.style.display == 'none')
        ShowElement(x);
    else
        HideElementAgain(x);
}

function randomNumber() {
    var number = Math.round(Math.random() * 10);
    if (number == 0)
        number = 1;
    return number;
}

window.onload = function() {
    if (document.getElementById && document.createElement) {
        if (document.getElementById("images")) {
            popUpImg();
        }
        if (document.getElementById("selectOddzial")) {
            selectOddzial();
        }
        if (document.getElementById("kontaktForm")) {
            setKontaktForm();
        }
    }
    MM_preloadImages('images/bu_aktualnosci01.gif, images/bu_ofirmie01.gif, images/bu_certyfikaty01.gif, images/bu_dobor_oleju01.gif, images/bu_kontakt01.gif, images/bu_kontakt01.gif, images/bu_kontakt01.gif');
}
