// hush.com
// paul shand @ touch

// general stuff

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// mootools stuff


// long winded way of positioning leaf graphics to get around IE z-index bug

var repositionLeafs = function(){
	repositionTopLeafs();
	repositionBottomLeafs(0);
}

var repositionTopLeafs = function(){
	var fullImageX = $('wrapperAll').getPosition();
	fullImageX = "-"+fullImageX.x+"px";
	$('leafTop').setStyles({left: fullImageX, display:'block'});
	
	return fullImageX;
}

var repositionBottomLeafs = function(offset){
	var fullImageX = $('wrapperAll').getPosition();
	fullImageX = "-"+fullImageX.x+"px";
	
	var windowWidth = window.getWidth();
	windowWidth = windowWidth+"px";
	
	var windowHeight = window.getHeight();
	windowHeight -= 208-offset;
	windowHeight = windowHeight+"px";
	
	$('leafBottom').setStyles({top: windowHeight, right: fullImageX, display:'block'});
}

window.addEvent('resize', repositionLeafs);


// reveal elements on page load

var showAll = function(){
	
	$('wrapperContent').setStyle('background-position', '0px 0px');
	$('fullImage').setStyles({display:'block', opacity: 0});
	$('wrapperContent').setStyles({display:'block', opacity: 0, top: 0});
	
	var x = new Chain();
	var myFx = new Fx.Tween('fullImage',{duration: 600});
	var myFx2 = new Fx.Morph('wrapperContent',{duration: 800});
	var myFx3 = new Fx.Tween('leafTop',{duration: 400});
	var myFx4 = new Fx.Tween('leafBottom',{duration: 400});
	
	fullImageX = repositionTopLeafs();
	repositionBottomLeafs(208);
	
	// get size of full image and wrapper for positioning of wrapper
	var heightWrapper = $('wrapperContent').getStyle('height');
	heightWrapper= heightWrapper.replace(/px/, "");
	parseInt(heightWrapper);
	heightWrapper=(Math.ceil(heightWrapper/2));
	
	var topFullImage = $('fullImage').getStyle('top');
	topFullImage= topFullImage.replace(/px/, "");
	parseInt(topFullImage);
	topFullImage=(Math.ceil(topFullImage/2));
	
	var heightFullImage = $('fullImage').getStyle('height');
	heightFullImage= heightFullImage.replace(/px/, "");
	parseInt(heightFullImage);
	heightFullImage=(Math.ceil(heightFullImage/2));
	
	var wrapperY = heightFullImage-heightWrapper-30;
	
	
	var one = function() { 
		myFx.start('opacity', 0, 1);
  };
	
	var two = function() { 
		myFx2.start({
			'opacity': [0, 1],
			'top': [-heightWrapper, wrapperY]
		});
  };
	
	var windowHeight = window.getHeight();
	windowHeightOffset = windowHeight;
	windowHeightOffset = windowHeightOffset+"px";
	windowHeight -= 208;
	windowHeight = windowHeight+"px";
	
	var three = function() { 
		myFx3.start('top', -247, 0);
		myFx4.start('top', windowHeightOffset, windowHeight);
  };
	
	x.chain(one); 
  x.chain(two); 
	x.chain(three); 
  x.callChain(); 
  x.callChain.delay(500, x); 
	x.callChain.delay(1500, x);
	
	//$('fullImage').tween('left', [-950, 0]);
};


window.addEvent('load', function(){
									 
	showAll();

}); 

