﻿

function loadXml(classe, url) {
    
    var xml;
    new Ajax.Request(url, {
        method: 'GET',
        onCreate: function () {
            //$('loader').toggleClassName('displayNone'); 
        },
        onSuccess: function (transport) {
            //$('loader').toggleClassName('displayNone');
            xml = transport.responseXML;
            classe.rssLoaded(xml);
        }
    });
}
function dateToString(date) {
    //alert(date + " - " + dateDiff(date, new Date()));

    var months = Array("Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic");
    var now = new Date();
    if (now.getDate() == date.getDate()) {
        return Date.padded2(date.getHours()) + ":" + Date.padded2(date.getMinutes());
    }
    return date.getDate() + " " + months[date.getMonth()];
}

Date.prototype.getWholeDays = function () {
    return Math.floor(new Date() / 1000 * 60 * 60 * 24);
};
Date.padded2 = function (hour) { var padded2 = parseInt(hour, 10); if (hour < 10) padded2 = "0" + padded2; return padded2; }




var newsMarquee = Class.create({

    initialize: function (element, options) {
        this.element = $(element);
        this.innerDiv = this.element.down('div');
        this.options = {
            speed: 3,
            control: true
        };
        Object.extend(this.options, options || {});

        //this.playScroll();
        this.loadRss();

    },

    addObserver: function () {
        this.element.observe('mouseover', this.pauseScroll.bind(this));
        this.element.observe('mouseout', this.playScroll.bind(this));
    },

    playScroll: function () {
        this.scrolling = true;
        this.startScroll();
    },

    pauseScroll: function () {
        if (this.timeout) {
            this.timeout.stop();
            this.scrolling = false;
        }
    },

    startScroll: function () {
        if (this.scrolling) {
            this.timeout = new PeriodicalExecuter(function () {
                this.executeScroll();
            } .bind(this), this.options.speed / 100);
        }
    },

    init: function () {
        this.initialWidth = this.element.getWidth();
        this.childWidth = 0;
        this.childs = this.innerDiv.childElements();
        this.childs[0].style.paddingLeft = this.initialWidth + 'px';
        this.childs[this.childs.length - 1].style.paddingRight = this.initialWidth + 'px';
        this.childs.each(function (node) {
            this.childWidth += node.getWidth();
        } .bind(this)
 		)
        this.innerDiv.style.width = this.childWidth + 'px';
    },

    loadRss: function () {
        url = "/Shared/GetNews.aspx";
        loadXml(this, url);

    },

    rssLoaded: function (xml) {
        this.showData(xml);
    },

    showData: function (xml) {
        //Inizializzo il mio contenuto
        var html = "";
        //Imposto in doc il mio elemento radice
        var doc = xml.documentElement;
        //Estraggo i vari nodi 'item' che compongono il mio file
        var items = doc.getElementsByTagName('item');

        for (var i = 0; i < items.length; i++) {
            var title = items[i].getElementsByTagName('title')[0].firstChild.data;
            //(\w+)([A-Z]+)(\w+)
            //var re = new RegExp("(\\w+«*»*)([A-ZÈ]+)(.*? )", "g");
            //var re = new RegExp("(«*\\w+»*)(«*[A-ZÈ]+)(.*? )", "g");
            var re = new RegExp("(«*[\\wàèéìòù]+»*)(«*[A-ZÈ]+)(.*?)", "g");
			
            title = title.replace(re, "$1 · $2$3");

            var link = items[i].getElementsByTagName('link')[0].firstChild.data;
            var data = items[i].getElementsByTagName('pubDate')[0].firstChild.data;
            var date = new Date(data);
            var dataS = dateToString(date);
            html += "<p><em>" + dataS + "</em> - <a target='_blank' href='" + link + "'>" + title + "</a></p>";
        }
        this.element.down('div').update(html);
        this.init();
        this.playScroll();
        if (this.options.control) {
            this.addObserver();
        }

    }



});

var horizontalMarquee = Class.create(newsMarquee,{

	initialize: function($super,element,options) {	
		$super(element,options);
	},

    init: function () {
        this.initialWidth = 50; //this.element.getWidth();
        this.childWidth = 0;
        this.childs = this.innerDiv.childElements();
        this.childs[0].style.paddingLeft = this.initialWidth + 'px';
        this.childs[this.childs.length - 1].style.paddingRight = this.initialWidth + 'px';
        this.childs.each(function (node) {
            this.childWidth += node.getWidth();
        } .bind(this)
 		    )
        this.innerDiv.style.width = this.childWidth + 'px';
    },

	
	executeScroll: function() {
		if (this.element.scrollLeft > (this.element.scrollWidth-this.initialWidth)) {
    		this.element.scrollLeft = 0;
  		}
		this.element.scrollLeft = this.element.scrollLeft+1;		
	}

});


var verticalMarquee = Class.create(newsMarquee,{

	initialize: function($super,element,options) {
		$super(element,options);		
		this.initialHeight = this.element.getHeight();
		this.innerDiv.style.paddingTop = this.initialHeight+'px';
  		this.innerDiv.style.paddingBottom = this.initialHeight+'px';	
	}, 
	
	executeScroll: function() {
		if (this.element.scrollTop>=(this.element.scrollHeight-this.initialHeight)) {
			this.element.scrollTop=0; 	
		}
		this.element.scrollTop = this.element.scrollTop+1;	
	}

});

