//var HomepageAccordionActive = true;

function InitializeAccordion() {
	var Togglers = $$('.InteractiveIndexFrame .Heading h3');
	//for (var i = 0; i < Togglers.length; i++) {
		//Togglers[i].set('html', '<span class="Chevron"></span> ' + Togglers[i].get('html'));
		/// Removed because it doesn't make sense... If they don't have CSS support, the javascript wouldn't work anyway...
		// <!--span>(click to show)</span-->
	//}
	Togglers.each( function(item) {
		item.set('html', '<span class="Chevron"></span> ' + item.get('html'));
		item.set('title', 'Click to activate');
	});
	function FixBoxHeight(theboxtofix) {
		if (theboxtofix) {
			//var InnerBlockObj = this.ActiveBlock.getElement('.IndexW');
			//var InnerBlockSize = InnerBlockObj.getSize();
			theboxtofix.setStyle('height', 'auto');
			var BlockSize = theboxtofix.getSize();
			theboxtofix.setStyle('height', BlockSize.y);
			//console.log('The block: '+theboxtofix.className+'; and is '+BlockSize.y+' pixels tall.');
			//console.log('The inner block: '+InnerBlockObj.className+'; and is '+InnerBlockSize.y+' pixels tall.');
			return BlockSize.y;
		}
		return null;
	}
	
	myAccordion = new Accordion($$('.InteractiveIndexFrame .Heading'), $$('.InteractiveIndexFrame .IndexBlock'), {
		//fixedHeight: true,
		display: 0,
		//alwaysHide: true,
		ActiveBlock: null,
		opacity: false,
		onActive: function(toggler, block) {
			toggler.addClass('Active');
			this.ActiveBlock = block;
		},
		onBackground: function(toggler, block) {
			toggler.removeClass('Active');
		},
		onComplete: function() {
			//console.log(this);
			//window.alert('Page loaded to here');
			FixBoxHeight(this.ActiveBlock);
			//if (this.getStyle('height') != '0px') {
			/// Set the height of "this" to the height of it's innermost child.
			//console.log(toggler, block, b2, b3);
		}
	});
	window.addEvent('resize', function() {
		FixBoxHeight(myAccordion.ActiveBlock);
	});
}

