function scroller(targetID, data) {
    this.id = targetID;
    this.options = "";
    this.data = data;

    this.newsTitle = new Array();
    this.newsLink = new Array();
    this.newsText = new Array();
    this.delay = 5000;
}

scroller.prototype.start = function() {
    this.loadData();
    this.setProperty();
    this.render();
}

scroller.prototype.loadData = function() {
    this.options = this.data;
    this.options = this.options.parseQuery();
}

scroller.prototype.setProperty = function() {
    this.delay = this.getProperty(this.options, "delay", "5000");
    $(this.id + "_CONTENT").style.width = this.getProperty(this.options, "width", "250px");
    $(this.id + "_CONTENT").style.height = this.getProperty(this.options, "height", "100px");
    $(this.id + "_CONTENT").style.backgroundColor = this.getProperty(this.options, "background", "#FFFFFF");
    $(this.id + "_CONTENT").style.borderStyle = this.getProperty(this.options, "border", "solid").toLowerCase();
    $(this.id + "_CONTENT").style.textAlign = this.getProperty(this.options, "align", "left").toLowerCase();
}

scroller.prototype.render = function() {
    var parent = this;
	var textScrollerHTML = "";
	for(i=0; i<this.newsTitle.length; i++) {
		textScrollerHTML += '<DIV id="' + this.id + '_TEXT' + i + '"' +
                            ' class="scrollerTEXT" style="WIDTH:' + (parseInt(Element.getDimensions(this.id + "_CONTENT")["width"]) - 16) + ';"' +
                            ' onMouseOver="scrollerPause(\'' + this.id + '\', ' + i + ')"' +
                            ' onMouseOut ="scrollerContinue(\'' + this.id + '\', ' + i + ')"' +
                            ' onClick="scrollerClick(\'' + this.id + '\', ' + i + ')">' +
                                '<SPAN id="' + this.id + '_VARIABLE"></SPAN>' +
                                '<SPAN id="' + this.id + '_TEXTTITLE' + i + '"' +
                                ' class="scrollerTEXTTITLE" ' +
                                ' title="url=' + this.newsLink[i] + '"' +
                                ' style="WIDTH:' + (parseInt(Element.getDimensions(this.id + "_CONTENT")["width"]) - 16) + ';"' + '>' +
                                this.newsTitle[i] + '</SPAN><BR />' + this.newsText[i] +
                            '</DIV>';
	}
	$(this.id + "_CONTENT").innerHTML = textScrollerHTML;
    $(this.id + "_VARIABLE").className = "height=" + Element.getDimensions(this.id + "_CONTENT")["height"] + "&count=" + this.newsTitle.length + "&delay=" + this.delay + "&say=0&enson=0&hidsay=0&hidson=0";
	setTimeout(function(){parent.renderCont();}, 500);
}

scroller.prototype.renderCont = function() {
    var parent = this;
	for(i=0; i<this.newsTitle.length; i++) {
		if(parseInt($(this.id + "_TEXT" + i).offsetHeight) <= 0) { setTimeout(function(){parent.renderCont();}, 1000); return; }
	}
	for(i=0; i<this.newsTitle.length; i++) {
        $(this.id + "_TEXTTITLE" + i).title += '&pos=' + $(this.id + "_TEXT" + i).offsetHeight;
    }
	for(i=0; i<this.newsTitle.length; i++) $(this.id + "_TEXT" + i).style.pixelTop = parseInt(Element.getDimensions(this.id + "_CONTENT")["height"]) + 200;
	scrollerTransition(this.id);
}

scroller.prototype.add = function(name, link, icon, content) {
    this.newsTitle[this.newsTitle.length] = name;
    this.newsLink[this.newsLink.length] = link;
    this.newsText[this.newsText.length] = formatNewsFeed(content, icon);
}

scroller.prototype.getProperty = function(sParameter, sProperty, sDefaultValue) {
    var sPropertyValue = sDefaultValue;
    if (sParameter[sProperty] != null) { if (sParameter[sProperty] != "") sPropertyValue = sParameter[sProperty]; }
    return sPropertyValue;
}

