var Site = {

  start: function() {
    Site.setAccordian();
    if ($('slider-nav')) {
      Site.setTheArea();
    }
    if ($('promo-trigger')) {
      Site.setPromo();
    }
    if ($('register-trigger')) {
      Site.setRegister();
    }
    if ($('intro')) {
      Site.setIntro();
    }
    if ($('slideshow')) {
      Site.setSlideshow();
    }
    if ($('map')) {
      $('map').fade('hide');
    }
    if ($('music')) {
      Site.setMusic();
    }
    if ($('directions')) {
      Site.setContactAccordian();
    }
  },
  
  setAccordian: function() {
    var myAccordion = new Accordion($$('.toggler'), $$('.element'), {
      alwaysHide: true,
      show: 10,
      opacity: false
    });
  },
  
  setContactAccordian: function() {
    togglers = $$('.locationToggler');
    for (var i=0; i < togglers.length; i++) {
      togglers[i].store('num', i);
      togglers[i].addEvent('click', function(e){
        e.stop();
  		  $$('.locationToggler').removeClass('current');
  			this.addClass('current');
  			$('directions').tween('margin-left', this.retrieve('num')*-516+'px');
      });
    }
  },
  
  setIntro: function() {
    var obj = new Swiff('intro.swf', {
        id: 'flashIntro',
        width: 948,
        height: 450,
        params: {
            wmode: 'opaque',
            bgcolor: '#000'
        }
    });
    $('flash').setProperty('html', '');
    obj.inject($('flash'));
  },
  
  setSlideshow: function() {
    var obj = new Swiff('/slideshow.swf', {
        id: 'flashIntro',
        width: 950,
        height: 300,
        params: {
            wmode: 'opaque',
            bgcolor: '#000'
        },
        vars: {
            header: $('slideshowType').src
        }
    });
    
    $('slideshow').setProperty('html', '');
    obj.inject($('slideshow'));
  },
  
  setPromo: function() {
    $('promo-trigger').addEvent('click', function(e){
  		e.stop();
  		if ($('register-slider')) {
        $('register-slider').tween('height', '0');
      }
  	  if ($('promo-slider').getStyle('height').toInt() < 100) {
    		$('promo-slider').tween('height', '900px');
  	  }
  	  else Site.closeSliders();
  		
  	});
    $('close-promo').addEvent('click', function(e){
      e.stop();
      Site.closeSliders();
    });
	},
	
	setRegister: function() {
    $$('a[href$="registration"], #register-trigger').addEvent('click', function(e){
  		e.stop();
  		if ($('promo-slider')) {
    		$('promo-slider').tween('height', '0');
  	  }
  	  if ($('register-slider').getStyle('height').toInt() < 100) {
    		$('register-slider').tween('height', '950px');
  	  }
  	  else $('register-slider').tween('height', '0');
  		
  	});
	},
	
	closeSliders: function() {
	  if ($('register-slider')) {
      $('register-slider').tween('height', '0');
    }
	  if ($('promo-slider')) {
      $('promo-slider').tween('height', '0');
    }
	},
  
  
  setTheArea: function() {
    var areas = $$('.area');
    areas.setStyle('display', 'none');
    areas.fade('hide');
    $('slider-close').fade('hide');
    $('slide-trigger').addEvent('click', function(e){
  		e.stop();
  	  if ($('slider').getStyle('width').toInt() < 100) {
    		$('slider-close').toggleClass('hidden');
    		$('slider-close').fade('1');
    		$('slider').tween('width', '182px');
  	  }
  	});
  	$('slider-close').addEvent('click', function(e){
  		e.stop();
  		$('slider-close').toggleClass('hidden');
  		$('slider-close').fade('hide');
  	  areas.fade('0');
  	  $('slider').tween('width', '0');
  	});
  	
  	$$('#slider-nav a').addEvent('click', function(e){
  	  e.stop();
  	  $$('#slider-nav a').removeClass('active');
  	  this.addClass('active');
  	  areas.setStyle('display', 'none');
  	  areas.fade('hide');
  	  var highlight = this.href.split('/');
  	  highlight = highlight[highlight.length -1];

      // display area
  	  $(highlight).setStyle('display', 'block');
  	  $(highlight).fade(1);
  		$('slider').tween('width', '516px');
  		
  		// add page up/down
  	  if ($(highlight).getChildren('.area-body-wrap')[0].getChildren('.area-body')[0].getSize().y.toInt() > 570) {
    	  var sliderUp = new Element('span', {'class': 'slider-up', 'text': 'more'});
      	var sliderDown = new Element('span', {'class': 'slider-down', 'text': 'less'});
      	sliderUp.clone().inject($(highlight), 'bottom');
    	  sliderDown.clone().inject($(highlight), 'bottom');    	
    	  Site.paginateAreas();
  	  }
  	  
  	});
	},
	
	paginateAreas: function(){
  	$$('.slider-up').addEvent('click', function(e){
  	  var body = this.getPrevious('.area-body-wrap').getChildren('.area-body')[0]
  	  var current = body.getStyle('margin-top').toInt();
  	  end = current - 580;
  	  if (end < - body.getDimensions().y.toInt()) {
  	    this.addClass('off');
  	  }
  	  else {
    	  body.tween('margin-top', end);  	    
  	  }
  	})
  	$$('.slider-down').addEvent('click', function(e){
  	  var body = this.getPrevious('.area-body-wrap').getChildren('.area-body')[0]
  	  var current = body.getStyle('margin-top').toInt();
  	  end = current + 580;
  	  if (end > 1) {
  	    this.addClass('off');
  	    end = 0;
  	  }
  	  body.tween('margin-top', end);  	    
  	})
  	
  },
  
  setMap: function() {
    
    ( function() {$('map').fade('1')}).delay(200);
    
    
    // hide small map on page load
    ( function() {$('map-small').tween('height', '320px')}).delay(3000);
    
    var myFx = new Fx.Morph($('map'), {duration: 3000, transition: Fx.Transitions.Sine.easeOut});
    myFx.start({
        'bottom': [55], 
        'right': [109]
    });
    
    // make map draggable
    (function() {var myMove = new Drag.Move($('map') , {'container': 'map-container'})}).delay(3100);

    
    // show small map
    $('open-map').addEvent('click', function(e) {
      e.stop();
      $('map-small').tween('height', '320px');
    })
    
    // hide small map and move big map
    $('map-small').addEvent('click', function(e) {
      e.stop();
			scale = 3;
      posX = e.client.x - $('map-small').getPosition().x;
      posX = posX * scale;
			posY = e.client.y - $('map-small').getPosition().y + 100;
      posY = posY * scale;
      myFx.start({
          'bottom': [posY], 
          'right': [posX]
      });
      
      // hide small map
      $('map-small').tween('height', '0');      
      
    });
    
    Site.setLotTips();    
    Site.setMapPopouts();
    Site.setMapPaginate();
    
  },
  
  setLotTips: function(){
    var tipz = new Tips('.lot a',{
      className: 'lottip',
      fixed: false,
      hideDelay: 50,
      showDelay: 100,
      offset: {'x': 15, 'y': -228}
    });

    // add nsf tooltips
    var nfstipz = new Tips('.blank a',{
      className: 'nfs-lottip',
      fixed: false,
      hideDelay: 50,
      showDelay: 100,
      offset: {'x': 15, 'y': -107}
    }); 
    
    // add sold tooltips
    var soldtipz = new Tips('.sold a',{
      className: 'sold-lottip',
      fixed: false,
      hideDelay: 50,
      showDelay: 100,
      offset: {'x': 15, 'y': -75}
    });
    
  },
  
  setMapPopouts: function(){
    
    $$('.popout-trigger').each(function(trigger) {
      var target = trigger.href.split('#')[trigger.href.split('#').length - 1];
      trigger.addEvent('click', function(e){
        e.stop();
      });
      trigger.addEvent('mouseenter', function(e){
        $(target).setStyle('display', 'block');
      });
      trigger.addEvent('mouseleave', function(e){
        $(target).setStyle('display', 'none');        
      });
    });
    
  },
  
  setMapPaginate: function(){
    var slider = new Element('span', {'class': 'paginate', 'text': 'more'});
  	slider.addEvent('click', function(e){
  	  e.stop();
  	  if ($('content-wrap').getStyle('margin-top').toInt() == 0) {
  	    end = '-700px';
  	    $$('.paginate')[0].set('text', 'top');
  	  }
  	  else {
  	    $$('.paginate')[0].set('text', 'more');
  	    end = 0;
	    }
  	  $('content-wrap').tween('margin-top', end);
  	});
  	slider.clone().cloneEvents(slider).inject($(sidebar), 'bottom');
	  
  },
  
  slideShow: function ( Obj ){  
      // Version 0.2; Created by Kow, 2008; http://skyweb.hu/kow  
      var pass = this;  
      // initializing  
      this.curImg = 0;  
      this.slides = Obj;  
      this.slides.each( function( img, index ){   
          if( index > 0 ) img.fade();   
          img.set('morph', { duration: 2000 });  
      }, this);  
      this.next = function(){  
          pass.curImg++;  
          this.slides.each( function( img, index ){   
              img.morph({ opacity: 0 });  
          }, this);  
          if( pass.curImg == this.slides.length ) pass.curImg = 0;  
          this.slides[ pass.curImg ].morph({ opacity: 1 });  
      }  
      this.prev = function(){  
          pass.curImg--;  
          this.slides.each( function( img, index ){   
              img.morph({ opacity: 0 });  
          }, this);  
          if( pass.curImg < 0 ) pass.curImg = this.slides.length - 1;  
          this.slides[ pass.curImg ].morph({ opacity: 1 });  
      }  
  },
  
  setMusic: function(){
    var music = new Element('embed', {'src': '/civilizations-lost.mp3', 'hidden': 'true', 'autostart': 'true',  'name': 'civilizations-lost', 'id': 'mp3'});
    if (Cookie.read('music') == 1) $('music').grab(music);
    else {
      $('music').toggleClass('off');
    }
    $('music').addEvent('click', function(e){
      e.stop();
      if ($('mp3')) {
        $('music').toggleClass('off');
        var myCookie = Cookie.write('music', 0, {duration: 999});
        $('mp3').destroy();
      }
      else {
        $('music').toggleClass('off');
        var myCookie = Cookie.write('music', 1, {duration: 999});
        $('music').grab(music);
      }
    })
  }

}

window.addEvent('domready', Site.start);

window.addEvent('load', function() {
  if ($('map')) {
    Site.setMap();
  }
  if ($$('.backandforth')[0]) {
    $$('.backandforth')[0].setStyle('height', $$('.backandforth img')[0].getStyle('height'));
    sshow = new Site.slideShow( $$('.backandforth img') );  
    // It will fade to the next frame every 3 seconds  
    sshow.next.periodical(5000, sshow );    
  }
});