var photoslideactive = true;
function InitializePhotoSlide3() {
	var cookieNameAutoStart = 'libAnimationAutoStartHomepage';
	var PicBoxObj = $('PhotoPan');
	if ( PicBoxObj ) {
		var myFx = new Fx.Tween(PicBoxObj, {property: 'background-position', duration: 0});
		var timecounter = 0;
		
		/// Specify defaults
		var ScrollPic = new Class({
			Implements: Options,
			options: {
				coords: '0 0',
				transitiontime: 4000,
				holdtime: 2000
			},
			initialize: function(options){
				this.setOptions(options);
			}
		});
		
		//var PictureCoords = new Array(
		//	new ScrollPic({ coords: '-80 0',			holdtime: 5000 }),
		//	new ScrollPic({ coords: '-510 0',		transitiontime: 20000,	holdtime: 100 }),
		//	new ScrollPic({ coords: '-510 -568',	holdtime: 0 }),
		//	new ScrollPic({ coords: '-250 -568',	transitiontime: 10000,	holdtime: 4000 }),
		//	new ScrollPic({ coords: '0 -568',		transitiontime: 2000,	holdtime: 0 })
		//);
		var PictureCoords = new Array(
			new ScrollPic({ coords: '-23 -0',		holdtime: 250	}),
			new ScrollPic({ coords: '-140 -13',		holdtime: 0,	transitiontime: 6000	}),
			new ScrollPic({ coords: '-424 -32',		holdtime: 1000	}),
			new ScrollPic({ coords: '-675 -6',		holdtime: 1000	}),
			new ScrollPic({ coords: '-721 -395',	holdtime: 1000	}),
			new ScrollPic({ coords: '-607 -398',	holdtime: 500,	transitiontime: 5000	}),
			new ScrollPic({ coords: '-451 -403',	holdtime: 0,	transitiontime: 4500	}),
			new ScrollPic({ coords: '-168 -476'		}),
			new ScrollPic({ coords: '0 0',			holdtime: 500	})
		);
		
		//var ScrollPicFunctions = new Array();
		//for (var i = 0; i < PictureCoords.length; i++) {
		//	var OptionsObj = PictureCoords[i].options;
		//	console.log('Adding:', OptionsObj);
		//	ScrollPicFunctions.push( function() {
		//		this.start(OptionsObj.coords, {duration: OptionsObj.transitiontime});
		//		console.log('Going to: '+OptionsObj.coords);
		//	}
		//	//.delay(OptionsObj.holdtime)
		//	);
		//	ScrollPicFunctions[i].delay(OptionsObj.holdtime);
		//}
		var totaltime = 0;
		for (var i = 0; i < PictureCoords.length; i++) {
			totaltime+= PictureCoords[i].options.transitiontime + PictureCoords[i].options.holdtime;
		}
		//console.log('Starting Fx. Starting again in '+(totaltime /1000) + ' seconds.');
		function AnimateOneLoop() {
			function AnimateOneTransition(index) {
				//console.log('Going to: ', index, ' @ ' + (timecounter/10));
				this.options.duration = PictureCoords[index].options.transitiontime;
				this.start(PictureCoords[index].options.coords);
			}
			
			myFx.start(PictureCoords[0].options.coords).wait(PictureCoords[0].options.holdtime)
				.chain( function() { AnimateOneTransition.run(1, this); } ).wait(PictureCoords[1].options.holdtime)
				.chain( function() { AnimateOneTransition.run(2, this); } ).wait(PictureCoords[2].options.holdtime)
				.chain( function() { AnimateOneTransition.run(3, this); } ).wait(PictureCoords[3].options.holdtime)
				.chain( function() { AnimateOneTransition.run(4, this); } ).wait(PictureCoords[4].options.holdtime)
				.chain( function() { AnimateOneTransition.run(5, this); } ).wait(PictureCoords[5].options.holdtime)
				.chain( function() { AnimateOneTransition.run(6, this); } ).wait(PictureCoords[6].options.holdtime)
				.chain( function() { AnimateOneTransition.run(7, this); } ).wait(PictureCoords[7].options.holdtime)
				.chain( function() { AnimateOneTransition.run(0, this); } )
		}
		function Counter() {
			timecounter++;
			window.status = 'Current Second: '+(timecounter/10);
		}
	
		//Counter();
		//Counter.periodical(100);
		//var myFx = new Fx.Tween('myElement', {property: 'opacity'});
		//myFx.start(1,0).chain(
			//Notice that "this" refers to the calling object (in this case, the myFx object).
		//	function(){ this.start(0,1); },
		//	function(){ this.start(1,0); },
		//	function(){ this.start(0,1); }
		//);
		
		function SlideActivate() {
				//ActivateNextMarker();
				//picturecicler = window.setInterval("ActivateNextMarker()",4000);
				//console.log(myFx);
				if (myFx.from && myFx.from.length) {
					myFx.resume();
					/// oh good
				}
				playpauseObj.setStyle('background-image', 'url(/images/Buttons/Pause.png)');
				//playpauseObj.style.backgroundImage = 'url(/images/Buttons/Pause.png)';
				//photoslideactive = true;
				
				AnimateOneLoop();
				photoanimationtimer = AnimateOneLoop.periodical(totaltime);
				Cookie.write(cookieNameAutoStart, true, {duration: 999});
		}
		function SlideDeactivate() {
				//window.clearInterval(picturecicler);
				//picturecicler = null;
				myFx.pause();
				playpauseObj.setStyle('background-image', 'url(/images/Buttons/Play.png)');
				//photoslideactive = false;
				//if (photoanimationtimer) {
					photoanimationtimer = $clear(photoanimationtimer);
				//}
				Cookie.write(cookieNameAutoStart, false, {duration: 999});
				//console.log('running deactivate');
		}
		
		var playpauseObj = new Element('div', {'class': 'PhotoButton PlayPause'});
		playpauseObj.addEvent('click', function() {
			if (photoanimationtimer) {
				SlideDeactivate();
			}
			else {
				SlideActivate();
			}
			//map.zoomIn();
		});
		playpauseObj.inject(PicBoxObj);
		
		var shouldthisautostart = Cookie.read(cookieNameAutoStart);
		//window.alert('should this be paused? '+shouldthisbepaused );
		if (shouldthisautostart == 'true' || shouldthisautostart == true) {
			AnimateOneLoop();
			var photoanimationtimer = AnimateOneLoop.periodical(totaltime);
			Cookie.write(cookieNameAutoStart, true, {duration: 999});
			//SlideActivate();
		}
		else {
			//window.alert('i just stopped this');
			
			SlideDeactivate();
			//playpauseObj.setStyle('background-image', 'url(/images/Buttons/Play.png)');
		}
		
	}
}

