
 
 /*
 
     Table of Contents
     -----------------
     Configuration
     Global Variables
     
     Spotlight Class Declaration
     - initialize()
	 - start()
	 - changeSpotlight()
	 - toggleSpotlight()
	 - updateNav()
	 - end()
	 
	 Miscellaneous Functions
	 - initSpotlight()
	
	 Function Calls
	 - addLoadEvent(initSpotlight)
	 
*/

// -----------------------------------------------------------------------------------

//	Configuration

// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------

//	Global Variables

var spotlightArray = new Array;
var eventArray = new Array;
var newsArray = new Array;
var newsLinkArray = new Array;
var newsLinkRef = "#";
var eventsLinkRef = "#";

// -----------------------------------------------------------------------------------

/*
    SpotlightItem Object Declaration
        thumb
        title
        subtitle
        body
        link
        
        initialize()
        render()
*/

var Spotlight = Class.create();

Spotlight.prototype = {
	
	initialize: function(type, thumb, title, subtitle, body, url) 
	{
		this.thumb = thumb;
		
		this.params = '';
		/*if(type == 'event'){
			this.params = 'height=550,width=530,';
		}*/
		this.type = type;
		this.title = title;
		this.subtitle = subtitle;
		this.body = body;
		this.url = url;
	},
	
	open: function(event)
	{
		window.open(this.url, 'ggurugSpotlight', 'height=550,width=530');
		Event.stop(event);
	},
	
	render: function() 
	{
		objItem = Builder.node
					('div', 
						{
							className: 'spotlightItem'
						}
					);
		
		if(this.thumb != ""){
			objThumb = Builder.node
						('div',
							{
								className: 'spotlightThumb'
							},
							[
								Builder.node
									('a',
										{
											href: this.url//,
											//onclick: "window.open(this.href, 'ggurugSpotlight', '"+this.params+"resizable=yes,scrollbars=yes');return false;"
										},
										[
											Builder.node
												('img',
													{
														className: 'spotlightImg',
														src: this.thumb
													}
												)
										]
									)
							]
						);
			objItem.appendChild(objThumb);
			if(this.type == 'event')
			{
				Event.observe(objThumb.firstChild, 'click', this.open.bind(this), false);
			}
		}
		
		objTitle = Builder.node
					('span',
						{
							className: 'spotlightTitle'
						},
						[
							Builder.node
								('a',
									{
										href: this.url//,
										//onclick: "window.open(this.href, 'ggurugSpotlight', '"+this.params+"resizable=yes,scrollbars=yes');return false;"
									}
								)
						]
					);
		Element.update(objTitle.firstChild, this.title);
		objItem.appendChild(objTitle);
		if(this.type == 'event')
		{
			Event.observe(objTitle.firstChild, 'click', this.open.bind(this), false);
		}
		
		if(this.subtitle != "")
		{
			objSubtitle = Builder.node
							('span',
								{
									className: 'spotlightSubtitle'
								}
							);
			Element.update(objSubtitle, this.subtitle);
			objItem.appendChild(objSubtitle);
		}
		
		objBody = Builder.node
					('div',
						{
							className: 'spotlightText'
						}
					);
		Element.update(objBody, this.body + '&nbsp;');
		
		objMoreLink = Builder.node
						('span',
							{
								className: 'spotlightMore'
							},
							[
								Builder.node
									('a',
										{
											href: this.url//,
											//onclick: "window.open(this.href, 'ggurugSpotlight', '"+this.params+"resizable=yes,scrollbars=yes');return false;"
										}
									)
							]
						);
		Element.update(objMoreLink.firstChild, "More&nbsp;>>");
		objBody.appendChild(objMoreLink);
		if(this.type == 'event')
		{
			Event.observe(objMoreLink.firstChild, 'click', this.open.bind(this), false);
		}
		
		objItem.appendChild(objBody);
		
		return objItem;
	}
}

var ggurugNews = Class.create();

