// not ninjas stuff. All the other bits.
// Will Dayble this one time went back and time and did this one time again, because it was THAT good the first time.

window.addEvent('domready', function() { 

	// ---------------------------------
	// homepage
	// ---------------------------------	
	if ($chk($('homePage'))){
		
		// set up the shiz
		var conversation = [];
		$('homePage').getElement('ol').getElements('li').each(function(item, index){
			var o = {}
			o.a = item.getElement('em').get('html');
			o.q = item.getElement('strong').get('html');
			conversation.push(o);
		});
		
		// the conversation
		convNum = 0;
		function converse(){
			
			$('question').empty().set('html', conversation[convNum].q);
			$('answer').empty().set('html', conversation[convNum].a);

			convNum++;
			if (convNum == (conversation.length)){convNum = 0}

			//delay then do again (allowing for the delay above for answer)
			$clear(convPeriod);
			convPeriod = converse.periodical(6000);

		}
		
		// DO IT!
		var convPeriod = converse.periodical(5000);
	
	
		$('answer').addEvents({
			'mouseenter' : function(){
				$clear(convPeriod);
			},
			'mouseleave' : function(){
				convPeriod = converse.periodical(5000);
			}		
		});
	
	}
	

	// ---------------------------------
	//m1 dropdown menu action
	// ---------------------------------	
	if ($('m1')){

		var m1 = $('m1');
		m1.getElements('ul').each(function(ul){
			ul.getChildren('li').each(function(li){

			//nonIE
			if (!Browser.Engine.trident ){
				var op = .8;
				li.set('opacity', op);
				if ($chk(li.getElement('p'))){
						li.addEvents({
							'mouseenter': function(){
								li.fade(1);
								li.addClass('active');
							},
							'mouseleave': function(){
								li.fade(op);						
								li.removeClass('active');
							}					
						});
					}
				} else {
				//IE version
				if ($chk(li.getElement('p'))){
						li.addEvents({
							'mouseenter': function(){
								li.addClass('active');
							},
							'mouseleave': function(){
								li.removeClass('active');
							}					
						});
					}
				}//ie
			});
		});//ul
	}//m1


	// ---------------------------------
	// Popups
	// ---------------------------------	
	if ($('thePopup')){
	
		//setup
		if (!Browser.Engine.trident ){
			$('thePopup').setStyle('display','block');
			$('thePopup').fade('hide');		
		} else {
			$('thePopup').setStyle('display','none');		
		}
	
		/* close */
		$$('.closesPopup').each(function(el){
			el.addEvent('click', function(e){
				e.stop();
				closePopup();
			});		
		});
				
	}//popup
	

	// ---------------------------------
	// Close popup
	// ---------------------------------	
	function closePopup(){
		if (!Browser.Engine.trident ){
			$('thePopup').fade('out');
		} else {
			$('thePopup').setStyle('display', 'none');
		}
	}
	
	
	// ---------------------------------
	// start and stop spinner for popups
	// ---------------------------------	
	var startSpinner = function(){
		$('spinner').getElement('span').setStyle('display', 'block');
		$('spinner').fade('show');
		$('leftArrow').setStyle('display', 'none');		
		$('rightArrow').setStyle('display', 'none');
	}

	var stopSpinner = function(){
		$('spinner').getElement('span').setStyle('display', 'none');
		$('spinner').fade('out');
		$('leftArrow').setStyle('display', 'block');		
		$('rightArrow').setStyle('display', 'block');
	}	
	

	// ---------------------------------
	// load HTML data into a popup!
	// ---------------------------------
	function popupContentFill(link){
	  var myHTMLRequest = new Request.HTML({
	    'url': link,
	    'headers': {contentStyle: 'ajax'},
	    'onComplete': function(zing){
	    
			 	// ---------------------------------
				// Luke, should be binded or whatever,
				// I sux at scope. :(				
				// ---------------------------------
				$('thePopupContent').empty();
				$('thePopupContent').set('html', myHTMLRequest.response.html);

					
				// scroll images
				$$('.scrollableImages').each(function(theContainer){
				
					var cont = theContainer.getElement('ul');
					var anchors = cont.getElements('li');
					var contHeight = cont.getScrollSize().y;
					var i=0;
					var contFX = new Fx.Morph(cont, {duration: 400, transition: Fx.Transitions.Back.easeIn});
					var periodLength = 7000;
			
					function scrollImg(direction){
						if (direction == "up"){
							i--;
							if (i == -1){
								contFX.start({top: -(cont.getSize().y-375)});
								i = (anchors.length-1);
							} else{
								var newTop = cont.getStyle('top').toInt() + 375;
								contFX.start({top: newTop})
							}
						} else {
							i++;
							if (i == anchors.length){
								contFX.start({top: 0});
								i = 0;
							} else{
								var newTop = cont.getStyle('top').toInt() -375;
								contFX.start({top: newTop})
							}
						}
//						console.log(i);
					
					}
					
					// mouseovers and scroll
					anchors.each(function(li){
						li.addEvents({
							'click': function(e){
								e.stop();
								scrollImg('down');
							},
							'load': function(){
								li.getElement('span').fade('hide');	
							},
							'mouseenter': function(){
								li.getElement('span').fade('in');	
							},
							'mouseleave': function(){
								li.getElement('span').fade('out');	
							}
						});
					});
					
					// and some more clicks
					$('leftArrow').addEvent('click', function(e){
						e.stop();
						scrollImg('up');
					});
					
					$('rightArrow').addEvent('click', function(e){
						e.stop();
						scrollImg('down');
					});
				
				});	
			
				// and finally, close the spinner
				stopSpinner.delay(500);

	    },
	    'onRequest': startSpinner,
	    'onCancel': closePopup
	  }).get();
	  
	}
	
	
	// ---------------------------------
	// load HTML data into a popup!
	// ---------------------------------

	//ie6
	if (!((Browser.Engine.trident) && (Browser.Engine.version == 4)	)){
		
		$$('.ajaxLink').each(function(a){
			a.addEvent('click', function(e){
			
				e.stop();		
				
				popupContentFill(a.get('href'));
	
				$('thePopupBacker').setStyles({
					'display': 'block',
					'height': window.getScrollSize().y
				});
				
				if (!Browser.Engine.trident ){
					$('thePopup').fade('in');
				} else {
					$('thePopup').setStyle('display', 'block');
				}
		
				$('thePopup').setStyle('top', (a.getPosition().y - 600));
				// how's this again?			window.Scroll('thePopup');
	
	
			});
		});
	
	}//ie6 is a 
	
});

