var NewsRotator = Class.create({
	options : null,
	newsItems : new Array(),
	thumbnailElement : null,
	newsItemsContainer : null,
	activeThumbnail : null,
	activeItem : null,
	activePos : -1,
	updator : null,
	running : false,
	activeTween : null,
	preloadedImages : new Array(),
	loadedCount : 0,
	controller : null,
	initialize : function(options) {
		this.options = {
				element : "newsBox",
				items : "newsItems",
				itemHeader : ".NewsItemHeaderText",
				itemHeaderText : ".NewsItemHeaderTextText",
				itemText : ".NewsItemText",
				itemBackground : ".NewsItemBackground",
				itemThumbnail : ".NewsItemThumbnail",
				thumbnailElement : "newsBoxThumbnails",
				textElement : "newsBoxText",
				headerElement : "newsBoxHeader",
				headerTextElement : "newsBoxHeaderText",
				thumbnailClass : "NewsBoxThumbnail",
			    thumbnailActiveClass : "NewsBoxThumbnailActive",
			    newsItemControllers : "newsItemControllers",
			    itemCategoryOrLink : ".NewsCategoryOrLink",
			    itemCategoryOrLinkRaw : ".NewsCategoryOrLinkRaw",
			    newsBoxBackgroundClickArea : "newsBoxBackgroundClickArea",
				updateInterval : 12,
				thumbnailsEnabled : true,
				fps : 18
		};
		if(!$(this.options.items)) {
			if($(this.options.element)){
				$(this.options.element).hide();
			}
		
		} else {
		
			this.controller = $(this.options.newsItemControllers);
			Object.extend(this.options, options);
		
			this.newsItemsContainer = $(this.options.items);
			
			this.extractNewsItems();
			if(this.options.thumbnailsEnabled) {
				this.thumbnailElement = $(this.options.thumbnailElement);
				this.createThumbnails();
			}
			//preloading caused images not to show up in IE7, so don't preload
			this.onImagesLoaded();

		}
	},
	onImagesLoaded : function() {
		
		if(this.newsItems.length>0) {
			this.setActiveItem(0,false);
		}
		this.start();
	},
	preloadImages : function() {
		this.preloadedImages.each(function(url){
			var img = new Image();
			img.onload = this.addLoadedCount.bind(this);
			//add count even if image doesn't exist 
			//to be sure the animation starts
			img.onerror = this.addLoadedCount.bind(this); 
			img.src = url;
		}.bind(this));
	},
	addLoadedCount : function() {
		
		this.loadedCount++;
		if(this.loadedCount == this.preloadedImages.length) {
			this.onImagesLoaded();
		}
	},
	extractNewsItems : function() {
		
		var children = this.newsItemsContainer.childElements();
		
		for(var i = 0; i<children.length; i++){
			var item = children[i]; <!-- changed back to i -->
			var n = new NewsItem();
			n.header = item.down(this.options.itemHeader);
			n.headerText = item.down(this.options.itemHeaderText);
			n.text = item.down(this.options.itemText);
			n.thumbnailUrl = item.down(this.options.itemThumbnail).src;
			n.thumbnailStyle = "background-image:url("+n.thumbnailUrl+");";
			n.backgroundUrl = item.down(this.options.itemBackground).src;
			if(i == 0) { //only preload first "big image"
				this.preloadedImages.push(n.backgroundUrl);
			}
			this.preloadedImages.push(n.thumbnailUrl);
			n.thumbnailElementId =  "newsThumbnail" + i;
			n.pos = i;
			var itemCategoryLink = item.down(this.options.itemCategoryOrLink);
			if (itemCategoryLink && itemCategoryLink != null) {
				n.categoryOrLink = itemCategoryLink;
			}
			var itemCategoryLinkRaw = item.down(this.options.itemCategoryOrLinkRaw);
			if (itemCategoryLinkRaw && itemCategoryLinkRaw != null) {
				n.categoryOrLinkRaw = itemCategoryLinkRaw;
			}
			
			this.newsItems.push(n);
		}
	},
	createThumbnails : function() {
		this.newsItems.each(function(item){
									 
			var img = new Element("img",{"src":item.thumbnailUrl,"border":"0"});
			
			var thumbDiv = new Element(
					"div",{ 'id': item.thumbnailElementId ,'class' : this.options.thumbnailClass }
				);
			thumbDiv.insert(new Element('img',{'src': item.thumbnailUrl, 'width' : 60, 'height' : 48}));
			this.thumbnailElement.insert(thumbDiv);
			thumbDiv.observe("click",function(event) { this.setActiveItemManually(item.pos,true);}.bind(this));
		}.bind(this)); 
					
	},
	setActiveItemManually : function(pos,fade) {
		this.stop();
		this.setActiveItem(pos,fade);
		
	},
	setActiveItem : function(pos,fade) {
		if(pos==this.activePos) {
			return;
		}
		if(this.activeThumbnail!=null) {
			this.activeThumbnail.removeClassName(this.options.thumbnailActiveClass);
		}

		var item = this.newsItems[pos];
		var e = $(this.options.headerElement);
		var text = $(this.options.textElement);
		var headerText = $(this.options.headerTextElement);
		var clickArea = $(this.options.newsBoxBackgroundClickArea);
		
		
		if(this.activeItem!=null) {
			//IE fix. IE removes the element entirely, so we have to save it.
			this.activeItem.header = e.down(this.options.itemHeader);
			if (text && text != null) {
				this.activeItem.text = text.innerHTML;
			}
		}

		e.style.backgroundImage = "url("+item.backgroundUrl+")";
		
		if (this.options.basicLobby) {
			if (item.headerText && item.headerText != null) {
				headerText.innerHTML = '<span>' + item.headerText.innerHTML + '</span>';
			} else {
				headerText.innerHTML = '<span></span>';
			}
			if (item.categoryOrLinkRaw && item.categoryOrLinkRaw != null && item.categoryOrLinkRaw.href && item.categoryOrLinkRaw.href != null) {
				e.style.cursor='pointer';
				e.onclick=function(event) {
					document.location.href=item.categoryOrLinkRaw.href;
				};
			} else {
				e.style.cursor='normal';
				e.onclick=null;
			}
			
			
		} else {
			e.down(this.options.itemHeader).replace(item.header);
			
			Event.stopObserving(clickArea, "click");
			if (item.categoryOrLink != null) {
				Event.observe(clickArea, "click", function(event) {
					Event.simulate(item.categoryOrLink, "click");
				});
			}
		}		
		if(fade) {
			Effect.Appear(e,{duration : 1.0, from : 0.1 , to : 1 });
		}
		if (text && text != null) {
			text.update(item.text);
		}
		if(this.options.thumbnailsEnabled) {
			$(item.thumbnailElementId).addClassName(this.options.thumbnailActiveClass);
			this.activeThumbnail = $(item.thumbnailElementId);
		}
		this.activePos = pos;
		this.activeItem = item;

	},
	start : function() {
		if(this.running == false) {
			
			this.updator = new PeriodicalExecuter(
					this.displayNextItem.bind(this),
					this.options.updateInterval
					);
			this.running = true;
		}
	},
	stop : function() {
		
		if(this.running == true) {
			this.updator.stop();
			this.updator = null;
			this.running = false;
		}
	},
	
	displayNextItem : function() {
		if (this.newsItems && this.newsItems.length > 0) {
			this.setActiveItem((this.activePos+1)%this.newsItems.length,true);
		}
	},
	displayPrevItem : function() {
		if (this.newsItems && this.newsItems.length > 0) {
			if (this.activePos == 0) {
				this.setActiveItem((this.newsItems.length - 1),true);
			} else {
				this.setActiveItem((this.activePos-1),true);
			}
		}
	}	
	

});
var NewsItem = Class.create({
	header : null,
	headerText: null,
	category : null,
	text : null,
	thumbnailUrl : null,
	backgroundUrl : null,
	selected : false,
	thumbnailElementId : null,
	categoryOrLink : null,
	categoryOrLinkRaw : null,
	pos : -1
});

var bigImageClicked = true;
function onRotatorImageClicked(targetUrl) {
	if (bigImageClicked) {
		
	}
}
function onOffBackgroundAreaClicked() {
	bigImageClicked = false;
}
