﻿function createXMLHttpRequest() {
    try { return new XMLHttpRequest(); } catch (e) { }
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
    alert("XMLHttpRequest not supported");
    return null;
}

function fillElementWithHtml(elementId, url) {

    var http_request = createXMLHttpRequest();

    if (http_request == null)
        return;
        
    if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
    }
    
    http_request.onreadystatechange = function() { showHtmlInElement(elementId, http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);

}

function showHtmlInElement(elementId, http_request) {
    var element;

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            element = document.getElementById(elementId);
            element.innerHTML = http_request.responseText;
        }
        else {
            alert('Wystąpił problem z zapytaniem.');
        }
    }

}

function showNews(elementId, url) {

    var http_request = createXMLHttpRequest();

    if (http_request == null)
        return;

    if (http_request.overrideMimeType) {
        http_request.overrideMimeType('application/json');
    }

    http_request.onreadystatechange = function() { renderNews(elementId, http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function renderNews(elementId, http_request) {
    
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {

            var data = eval('(' + http_request.responseText + ')');
            
            var aktualnosciContainer = document.getElementById(elementId);
            
            for (var i = 0; i < data.aktualnosci.length; i++) {

                var span = document.createElement('span');

                span.appendChild(document.createTextNode(data.aktualnosci[i].linkTekst));
                
                aktualnosciContainer.appendChild(span);

                var divAktualnosc = document.createElement('div');
                divAktualnosc.id = data.aktualnosci[i].id;
                divAktualnosc.className = 'aktualnosc-content';

                aktualnosciContainer.appendChild(divAktualnosc);
                
                fillElementWithHtml(data.aktualnosci[i].id, data.aktualnosci[i].url);
            }

//            $("#aktualnosci span").click(
//            function() {
//                $(this).next("div").toggle("normal");
//            }
            //            );

            $("#aktualnosci span").toggle(
                function() {
                    $(this).next("div").show("normal");
                    $(this).css("background-image", "url('images/sm-arrow-down.png')");
                }, function() {
                    $(this).next("div").hide("normal");
                    $(this).css("background-image", "url('images/sm-arrow-right.png')");
                }
            );

        }
        else {
            alert('Wystąpił problem z zapytaniem.');
        }
    }
}
