/**
 * MDVTicker (mdvJavaScriptTickerComponent)
 * Implementation of a common ticker
 *
 * $Id: mdvTicker.js,v 1.2 2008/07/08 21:54:20 mdvti Exp $
 *
 * Software Version History:
 *
 * 1.1	2006/09/01	TI		HEAD:		Version added. Initial Revision.
 * 1.2	2006/09/02	TI		HEAD:		Refresh Header.
 *
 */


/**
 * MDVTicker
 * @param {string} name
 * @param {object} div
 * @param {string} url
 */
 function MDVTicker(name, div, url) {
 	// Name of ticker instance.
	this._name = name;

	// Check whether we are working with IE.
 	this._isIE = document.all;

 	// Width of the scrolling ticker.
 	this._width = -1;

 	// Width of the text within the scrolling ticker
 	this._textWidth = -1;

 	// Offset of the scrolling text. Perhaps we want to wait a few seconds before scrolling starts (default: 0).
 	this._offset = 0;

 	// Scroll speed of the ticker (default: 2).
 	this._speed = 2;

	// Do we need to stop the ticker if mouse is hovering over it (default: true)?
 	this._pauseIt = true;

 	// Do we need to update the ticker?
 	this._updateIt = true;

 	// Link URL
 	this._linkUrl;

 	// Ticker URL
 	this._url = url;

 	// Default text for ticker
 	this._defaultText = null;

 	// Message separator
 	this._separator = '|';

 	// Ticker data for srolling. Will be fetched from the ticker URL.
 	this._text = null;

 	// The ticker object (div element)
 	this._ticker = div;

 	// The scrolling element (div element)
 	this._scroller = null;

 	// Indicates whether we need to update (don't change this default value!).
 	this._doUpdate = false;
 }

 MDVTicker.prototype._updateData = function (refresher) {
 	this._doUpdate = false;

 	if (typeof this._url == 'string' && this._url == '' && this._defaultText != '') {
 		this._updateText(this._defaultText);
 		return false;
 	}

 	var _stamp = new Date().getTime();
	var _params = null;
	var _tickerUrl = null;
	
	if (typeof this._url == 'string') {
	 	_tickerUrl = this._url + '/XML_REQUEST';
	 	_params = { ts: _stamp, language: 'en', refresh: refresher };
	 	_params = $H(_params);
	 	_params = _params.toQueryString();
	} else if(typeof this._url == 'function' || typeof this._url == 'object') {
		_tickerUrl = this._url.apply();
		_params = { ts: _stamp, language: 'en', refresh: refresher, itdLPxx_provider: 'ticker' };
	 	_params = $H(_params);
	 	_params = _params.toQueryString();
	}

	var _ajaxRequest = new Ajax.Request(_tickerUrl, { parameters: _params, method: 'post', asynchronous: true, requestHeaders: ['Cache-Control', 'no-cache', 'x-mdvTicker-refresh', refresher], onComplete: _MDVTicker_onAjaxComplete });

 	return true;
 };
 
 MDVTicker.prototype._hasData = function () {
 };

  MDVTicker.prototype._hasNoData = function () {
 };

 MDVTicker.prototype._processData = function (response) {
 	if (response.status != 200) {
 		this._updateText(this._defaultText);
 		return false;
 	}

 	var addInfos = null;
	if (response.responseXML) {
		var addInfos = response.responseXML.getElementsByTagName('itdAdditionalTravelInformation');
	 	var newText = '';
	}

 	if (addInfos && addInfos.length <= 0 && this._defaultText != null) {
 		this._updateText(this._defaultText);
 		return false;
 	} else {
		if (response.responseText.length <= 0) {
			this._hasNoData();
			return false;
		}

		this._hasData();
		// Get width
		this._width = this._getObjWidth(this._ticker);
		this._updateText(response.responseText);
		
		return true;
	}

 	newText = '';

 	for (var i=0; i < addInfos.length; i++) {
 		var infoLinkText = addInfos[i].getElementsByTagName('infoLinkText');

 		if (infoLinkText[0].nodeName != 'infoLinkText' && infoLinkText[0].nodeType != 3)
 			continue;

 		newText += infoLinkText[0].firstChild.data;

 		if (i != addInfos.length - 1) {
 			newText += ' ';
			newText += this._separator;
 			newText += ' ';
 		}
 	}

 	this._updateText(newText);

 	return true;
 };

 MDVTicker.prototype.execute = function () {
 	if (!this._ticker)
 		return false;

 	if (!this._isIE)
 		this._speed -= 1;

	this._ticker.style.position = 'relative';
	this._ticker.style.overflow = 'hidden';

	// Get width
	this._width = this._getObjWidth(this._ticker);

	this.setOffset(Math.floor(this._width * 0.1 + 0.5));

	// Keep a reference in the document object for AJAX call back.
	if (!document.mdvTickers)
		document.mdvTickers = new Array();

	document.mdvTickers.push(this);

	// Create the inner scrolling element
	this._scroller = document.createElement('div');
	this._scroller.id = 'mdvTicker_' + this._name;
	this._scroller.style.position = 'absolute';
	this._scroller.style.left = (this._width + this._offset + 8) + 'px';
	this._scroller.style.top = '4px';
	this._scroller._doPause = false;
	this._ticker.appendChild(this._scroller);

	this._ticker.onmouseout = this.onmouseout;
	this._ticker.onmouseover = this.onmouseover;

	// Check whether we have a location where we can fetch the ticker data, if not the default will remain.
	this._updateData(false);

	return true;
 };

 MDVTicker.prototype.doScrolling = function () {
 	if (this._pauseIt && this._ticker._doPause) {
 		return false;
 	}
	
	// Get width
	this._width = this._getObjWidth(this._ticker);
	
	if(this._defaultText=='' && this._text == '<a href=\"undefined\" target=\"_blank\"></a>'){
		return false;
	}
	
 	if (parseInt(this._scroller.style.left)>(this._textWidth *(-1)+8)) {
 		this._scroller.style.left = (parseInt(this._scroller.style.left) - this._speed) + 'px';
 	} else {
 		if (this._doUpdate)
 			this._updateData(true);
		this._scroller.style.left = (this._width + this._offset + 8) + 'px';
 	}

 	return true;
 };

 MDVTicker.prototype._updateText = function (text) {
 	if (!this._scroller)
 		return false;
		
	// Get width
	this._width = this._getObjWidth(this._ticker);

 	this._text = text;

 	if (this._linkUrl && this._linkUrl != '') {
 		this._text = '<a href="'	+ this._linkUrl + '" target="_blank">' + this._text + '</a>';
 	}

 	this._scroller.innerHTML = this._text;
 	this._textWidth = this._getObjWidth(this._scroller);
 };

 /* Getter and Setter */
 MDVTicker.prototype.setDefaultText = function (text) {
 	this._defaultText = text;
 };

 MDVTicker.prototype.setSpeed = function (speed) {
 	if (speed > 0)
	 	this._speed = speed;
 };

 MDVTicker.prototype.setOffset = function (offset) {
 	this._offset = offset;
 };

 MDVTicker.prototype.setPauseIt = function (pauseIt) {
 	this._pauseIt = pauseIt;
 };

 MDVTicker.prototype.setUpdateIt = function (updateIt) {
 	this._updateIt = updateIt;
 };

 MDVTicker.prototype.setSeparator = function (separator) {
 	this._separator = separator;
 };

 MDVTicker.prototype.setLinkUrl = function (url) {
	this._linkUrl = url;
 };

 MDVTicker.prototype.update = function () {
 	if (this._updateIt)
	 	this._doUpdate = true;
	else
		this._doUpdate = false;
 }

 MDVTicker.prototype._getObjWidth = function (obj)  {
    var result = 0;
    if (obj.offsetWidth) {
        result = obj.offsetWidth;
    } else if (obj.clip && obj.clip.width) {
        result = obj.clip.width;
    } else if (obj.style && obj.style.pixelWidth) {
        result = obj.style.pixelWidth;
    }
    return parseInt(result);
 };

 MDVTicker.prototype._getObjHeight = function (obj)  {
    var result = 0;
    if (obj.offsetHeight) {
        result = obj.offsetHeight;
    } else if (obj.clip && obj.clip.height) {
        result = obj.clip.height;
    } else if (obj.style && obj.style.pixelHeight) {
        result = obj.style.pixelHeight;
    }
    return parseInt(result);
 };

 MDVTicker.prototype.onmouseover = function (e) {
		this._doPause = true;
 };

 MDVTicker.prototype.onmouseout = function (e) {
	this._doPause = false;
 };

 function _MDVTicker_onAjaxComplete(response) {
 	if (document.mdvTickers) {
	 	for (var i=0; i < document.mdvTickers.length; i++) {
	 		if (document.mdvTickers[i]._processData)
		 		document.mdvTickers[i]._processData(response);
	 	}
 	}
 }