function getRandom(min,max) {randNum=min-1; while ((randNum<min) || (randNum>max)) { randNum=Math.round(Math.random()*(max+1)); } return (randNum);}

var fVersion=deconcept.SWFObjectUtil.getPlayerVersion();
var useFlash=(fVersion['major']>=8) ? true : false ;

function launchTimeline() {
	if (useFlash) {
		var so = new SWFObject(swfPath, 'flash', '1000', '450', '8', '#ffffff');
		so.addVariable('xmlPath',xmlPath);
		
		so.addVariable('timelineIntroCookie',readCookie('timelineIntroCookie'));

		so.addVariable('introTitleColor',introTitleColor);
		so.addVariable('introTitleSize',introTitleSize);
		so.addVariable('introTitle',introTitle);
		so.addVariable('introBodyCopyColor',introBodyCopyColor);
		so.addVariable('introBodyCopySize',introBodyCopySize);
		so.addVariable('introBodyCopy',introBodyCopy);

		so.addVariable('panelTextYearColor',panelTextYearColor);
		so.addVariable('panelTextYearSize',panelTextYearSize);
		so.addVariable('panelTextCategoryColor',panelTextCategoryColor);
		so.addVariable('panelTextCategorySize',panelTextCategorySize);
		so.addVariable('panelTextTitleColor',panelTextTitleColor);
		so.addVariable('panelTextTitleSize',panelTextTitleSize);
		so.addVariable('panelTextBodyCopyColor',panelTextBodyCopyColor);
		so.addVariable('panelTextBodyCopySize',panelTextBodyCopySize);
		so.addVariable('rolloverYearColor',rolloverYearColor);

		so.addParam('wmode','transparent');
		so.write('flashholder');
	} else {
		new Ajax.Request(xmlPath, { method: 'get', onSuccess: function(transport) {noFlashDraw(transport.responseText);} });
	}
}
Event.observe(window, 'load', launchTimeline);

function showSkipLink() {
	new Insertion.Top('timeline_wrapper','<div id="skipLoad" onClick="$(\'flashholder\').update();$(\'skipLoad\').remove();new Ajax.Request(xmlPath, { method: \'get\', onSuccess: function(transport) {noFlashDraw(transport.responseText);} });">'+skipThisLoadAndGoToNoFlash+'</div>');
}
function hideSkipLink() {
	$('skipLoad').remove();
}

// NOFLASH DRAW FUNC
var yearArray=new Array();
var cYear;
function noFlashDraw(xmlData) {
	$('flashholder').update('<div id="noflash-wrapper"><div id="left-wrapper"><div id="instructions">'+noFlash_Instructions+'</div></div><div id="right-wrapper"></div></div>');
	new Insertion.After('instructions', '<div id="years"></div>');
	new Insertion.After('years', '<div id="events"></div>');
	new Insertion.After('events', ('<div id="flashlink">'+noFlash_GetFlash+'</div>'));
	new Insertion.Bottom('right-wrapper', '<div id="panel"></div>');
	new Insertion.Bottom('panel', '<div id="panel-loadindicator"><img src="noflash/noflash_loadindicator.gif" width="16" height="16"/></div>');
	new Insertion.Bottom('panel', '<div id="panel-background"></div>');
	new Insertion.Bottom('panel', '<div id="panel-overlay"></div>');
	$('panel-overlay').setStyle({ opacity: 0.65 });
	new Insertion.Bottom('panel', '<div id="panel-category"></div>');
	new Insertion.Bottom('panel', '<div id="panel-title"></div>');
	new Insertion.Bottom('panel', '<div id="panel-bodycontent"></div>');
	new Insertion.Bottom('right-wrapper', '<div id="panel-corners"><img src="noflash/noflash_corners.gif" width="508" height="286"/></div>');
	tempArray=xmlData.split('<year id="');
	for (y=1;y<tempArray.length;y++) {
		cYear=tempArray[y].slice(0,4);
		new Insertion.Bottom('years', '<span class="year" id="year-'+cYear+'">'+cYear+'</span>');
		tempArray2=tempArray[y].split('<event');
		yearArray[cYear]=new Array();
		for (e=1;e<tempArray2.length;e++) {
			cEvent=e;
			tempArray3=tempArray2[cEvent].split('</');
			yearArray[cYear][cEvent]=new Array();
			for (p=0;p<tempArray3.length-2;p++) {
				tempArray3[p]=tempArray3[p].slice(tempArray3[p].indexOf('<')+1);
				pName=tempArray3[p].slice(0,tempArray3[p].indexOf('>'));
				pContent=tempArray3[p].slice(tempArray3[p].indexOf('>')+1);
				pContent=pContent.strip();
				if ((pName!='event') && (pName!='year') && (pContent!='')) {
					yearArray[cYear][cEvent][pName]=unescape(pContent);
				}
				//add styles
				yearArray[cYear][cEvent]['category']='<span style="font-size:'+panelTextCategorySize+'px; color:'+panelTextCategoryColor+';">'+yearArray[cYear][cEvent]['category']+'</span>';				
				yearArray[cYear][cEvent]['title']='<span style="font-size:'+panelTextTitleSize+'px; color:'+panelTextTitleColor+';">'+yearArray[cYear][cEvent]['title']+'</span>';				
				yearArray[cYear][cEvent]['body_copy']='<span style="font-size:'+panelTextBodyCopySize+'px; color:'+panelTextBodyCopyColor+';">'+yearArray[cYear][cEvent]['body_copy']+'</span>';
			}
		}
	}
	updateEvents(cYear);
}

