/**
 * Build effect for headlines news block. 
 * This effect also exist in /data/js/ProtoTabs/SneakyNewsTabs.js, so may need to refactor later with that file.
 */
var Headlines = Class.create({
	imageBox: null,
	newsPlaceHolderImage: null,
	initialize: function(element, options) {
		this.options = Object.extend(
			{newsPlaceHolderImage : "/data/js/prototabs/news_image_placeholder.jpg"},
			options || {}
		);
	
		this.imageBox = $$("#" + element + " .news_image_box");
		if(this.imageBox.length == 0){
			alert("Headlines javascript library: We need a div with class = 'news_image_box'. Please add one.");
			return;
		}
		this.imageBox = this.imageBox.pop();

		this.image = $$("#" + element + " .news_image");
		if(this.image.length == 0){
			alert("Headlines javascript library: We need an image with class = 'news_image'. Please add one.");
			return;
		}
		this.image = this.image.pop();
		
		if(Object.isUndefined(this.image.src)){
			this.image.src = this.options.newsPlaceHolderImage;
		}
		
		this.overlay = $$("#" + element + " .overlay").first();
		if(Object.isUndefined(this.overlay)){
			alert("Headlines javascript library: We need an element for with class = 'overlay'. Please add one.");
			return;
		}
		this.overlay.hide();
		
		this.newsSummary = $$("#" + element + " .news_summary").first();
		if(Object.isUndefined(this.newsSummary)){
			alert("Headlines javascript library: We need an element for with class = 'news_summary'. Please add one.");
			return;
		}

		
		this.newsTitle = $$("#" + element + " .news_title").first();
		if(Object.isUndefined(this.newsTitle)){
			alert("Headlines javascript library: We need an element for with class = 'news_title'. Please add one.");
			return;
		}
		
		that = this;
		var newsLinks = $$("#" + element + " a");
		newsLinks.each(function(newsLink){
			newsLink.observe("mouseover",that.changeImageAndSummary.bind(that));
		});
		
		//add style for image box
		this.imageBox.setStyle({position:"relative"});
		
		this.headlinesBoxContainer = $(element);
	},
	
	changeImageAndSummary: function(event){
		this.overlay.show();
		
		var element = Event.element(event);
		//change to image of selected news
		this.image.src = element.next("input",0).value;
		if(Object.isUndefined(this.image.src) || (this.image.src == "")){
			this.image.src = this.options.newsPlaceHolderImage;
		}
		
		//change to summary of selected news
		this.newsSummary.update(element.next("input",1).value);
		
		//change to title of selected news
		if(!Object.isUndefined(element.innerHTML)){
			var selectedNewsTitle = element.innerHTML; 
			this.newsTitle.update(selectedNewsTitle);
		}
		
		//remove selected class of other news.
		var headlinesBoxContainerId = this.headlinesBoxContainer.readAttribute("id");
		var newsLinkDivs = $$("#" + headlinesBoxContainerId + " div");
		newsLinkDivs.each(function(newsLinkDiv){
			newsLinkDiv.removeClassName("selected");
		});
		//change style of outer div of news link
		element.up(0).addClassName("selected");		
	}
});