function InitializePhotoSlide2() {
	var PicBoxObj = $('PhotoPan');
	if ( PicBoxObj ) {
		var myFx = new Fx.Tween(PicBoxObj, {property: 'background-position', duration: 0});
			
		var ScrollPic = new Class({
			Implements: Options,
			options: {
				coords: '0 0',
				transitiontime: 2000,
				holdtime: 2000
			},
			initialize: function(options){
				this.setOptions(options);
			}
		});
		
		var PictureCoords = new Array(
			new ScrollPic(),
			new ScrollPic({ coords: '0 -568' }),
			new ScrollPic({ coords: '-30 -60', transitiontime: 6000 }),
			new ScrollPic({ coords: '-10 -600', transitiontime: 1000, holdtime: 20000 })
		);
		
		var ScrollPicFunctions = new Array();
		for (var i = 0; i < PictureCoords.length; i++) {
			var OptionsObj = PictureCoords[i].options;
			console.log('Adding:', OptionsObj);
			ScrollPicFunctions.push( function() {
				this.start(OptionsObj.coords, {duration: OptionsObj.transitiontime});
				console.log('Going to: '+OptionsObj.coords);
			}
			//.delay(OptionsObj.holdtime)
			);
			ScrollPicFunctions[i].delay(OptionsObj.holdtime);
		}
		console.log('Starting Fx', ScrollPicFunctions);
		myFx.start(PictureCoords[0].options.coords).chain( ScrollPicFunctions.each() );
		
		//var myFx = new Fx.Tween('myElement', {property: 'opacity'});
		//myFx.start(1,0).chain(
			//Notice that "this" refers to the calling object (in this case, the myFx object).
		//	function(){ this.start(0,1); },
		//	function(){ this.start(1,0); },
		//	function(){ this.start(0,1); }
		//);
	}
}

function InitializePhotoSlide() {
	var PicBoxObj = $('PhotoPan');
	if ( PicBoxObj ) {
		var myFx = new Fx.Tween(PicBoxObj, {property: 'background-position', duration: 0});
		
		var PictureCoords = new Array(
			{coords: '0 0', transitiontime: 2000, holdtime: 2000},
			{coords: '0 -568', transitiontime: 2000, holdtime: 1000},
			{coords: '-30 -60', transitiontime: 6000, holdtime: 1000},
			{coords: '-10 -600', transitiontime: 1000, holdtime: 200}
		);
		var currentpicindex = 0;
		
		var totaltime = 0;
		for (var i = 0; i < PictureCoords.length; i++) {
			totaltime+= PictureCoords[i].transitiontime + PictureCoords[i].holdtime;
		}
		
		var timecounter = 0;
		function PicTransition(TransDataObj) {
			//element = $(this);
				console.log('The current time is '+(timecounter / 10)+' seconds after load to: '+TransDataObj.coords);
				PicBoxObj.set('tween', {duration: TransDataObj.transitiontime})
				myFx.start(TransDataObj.coords, {duration: 50});//.chain(
		}
		
		function Pulse() {
			//element = $(this);
			var delaytime = 0;
			for (var i = 0; i < PictureCoords.length; i++) {
				delaytime+= PictureCoords[i].holdtime;
				//console.log(delaytime);
				//myFx.set('background-position', '100% 0');
				//element.set('tween', {property: 'background-color', duration: 1000})
				PicTransition.delay(delaytime, PicBoxObj, PictureCoords[i]);
				//	function(){ this.start(PictureCoords[nextpicindex]); }
				//);
				delaytime+= PictureCoords[i].transitiontime;
				//console.log(delaytime, PictureCoords[i]);
			}
			
			//	console.log(PictureCoords[currentpicindex], currentpicindex);
			timecounter = 0;
		}
		function Counter() {
			timecounter++;
			window.status = 'Current Second: '+(timecounter/10);
		}
	
			Counter();
			Counter.periodical(100);
			Pulse();
			Pulse.periodical(totaltime);
	}
}

function InitializeToggler() {
	var linkObj = new Element('a', { 'href': (HomepageAccordionActive ? '?acc=no' : '/'), 'text': (HomepageAccordionActive ? '[+] Expand All' : '[-] Collapse All') });
	var togglerObj = new Element('div', {'class': 'ToggleAccordion' });
	togglerObj.grab(linkObj);
	togglerObj.inject($$('.IndexFrame')[0], 'before');
}


//window.addEvent('load', function() {
	//if (HomepageAccordionActive) {
	//	InitializeAccordion();
	//}
	//window.alert('this is broken..,.');
	//InitializeToggler();
	//InitializePhotoSlide3();
//});