window.addEvent('domready', function() {
																		 
	$('wrapperContent').setStyles({display:'none', opacity: 0});
	
	// nav fade on mouseover and mouseout only for inactive links
	
	$$('#nav a.inactive').each(function(element) {
																			
		element.addEvent('mouseover', function(){
			this.tween('color', '#FFC425');
		});
		
		element.addEvent('mouseout', function(){
			this.tween('color', '#D9D9D9');
		});
		
	});
	
	// draggable text area, disabled for some pages
		
	if (sectionID != 'signup' && sectionID != 'news') {
	
		//$('wrapperContent').setStyle('cursor', 'pointer');
		
		var wrapperDrag = new Drag.Move('wrapperContent', {
	 
			container: 'fullImage',
			
			onStart: function(el){
					
			},
			
			onComplete: function(el){
					var elTop = el.getStyle('top');
					var myCookie = Cookie.write("wrapperPosition", elTop);
			}
	 
		});
	
	}
	
	// add .detach() to the above instance for the following to activate drag on element event
	
	/*var dragEl = $('drag');

	dragEl.addEvent('click', function(e){
		e.stop();
		dragEl.tween('color', '#FFC425');
	});
		
	dragEl.addEvent('mouseover', function(){
		wrapperDrag.attach();
		this.tween('color', '#FFC425');
	});
	
	dragEl.addEvent('mouseout', function(){
		wrapperDrag.detach();
		this.tween('color', '#D9D9D9');
	});*/
	
	// hide main text area and animate toggle button state
	
	var toggleEl = $('toggle');
	
	var wrapperMorph = new Fx.Morph('wrapperContent', { 'duration': 800 });

	toggleEl.addEvent('click', function(e){
		e.stop();
		var status = this.get('html');
		if (status == 'Show Text') {
			$('content').fade('in');
			this.set('html', 'Hide Text');
			wrapperMorph.start({ 'background-position': '0px 0px' });
		} else {
			this.set('html', 'Show Text');
			$('content').fade('out');
			$('wrapperContent').setStyle('background-position', '0px 0px');
			wrapperMorph.start({ 'background-position': '0px -740px' });
		}
		
		toggleEl.tween('color', '#FFC425');
		//setupAccordion();
	});
		
	
	// news page accordion
	
	function setupNewsAccordion() {
		
			var mainImage = $('fullImage');
			var currentImage = 0;
			mainImage.setStyle('background-image', 'url('+newsImageArray[currentImage]+')');
			mainImage.setStyle('background-repeat', 'repeat');
			
			function swapImage() {
			
			
				var myFxOut = new Fx.Tween(mainImage, {property: 'opacity', duration: 500, onComplete: setNewBackgroundImage});
				var myFxIn = new Fx.Tween(mainImage, {property: 'opacity', duration: 700});
				
				function setNewBackgroundImage() {
					
					mainImage.setStyle('background-image', 'url('+newsImageArray[currentImage]+')');
					myFxIn.start(0, 1);
				}
							
				myFxOut.start(1, 0);
				
			}
			
			var elementIdentifier = 0 ;
			
			$$('.toggler').each(function(element) {
																				
				element.name = elementIdentifier;
				elementIdentifier++;
				
				element.addEvent('mouseover', function(e){
					this.tween('background-position', 0);
					var size = this.getSize();
				});
				
				element.addEvent('mouseout', function(e){
					this.tween('background-position', -1050);
				});
				
				element.addEvent('click', function(e){
					currentImage = element.name;
					swapImage();
				});
			
			});
		
			var accordion = new Accordion($$('.toggler'),$$('.element'), {
			opacity: 0,
			show: 0,
			onActive: function(toggler) { 
				toggler.setStyle('color', '#FFC425');
				
			},
			onBackground: function(toggler) {
				toggler.setStyle('color', '#FFC425');
				
			}
		});
	
	}
	
	if (typeof(newsImageArray)!='undefined') {
		setupNewsAccordion();
	}
	
	// gallery page functions
	
	function gallery() {
		
		var mainImage = $('content');
		var elLeft = $('galleryLeft');
		var elRight = $('galleryRight');
		var elCount = $('galleryCount');
		
		var currentImage = 0;
		
		mainImage.setStyle('background-image', 'url('+galleryImageArray[currentImage]+')');
		
		elLeft.set('html', 'Previous Image');
		elRight.set('html', 'Next Image');
		
		function updateCount() {
			var count = currentImage+1;
			elCount.set('html', count+' of '+galleryImageArray.length);
		}
				
		function swapImage() {
			
			
			var myFxOut = new Fx.Tween(mainImage, {property: 'background-position', duration: 500, onStart: disableNav, onComplete: setNewBackgroundImage});
			var myFxIn = new Fx.Tween(mainImage, {property: 'background-position', duration: 700, onComplete: enableNav});
			
			function setNewBackgroundImage() {
				
				mainImage.setStyle('background-image', 'url('+galleryImageArray[currentImage]+')');
				myFxIn.start(-780, 0);
			}
						
			myFxOut.start(0, -780);
														
		}
		
		function enableNav() {
			
			updateCount();
			elLeft.setStyle('cursor', 'pointer');
			elRight.setStyle('cursor', 'pointer');
			elLeft.tween('color', '#FFFFFF');
			elRight.tween('color', '#FFFFFF');
			elCount.tween('color', '#FFFFFF');
			
			elLeft.addEvent('mouseover', function(e){
				this.tween('color', '#FFC425');
				e.stop();
			});
		
			elLeft.addEvent('mouseout', function(e){
				this.tween('color', '#FFFFFF');
				e.stop();
			});
		
			elRight.addEvent('mouseover', function(e){
				this.tween('color', '#FFC425');
				e.stop();
			});
		
			elRight.addEvent('mouseout', function(e){
				this.tween('color', '#FFFFFF');
				e.stop();
			});
		
			elLeft.addEvent('click', function(e){
				e.stop();
				if (currentImage == 0) {
					currentImage = images.length;
				}
				currentImage --;
				swapImage();
			});
		
			elRight.addEvent('click', function(e){
				e.stop();
				if (currentImage == images.length -1) {
					currentImage = 0;
				} else {
					currentImage ++;
				}
				swapImage();
			});
		}
		
		function disableNav() {
			
			elLeft.removeEvents();
			elRight.removeEvents();
			
			elLeft.setStyle('cursor', 'default');
			elRight.setStyle('cursor', 'default');
			elLeft.tween('color', '#333333');
			elRight.tween('color', '#333333');
			elCount.tween('color', '#333333');
			
		}
		
		function initGalleryNav() {
			
			enableNav();
			updateCount();
			
		}
		
		initGalleryNav();
		
	}
	
	if (typeof(galleryImageArray)!='undefined') {
		var images = new Asset.images(galleryImageArray, {onComplete: gallery});
	}
		
});