function updateEvents(whichYear) {
	$('events').update('<div class="eventYear">'+whichYear+'</div>');
	whichEvent=1;
	while ((yearArray[whichYear][whichEvent]!=undefined) && (whichEvent<6)) {
		new Insertion.Bottom('events', '<div class="event" id="event-'+whichYear+'-'+whichEvent+'">'+yearArray[whichYear][whichEvent]['rollover']+'</div>');
		Behaviour.apply();
		whichEvent++;
	}
	$('year-'+whichYear).style.color='#003580';
	$('year-'+whichYear).style.cursor='default';
	$('year-'+whichYear).style.textDecoration='none';
	$('year-'+whichYear).clicked=true;
	$('event-'+whichYear+'-'+(whichEvent-1)).style.color='#003580';
	$('event-'+whichYear+'-'+(whichEvent-1)).style.fontWeight='bold';
	$('event-'+whichYear+'-'+(whichEvent-1)).style.margin='0px 0px 4px 0px';
	$('event-'+whichYear+'-'+(whichEvent-1)).style.cursor='default';
	$('event-'+whichYear+'-'+(whichEvent-1)).style.textDecoration='none';
	$('event-'+whichYear+'-'+(whichEvent-1)).clicked=true;
	updatePanel(whichYear,whichEvent-1);
}

