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 queryStr=String(window.location);
var useFlash=(fVersion['major']>=8) ? ((queryStr.indexOf('?noflash')>=0) ? false : true ) : false ;

var stageInitialHeight=654;
var stageFullHeight=654;

var xmlData;

function launchModule() {
	$('flashholder').update('');
	if (useFlash) {
		$('flashholder').style.display='none';
		new Ajax.Updater('flashholder',pageRoot+xmlPath,{method: 'get', onComplete:function(){
			contentCount=$('flashholder').select('panel').length;
			if (contentCount<=6) {stageInitialHeight=450;}
			else if ((contentCount>6)&&(contentCount<=8)) {stageInitialHeight=350;}
			else if (contentCount>8) {stageInitialHeight=300;}
			else {stageInitialHeight=654}
			$('flashholder').update('');
			$('flashholder').style.display='block';
			var so = new SWFObject(swfPath, 'flash', '982', stageInitialHeight, '8', '#ffffff');
			so.addVariable('xmlPath',xmlPath);
			so.addVariable('stageInitialHeight',stageInitialHeight);
			so.addVariable('stageFullHeight',stageFullHeight);
			so.addVariable('pathRoot',((pageRoot==undefined) ? '' : pageRoot) );
			so.addVariable('baseTrackingURL',window.location.pathname.slice(1));
			so.addParam('wmode','transparent');
			so.addParam('scale','noborder');
			so.write('flashholder');
			$('flashholder').style.height=stageInitialHeight+'px';
		}});
	} else {
		new Ajax.Request(xmlPath, { method: 'get', onSuccess: function(transport) {xmlData=transport.responseText; xmlData=xmlData.split('\r\n').join(''); noFlashDraw();} });
	}
}
Event.observe(window, 'load', launchModule);
Event.observe(window, 'load', doSIFR);

function doSIFR() {
	if (useFlash && useSIFR && (typeof sIFR == "function")) {
		sIFR.bHideBrowserText = true;
		//sIFR.debug();
		sIFR.setup();
		sIFR.replaceElement("#sifr_head", named({sFlashSrc: "/_admin/swf/metabold.swf", sColor: "#000000", sWmode:'transparent'}));
	}
}
function showSkipLink() {
	new Insertion.Top('module_wrapper','<div id="skipLoad" onClick="this.remove(); useFlash=false; launchModule();">'+skipThisLoadAndGoToNoFlash+'</div>');
}
function hideSkipLink() {
	$('skipLoad').remove();
}

function resizeStageOnPanelClick() {
	dur=timings['long'];
	new Effect.Parallel([
			new Effect.Morph('flashholder', {
				style: 'height: '+stageFullHeight+'px;',
				sync:true
			}),
			new Effect.Morph('flash', {
				style: 'height: '+stageFullHeight+'px;',
				sync:true
			})
		],
		{duration: dur, fps: 30}
	);
}

var config=new Object();
var textSizes=new Object();
var timings=new Object();
var panelText=new Array();

//these can be overridden through XML <config id="SETTING_NAME">123</config>
config['panelFullWidth']=660;
config['panelFullHeight']=386;
config['panelSmallScale']=0.25;
config['introPanelSeparation']=16;
config['introSidePadding']=68;
config['panelSmallBorderScale']=0.98;
config['introRotation']=270;
config['fadeFilterOff']={contrast:1,brightness:1,saturation:1};
config['fadeFilterOn']={contrast:0.7,brightness:1.4,saturation:0.9};
config['rolloverScaleAmount']=1.13;
config['rolloverImgScaleAmount']=1.08;

//these can be overridden through XML <textsize id="SETTING_NAME">123</textsize>
textSizes['head']=32;
textSizes['headsmall']=65;
textSizes['subhead']=18;
textSizes['body']=13;
textSizes['body2']=11;
textSizes['link']=12;
textSizes['link2']=12;

//these can be overridden through XML <timing id="SETTING_NAME">123</timing>
timings['short']=0.1;
timings['medium']=0.25;
timings['long']=0.75;
timings['intromoves']=1.25;
timings['rollover']=0.1;
timings['rolloverImageScale']=1.5;
timings['rollout']=0.25;

