﻿function cmsNewsTicker( instance, nodeid, width, height, query_id, query_type, delay_display, delimiter, date_format ) {
	this.instance = instance;
	this.nodeid = nodeid;
	this.width = width;
	this.height = height;
	this.query_id = query_id;
	this.query_type = query_type;
	this.delay_display = delay_display;
	this.delimiter = delimiter;
	this.date_format = date_format;  /* may be something like 'd.m.Y: ', see FormatDate() */
	
	this.pos = 0;
	this.node = null;
	this.news = new Array();
	this.IE = document.all&&!window.opera;
	this.DOM = document.getElementById&&!this.IE;
	this.delay_init = 100;
	this.interval = null;

	if (this.DOM||this.IE) this.Initialize();
}


cmsNewsTicker.prototype.Initialize = function() {
	var _this = this;
	$.ajax({ type: "GET", url: "/__/frontend/handler/document.php", data: "id=" + this.query_id + "&type=" + this.query_type, dataType: "xml", success: function(xml){
		$("news > entry", xml).each( function() {
			_this.news.push({title:$("title", this).text(), url: $("url", this).text(), url_target: $("url_target", this).text(), date: $("date", this).text()});
		});
		window.setTimeout(_this.instance + ".SetContent()", _this.delay_init);
	}});
}


cmsNewsTicker.prototype.CreateNodes = function() {

	var outer_node = this.IE ? document.all[this.nodeid] : document.getElementById(this.nodeid);

	if (!outer_node) return false;
	
	outer_node.style.overflow = "hidden";
	outer_node.style.width = this.width + "px";
	outer_node.style.height = this.height + "px";
	
	var inner_node = document.createElement('div');
	inner_node.style.overflow = "hidden";
	inner_node.style.position = "absolute";
	inner_node.style.width = this.width + "px";
	inner_node.style.height = this.height + "px";
	inner_node.style.clip = "rect(0px " + this.width + "px " + this.width + "px 0px)";
	outer_node.appendChild(inner_node);
	
	this.node = document.createElement('span');
	this.node.style.whiteSpace = "nowrap";
	this.node.style.position = "relative";
	inner_node.appendChild(this.node);

	return true;
}


cmsNewsTicker.prototype.FormatDate = function(date_object, character) {
    switch (character) {
		case 'Y': return date_object.getFullYear();
		case 'y': return date_object.getYear();
		case 'n': return date_object.getMonth()+1;
		case 'm': return (date_object.getMonth() < 9) ? ('0' + parseInt(date_object.getMonth())+1) : parseInt(date_object.getMonth())+1;
		case 'j': return date_object.getDate()+1;
		case 'd': return (date_object.getDate() < 10) ? ('0' + date_object.getDate()) : date_object.getDate();
	    default:  return character;
	}
}


cmsNewsTicker.prototype.GetFormattedDate = function( datestring ) {
	var return_string = "";

	var regexp = "^([0-9]{4})((-?([0-9]{2})(-?([0-9]{2}))?)|(-?([0-9]{3}))|(-?W([0-9]{2})(-?([1-7]))?))?$";
	var d = datestring.match(new RegExp(regexp));
	if (!d) return return_string;

	var date_object = new Date();
	date_object.setFullYear(d[1]);
	date_object.setMonth(d[4] - 1); 
	date_object.setDate(d[6]);

	for (var i = 0; i < this.date_format.length; ++i) return_string += this.FormatDate(date_object, this.date_format.charAt(i));
	return return_string;
}


cmsNewsTicker.prototype.SetContent = function() {
	var _this = this;
	var classNode;
	var parentNode;
	if (!this.CreateNodes()) {
	    // DOM not yet ready - try again later
		window.setTimeout(this.instance + ".SetContent()", 1000);
	    return false;
	}

	for(i=0; i<3; i++) {
		for(a=0; a<this.news.length; a++) {

			// news_delimiter
			classNode = document.createElement('span');
			classNode.className = 'news_delimiter';
			this.node.appendChild(classNode);
			classNode.appendChild(document.createTextNode(this.delimiter));

			// set parentNode
			if (this.news[a]['url'] != '') {
				parentNode = document.createElement('a');
				parentNode.href = this.news[a]['url'];
				parentNode.target = (this.news[a]['url_target'] == 'blank') ? '_blank' : '_self';
				this.node.appendChild(parentNode);
			} else {
				parentNode = this.node;
			}

			// news_date
			if (this.date_format != '') {
				classNode = document.createElement('span');
				classNode.className = 'news_date';
				parentNode.appendChild(classNode);
				var date_string = this.GetFormattedDate(this.news[a]['date']);
				classNode.appendChild(document.createTextNode(date_string));
			}

			// news_title
			classNode = document.createElement('span');
			classNode.className = 'news_title';
			classNode.appendChild(document.createTextNode(this.news[a]['title']));
			parentNode.appendChild(classNode);
		}
	}
	this.Start();
	this.node.onmouseover = function() { eval(_this.instance + ".Stop();"); };
	this.node.onmouseout = function() { eval(_this.instance + ".Start();"); }; }


cmsNewsTicker.prototype.Start = function() {
	this.interval = window.setInterval( this.instance + ".DoTick()", this.delay_display); }

cmsNewsTicker.prototype.Stop = function() {
	if (this.interval) window.clearInterval(this.interval);
}

cmsNewsTicker.prototype.DoTick = function() {
	var offset = this.node.offsetWidth/3;
	if (Math.abs(this.pos) > offset) this.pos=0;
	this.node.style.left=this.pos+'px';
	this.pos=parseInt(this.pos)-1;
}