/* js for core */
var core = function(){
  return {
		RENTAL_DISPLAY: 1,
		LIFESTYLE_DISPLAY: 2,
		EVENTS_DISPLAY: 4,
		MANAGEMENT_DISPLAY: 5,
		OUTDOOR_DISPLAY: 3,
		OASYS_DISPLAY: 6,
		HOME_DISPLAY: 7,
		SIX_DISPLAY: 8,
		IDLE: 0,
		
		inProgress: 0,
		frontToggled: false,
		swapInterval: 9000,
		imageCount: 6,
		userPaused: false,
		nextLock: false,
		loadDelay: 400,
		prevDelay: 100,
		imagesLocked: false,
		
	  init: function(){
			this.launchNav();
			this.launchHeader();
			this.setOnclickToggle();
			this.openOnloadContent();
			this.setSkiMapHandler();

			if(typeof IMAGE_COUNT != 'undefined'){
				this.imageCount = IMAGE_COUNT;
			}
			//document.body.oncontextmenu = function(){ return false; };
		},
		
		setSkiMapHandler: function(){
		  var el = $('show-ski-map');
			if(el != null){
			  el.observe('click', this.showSkiMap.bindAsEventListener(this));
				if(!$('ski-map')){
				  $('container').insert('<div id="ski-map"><div id="sm-close-button"></div><div class="sm-title">Ski Areas:</div><img src="/img/public/ski-area-map.gif" /></div>');
				}
				$('sm-close-button').observe('click', function(){
          new Effect.Fade('mask', {duration:.2});
			    new Effect.Fade('ski-map', {duration:.2});
				});
			}
		},
		
		showSkiMap: function(){
		  new Effect.Appear('mask', {duration:.2});
			new Effect.Appear('ski-map', {duration:.2});
			
		},
		
		setOnclickToggle: function(){
		  var els = $A($('container').select('.onclick-toggle'));
			if(els.length > 0){
			  els.each(function(el){
				  el.observe('click', toggleHeight);
				});
			}
		},
		
		openOnloadContent: function(){
		  var els = $A($('main').select('.open-onready'));
			if(els.length > 0){
			  els.each(function(el){
				  toggleHeight(el);
				});
			}
			var els2 = $A($('main').select('.open-onload'));
			if(els2.length > 0){
			  els2.each(function(el){
				  Event.observe(window, 'load', function(){ toggleHeight(el); });
				});
			}
		},
		
		launchHeader: function(){
			this.img1 = $('base-image');
			this.img2 = $('back-image');
			this.showing = this.img1;
			this.hidden = this.img2;
		
			this.pos = POS + 1;
			this.imageMarkers = $A($('image-position-display').select('.image-position-marker'));

			$('user-controls').show();
			$('user-pause').observe('click', this.userPause.bindAsEventListener(this));
			$('user-play').observe('click', this.userPlay.bindAsEventListener(this));
			$('user-next').observe('click', this.userNext.bindAsEventListener(this));
			$('user-prev').observe('click', this.userPrev.bindAsEventListener(this));
			
			this.setImageMarker(POS);
		  this.refreshInterval();
			
			if(ON_SIX == 1){
				this.runSixDisplay();
			}

		},
		
		refreshInterval: function(int){
			int = int || this.swapInterval;
		  clearTimeout(this.interval);
			this.interval = setTimeout(this.nextImage.bindAsEventListener(this), int);
		},
		
		nextImage: function(){
			if(this.imagesLocked == true){
			  return;
			}
			this.imagesLocked = true;
			if(this.showing == this.img1){
			  new Effect.Opacity('base-image', { from: 1, to: 0, duration: 0.6, afterFinish: this.loadNext.bindAsEventListener(this) });
		  } else {
			  new Effect.Opacity('base-image', { from: 0, to: 1, duration: 0.6, afterFinish: this.loadNext.bindAsEventListener(this) });
		  }
			this.setImageMarker();
			if(this.userPaused != true){
		    this.refreshInterval();
			}
		},
		
		setImageMarker: function(pos){
		  this.imageMarkers.each(function(el){
        el.removeClassName('on');
			});
			pos = pos || this.pos;
			$('ip-marker-'+pos.toString()).addClassName('on');
		},
		
		userNext: function(){
		  //this.userPaused = false;
			if(this.nextLock == true){
			  return;
			}
			this.nextLock = true;
			$('user-next').addClassName('pressed');
			setTimeout(this.nextUnlock.bindAsEventListener(this), 1200);
			this.nextImage();
		},
		
		nextUnlock: function(){
		  this.nextLock = false;
			$('user-next').removeClassName('pressed');
			$('user-prev').removeClassName('pressed');
		},
		
		prevImage: function(){
			//this.swapShowing();
			var src = this.hidden.src.split('/header/');
		  var newSrc = src[0] + '/header/';
			
			this.pos -= 2;
			
			if(this.pos < 1){
				this.pos = 1;
			}
			var path = this.pos + '.jpg';
		  newSrc += path;
		  this.hidden.src = newSrc;

			setTimeout(this.nextImage.bindAsEventListener(this), this.prevDelay);
			if(this.userPaused != true){
		    this.refreshInterval();
			}
		},
		
		userPrev: function(){
		  //this.userPaused = false;
  		if(this.nextLock == true){
			  return;
			}
			this.nextLock = true;
			$('user-prev').addClassName('pressed');
			setTimeout(this.nextUnlock.bindAsEventListener(this), this.loadDelay);
			this.prevImage();
		},
		
		swapShowing: function(){
		  this.hidden = this.showing == this.img1? this.img1 : this.img2;
		  this.showing = this.showing == this.img1? this.img2 : this.img1;
		},
		
		loadNext: function(){
			this.imagesLocked = false;
			this.swapShowing();
			
			var src = this.hidden.src.split('/header/');
		  var newSrc = src[0] + '/header/';
			
			this.pos++;
			
			if(this.pos > this.imageCount){
				this.pos = 1;
			}
			var path = this.pos + '.jpg';
		  newSrc += path;
		  this.hidden.src = newSrc;
		},
		
		pause: function(){
		  clearTimeout(this.interval);
		},
		
		userPause: function(){
		  this.userPaused = true;
			$('user-pause').hide();
			$('user-play').show();
			this.pause();
		},
		
		play: function(){
			if(this.userPaused == true) return;
		  this.refreshInterval();
		},
		
		userPlay: function(){
			this.userPaused = false;
			$('user-play').hide();
			$('user-pause').show();
		  this.refreshInterval(400);
		},

    toggleFront: function(){
		  if(!$('front').hasClassName('short')){
				$('front').addClassName('short');
				this.frontToggled = true;
				new Effect.Scale($('front'), 60, { duration: .3, scaleContent: true, scaleX: false});//
			} else {
				$('front').removeClassName('short');
				this.frontToggled = false;
				new Effect.Scale($('front'), 153, { duration: .3, scaleContent: true, scaleX: false});//
			}
		},
		
		launchNav: function(){
		  document.observe('mousemove', this.mouseMove.bindAsEventListener(this));
		},
		
		mouseMove: function(e){
		  var el = Event.element(e);
			if(el.id == 'nav' || el.descendantOf('nav')){
			  this.runNav(el);
			} else if(el.id == 'front' || el.descendantOf('front')){
			  // do nothing?
			} else {
			  this.revertNav();
			}
			/*if((el.id == 'main' || el.descendantOf('main'))){
				if(this.frontToggled == false){
					this.toggleFront();
				}
			} else {
				if(this.frontToggled == true){
					this.toggleFront();
				}
			}*/
		},
		
		revertNav: function(){
			if(this.locked == true || this.inProgress == this.IDLE) return;
			if(ON_SIX == 1){
				this.runSixDisplay();
			} else {
		    // set everything back to normal
			  this.zeroIndex();
			  this.doAppear('default-display');
			  this.play();
			  this.inProgress = this.IDLE;
			}
		},
		
		clear: function(){
			if(this.locked == true) return;
			this.zeroIndex();
			this.lock();
		  //new Effect.Fade('default-display',{ duration: .01, afterFinish: this.unlock.bindAsEventListener(this) });
		},
		
		lock: function(){
		  this.locked = true;
		},

		unlock: function(){
		  this.locked = false;
		},

		runNav: function(el){
			if(this.locked == true) return;
		  el = $(el); // IE probably loses the extended element here
			if(el.tagName == 'A'){
			  switch(el.name){
				  case 'home':
					  this.pause();
            this.runHomeDisplay();
						return;
					case 'events':
					  this.pause();
					  this.runEventsDisplay();
					  break;
					case 'oasys':
					  this.pause();
					  this.runOasysDisplay();
					  break;
					case 'property':
					  this.pause();
					  this.runManagementDisplay();
					  break;
					case 'rental':
					  this.runRentalDisplay();
					  break;
					case 'lifestyle':
					  this.pause();
					  this.runLifestyleDisplay();
					  break;
					case 'outdoor':
					  this.pause();
					  this.runOutdoorDisplay();
					  break;
					case 'six':
					  this.pause();
					  this.runSixDisplay();
					  break;
					default:
					  this.revertNav();
				}
			} else {
			  // hmmm.
			}
		},
		
		zeroIndex: function(){
		  $('home-display').hide();
			$('rental-display').hide();
			$('lifestyle-display').hide();
			$('outdoor-display').hide();
			$('management-display').hide();
			$('events-display').hide();
			$('oasys-display').hide();
			$('six-display').hide();
			$('default-display').hide();
		},
		
		doAppear: function(id){
		  this.lock();
			new Effect.Appear(id, {duration: .15, afterFinish: this.unlock.bindAsEventListener(this) });
		},
		
		runEventsDisplay: function(){
			if(this.inProgress == this.EVENTS_DISPLAY) return;
			this.clear();
			this.inProgress = this.EVENTS_DISPLAY;
			this.doAppear('events-display');
		},
		
		runHomeDisplay: function(){
			if(this.inProgress == this.HOME_DISPLAY) return;
			if(this.inProgress != this.HOME_DISPLAY && PAGE == 'b'){
				this.revertNav();
				return;
			}
			this.clear();
			this.inProgress = this.HOME_DISPLAY;
			this.doAppear('home-display');
		},
		
		runOasysDisplay: function(){
			if(this.inProgress == this.OASYS_DISPLAY) return;
			this.clear();
			this.inProgress = this.OASYS_DISPLAY;
			this.doAppear('oasys-display');
		},
		
		runManagementDisplay: function(){
			if(this.inProgress == this.MANAGEMENT_DISPLAY) return;
			if(this.inProgress != this.MANAGEMENT_DISPLAY && PAGE == 'property'){

			}
		  if(this.inProgress == this.MANAGEMENT_DISPLAY) return;
			this.clear();
			this.inProgress = this.MANAGEMENT_DISPLAY;
			this.doAppear('management-display');
		},
		
		runRentalDisplay: function(){
			if(this.inProgress == this.RENTAL_DISPLAY) return;
			if(this.inProgress != this.RENTAL_DISPLAY && PAGE == 'rental'){

      }
						
		  if(this.inProgress == this.RENTAL_DISPLAY) return;
			this.clear();
			this.inProgress = this.RENTAL_DISPLAY;
			this.doAppear('rental-display');
		},
		
		runLifestyleDisplay: function(){
		  if(this.inProgress == this.LIFESTYLE_DISPLAY) return;
			if(this.inProgress != this.LIFESTYLE_DISPLAY && PAGE == 'lifestyle'){

      }
			this.clear();
			this.inProgress = this.LIFESTYLE_DISPLAY;
			this.doAppear('lifestyle-display');
		},
		
		runOutdoorDisplay: function(){
		  if(this.inProgress == this.OUTDOOR_DISPLAY) return;
			if(this.inProgress != this.OUTDOOR_DISPLAY && PAGE == 'outdoor'){

			}
			this.clear();
			this.inProgress = this.OUTDOOR_DISPLAY;
			this.doAppear('outdoor-display');
		},
		
		runSixDisplay: function(){
			if(this.inProgress == this.SIX_DISPLAY) return;
			if(this.inProgress != this.SIX_DISPLAY && PAGE == 'six'){

      }

			this.clear();
			this.inProgress = this.SIX_DISPLAY;
			this.doAppear('six-display');
		}
	}
}();
if(document.all){
Event.observe(window, 'load', core.init.bindAsEventListener(core));
} else {
Event.observe(window, 'dom:loaded', core.init.bindAsEventListener(core));
}

function toggleHeight(e){
	if(typeof e.tagName == 'undefined'){
		el = Event.element(e);
		if(!el.hasClassName('onclick-toggle')){
		  return;
		}
	} else {
		el = $(e);
	}
	if(el.className == 'map-container') return;
	if(el.$locked == true) return;
	el.$locked = true;
  var h = el.down().getHeight() + 4;
  el.toggleClassName('show');
	if(el.hasClassName('show')){
    new Effect.YChange(el, { y: h, duration: .7, afterFinish: function(){ el.$locked = false; } });
	} else {
	  new Effect.YChange(el, { y: -h, duration: .45, afterFinish: function(){ el.$locked = false; }  });
	}
}

Effect.YChange = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = arguments[1] || { };
    this.start(options);
  },
  setup: function() {
    this.originalHeight = parseFloat(this.element.getStyle('height') || '0');
  },
  update: function(position) {
    this.element.setStyle({
      height: ((this.options.y  * position) + this.originalHeight).round() + 'px'
    });
  }
});