//other configs not designed to be controllable through XML
config['loadBarWidth']=100;

function parseXML(nodeName,sourceXML) {
	if (sourceXML==undefined) {sourceXML=xmlData;}
	outData=new Object();
	nonIDcount=0;
	while (sourceXML.indexOf('<'+nodeName)>=0) {
		startData=sourceXML.slice(sourceXML.indexOf('<'+nodeName),sourceXML.indexOf('</'+nodeName+'>')+nodeName.length+3);
		sourceXML=sourceXML.slice(0,sourceXML.indexOf(startData))+sourceXML.slice(sourceXML.indexOf(startData)+startData.length);
		if (startData.indexOf('id="')>=0) {
			dataArray=startData.split('<'+nodeName+' id="');
			for (i=1; i<dataArray.length; i++) {
				temp=dataArray[i].split('</'+nodeName+'>');
				temp=temp.join('');
				temp2=temp.slice(temp.indexOf('">')+2);
				if (isNaN(temp2)) {
					outData[temp.slice(0,temp.indexOf('">'))]=temp2;
				} else {
					outData[temp.slice(0,temp.indexOf('">'))]=Number(temp2);
				}

			}
		} else {
			outData[nonIDcount]=startData.slice(startData.indexOf('<'+nodeName+'>')+nodeName.length+2,startData.indexOf('</'+nodeName+'>'));
			nonIDcount++;
		}
	}
	return outData;
}

function defaulty(prop,defaultVal) {
	return (prop==undefined) ? defaultVal : prop;
}
	
function copyProps(sObj,tObj,copyMissing,copyList) {
	tObj=defaulty(tObj,new Object());
	copyMissing=defaulty(copyMissing,true);
	copyList=defaulty(copyList,new Array());
	//make new source object from copylist if copylist is not empty
	if (copyList.length>0) {
		var tempObj=sObj;
		sObj={};
		for (var copyListProp in copyList) {
			sObj[copyList[copyListProp]]=tempObj[copyList[copyListProp]];
		}
	}
	if (tObj==null) {tObj=new Object();}
	if (typeof(sObj)=='object') {
		for (var prop in sObj) {
			if (copyMissing || tObj[prop]!=null) {
				if ((typeof(sObj[prop])=='string') && ((typeof(tObj[prop])=='number'))) {
					tObj[prop]+=Number(sObj[prop]);
				} else {
					tObj[prop]=sObj[prop];
				}
			}
		}
	}
	return tObj;
}

