function ticker_itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, ticker_itemList.length);
    carousel.add(i, ticker_getItemHTML(ticker_itemList[idx - 1]));
};

function ticker_initCallback(carousel) {   
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function ticker_formatDate(pd) {
    var myData = pd;
    da = new Date(myData);
    db = da.toGMTString();
    dc = db.split(" ");
    if (eval(dc[3]) < 1970) {
        dc[3] = eval(dc[3])+100;
    }
    dd = dc[2] + " " + dc[1] + ", " + dc[3];
    return dd;
} 

function ticker_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};

function ticker_getItemHTML(item) {
    item_link = item.link;
    item_link = item_link.replace(/"/g, '&quot;');
    return '<div><a href="' + item_link + '" title="' + item.title + '">' + ticker_truncate(item.title, 86) + '</a> - ' + item.date + '</div>';
};

function ticker_getNodeText(node) {
    var text = "";
    if(node.text) text = node.text;
    if(node.firstChild) text = node.firstChild.nodeValue;
    return text;
}

function ticker_truncate(str, length, suffix) {
    if (str.length <= length) {
        return str;
    }

    if (suffix == undefined) {
        suffix = '...';
    }

    return str.substr(0, length).replace(/\s+?(\S+)?$/g, '') + suffix;
};

$(document).ready(function() {

    var curdate = new Date();
    var loc = window.location.toString();
    var strpos = loc.search(/valve/);   
    
    if (strpos > 0) {
        res = '../xml/currentfeed.xml?';
    }
    else {
        res = 'http://w3.upm-kymmene.com/upm/internet/cms/upmcms.nsf/UPMNewsTickerFeed.xml?OpenPage&';
    }
    
    $.get(res+curdate.getTime(), function(xml) {

        ticker_itemList = new Array();
        counter = 0;
    
        $("item", xml).each(function() {        
            ticker_itemList[counter] = new Array();        
            $(this).find("link").each(function(){
                    ticker_itemList[counter]['link'] = ticker_getNodeText(this);
            }).end().find("title").each(function(){
                    ticker_itemList[counter]['title'] = ticker_getNodeText(this);
            }).end().find("pubDate").each(function(){
                    ticker_itemList[counter]['date'] = ticker_getNodeText(this);
            });        
            counter++;
        });
        
        if (ticker_itemList) {
            $('#ticker').jcarousel({
                wrap: 'circular',
                auto: 4,
                scroll: 1,
                animation: 1400,
                itemVisibleInCallback: {onBeforeAnimation: ticker_itemVisibleInCallback},
                itemVisibleOutCallback: {onAfterAnimation: ticker_itemVisibleOutCallback},            
                initCallback: ticker_initCallback        
            });                
            
            if ($.browser.safari) {
                $("#ticker").css("padding-top", "1px");
            }        
        }
    });      
});
