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",
            nextLeftArrow: "newsBoxLeft",
            nextRightArrow: "newsBoxRight",
            counterElement : "newsBoxCounter",
            newsBoxContentDiv : "newsBoxContentDiv",
            updateInterval : 12,
            thumbnailsEnabled : true,
            thumbnailWidth : 88,
            thumbnailHeight : 46,
            fps : 18,
            hideIfEmpty : false
        };
        Object.extend(this.options, options);
        if(!$(this.options.items)) {
            if($(this.options.element)){
                $(this.options.element).hide();
            }
            if(this.options.hideIfEmpty == true) {
                $(this.options.newsBoxContentDiv).hide();
            }
			
		
        } else {
		
            this.controller = $(this.options.newsItemControllers);
            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();
            var element = $(this.options.nextLeftArrow);
            if (element) {
                element.observe('click', this.displayPrevItem.bind(this));
            }
            element = $(this.options.nextRightArrow);
            if (element) {
                element.observe('click', this.displayNextItem.bind(this));
            }

                        
        }
    },
    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' : this.options.thumbnailWidth,
                'height' : this.options.thumbnailHeight
                }));
            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>';
            }
            Event.stopObserving(e, "click");
			
            if (item.categoryOrLinkRaw && item.categoryOrLinkRaw != null && item.categoryOrLinkRaw.href && item.categoryOrLinkRaw.href != null) {
                e.style.cursor='pointer';
                Event.observe(e, "click", function() {
                    document.location.href=this;
                }.bind(item.categoryOrLinkRaw.href));
            } else {
                e.style.cursor='normal';
                e.onclick=null;
            }
			
			
        } else {
            var h = e.down(this.options.itemHeader);
            if(h) {
            	h.replace(item.header);
            }
			
            Event.stopObserving(clickArea, "click");
            if (item.categoryOrLink != null) {
                Event.observe(clickArea, "click", function(event) {
                    if(item.categoryOrLink.target && item.categoryOrLink.target=="_blank") {
                        window.open(item.categoryOrLink.href);
                    } else {
                        document.location.href=item.categoryOrLink.href;
                    }
                });
            }
        }
        var counterElem = $(this.options.counterElement);
        if (counterElem && counterElem != null && counterElem != 'undefined') {
            counterElem.innerHTML = '' + (pos + 1) + '/' + this.newsItems.length;
        }
        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,
    linkTarget : null
});

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