// NOFLASH DRAW FUNC
var smallHeadSizeCalculated=0;
var panels=new Array();
function noFlashDraw() {
	//yank xml comments
	while (xmlData.indexOf('<!--')>=0) {
		yankedComment=xmlData.slice(xmlData.indexOf('<!--'),xmlData.indexOf('//-->')+5);
		xmlData=xmlData.slice(0,xmlData.indexOf(yankedComment))+xmlData.slice(xmlData.indexOf(yankedComment)+yankedComment.length);
	}

	//get text sizes
	temp=parseXML('config');
	copyProps(temp,config);
	temp=parseXML('timing');
	copyProps(temp,timings);
	temp=parseXML('textsize');
	copyProps(temp,textSizes);
	
	//console.log(config);	
	//console.log(timings);
	//console.log(textSizes);
	
	flashCompensation=3; //mysterious difference in font layout between flash and HTML, this modifies leading to compensate

	//change font size tags to inline css and add line-height
	xmlData=xmlData.replace(
		new RegExp('size="(.[0-9]{1})','gi'),
		function($1){
			pixelsize=$1.replace('size="','');
			newStr=$1.replace('size="','style="font-size:');
			newStr+='px; line-height: '+(Number(pixelsize)+Number(textSizes['leading'])+flashCompensation)+'px; ';
			return newStr;
		}
	);

	//add inline styles to p tags based on textSize settings
	xmlData=xmlData.replace(/<p>/g,'<p style="line-height: '+(Number(textSizes['body'])+Number(textSizes['leading'])+flashCompensation)+'px; padding: 0px; margin: 0px 0px '+(Number(textSizes['paragraphspacing'])+1)+'px 0px; ">');

	//process main content
	panelText=parseXML('panel');
	dataPiecesArray=new Array('bgimage','head','body','body_link','body_url','subhead_left','body_left','link_left','url_left','subhead_right','body_right','link_right','url_right');
	for (var whichPanel in panelText) {
		temp=panelText[whichPanel];
		panelText[whichPanel]=new Object();
		for (j=0; j<dataPiecesArray.length; j++) {
			if (temp.indexOf('<'+dataPiecesArray[j]+'>')>=0){
				panelText[whichPanel][dataPiecesArray[j]]=temp.slice(temp.indexOf('<'+dataPiecesArray[j]+'>')+dataPiecesArray[j].length+2,temp.indexOf('</'+dataPiecesArray[j]+'>'));
			}
		}
	}
		
	//postprocess
	smallHeadSizeCalculated=Math.floor(textSizes['headsmall']*config['panelSmallScale']);
	
	//drop in text, update tablist, 'click' default
	var selectedDrawn=true;
	for (var whichPanel in panelText) {
		var li1=getLinkInfo(panelText[whichPanel]['body_url']);
		var li2=getLinkInfo(panelText[whichPanel]['url_left']);
		var li3=getLinkInfo(panelText[whichPanel]['url_right']);
		
		var u1=li1[0];
		var u2=li2[0];
		var u3=li3[0];
	
		var t1=li1[1];
		var t2=li2[1];
		var t3=li3[1];
		
		var ws1=li1[2];
		var ws2=li2[2];
		var ws3=li2[2];

		//var t1=(panelText[whichPanel]['body_url'].lastIndexOf('.pdf')==(panelText[whichPanel]['body_url'].length-4)) ? '_blank' : '_self' ;
		//var t2=(panelText[whichPanel]['url_left'].lastIndexOf('.pdf')==(panelText[whichPanel]['url_left'].length-4)) ? '_blank' : '_self' ;
		//var t3=(panelText[whichPanel]['url_right'].lastIndexOf('.pdf')==(panelText[whichPanel]['url_right'].length-4)) ? '_blank' : '_self' ;
		var oc1;
		var oc2;
		var oc3;

		var tStr=panelText[whichPanel]['head']+': LM Top';
		if (t1=='_blank') {
			if (u1.indexOf('.pdf')>0) {
				oc1='emcTrack(\''+tStr+'\',\''+u1+'\',\'d\');';
			} else {
				oc1='emcTrack(\''+tStr+'\');';
			}
		} else if (t1=='_pop') {
			var dims=ws1.split('X');
			oc1='var popWin=window.open(\''+u1+'\', \'\', \'width='+dims[0]+',height='+dims[1]+',toolbar=no,scrollbars=no\'); popWin.focus(); emcTrack(\''+tStr+'\'); return false;';
		} else {
			oc1='emcTrack(\''+tStr+'\');';
		}

		var tStr=panelText[whichPanel]['head']+': LM Bottom Left';
		if (t2=='_blank') {
			if (u2.indexOf('.pdf')>0) {
				oc2='emcTrack(\''+tStr+'\',\''+u2+'\',\'d\');';
			} else {
				oc2='emcTrack(\''+tStr+'\');';
			}
		} else if (t2=='_pop') {
			var dims=ws2.split('X');
			oc2='var popWin=window.open(\''+u2+'\', \'\', \'width='+dims[0]+',height='+dims[1]+',toolbar=no,scrollbars=no\'); popWin.focus(); emcTrack(\''+tStr+'\'); return false;';
		} else {
			oc2='emcTrack(\''+tStr+'\');';
		}
		
		var tStr=panelText[whichPanel]['head']+': LM Bottom Right';
		if (t3=='_blank') {
			if (u3.indexOf('.pdf')>0) {
				oc3='emcTrack(\''+tStr+'\',\''+u3+'\',\'d\');';
			} else {
				oc3='emcTrack(\''+tStr+'\');';
			}
		} else if (t3=='_pop') {
			var dims=ws3.split('X');
			oc3='var popWin=window.open(\''+u3+'\', \'\', \'width='+dims[0]+',height='+dims[1]+',toolbar=no,scrollbars=no\'); popWin.focus(); emcTrack(\''+tStr+'\'); return false;';
		} else {
			oc3='emcTrack(\''+tStr+'\');';
		}

		//var oc1=(t1=='_blank')
		//	? 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Top\',\''+u1+'\',\'d\');"'
		//	: 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Top\');"' ;
		//var oc2=(t2=='_blank')
		//	? 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Bottom Left\',\''+u2+'\',\'d\');"'
		//	: 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Bottom Left\');"' ;
		//var oc3=(t3=='_blank')
		//	? 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Bottom Right\',\''+u3+'\',\'d\');"'
		//	: 'onclick="emcTrack(\''+panelText[whichPanel]['head']+': LM Bottom Right\');"' ;
		
		var panelHTML='';

		panelHTML+='<div id="'+whichPanel+'" class="panelGroup" style="background-position: 8px 8px; background-image:url('+panelText[whichPanel]['bgimage'].split('.jpg').join('_noflash.jpg')+'); cursor: '+((selectedDrawn) ? 'pointer' : 'auto' )+';" onclick="changePanels(\'panelGroup\',this.id)">';
		panelHTML+='	<div class="roundedTop"><img src="flash/noflash/roundedFrameTop.gif" width="660" height="12"></div>';
		panelHTML+='	<div class="roundedCenter" style="height:'+((selectedDrawn) ? 32 : 352 )+'px;">';
		panelHTML+='			<div class="head" style="font-size:'+((selectedDrawn) ? smallHeadSizeCalculated : (textSizes['head']-6) )+'px; line-height: '+(textSizes['head']-2)+'px; ';
					panelHTML+='padding: '+((selectedDrawn) ? '2px 0px 0px 22px' : '20px 0px 0px 22px' )+';">'+panelText[whichPanel]['head']+'</div>';
		panelHTML+='			<div class="body" style="font-size:'+(textSizes['body']+0)+'px; line-height:'+(textSizes['body']+4)+'px; display:'+((selectedDrawn) ? 'none' : 'block' )+'">'+panelText[whichPanel]['body']+'</div>';
		panelHTML+='		<div class="textBoxes" style="display:'+((selectedDrawn) ? 'none' : 'block' )+'">';
		panelHTML+='			<div class="body_link" style="font-size:'+(textSizes['link']-0)+'px;"><a href="'+u1+'" target="'+t1+'" onclick="'+oc1+'">'+panelText[whichPanel]['body_link']+'</a></div>';
		panelHTML+='			<div class="boxes">';
		panelHTML+='				<div class="box_left">';
		panelHTML+='					<div class="subhead_left" style="font-size:'+(textSizes['subhead']-2)+'px;">'+panelText[whichPanel]['subhead_left']+'</div>';
		panelHTML+='					<div class="body_left" style="font-size:'+(textSizes['body2']+0)+'px; line-height:'+(textSizes['body2']+4)+'px;">'+panelText[whichPanel]['body_left']+'</div>';
		panelHTML+='					<div class="link_left" style="font-size:'+(textSizes['link2']-0)+'px;"><a href="'+u2+'" target="'+t2+'" onclick="'+oc2+'">'+panelText[whichPanel]['link_left']+'</a></div>';
		panelHTML+='				</div>';
		panelHTML+='				<div class="box_right">';
		panelHTML+='					<div class="subhead_right" style="font-size:'+(textSizes['subhead']-2)+'px;">'+panelText[whichPanel]['subhead_right']+'</div>';
		panelHTML+='					<div class="body_right" style="font-size:'+(textSizes['body2']+0)+'px; line-height:'+(textSizes['body2']+4)+'px;">'+panelText[whichPanel]['body_right']+'</div>';
		panelHTML+='					<div class="link_right" style="font-size:'+(textSizes['link2']-0)+'px;"><a href="'+u3+'" target="'+t3+'" onclick="'+oc3+'">'+panelText[whichPanel]['link_right']+'</a></div>';
		panelHTML+='				</div>';
		panelHTML+='			</div>';
		panelHTML+='		</div>';
		panelHTML+='	</div>';
		panelHTML+='	<div class="roundedBottom"><img src="flash/noflash/roundedFrameBottom.gif" width="660" height="12"></div>';
		panelHTML+='</div>';
		
		selectedDrawn=true;
		Insertion.Bottom('flashholder',panelHTML);
	}
	
	changePanels('panelGroup',$$('.panelGroup')[0].id);
	
}

