﻿
/*************************\
* ITALCOM S.p.A.          *
* Library: CPlugin.js     *
* Version: 1.0            *
* Require: Core.js        *
* Objects: CPlayer        *
*          CScroller      *
\*************************/

 ///////////
// CPlayer
//
function CPlayer(id, className)
{
	this.id = id;
	this.cls = className == null ? "CPlayer" : className;
	this.idContainer = this.id+"_container";
	if (window.PlayerList == null)
		window.PlayerList = [];
}
CPlayer.prototype.defaultText = Language != null ? Language.label("Componente non installato") : "xxx Componente non installato";
CPlayer.prototype.defaultImage = null;
CPlayer.prototype.downloadUrl = null;
CPlayer.prototype.play = EmptyFunction;

CPlayer.prototype.HTML
=//===================
function()
{
	var HTML = new CString();
	HTML.append('<table id="{id}_TABLE" class="{cls}-TABLE" cellspacing="0" cellpadding="0" border="0" summary="CPlayer"><tr><td class="{cls}-TD">', this);
	var a1 = "";
	var a2 = "";
	if (!IsEmpty(this.downloadUrl))
	{
		a1 = StringFormat('<a class="{cls}-A-Text" target="_blank" href="{downloadUrl}">', this);
		a2 = '</a>';
	}
	if (!IsEmpty(this.defaultText))
		HTML.append('<p class="{cls}-P-Text">'+ a1 +'{defaultText}'+ a2 +'</p>', this);
	if (!IsEmpty(this.defaultImage))
		HTML.append('<p class="{cls}-P-Image">'+ a1 +'<img class="{cls}-IMG" src="{defaultImage}" />'+ a2 +'</p>', this);
	HTML.append('</td></tr></table>');
	return HTML.toString();
}

CPlayer.prototype.writeHTML
=//========================
function(idContainer)
{
	if (IsEmpty(idContainer))
		idContainer = this.idContainer;
	else
		this.idContainer = idContainer;
	var container = document.getElementById(this.idContainer);
	if (container == null) 
		document.write(StringFormat('<div id="{idContainer}" class="{cls}-DIV">', this)+ this.HTML() +'</div>');
	else
		container.innerHTML = this.HTML();
}


 /////////////
// CScroller
//
function CScroller(id, cls)
{
	if (IsEmpty(cls)) cls = "CScroller";
	
	this.id = id;
	this.cls = cls;
	this.timer = null;
	this.count = 0;
	this.object = null;
	this.direction = ""; // "left" "right" "up" "down"
}
CScroller.prototype.msecDelay = 10;
CScroller.prototype.pxStep = 30;
CScroller.prototype.nStep = 15;

CScroller.prototype.scroll
=//=======================
function (object_id, direction, nPixel, nStep, msecDelay)
{
	if (nPixel > 0) this.nPixel = nPixel;
	if (nStep > 0) this.nStep = nStep;
	if (msecDelay > 0) this.msecDelay = msecDelay;
	
	// Avvio scroll
	if (object_id != null)
	{
		this.object = IsString(object_id) ? document.getElementById(object_id) : object_id;
		window.clearTimeout(this.timer);		
		this.direction = direction;
		this.count = 0;
		this.timer = window.setTimeout(this.id+".scroll()", 0);
	}
	// oppure continuazione se object_id è null
	else
	{
		if (this.count >= this.nStep)
		{
			window.clearTimeout(this.timer);
			this.timer = null;
			return;
		}
		this.count++;
		var scroll;
		switch (this.direction)
		{
			case "left":
				scroll = this.object.scrollLeft + this.pxStep;
				if (scroll + this.object.offsetWidth > this.object.scrollWidth)
					scroll = this.object.scrollWidth - this.object.offsetWidth;
				this.object.scrollLeft = scroll;
				break;
			case "right":
				scroll = this.object.scrollLeft - this.pxStep;
				if (scroll < 0 )
					scroll = 0;
				this.object.scrollLeft = scroll;
				break;
			case "top":
				break;
			case "bottom":
				break;
		} 
		this.timer = window.setTimeout(this.id+".scroll()", this.msecDelay);
	}
}