(function () {
// wywolywanie cufona
function cufonReplace() { 
	Cufon.replace('h2');
	Cufon.replace('.homeBox h3');
}


// otwierania linku o klasie openNew w nowym oknie
function newWindow () {
    function click() {
        window.open(this.href);
        return false;
    }
    var aA = kf.$$('openNew', 'a'), i;
    for (i = aA.length;i;) {
        i -= 1;
        aA[i].onclick = click;
    }
}

// pokazywanie formularza w kontaktach
function contactForm() {
    function show() {
        var formId = this.hash.replace('#', '');
        kf.$(formId).style.display = 'block';
        return false;
    }
    function close() {
        this.parentNode.parentNode.style.display = 'none';
        return false;
    }
    var aS = kf.$$('send', 'a');
    var aC = kf.$$('close', 'span');
    var i;
    for (i = aS.length;i;) {
        i -= 1;
        aS[i].onclick = show;
    }
    for (i = aC.length;i;) {
        i -= 1;
        aC[i].onclick = close;
    }
}

kf.addOnLoad(function() {
	newWindow();
	cufonReplace();
    var aPF = kf.$$('personalForm');
    var ajax = new HTTPRequest(), i;
	if(aPF.length) {
        contactForm();
        var sending = false;
        for (i = aPF.length;i;) {
            i -= 1;
            aPF[i].onsubmit = function () {
                var that = this;
                if (sending) {
                    alert('Jedna z wiadomości jest obecnie wysyłana.');
                    return false;
                }
                ajax.post(that.action, {'mail[id]': that.id, 'mail[from]':that.elements[1].value, 'mail[txt]': that.elements[2].value}, {'start':function () {
                    sending = true;
                }, 'open':function () {
                    sending = true;
                }, 'send':function () {
                    sending = true;
                }, 'load':function () {
                    sending = true;
                }, 'end': function () {
                    sending = false;
                    that.style.display = 'none';
                }});
                return false;
            }
        }
    }
});
})();
