///<reference path="jquery-1.3.2-vsdoc2.js" />

var quote_place;
var press_place;
var report_place;
var article_place;
var press_list;


function jsonCallbackArticle(o) {
    var placeholder = article_place;
    if (o && (el = document.getElementById(placeholder))) {
        el.style.display = 'block';
        //Custom html inside placeholder element
        for (var i = 0; i < o.data.length; i++) {
            el.innerHTML += '<div>' +
			o.data[0].finstatgraph + '</div>'
        }
    }

}

function xmlToList(o, comp, i, nb) {
    $(document).ready(function() {


        press_list = $('#press_list li.list:nth-child(' + i + ')');
        if (o && press_list) {
            var ul = $('<ul class="list">');
            press_list.append(ul);
            var li;
            var p;
            var h2;
            //var a;
            //var link;
            //var body;
            var numberOfNodes = nb;


            if (numberOfNodes == null) {
                
                numberOfNodes = o.childNodes.length;
            }

            for (var x = 0; x < numberOfNodes; x++) {
                if (o.childNodes[x].nodeName == 'release') {
                    for (var y = 0; y < o.childNodes[x].childNodes.length; y++) {
                        if (o.childNodes[x].childNodes[y].nodeName == 'published') {
                            li = $('<li>');
                            ul.append(li);
                            p = $('<p>');
                            li.append(p);
                            p.text(o.childNodes[x].childNodes[y].text.substr(0, 10));
                        }
                        if (o.childNodes[x].childNodes[y].nodeName == 'headline') {
                            h2 = $('<h2>');
                            li.append(h2);
                            
                            h2.html(o.childNodes[x].childNodes[y].text);

                            

                        }
                    }
                }
            }
            return;
        }
    });
}

// Raporter som listas på startsidan
function initListLeft2(data, obj, company, url, index, nb) {

    if (nb == "") {
        nb = 1;
    }

    if (url.indexOf('s=json') <= 0) {
        var http = new JKL.ParseXML.DOM(url);
        data = http.parse();
        xmlToList(data, comp, index, nb);
    } else {

        data = data.substr(0, data.length - 1) + ',' + index + ',' + nb + ',' + 'company' + ');';
        eval(data);
    }
}
function jsonCallbackReport(o, i, nb, company) {
    $(document).ready(function() {
        press_list = $('#press_list2 li.list:nth-child(' + i + ')');
        if (o && press_list) {
            var ul = $('<ul id="report_place" class="list">');
            press_list.append(ul);
            var li;
            var p;
            var h2;
            var a;
            var link;
            var counter = 0;

            jQuery.each(o.data, function(index, item) {
                if (counter < nb) {
                    li = $('<li>');
                    ul.append(li);
                    p = $('<p class="date">');
                    li.append(p);
                    p.text(item.published.substr(0, 10));
                    h2 = $('<p>');
                    li.append(h2);
                    a = $('<a>');
                    h2.append(a);
                    a.html(item.headline);
                    link = '/' + shortLang(item.language) + '/Pressrum-Feeds/Press1/?&afw_id=' + item.id + '&afw_lang=' + item.language + '&p=' + company;
                    a.attr('href', link);
                }
                counter++;
            });
        }
    });
}


// Pressreleaser som listas
function initList(data, obj, company, url, index, nb) {

    if (nb == "") {
        nb = 1;
    }
    
    if (url.indexOf('s=json') <= 0) {
        var http = new JKL.ParseXML.DOM(url);
        data = http.parse();
        xmlToList(data, comp, index, nb);
    } else {

    data = data.substr(0, data.length - 1) + ',' + index + ',' + nb + ',' + 'company' + ');';
        eval(data);
    }
}

function jsonCallbackPressList(o, i, nb, company) {
    $(document).ready(function() {
        press_list = $('#press_list li.list:nth-child(' + i + ')');
        if (o && press_list) {
            var ul = $('<ul class="list">');
            press_list.append(ul);
            var li;
            var p;
            var h2;
            var a;
            var link;
            var counter = 0;
            var tempDate;
            var tempTime;

            jQuery.each(o.data, function(index, item) {
                if (counter < nb) {
                    li = $('<li>');
                    ul.append(li);
                    p = $('<p class="date">');
                    li.append(p);
                    tempDate = item.published.substr(0, 10);
                    tempTime = item.published.substr(11, 5);
                    p.text(tempDate + ' ' + tempTime);
                    h2 = $('<h2>');
                    li.append(h2);
                    a = $('<a>');
                    h2.append(a);
                    a.html(item.headline);
                    link = '/' + shortLang(item.language) + '/Pressrum-Feeds/Press1/?&afw_id=' + item.id + '&afw_lang=' + item.language + '&p=' + company;
                    a.attr('href', link);
                }
                counter++;
            });
        }
    });
}

function shortLang(lang) {
    if (lang == 'Swedish') {
        return 'sv';
    } else {
        return 'en';
    }
}


// Kursdata startsidan
function initListLeft1(data, obj, company, url, index, nb) {
    if (nb == "") {
        nb = 1;
    }
    if (url.indexOf('s=json') <= 0) {
        var http = new JKL.ParseXML.DOM(url);
        data = http.parse();
        xmlToList(data, comp, index, nb);
    } else {
    data = data.substr(0, data.length - 1) + ',' + index + ',' + nb + ',' + 'company' + ');';
        eval(data);
    }
}
function jsonCallbackPressList1(o, i, nb, company) {
    var divG;
    divG = $('#finance');
    divG.html('');
    divG.html(divG.html() + '<p class="date">' + o.data[0].time + '</p>');
    divG.html(divG.html() + '<p>' + o.data[0].shortname + o.data[0].lastprice + ' | ' + o.data[0].diff + ' | ' + o.data[0].volume + '</p>');
}