function scrollerTransition(targetID) {
    var textScrollerVariable = $(targetID + "_VARIABLE").className.parseQuery();
    var newsHeight = parseInt(textScrollerVariable["height"]);
    var newsCount = parseInt(textScrollerVariable["count"]);
    var delay = parseInt(textScrollerVariable["delay"]);
    var say = parseInt(textScrollerVariable["say"]);
    var enson = parseInt(textScrollerVariable["enson"]);
    var hidsay = parseInt(textScrollerVariable["hidsay"]);
    var hidson = parseInt(textScrollerVariable["hidson"]);

	$(targetID + "_CONTENT").filters[0].apply();
	for(i=hidson; i<=hidsay; i++) $(targetID + "_TEXT" + i).style.visibility = "hidden";

	var cvar = 0, tpos = 0, art = 1, topla = 0;
    var textLinkVariable;
	do {
        if(art == 1) { topla = 0; } else { topla = 10; }
        textLinkVariable = $(targetID + "_TEXTTITLE" + say).title.parseQuery();
        cvar = cvar + parseInt(textLinkVariable["pos"]) + topla;
        if(cvar < (parseInt(Element.getDimensions(targetID + "_CONTENT")["height"]) - 10)) { say++; if(say >= newsCount) { art = 1; break; } }
        art++;
	} while(cvar < (parseInt(Element.getDimensions(targetID + "_CONTENT")["height"]) - 10));
    if (enson == say) { say++; $(targetID + "_CONTENT").style.height = cvar + 12; }
    else { $(targetID + "_CONTENT").style.height = newsHeight; }

	say--;
	for(i=enson; i<=say; i++) {
		$(targetID + "_TEXT" + i).style.pixelLeft = 8;
		$(targetID + "_TEXT" + i).style.pixelTop = 4 + tpos;
        textLinkVariable = $(targetID + "_TEXTTITLE" + i).title.parseQuery();
		tpos = tpos + parseInt(textLinkVariable["pos"]) + 10;
		$(targetID + "_TEXT" + i).style.visibility = "visible";
	}
	hidsay = say; hidson = enson;

	say++;
	if(say >= newsCount) { say=0; }
	enson = say;

	$(targetID + "_CONTENT").filters[0].play();
	$(targetID + "_VARIABLE").className = "height=" + newsHeight + "&count=" + newsCount + "&delay=" + delay + "&say=" + say + "&enson=" + enson + "&hidsay=" + hidsay + "&hidson=" + hidson;
	eval(targetID + '_TIMEOUT = setTimeout("scrollerTransition(\'' + targetID + '\')", ' + delay + ')');
}

function scrollerPause(targetID, iNewsID) {
	eval('clearTimeout(' + targetID + '_TIMEOUT)');
    $(targetID + "_TEXT" + iNewsID).className = "scrollerTEXT scrollerTEXT_HILITE";
    $(targetID + "_TEXTTITLE" + iNewsID).className = "scrollerTEXTTITLE scrollerTEXTTITLE_HILITE";
    $(targetID + "_TEXT" + iNewsID).style.cursor = 'hand';
    $(targetID + "_TEXTTITLE" + iNewsID).style.cursor = 'hand';
}

function scrollerContinue(targetID, iNewsID) {
	$(targetID + "_TEXT" + iNewsID).className = "scrollerTEXT";
	$(targetID + "_TEXTTITLE" + iNewsID).className = "scrollerTEXTTITLE";
	scrollerTransition(targetID);
}

function scrollerClick(targetID, iNewsID) {
    var newsLink = $(targetID + "_TEXTTITLE" + iNewsID).title.parseQuery();
    if(newsLink["url"].substring(0,11) == "javascript:") { eval("" + newsLink["url"]); }
    else { window.open(newsLink["url"], "_blank"); }
}

function formatNewsFeed(text, imageSrc) {
    var NewsFeed = "<TABLE border=0 cellPadding=0 cellSpacing=0><TR>" +
                        "<TD vAlign=top>" + ((imageSrc == "") ? "&nbsp;" : "<IMG src='" + imageSrc + "' />") + "</TD>" +
                        "<TD style='PADDING-LEFT:2px;FONT-SIZE:75%;LINE-HEIGHT:12pt;VERTICAL-ALIGN:top;'>" + text + "</TD>" +
                   "</TR></TABLE>";
    return (NewsFeed);
}

