﻿/* function of type writer */
function typist(displayItem, changeInterval, typeSpeed)
	{
		//-private variables
		var _items = new Array();
		var _dispalayItem;
		var _curentItem = -1;
		var _curentChar = 0;
		var _interval = 0;
		var _onTheFly = false;
		//-_User defined Data Type: titleItem
		function titleItem(itemTitle, itemLink)
		{
			this.title = itemTitle;
			this.link = itemLink;
		}
		//-Inotialize
		if (typeof(displayItem) == "string")
			_dispalayItem = document.getElementById(displayItem);
		else
			_dispalayItem = displayItem;
		//-private methods
		//-_type curent item
		function type()
		{
			if(_dispalayItem.innerHTML.length >= 2)//hide cursor
				_dispalayItem.innerHTML = _dispalayItem.innerHTML.substring(0, _dispalayItem.innerHTML.length - 1);
			_dispalayItem.innerHTML += _items[_curentItem].title.charAt(_curentChar);
			_curentChar++;
			if (_curentChar < _items[_curentItem].title.length)
			{
				window.setTimeout(showCursor, 20);
				window.setTimeout(type, typeSpeed);
			}else{
				_curentChar = 0;
				_onTheFly = false;
			}
		}
		//-_Show cursore at the end of typed string
		function showCursor()
		{
			_dispalayItem.innerHTML += "_";
		}
		////////////////////////////////////////////////
		//-public methods
		//-_register a title and it's related link
		this.registerItem = function(title, link)
		{
			var item = new titleItem(title, link);
			_items.push(item);
		}
		//-_set information of next item and start to type it.
		function _nextItem()
		{
			if (!_onTheFly)
			{
				_curentItem++;
				if (_curentItem == _items.length)
					_curentItem = 0;
				if(_dispalayItem.href)_dispalayItem.href = _items[_curentItem].link;
				_dispalayItem.innerHTML = "";
				_onTheFly = true;
				type();
			}
		}
		this.nextItem = _nextItem;
		//-_set information of previos item and start to type it.
		function _prevItem()
		{
			if (!_onTheFly)
			{
				_curentItem--;
				if (_curentItem < 0)
					_curentItem = _items.length - 1;
				if(_dispalayItem.href)_dispalayItem.href = _items[_curentItem].link;
				_dispalayItem.innerHTML = "";
				_onTheFly = true;
				type();
			}
		}
		this.prevItem = _prevItem;
		//-_Start to type register items.
		this.start = function()
		{
			if (_items.length > 0)
			{
				_nextItem();
				_interval = window.setInterval(_nextItem, changeInterval);
			}
		}
		//-_Stop typing
		this.stop = function()
		{
			if (_interval != 0)
				window.clearInterval(_interval);
		}
		//-_Returns If the typing is onthefly!
		this.isOnTheFly = function()
		{
			return _onTheFly;
		}
	}

