if ( window.cbbc == undefined )
	cbbc = { };

(function(){
	var $ = glow.dom.get;
	cbbc.control = {
		tick: undefined,
		time: 0,
		isFirstTime: true,
		isPlaying: false,
		start: 		function(){
			if(!cbbc.control.tick || cbbc.control.tick == undefined){			
				var openListener = glow.events.addListener(
					cbbc.widgets.Mask,
					'open',
					cbbc.control.events
				);
				var closeListener = glow.events.addListener(
					cbbc.widgets.Mask,
					'close',
					cbbc.control.events
				);
				cbbc.control.isPlaying = true;
				cbbc.control.tick = window.setInterval(function() { cbbc.control.loop() }, 500);
			}
		},
		loop:		function(){
			this.time += 500;
			if(this.time % cbbc.newsticker.interval == cbbc.newsticker.animator.tweenTime){
				if(this.isFirstTime){
					this.isFirstTime = false;
				}else {
					if(!cbbc.newsticker.isHeld){
						cbbc.newsticker.next();
						cbbc.newsticker.animator.animate();
						//cbbc.newsticker.anim.start();
					}
				}
			}
			if(this.time % cbbc.newsticker.interval == 0){
				if(!cbbc.newsticker.isHeld){
					cbbc.newsticker.next();
					cbbc.newsticker.animator.animate();
					//cbbc.newsticker.anim.start();
				}
			}
			for(var i = 0; i<cbbc.infoticker.instances.length; i++){
				var instance = cbbc.infoticker.instances[i];
				if(this.time % instance.interval == 0){
					if(!instance.isHeld){
						cbbc.infoticker.next(instance);
					}
				}
			}
		},
		pause:		function(){
			if(cbbc.control.isPlaying){
				window.clearInterval(cbbc.control.tick);
				cbbc.control.isPlaying = false;
			}
		},
		resume:		function(){
			if(!cbbc.control.isPlaying){
				window.clearInterval(cbbc.control.tick);
				cbbc.control.tick = window.setInterval(function() { cbbc.control.loop() },100);
				cbbc.control.isPlaying = true;
			}
		},
		events: function(e){
			if(e.type == "open"){
				cbbc.control.pause();
			}
			if(e.type == "close"){
				cbbc.control.resume();
			}
		}
	},
	cbbc.newsticker = {
		listId: 		'#cbbc-news-ticker-list',
		slideId: 		'nr-slide-',
		iteration:		0,
		tickerlist:		null,
		interval: 5000,
		isHeld: false,
		anim: null,
		init: 			function(info, interval, listId){
			if (interval != undefined) this.interval = interval;
			if (listId != undefined) cbbc.newsticker.listId = listId;
			if( typeof(info) == "string" ){
				this.getFeed(info);
			} else if ( info == null||info == undefined ) {
				this.startTicker();
			} else {
				return false;
			}
		},
		getFeed:		function(xml){
			var isXML = false;
			if(xml.match(/\.xml/g) != null){
				isXML = true;
			}
			var getRef = glow.net.get(xml, {
				onLoad: function(response) {
					cbbc.newsticker.writeFeedToDom(isXML, response);
				},
				onError: function(response) {
					return false;
				}
			});		
		},
		writeFeedToDom: 	function(isXML, response){
			var rspTxt;
			if(isXML){
				rspTxt = glow.dom.get( response.xml() );
			} else {
				var txt_arr = response.text().split("<news>");
				var txt = "<news>"+txt_arr[1];
				try {
					var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(txt);
					rspTxt = glow.dom.get(xmlDoc);
				} catch(e) {
					try {
						rspTxt = glow.dom.create( txt );		
					}
					catch(e) {return}
				}			
			}
			var items = rspTxt.get("item");
			
			if(items == undefined || items.length == 0){
				cbbc.newsticker.isHeld = true;
				return;
			} else {
				
				$(cbbc.newsticker.listId).empty();
							
				items.each(function(i){
					this == items[i];
					var item = $(this);
					glow.dom.create(
						"<li><p class='holder'><a class='news' href='"+item.attr("url")+"'>"+item.attr("description")+"</a></p></li>"
					).appendTo(cbbc.newsticker.listId).css("display", "none");
				});
				this.startTicker();
			}
		},
		startTicker: 		function(){
			this.tickerlist = $(this.listId+" li");
			var tl = this.tickerlist;
			tl.each(function(i){
				this == tl[i];
				var item = $(this);
				glow.events.addListener(
					item, 
					'mouseover',
					cbbc.newsticker.events
				);
				glow.events.addListener(
					item,
					'mouseout',
					cbbc.newsticker.events
				);
			});
			cbbc.newsticker.next();
			cbbc.newsticker.animator.animate();
			cbbc.control.start();
		},
		next: 		function(){
			var itemlist = this.tickerlist;
			if(itemlist){
				itemlist.each(function(i){
					this == itemlist[i];
					var item = $(this);
					if(item.hasClass("cbbc-news-current")){
						item.removeClass("cbbc-news-current");
					}
					if(i==cbbc.newsticker.iteration){
						item.addClass("cbbc-news-current");
						var href=item.get("a").attr("href");
						$("a#readmore").attr("href", href);
						$("a#readmorebutt").attr("href", href);
					}
				})
				if(this.iteration < itemlist.length-1){
					this.iteration++;
				} else {
					this.iteration = 0;
				}
			}
		},
		events: function(e){
			if(e.type == "mouseover"){
				cbbc.newsticker.anim.stop();
				cbbc.newsticker.isHeld = true;
			}
			if(e.type == "mouseout"){
				cbbc.newsticker.anim.resume();
				cbbc.newsticker.isHeld = false;
			}
		},
		animator: {
			tweenTime: undefined,
			animate: 	function(){
				if(this.tweenTime == undefined){
					this.tweenTime = cbbc.newsticker.interval;
				}
				$(".news").css("left", "375px");
				var left = "-400px";
				var right = "375px";
				cbbc.newsticker.anim = glow.anim.css("li.cbbc-news-current .news", this.tweenTime/1000, {
					"left": {from: right, to: left }
				}).start();
			}
		}
	},
	cbbc.infoticker = {
		instances: [],
		init:			function(list, interval){
			var _interval = interval;
			//create array of list items
			var listNodes = $("ul#cbbc-" + list + "-list li");
			this.instances.push({ interval: _interval, nodes: listNodes, iteration: 0, isHeld: false });
			this.startTicker(this.instances[this.instances.length-1]);
		},
		startTicker:	function(instance){
			var tl = instance.nodes;
			tl.each(function(i){
				this == tl[i];
				var item = $(this);
				glow.events.addListener(
					item, 
					'mouseover',
					cbbc.infoticker.events
				);
				glow.events.addListener(
					item,
					'mouseout',
					cbbc.infoticker.events
				);
			});
			cbbc.infoticker.next(instance);
			cbbc.control.start();
		},
		next:			function(instance){
			var list = instance.nodes;
			var iteration = instance.iteration;
			//postion all but one list item out of sight
			list.each(function(i){
				if(i!=iteration){ 
					list [i].style.position =  "absolute";
					list [i].style.left =  "-2500px";
					list [i].style.width =  "1px";
				}else{ 
					list [i].style.position =  "";
					list [i].style.left =  "";
					list [i].style.width =  "";
				}
			});
			if(instance.iteration < list.length-1){ instance.iteration++; }
			else{ instance.iteration = 0; }
		},
		events: function(e){
			if(e.type == "mouseover"){
				for(var i = 0; i<cbbc.infoticker.instances.length;i++){
					if(cbbc.infoticker.instances[i].nodes.parent().attr("id") == $(e.attachedTo).parent().attr("id")){
						cbbc.infoticker.instances[i].isHeld = true;
					}
				}	
			}
			if(e.type == "mouseout"){
				for(var i = 0; i<cbbc.infoticker.instances.length;i++){
					if(cbbc.infoticker.instances[i].nodes.parent().attr("id") == $(e.attachedTo).parent().attr("id")){
						cbbc.infoticker.instances[i].isHeld = false;
					}
				}
			}
		}
	}
})();