/*
#VERSION_HISTORY_START

V1.02 - 06/09/10 TS
- added support for the html content to be pre-rendered using the supporting PHP class ConveyorContent

V1.01 - 23/02/10 TS
- added structure to allow the initialisation to be triggered at a later stage
- added PostShow() and PostHide() methods triggered from outside the object after it's parent object has been shown / hidden

#VERSION_HISTORY_END
*/

function Conveyor() {
	this.conveyorElements = new Array();
	this.conveyorWidth = 200;
	this.conveyorHeight = 50;
	this.sliderSpeed = 1;
	this.sliderDirection = 'left';
	this.delayInit = false;
	this.isInit = false; // used to determine whether the conveyor has been initialised
	this.crawlerIndex = null;
	
	this.AddConveyorImage = AddConveyorImage;
	this.AddContent = AddContent;
	this.SetConveyorWidth = SetConveyorWidth;
	this.SetConveyorHeight = SetConveyorHeight;
	this.SetSlideSpeed = SetSlideSpeed;
	this.SetDirection = SetDirection;
	
	/* define class methods */
	
	// indicates that the conveyor will not be initialised straight away
	this.DelayInit = function() {
		this.delayInit = true;
	}
	
	// initialise the plugin
	this.InitConveyor = function() {
		var conveyorId = "conveyor"+this.gadgetId;
		var cWidth = this.conveyorWidth;
		var cHeight = this.conveyorHeight;
		var sDirection = this.sliderDirection;
		var sSpeed = this.sliderSpeed;
		
		marqueeInit({
			uniqueid: conveyorId,
			style: {
				'padding': '0px',
				'width': cWidth+'px',
				'height': cHeight+'px'
			},
			direction: sDirection,
			inc: sSpeed, //speed - pixel increment for each iteration of this marquee's movement
			mouse: 'pause', //mouseover behavior ('pause' 'cursor driven' or false)
			noAddedSpace: true,
			moveatleast: 1,
			neutral: 150,
			savedirection: true
		});
		
		// work out the crawler index of the current active crawler - this can be used to do things like pause the scroller
		this.crawlerIndex = marqueeInit.ar.length - 1;
		
		this.isInit = true;
	}
	
	// creates the html needed by the plugin to render the conveyor
	this.CreateConveyor = function(supressHTMLOutput) {
		// if content has already been outputted by PHP class
		if (supressHTMLOutput != true) {
			var conveyorId = "conveyor"+this.gadgetId;
			document.write('<div id="abcd" style="width: '+this.conveyorWidth+'px; height: '+this.conveyorHeight+'px; overflow: hidden;"><div class="marquee" id="'+conveyorId+'">');
			var i;
			for (i = 0; i < this.conveyorElements.length; i++) {
				document.write(this.conveyorElements[i]);
			}
			document.write('</div></div>');
		}
		
		if (this.delayInit !== true) this.InitConveyor();
	}
	
	// pause / un pause the conveyor
	this.PauseConveyor = function(pauseAction) {
		// default to stop the marquee
		if (typeof(pauseAction) == 'undefined') pauseAction = true;
		// un-pause the conveyor
		if (this.crawlerIndex !== null) {
			marqueeInit.ar[this.crawlerIndex].stopMarquee = pauseAction;
		}
	}
	
	// this method will run after the gadget is shown by the master page class
	this.PostShow = function() {
		if (this.isInit === true) {
			this.PauseConveyor(false);
		} else {
			this.InitConveyor();
		}
	}
	
	// this method will run after the gadget is hidden by the master page class
	this.PostHide = function() {
		this.PauseConveyor(true);
	}
}

function AddConveyorImage(imageSrc, linkUrl) {
	imageSrc = '<img src="'+imageSrc+'" class="conveyorItem" />';
	if (linkUrl) {
		imageSrc = '<a href="'+linkUrl+'">'+imageSrc+'</a>';
	}
	this.conveyorElements.push(imageSrc);
}

function AddContent(contentHTML, contentWidth) {
	this.conveyorElements.push(contentHTML);
}

function SetConveyorWidth(width) {
	this.conveyorWidth = width;
}

function SetConveyorHeight(height) {
	this.conveyorHeight = height;	
}

function SetSlideSpeed(slideSpeed) {
	this.sliderSpeed = slideSpeed;
}

function SetDirection(sliderDirection) {
	this.sliderDirection = sliderDirection;
}