function updatePanel(whichYear,whichEvent) {
	switch (yearArray[whichYear][whichEvent]['template']) {
		case 'panel_right':
			$('panel-loadindicator').style.left='13px';
			$('panel-loadindicator').style.top='12px';
			$('panel-overlay').style.left='276px';
			$('panel-overlay').style.top='0px';
			$('panel-overlay').style.width='232px';
			$('panel-overlay').style.height='286px';
			$('panel-category').style.left='289px';
			$('panel-category').style.top='15px';
			$('panel-category').style.width='205px';
			$('panel-category').style.height='23px';
			$('panel-title').style.left='289px';
			$('panel-title').style.top='33px';
			$('panel-title').style.width='205px';
			$('panel-title').style.height='92px';
			$('panel-bodycontent').style.left='289px';
			$('panel-bodycontent').style.top='144px';
			$('panel-bodycontent').style.width='205px';
			$('panel-bodycontent').style.height='130px';
			break;
		case 'panel_bottom':
			$('panel-loadindicator').style.left='13px';
			$('panel-loadindicator').style.top='12px';
			$('panel-overlay').style.left='0px';
			$('panel-overlay').style.top='140px';
			$('panel-overlay').style.width='508px';
			$('panel-overlay').style.height='146px';
			$('panel-category').style.left='14px';
			$('panel-category').style.top='154px';
			$('panel-category').style.width='235px';
			$('panel-category').style.height='23px';
			$('panel-title').style.left='14px';
			$('panel-title').style.top='172px';
			$('panel-title').style.width='235px';
			$('panel-title').style.height='92px';
			$('panel-bodycontent').style.left='266px';
			$('panel-bodycontent').style.top='154px';
			$('panel-bodycontent').style.width='229px';
			$('panel-bodycontent').style.height='108px';
			break;
		case 'panel_left':
			$('panel-loadindicator').style.left='479px';
			$('panel-loadindicator').style.top='12px';
			$('panel-overlay').style.left='0px';
			$('panel-overlay').style.top='0px';
			$('panel-overlay').style.width='231px';
			$('panel-overlay').style.height='286px';
			$('panel-category').style.left='14px';
			$('panel-category').style.top='15px';
			$('panel-category').style.width='205px';
			$('panel-category').style.height='23px';
			$('panel-title').style.left='14px';
			$('panel-title').style.top='33px';
			$('panel-title').style.width='205px';
			$('panel-title').style.height='92px';
			$('panel-bodycontent').style.left='14px';
			$('panel-bodycontent').style.top='144px';
			$('panel-bodycontent').style.width='205px';
			$('panel-bodycontent').style.height='130px';
			break;
		}
	$('panel-category').update(yearArray[whichYear][whichEvent]['category']);
	$('panel-title').update(yearArray[whichYear][whichEvent]['title']);
	$('panel-bodycontent').update(yearArray[whichYear][whichEvent]['body_copy']);
	$('panel-background').style.backgroundImage='none'; // necessary for opera to display progress indicator
	$('panel-background').style.backgroundImage='url('+yearArray[whichYear][whichEvent]['background_image']+')';
}

Behaviour.register({
	'.year' : function (e) {
		e.onclick=function() {
			if (!$(e).clicked) {
				for (y=0;y<$('years').descendants().length;y++) {
					$('years').descendants()[y].style.color='#ffffff';
					$('years').descendants()[y].clicked=false;
					$('years').descendants()[y].style.cursor='pointer';
				}
				$(e).style.color='#003580';
				$(e).style.cursor='default';
				$(e).style.textDecoration='none';
				$(e).clicked=true;
				vals=e.id.split('-');
				updateEvents(vals[1]);
			}
		}
		e.onmouseover=function() {
			if (!$(e).clicked) {
				$(e).style.color='#003580';
				$(e).style.textDecoration='underline';
			}
		}
		e.onmouseout=function() {
			if (!$(e).clicked) {
				$(e).style.color='#ffffff';
			}
			$(e).style.textDecoration='none';
		}
	},
	'.event' : function (e) {
		e.onclick=function() {
			if (!$(e).clicked) {
				for (y=0;y<$('events').descendants().length;y++) {
					if ($('events').descendants()[y].hasClassName('event')) {
						$('events').descendants()[y].style.color='#000000';
						$('events').descendants()[y].style.fontWeight='normal';
						$('events').descendants()[y].style.margin='0px 0px 3px 0px';
						$('events').descendants()[y].style.cursor='pointer';
						$('events').descendants()[y].clicked=false;
					}
				}
				$(e).style.color='#003580';
				$(e).style.fontWeight='bold';
				$(e).style.margin='0px 0px 4px 0px';
				$(e).style.cursor='default';
				$(e).style.textDecoration='none';
				$(e).clicked=true;
				vals=e.id.split('-');
				updatePanel(vals[1],vals[2]);
			}
		}
		e.onmouseover=function() {
			if (!$(e).clicked) {
				$(e).style.color='#003580';
				$(e).style.textDecoration='underline';
			}
		}
		e.onmouseout=function() {
			if (!$(e).clicked) {
				$(e).style.color='#000000';
				$(e).style.fontWeight='normal';
			}
			$(e).style.textDecoration='none';
		}
	}
});