ggurugNews.prototype = {
	
	initialize: function(headline, summary, thumb, url) 
	{
		this.headline = headline;
		this.summary = summary;
		this.thumb = thumb;
		this.url = url;
	},
	
	render: function()
	{
		objItem = Builder.node
					('div',
						{
							className: 'newsStory'
						}
					);
		
		if(this.thumb != "")
		{
			objThumb = Builder.node
						('div',
							{
								className: 'newsThumb'
							},
							[
								Builder.node
									('a',
										{
											className: 'newsImgLink',
											href: this.url//,
											// onclick: 'window.open(this.href);return false;'
										},
										[
											Builder.node
												('img',
													{
														className: 'newsImg',
														src: this.thumb
													}
												)
										]
									)
							]
						);
			objItem.appendChild(objThumb);
		}
		
		objHeadline = Builder.node
						('span',
							{
								className: 'newsHeadline'
							},
							[
								Builder.node
									('a',
										{
											className: 'newsLink',
											href: this.url//,
											// onclick: 'window.open(this.href);return false;'
										}
									)
							]
						);
		Element.update(objHeadline.firstChild, '<span>' + this.headline + '</span>');

		objItem.appendChild(objHeadline);
		
		objAbstract = Builder.node
						('div',
							{
								className: 'newsAbstract'
							}
						);
		Element.update(objAbstract, this.summary + ' ');
		
		objMoreLink = Builder.node
						('span',
							{
								className: 'newsMore'
							},
							[
								Builder.node
									('a',
										{
											href: this.url//,
											//onclick: "window.open(this.href, 'ggurugSpotlight', '"+this.params+"resizable=yes,scrollbars=yes');return false;"
										}
									)
							]
						);
		Element.update(objMoreLink.firstChild, "More&nbsp;>>");
		objAbstract.appendChild(objMoreLink);
		
		objItem.appendChild(objAbstract);
		
		return objItem;
	}
}

var ggurugNewsLink = Class.create();

ggurugNewsLink.prototype = {
	
	initialize: function(headline,url) {
		this.headline = headline;
		this.url = url;
	},
	
	render: function() {
		objItem = Builder.node
					('div',
						{
							className: 'newsItem'
						}
					);
		
		objTitle = Builder.node
						('span',
							{
								className: 'newsLinkHeadline'
							},
							[
								Builder.node
									('a',
										{
											href: this.url//,
											//onclick: 'window.open(this.href);return false;'
										}
									)
							]
						);
		Element.update(objTitle.firstChild, this.headline);
	
		objItem.appendChild(objTitle);
		
		return objItem;
	}
}

var ggurugEvent = Class.create();

ggurugEvent.prototype = {
	
	initialize: function(title,subtitle,date,url) {
		this.title = title;
		this.subtitle = subtitle;
		this.date = date;
		this.url = url;
	},
	
	render: function() {
		objItem = Builder.node
					('div',
						{
							className: 'eventItem'
						}
					);
		
		objTitle = Builder.node
					('span',
						{
							className: 'eventTitle'
						},
						[
							Builder.node
								('a',
									{
										href: this.url,
										onclick: "window.open(this.href,'ggurugEvent', 'height=500,width=520');return false;"
									}
								)
						]
					);
		Element.update(objTitle.firstChild, this.title);
		objItem.appendChild(objTitle);
		
		if(this.subtitle != "")
		{
			objSubtitle = Builder.node
							('span',
								{
									className: 'eventSubtitle'
								}
							);
			Element.update(objSubtitle, this.subtitle);
			objItem.appendChild(objSubtitle);
		}
		
		objDate = Builder.node
					('span',
						{
							className: 'eventDate'
						}
					);
		Element.update(objDate, this.date);
		objItem.appendChild(objDate);
		
		return objItem;
	}
}

/*
    Widget Class Declaration
     - initialize()
     - add()
	 - change()
	 - toggle()
	 - updateNav()
*/

var Widget = Class.create();