function getLinkInfo(inData) {
	var winSpec='notsetyet';
	var isPDF=(inData.substr(inData.lastIndexOf(".")+1)=='pdf') ? true : false;
	var theURL='';
	if ( 
		inData.indexOf('[')>=0
		&&
		inData.indexOf(']')>0
		&&
		inData.indexOf('[')<inData.indexOf(']')
	) {
		// IF SETTINGS IN BRACKETS ARE FOUND, DO THIS:
		winSpec=inData.slice(0,inData.indexOf(']')+1);
		winSpec=winSpec.split(' ').join('').split('[').join('').split(']').join('').toUpperCase();
		theURL=inData.slice(inData.indexOf(']')+1);
		if (isPDF) {
			if (winSpec=='notsetyet') {winSpec='NEW';}
		}
		if (winSpec.indexOf('X')>=0) {
			return [theURL,'_pop',winSpec];
		} else if (winSpec=='NEW') {
			return [theURL,'_blank'];
		} else {
			return [theURL,'_self'];
		}
	} else {
		// no bracketed settings found, do regular link
		theURL=inData;
		if (isPDF) {
			return [theURL,'_blank'];
		} else {
			return [theURL,'_self']
		}
	}
}

var cPanel;
var isAnimating=false;
function changePanels(whichGrp,tabID) {
		
	var animsArray=new Array();
	if (!isAnimating) {

		if (tabID!=cPanel) {
		
			//TRACKING
			//make tracking string
			tStr=$$('#'+tabID+' .head')[0].innerHTML;
			//do tracking
			emcTrack(tStr,'','o');
			//END TRACKING
			
			cPanel=tabID;
			
		}
	

		$$('.'+whichGrp).each(function(e){
			if ($(tabID).identify()==$(e).identify()) {
				$(e).select('.body')[0].style.display='block';
				$(e).select('.textBoxes')[0].style.display='block';
				$(e).select('.head')[0].style.fontSize=(textSizes['head']-6)+'px';
				animsArray.push(new Effect.Morph($(e).select('.roundedCenter')[0],{style:'height:352px;', duration: 1, fps: 30, sync:true}) );
				$(e).style.cursor='auto';
				$(e).select('.head')[0].style.padding='20px 0px 0px 22px';
			} else {
				$(e).select('.head')[0].style.fontSize=smallHeadSizeCalculated+'px';
				$(e).style.cursor='pointer';
				$(e).select('.head')[0].style.padding='2px 0px 0px 22px';
				animsArray.push(new Effect.Morph($(e).select('.roundedCenter')[0],{style:'height:32px;', duration: 1, fps: 30, sync:true, afterFinish:function () {
						$(e).select('.body')[0].style.display='none';
						$(e).select('.textBoxes')[0].style.display='none';
					},
					afterUpdate: function() { 
						/* this staggeringly stupid fix is needed by IE 6 AND 7 to avoid "leaving behind" the heads when it animates the page.
						  i have no idea why it happens or exactly why this fixes it other than that by turning the heads on and off in every frame
						  it seems to force IE to "keep track" of them and not leave them unanimated on the page (which should be impossible since
						  the parent object moves...) all I can say is "I HATE IE" */
						$(e).select('.head')[0].style.display='none';
						$(e).select('.head')[0].style.display='block';
					}
					}) );
			}
		});
		isAnimating=true;
		new Effect.Parallel(animsArray,{afterFinish:function () {isAnimating=false;}});
	}
}

