/**
 * 'NewsTicker' Javascript Object
 * 
 * @author Avery Brooks
 * @requires jQuery
 * @copyright 2009/04
 * 
 */

ticker = function(options) {

		/* must has jQuery */
		if (!$) return false;

		this.containerID = "";

		this.timer = -1;
		this.container = -1;
		this.lists = -1;

		this.increment = 1;
		this.interval = 100;

		// how many copies to make
		this.clones = 1;

		// stop on mouseover
		this.mousehold = false;

		this.init = function(options) {

			if (!options.container) return false;

			this.containerID = options.container.replace("#","");

			// set up reference holder if not present
			if (!parent.___tickerstorage)
				parent.___tickerstorage = {};

			___tickerstorage[this.containerID] = this;

			if (options.clones)
				this.clones = options.clones;

			if (!options.ticker_css) {
				options.ticker_css = {"position":"relative","width":"20000px","min-height":"20px","overflow":"hidden"};
			}
			if (!options.lists_css) {
				options.lists_css = {"display":"inline","position":"absolute","left":0,"list-style-type":"none"};
			}
			if (!options.items_css) {
				options.items_css = {"display":"inline","white-space":"nowrap","-moz-user-select":"none","-khtml-user-select":"none","user-select":"none"};
			}
			if (options.mousehold) this.mousehold = true;

			// settings
			this.lastleft = 0;
			this.increment = options.increment;
			this.interval = options.interval;

			// get objects
			this.container = $(options.container);
			this.lists = this.container.children("UL");

			// apply css
			this._css(options.ticker_css, this.container);
			this._css(options.lists_css, this.lists);
			this._css(options.items_css, this.lists.children("LI"));

			// two safties to prevent runaway javascript here
			this.maxcopies = 20;
			var _list = this.container.children("UL:first");

			/*
				var tries = 1;
				if (!options.nofill) {
					while(!this._enoughwidth() && this.container.length < this.maxcopies && tries < this.maxcopies) {
						_list
							.clone()
							.appendTo(this.container)
							.css("left",_list.width() * tries);
						tries ++ ;
					}
				}
			*/

			var tries = 1;
			while(tries<this.clones+1) {
				_list.clone().appendTo(this.container).css("left",_list.width() * tries);
				tries ++;
			}

			// start the timer
			this._run();

			// hold on mouseover ?
			if (this.mousehold) {
				this.container.bind("mouseover",function(e){
					var _ref = $(this).attr("id");
					___tickerstorage[_ref]._hold();
				});
				this.container.bind("mouseout",function(e){
					var _ref = $(this).attr("id");
					___tickerstorage[_ref]._run();
				});
			}

			return this;
		};

		this._childrenwiderthanparent = function() {
			var wid = 0;
			var num = 0;
			this.container.children("UL").each(function(){num+=1;wid+=$(this).width();});
			return (wid > (this.container.width()));
		}

		this._enoughwidth = function() {
			var wid = 0;
			var num = 0;
			var sibs = this.container.length;
			this.container.children("UL").each(function(){
				num+=1;
				if (num < sibs) wid+=$(this).width();
			});
			return (wid > (this.container.width()));
		}

		this._css = function(css,obj) {
			for(i in css) $(obj).css(i,css[i]);
		};

		this._timehandler = function() {
			var increment = this.increment;
			this.container.children("UL").each(function(){
				var newleft = parseInt($(this).css("left")) - increment;
				$(this).css("left",newleft); // this.lastleft
				if ($(this).width() + newleft < 0) {
					var refObj = $(this).parent().children("UL:last");
					var resetleft = (refObj.width() + parseInt(refObj.css("left")));
					$(this).remove().appendTo(refObj.parent()).css("left",resetleft);
				}
			});
		};

		this._run = function() {
			this.timer = setInterval(
				"___tickerstorage['"+this.containerID+"']._timehandler()", this.interval
			);
		};

		this._hold = function() {
			try {clearInterval(this.timer);} catch(e) {}
		};

		// "constructor"
		return this.init(options);

}