Widget.prototype = {
	initialize: function(widgetTitle, widgetId, widgetDataArray, dataPerPage, animation, defaultOpen, enableMin, randomStart, queue) {
		if(widgetDataArray.length == 0)
		{
			return;
		}
		this.active = 0;
		this.widgetTitle = widgetTitle;
		this.widgetId = widgetId;
		this.widgetDataArray = widgetDataArray;
		this.animation = animation;
		this.defaultOpen = defaultOpen;
		this.widgetDataPerPage = dataPerPage;
		this.widgetPages = Math.ceil(widgetDataArray.length/dataPerPage);
		if(queue != null && queue != ''){
			this.scope = queue;
		}else{
			this.scope = this.widgetId + 'queue';
		}
		this.minimizable = enableMin;
		this.randomize = randomStart;
		
		if(this.widgetPages > 1){
			rightarrowOff = "./common/images/home/right.jpg";
			leftarrowOff = "./common/images/home/left.jpg";
		}else{
			rightarrowOff = "./common/images/home/disabled.jpg";
			leftarrowOff = "./common/images/home/disabled.jpg";
		}
		if(this.minimizable){
			if(this.defaultOpen){
				minimizeOff = "./common/images/home/minus.jpg";
			}else{
				minimizeOff = "./common/images/home/plus.jpg";
			}
		}else{
			minimizeOff = "./common/images/home/disabled.jpg";
		}
		
		barContainer = Builder.node
		('div', 
			{
				id: this.widgetId + 'Bar',
				className:'widgetBar sectionTitle purpleTitle',
				style:'display:none;'
			},
			[
				Builder.node
				('div',
					[
						Builder.node
						('div', 
							{
								id: this.widgetId + 'Title', 
								className:'widgetTitle'
							}, this.widgetTitle
						),
						Builder.node
						('div', 
							{
								id: this.widgetId + 'Nav', 
								className:'widgetNav', 
								style:'display:none;'
							},
							[
								Builder.node
								('div',
									{
										id: this.widgetId + 'NextContainer',
										className: 'widgetNextContainer',
										style:'height:14px;width:14px;'
									},
									[
										Builder.node
										('div', 
											{
												id: this.widgetId + 'Next', 
												className: 'widgetNext', 
												style:'height:14px;width:14px;'
											},
											[
												Builder.node
													('img', 
														{
															src: rightarrowOff
														}
													)
											]
										)
									]
								),
								Builder.node
								('div',
									{
										id: this.widgetId + 'ToggleContainer',
										className: 'widgetToggleContainer',
										style:'height:14px;width:14px;'
									},
									[
										Builder.node
										('div', 
											{
												id: this.widgetId + 'Toggle', 
												className: 'widgetToggle', 
												style:'height:14px;width:14px;'
											},
											[
												Builder.node
													('img', 
														{
															src: minimizeOff
														}
													)
											]
										)
									]
								),
								Builder.node
								('div',
									{
										id: this.widgetId + 'PrevContainer',
										className: 'widgetPrevContainer',
										style:'height:14px;width:14px;'
									},
									[
										Builder.node
										('div', 
											{
												id: this.widgetId + 'Prev', 
												className: 'widgetPrev', 
												style:'height:14px;width:14px;'
											},
											[
												Builder.node
												('img', 
													{
														src: leftarrowOff
													}
												)
											]
										)
									]
								)
							]
						)
					]
				)
			]
		);
		
		$(this.widgetId).appendChild(barContainer);
		widgetBody = Builder.node
		('div',
			{
				id: this.widgetId + 'Outer',
				className: 'widgetOuter'
			},
			[
				Builder.node
				('div',
					{
						id: this.widgetId + 'Body',
						className: 'widgetBody',
						style:'display:none;'
					},
					[
						Builder.node
						('div',
							{
								id: this.widgetId + 'Page',
								className: 'widgetPage'
							}
						)
					]
				)
			]
		);
		
		$(this.widgetId).appendChild(widgetBody);
		this.open = false;
		if(this.randomize)
		{
			this.active = Math.round((this.widgetPages - 1)*Math.random());
		}
		this.updateContent();
	},
	
	change: function(widgetPage) {   
		this.active = widgetPage;
		
		new Effect.toggle(this.widgetId + 'Body', this.animation, { duration: 1, afterFinish: this.update.bind(this), queue:{position:'end',scope:this.scope}});
	},
	
	start: function(transition, highlight) {
		if(transition != null && transition != ""){
			new Effect.Appear( this.widgetId + 'Bar', { duration: 1,  queue:{position:'end',scope:this.scope}});
			if(this.defaultOpen){
				this.open = true;
				new Effect.toggle(this.widgetId + 'Body', transition, {duration: 1,  queue:{position:'end',scope:this.scope}});
			}
		}else{
			Element.show(this.widgetId + 'Bar');
			if(this.defaultOpen){
				this.open = true;
				Element.show(this.widgetId + 'Body');
			}
		}

		new Effect.Event({ afterFinish:this.startNav.bind(this), queue:{ position:'end', scope:this.scope}});
		
		if(highlight){
			new Effect.Highlight(this.widgetId + 'Body', {startcolor:'#e9e9e9', endcolor:'#ffffff', restorecolor:'#ffffff',  queue:{position:'end',scope:this.scope}});
			new Effect.Highlight(this.widgetId + 'Body', {startcolor:'#ffffff', endcolor:'#e9e9e9', restorecolor:'#e9e9e9',  queue:{position:'end',scope:this.scope}});
			new Effect.Highlight(this.widgetId + 'Body', {startcolor:'#e9e9e9', endcolor:'#ffffff', restorecolor:'#ffffff',  queue:{position:'end',scope:this.scope}});
			new Effect.Highlight(this.widgetId + 'Body', {startcolor:'#ffffff', endcolor:'#e9e9e9', restorecolor:'#e9e9e9',  queue:{position:'end',scope:this.scope}});
		}
	},
	
	startNav: function(elem){
		Element.setOpacity(this.widgetId + 'Next', 0.0);
		Element.setOpacity(this.widgetId + 'Prev', 0.0);
		Element.setOpacity(this.widgetId + 'Toggle', 0.0);
		
		Element.show(this.widgetId + "Nav");
		if(this.widgetPages > 1 && this.minimizable){
			new Effect.Parallel(
				[
					new Effect.Opacity(this.widgetId + 'Next', { sync:true, from:0.0, to:1.0 }),
					new Effect.Opacity(this.widgetId + 'Prev', { sync:true, from:0.0, to:1.0 }),
					new Effect.Opacity(this.widgetId + 'Toggle', { sync:true, from:0.0, to:1.0 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
			new Effect.Parallel(
				[
					new Effect.Opacity(this.widgetId + 'Toggle', { sync:true, from:1.0, to:0.2 }),
					new Effect.Opacity(this.widgetId + 'Next', { sync:true, from:1.0, to:0.2 }),
					new Effect.Opacity(this.widgetId + 'Prev', { sync:true, from:1.0, to:0.2 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.widgetPages > 1){
			new Effect.Parallel(
				[
					new Effect.Opacity(this.widgetId + 'Next', { sync:true, from:0.0, to:1.0 }),
					new Effect.Opacity(this.widgetId + 'Prev', { sync:true, from:0.0, to:1.0 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
			new Effect.Parallel(
				[
					new Effect.Opacity(this.widgetId + 'Next', { sync:true, from:1.0, to:0.2 }),
					new Effect.Opacity(this.widgetId + 'Prev', { sync:true, from:1.0, to:0.2 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.minimizable){
			new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:0.0, to:1.0, queue:{position:'end', scope: this.widgetId + 'Nav'}});
			new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:1.0, to:0.2, queue:{position:'end', scope: this.widgetId + 'Nav'}});
		}
		
		this.enableNav();
	},
	
	update: function(obj) {
		this.updateContent();
		new Effect.toggle(this.widgetId + 'Body', this.animation, { duration: 1, afterFinish: this.enableNav.bind(this),  queue:{position:'end',scope:this.scope} });
	},
	
	updateContent: function() {
		container = document.createElement("div");
		offset = this.active*this.widgetDataPerPage;
		for(i=offset;i< offset + this.widgetDataPerPage;i++)
		{
			nextDataPoint = i;
			if(nextDataPoint < this.widgetDataArray.length){
				container.appendChild(this.widgetDataArray[nextDataPoint].render());
			}
		}
		if(this.widgetId.indexOf('News') != -1){
			newsLink = Builder.node
						('div', 
							{ 
								className:'moreNewsLink' 
							}, 
							[
								Builder.node
									('a', 
										{ 
											href: newsLinkRef
										},
										'Spotlight Home >>'
									)
							]
						);
			container.appendChild(newsLink);
		}else if(this.widgetId.indexOf('events') != -1){
			eventLink = Builder.node
						('div', 
							{ 
								className:'moreEventsLink' 
							}, 
							[
								Builder.node
									('a', 
										{ 
											href: eventsLinkRef
										},
										'Events Home >>'
									)
							]
						);
			container.appendChild(eventLink);
		}
		
		Element.update(this.widgetId + 'Page', container.innerHTML);
	},
	
	disableNav: function(obj) {
		next = $(this.widgetId + 'Next');
		prev = $(this.widgetId + 'Prev');
		toggle = $(this.widgetId + 'Toggle');
		
		if(this.widgetPages > 1 && this.open && this.minimizable){
			new Effect.Parallel(
				[
					new Effect.Opacity(next, { sync:true, to:0.2 }),
					new Effect.Opacity(prev, { sync:true, to:0.2 }),
					new Effect.Opacity(toggle, { sync:true, to:0.2 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.widgetPages > 1 && this.open){
			new Effect.Parallel(
				[
					new Effect.Opacity(next, { sync:true, to:0.2 }),
					new Effect.Opacity(prev, { sync:true, to:0.2 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.minimizable){
			new Effect.Opacity(toggle, { duration: 0.5, to:0.2, queue:{position:'end', scope: this.widgetId + 'Nav'}});
		}
		
		if(this.widgetPages > 1 && this.open){
			next.onclick = '';
			prev.onclick = '';
			next.onmouseover = '';
			prev.onmouseover = '';
			next.onmouseout = '';
			prev.onmouseout = '';
			next.style.cursor = 'default';
			prev.style.cursor = 'default';
		}
		
		if(this.minimizable){
			toggle.onclick = '';
			toggle.onmouseover = '';
			toggle.onmouseout = '';
			toggle.style.cursor = 'default';
		}
		
		if(!this.open){
			$(this.widgetId + 'Title').onclick = '';
			$(this.widgetId + 'Title').style.cursor = 'default';
		}
	},
	
	enableNav: function(obj) {
		next = $(this.widgetId + 'Next');
		prev = $(this.widgetId + 'Prev');
		toggle = $(this.widgetId + 'Toggle');
		
		if(this.widgetPages > 1 && this.open && this.minimizable){
			new Effect.Parallel(
				[
					new Effect.Opacity(next, { sync:true, from:0.2, to:0.6 }),
					new Effect.Opacity(prev, { sync:true, from:0.2, to:0.6 }),
					new Effect.Opacity(toggle, { sync:true, from:0.2, to:0.6 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.widgetPages > 1 && this.open){
			new Effect.Parallel(
				[
					new Effect.Opacity(next, { sync:true, from:0.2, to:0.6 }),
					new Effect.Opacity(prev, { sync:true, from:0.2, to:0.6 })
				],
				{
					duration: 0.5, 
					queue: { position:'end', scope:this.widgetId + 'Nav' }
				}
			);
		}else if(this.minimizable){
			new Effect.Opacity(toggle, { duration: 0.5, from:0.2, to:0.6, queue:{position:'end', scope: this.widgetId + 'Nav'}});
		}
		
		if(this.widgetPages > 1 && this.open){
			prev.onclick = this.prev.bindAsEventListener(this);
			next.onclick = this.next.bindAsEventListener(this);
			
			next.onmouseover = this.nextIn.bindAsEventListener(this);
			next.onmouseout = this.nextOut.bindAsEventListener(this);
			
			prev.onmouseover = this.prevIn.bindAsEventListener(this);
			prev.onmouseout = this.prevOut.bindAsEventListener(this);
			
			next.style.cursor = 'pointer';
			prev.style.cursor = 'pointer';
		}
		
		if(this.minimizable){
			toggle.onclick = this.toggle.bindAsEventListener(this);
			toggle.onmouseout = this.toggleOut.bindAsEventListener(this);
			toggle.onmouseover = this.toggleIn.bindAsEventListener(this);
			toggle.style.cursor = 'pointer';
		}
		
		if(!this.open){
			title = $(this.widgetId + 'Title');
			title.onclick = this.toggle.bindAsEventListener(this);
			title.style.cursor = 'pointer';
		}
		
	},
	
	updateToggle: function(obj) {
		new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:0.2, to:0.0, queue:{position:'end', scope: this.widgetId + 'Nav'}, afterFinish:this.changeToggle.bind(this)});
	},
	
	changeToggle: function(obj){
		plus = "./common/images/home/plus.jpg";
		minus = "./common/images/home/minus.jpg";
		
		if(this.open)
		{
			$(this.widgetId + 'Toggle').firstChild.src = plus;
			this.open = false;
		}else{
			$(this.widgetId + 'Toggle').firstChild.src = minus;
			this.open = true;
		}
		new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:0.0, to:0.2, queue:{position:'end', scope: this.widgetId + 'Nav'}, afterFinish:this.enableNav.bind(this)});
	},
	
	nextIn: function() {
		if(this.open){
			new Effect.Opacity(this.widgetId + 'Next', { duration: 0.5, from:0.6, to:1.0, queue:{scope: this.widgetId + 'Nav'}});
		}
	},
	
	nextOut: function() {
		if(this.open){
			new Effect.Opacity(this.widgetId + 'Next', { duration: 0.5, from:1.0, to:0.6, queue:{scope: this.widgetId + 'Nav'}});
		}
	},
	
	prevIn: function() {
		if(this.open){
			new Effect.Opacity(this.widgetId + 'Prev', { duration: 0.5, from:0.6, to:1.0, queue:{scope: this.widgetId + 'Nav'}});
		}
	},
	
	prevOut: function() {
		if(this.open){
			new Effect.Opacity(this.widgetId + 'Prev', { duration: 0.5, from:1.0, to:0.6, queue:{scope: this.widgetId + 'Nav'}});
		}
	},
	
	toggleIn: function() {
		new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:0.6, to:1.0, queue:{scope: this.widgetId + 'Nav'}});
	},
	
	toggleOut: function() {
		new Effect.Opacity(this.widgetId + 'Toggle', { duration: 0.5, from:1.0, to:0.6, queue:{scope: this.widgetId + 'Nav'}});
	},
	
	prev: function(e) {
		this.disableNav();
		var previous = this.active - 1;
		if(previous < 0)
		{
			previous = this.widgetPages - 1;
		}
		this.change(previous);
	},
	
	next: function(e) {
		this.disableNav();
		next = this.active + 1;
		//alert(next);
		if(next >= this.widgetPages)
		{
			next = 0;
		}
		//alert(next);
		this.change(next);
	},
	
	ontoggle: function() {
		
	},
	
	toggle: function(e) {
		this.ontoggle();
		this.hide();
	},
	
	hide: function() {
		Effect.toggle(this.widgetId + 'Body', this.animation, { duration: 1, beforeStart: this.disableNav.bind(this), afterFinish: this.updateToggle.bind(this),  queue:{position:'end',scope:this.scope} });
	}
}

function addNews(headline,summary,thumb,url)
{
	newsArray.push(new ggurugNews(headline,summary,thumb,url));
}

function addNewsLink(headline,url)
{
	newsLinkArray.push(new ggurugNewsLink(headline,url));
}

function addSpotlight(type, image, title, subtitle, body, url) 
{
	spotlightArray.push(new Spotlight(type, image, title, subtitle, body, url));
}

function addEvent(title,subtitle,time,url)
{
	eventArray.push(new ggurugEvent(title,subtitle,time,url));
}

