var newsLinks=$A();
var lightviewLinkString=new String("<a href='#{url}' title='#{title} :: :: topclose:true,autosize:true,width:1000,height:600' class='#{classes}lightview' target='_blank' rel='iframe'>#{linktext}</a>");

var NewsPreview=Class.create({
	initialize: function(div) {
		this._div=div;
		this.dayText=new String(this._div.down('.date').innerHTML).strip();
		this.sourceName=new String(this._div.down('.source_name').innerHTML).strip();
		this.url=new String(this._div.down('.url').innerHTML).strip();
		this.headlineText=new String(this._div.down('.headline_text').innerHTML).strip();
		this.summaryText=new String(this._div.down('.summary_text').innerHTML).strip();
	},
	htmlString: function() {
		return "<li class='newsBlock'><div class='newsDate'>"+this.dayText+"</div><div class='newsSource'>"+this.sourceName+"</div>"+ lightviewLinkString.interpolate({ url: this.url, title: this.headlineText, linktext: this.headlineText, classes: "newsHeadlineLink "}) + "<div class='newsBlurb'><p>"+this.summaryText+" "+ lightviewLinkString.interpolate({ url: this.url, title:this.headlineText, linktext: "[more...]", classes: "more " }) + "</p></div></li>";
	}
});

// load the slideshow

var slides=new $A();
var numSlides=0;
var sIndex=0;
var transitionTime=.35;
var slideWait=6;
var topSlide, bottomSlide;
var topText, bottomText;

var Slide=Class.create({
	initialize: function(li) {
		this.imSrc=li.down('img').src;
		if(typeof(this.imSrc)=='undefined') return null;
		this.im=new Element('img');
		this.im.src=this.imSrc;
		this.caption=li.down('span').innerHTML.strip();
		this.captionClassName=li.down('span').className;
		this.textSize=40;
	}
});
function startShow() {
	topText=$('textTop');
	bottomText=$('textBot');
	topSlide=$$('#ss_wrap img#top_slide').first();
	bottomSlide=$$('#ss_wrap img#bot_slide').first();
	advanceShow();
}
function advanceShow() {
	sIndex++;
	if(sIndex>=numSlides) sIndex=0;
	switchIms();
	topText.className="caption "+slides[sIndex].captionClassName;
	topText.innerHTML=slides[sIndex].caption;
	topText.appear({
		duration: transitionTime
	})
	bottomText.fade({
		duration: transitionTime
	})
	topSlide.onload=function() {
		topSlide.appear({
			duration: transitionTime
		});
		advanceShow.delay(slideWait)
	}
	topSlide.src=slides[sIndex].imSrc;
}
function switchIms() {
	bottomText.className=topText.className;
	bottomText.innerHTML=topText.innerHTML;
	bottomText.show();
	bottomSlide.src=topSlide.src;
	topText.hide();
	topSlide.hide();
}

var loadMpNews=function() {
	new Ajax.Updater('news_preview_data', '../tools/news/home-news.xhtml?b', {method:'get', onComplete: function(evt) {
		var _tUl=$$('ul.homeNewsItems').first();
		if(evt.status!=200) {
			_tUl.update("<li class='error'>There was a problem loading the latest headlines. Please visit our <a href='PageServer?pagename=press_stay_all'>Press &amp; Media page</a> for full updates.</li>")
		} else {
			newsLinks=$$('#news_preview_data div.news_article').map(function(nDiv) { return new NewsPreview(nDiv) });
			var _nlText=newsLinks.inject('', function(str, nl){ return str+nl.htmlString.call(nl); });
			_tUl.fade({
				duration: 0.12,
				afterFinish: function() {
					_tUl.update(_nlText);
					_tUl.select('li').each(function(nli){
						nli.observe('mouseover', function(evt) {
							nli.addClassName('hovered');
						}.bind(this))
						nli.observe('mouseout', function(evt) {
							nli.removeClassName('hovered');
						}.bind(this))
					})
					_tUl.appear({ duration: 0.12})
				}.bind(this)
			});
		}
	}});
}
var loadSlideshow=function() {
	if($('ss_data')) {
		new Ajax.Updater('ss_data', '../tools/xhtml/homepage_slideshows/partners-meeting-2009.xhtml', {
			method: 'get',
			onComplete: function() {
				slides=$$('#load_slides li.slide').map(function(sld){
					return new Slide(sld);
				});
				numSlides=slides.length;
				startShow();
			}
		});
	}
}
