// general stuff

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// reveal elements on page load

var heightWrapper2=0;
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: 900});
	var myFx2 = new Fx.Morph('wrapperContent',{duration: 1000});
	
	fullImageX = 0;
	
	// get size of full image and wrapper for positioning of wrapper
	var heightWrapper = $('wrapperContent').getStyle('height');
	heightWrapper2 = $('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";
	
	x.chain(one); 
    x.chain(two); 
    x.callChain(); 
    x.callChain.delay(500, x); 
	x.callChain.delay(1500, x);
};


window.addEvent('load', function(){
		
    shaun();						 
	showAll();

}); 

//window.addEvent('domready', function() {

	function shaun() {																	 
	$('wrapperContent').setStyles({display:'none', opacity: 0});
	
	// nav fade on mouseover and mouseout only for inactive links
	
	$$('#nav a').each(function(element) {
																			
		element.addEvent('mouseover', function(){
			this.tween('color', '#af1203');
		});
		
		element.addEvent('mouseout', function(){
        	if (this.className!="current") {
				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);
			}
	 
		});
	
	}
    if ($defined($('txtNameFirst'))) {
        $('txtNameFirst').addEvent('mousedown',function(e){
            e.stopPropagation();
        })
    }
    if ($defined($('txtNameLast'))) {
        $('txtNameLast').addEvent('mousedown',function(e){
            e.stopPropagation();
        })
    }
    if ($defined($('txtEmail'))) {
        $('txtEmail').addEvent('mousedown',function(e){
            e.stopPropagation();
        })
	}
	// hide main text area and animate toggle button state
	
	var toggleEl = $('toggle');
	
	var wrapperMorph = new Fx.Morph('wrapperContent', { 'duration': 500, 'link': 'chain'});
   
	var contentFade = new Fx.Morph('content', { 'duration': 500, 'link': 'chain'});
    
    wrapperMorph.addEvent('complete', function() {
		if ($('wrapperContent').getStyle('top').slice(0,3) > 370) {
            wrapperMorph.start({ 'top': '370px'});
        }
	});

	toggleEl.addEvent('click', function(e){
		e.stop();
        
		var status = this.get('html');
		if (status == 'Show Text') {
			this.set('html', 'Hide Text');
            // opening
            var showChain = new Chain();
	
            var showContent = function() { 
            	contentFade.start({ 'opacity': 1 });
            };
                
            var ResizeContent = function() { 
            	if ($('wrapperContent').getStyle('top').slice(0,3) > 370) {
                	wrapperMorph.start({ 'top': '370px', 'height': heightWrapper2+'px' });
               	} else {
            		wrapperMorph.start({ 'height': heightWrapper2+'px' });
                }
            };
            showChain.chain(ResizeContent); 
            showChain.chain(showContent); 
            showChain.callChain(); 
    		showChain.callChain.delay(500, showChain); 
            
		} else {
        	// closing
			this.set('html', 'Show Text');
            
            var hideChain = new Chain();
	
            var hideContent = function() { 
            	contentFade.start({ 'opacity': 0 });
            };
                
            var ResizeContent = function() { 
            	wrapperMorph.start({ 'height': '47px' });
            };
            hideChain.chain(hideContent); 
            hideChain.chain(ResizeContent); 
            hideChain.callChain(); 
    		hideChain.callChain.delay(500, hideChain); 
		}
		
		toggleEl.tween('color', '#D9D9D9');
	});
	
	// 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});
	}
	}	
//});
