

var NonFiction = Class.create({
  initialize : function(){
    this.setup_portfolio();
    this.setup_featured();
    this.setup_recent();
    this.setup_archive();
    this.setup_map();
    this.setup_bg();
    this.setup_font();
    this.setup_grids();
    this.nav_hover();
    
  },
  
  setup_grids: function(){
    if($('grids')){
     //get the images
     var thumbs = $$('.gridthumb');
     for (var i=0; i < thumbs.length; i++) {
      thumbs[i].observe('mouseover', function(event){
        Event.element(event).morph('width:210px; margin:30px;',{duration: .25, transition: 'easeInOutQuint'})
        });
      thumbs[i].observe('mouseleave', function(event){
        Event.element(event).morph('width:200px; margin:10px;',{duration: .25, transition: 'easeInOutQuint'})
        });
        
     }; 
    }
  },
  
  setup_featured: function(){
    var t = this;
    var project_container = $$('.home #featured_projects').first();
    var featured_items = $$('.home .featured_project');
    var featured_items_count = featured_items.length - 1;
    if(project_container){
      var pos = 0;
       var slideshow = new PeriodicalExecuter(function(i){
          this.rotate_featured(pos);
          pos = this.adjust_counter(pos, featured_items_count);
        }.bind(this, pos, featured_items_count), 14);
      
      $('arrow_right').observe('click', function(e){
        pos = (pos != featured_items_count)?pos+1:0;
         t.rotate_featured(pos);
         e.stop();
      });      
      
      $('arrow_left').observe('click', function(e){
        pos = (pos == 0)?featured_items_count:pos-1;
         t.rotate_featured(pos);
         e.stop();
      });

      
    }
  },
  
  rotate_featured: function(position){

    // var featured_items = $$('.home .featured_project');
    // var outgoing = featured_items[position - 1];
    // var incoming = featured_items[position];
    // if(outgoing){
    //   var text = outgoing.select('.featured_content').first();
    //   var image = outgoing.select('.home_featured_image').first();
    //   // text.morph('opacity: 0;', 'slow', function(){ outgoing.fade() });
    //   outgoing.fade();
    // }
    // if(incoming){
    //  incoming.appear(); 
    // }
    var pixel_position = position * 960;
    $('featured_projects_carousel').morph('left: -'+pixel_position+'px', { transition: 'easeOutExpo' })
  },
  
  adjust_counter: function(pos, featured_items_count){
    if(pos < featured_items_count){
      pos++;
    }else{
      pos = 0;
    }
    return pos;
  }, 

  setup_portfolio: function(){
    //alert('wtf');
    //case study
    var t = this;
    if($('read')){
      $('read').observe('click', function(e){
        if($('project_thumbs')){
          $('project_thumbs').toggle().morph('left:-400px;', 0.3);
          $('view').morph('opacity:1.0;z-index:10;',0.25);
          $('read').morph('opacity:0.0;z-index:1;',0.25);
          $('case_study').toggle();
        }
        $('case_study').morph('left:0px;', 0.3);
      });
    }
      if($('view')){
      $('view').observe('click', function(e){
        $('project_thumbs').toggle().morph('left:0px;', 0.3);
        if($('case_study')){
          $('case_study').toggle().morph('left:-400px', 0.3);          
          $('read').morph('opacity:1.0;z-index:10;',0.25);
          $('view').morph('opacity:0.0;z-index:1;',0.25);
        }
      });
    }
    
    //thumbnails
    this.project_thumbs = $$('.project_thumbs img');
    this.project_thumbs.each(function(img) {
      img.observe('click', function(e){
        this.rotate_portfolio(img);
        e.stop();
      }.bind(this));
    }.bind(this));   
     
  },
  
  rotate_portfolio: function(img){
    var featured_image_container = $('featured_image');
    var featured_image_elem = featured_image_container.select('img').first();
    this.project_thumbs.each(function(thumb) { thumb.removeClassName('active'); });
    img.addClassName('active')
    $('loading').morph('opacity: 0.5; top: 210px;', 'slow');
    featured_image_container.morph('opacity: 0;', function(){ featured_image_elem.src = null; featured_image_elem.setStyle('margin-top: 0px;'); featured_image_elem.src = img.getAttribute('rel'); });
    
    var check = new PeriodicalExecuter(function(i) {
      if (!(typeof featured_image_elem.naturalWidth != "undefined" && featured_image_elem.naturalWidth == 0)) {
        $('loading').morph('opacity: 0; top: 200px', 'fast');
        featured_image_container.morph('opacity: 1');
        featured_image_elem.morph('margin-top: -5px;', 'slow')
        check = null;
      }
    }.bind(this, check), 1);
  },
  
  setup_tooltip: function(){
    $$('.recent_project .tooltip').invoke('appear');
    $$('.recent_project').each(function(project) {
      var tip = project.select('.tooltip').first();
      tip.setStyle('display: none;')
      // project.observe('mouseover', function(e){ tip.appear(); tip.morph({opacity: 1, top: '-190px'}, 'slow'); e.stop(); });
      // project.observe('mouseout', function(e){ tip.morph({opacity: 0, top: '-180px'}, 'fast'); tip.hide(); e.stop(); });
    });    
  },
  
  setup_recent: function(){
    
    $$('.recent_projects_scroller').each(function(scroller) {
      var scroller_list = $('recent_projects_carousel');
      var pos = 0;
      var width = 240;
      var amount = 0;
      var total = scroller.select('li').length;
      $$('.arrow').each(function(arrow) {
        arrow.observe('click', function(e){
          var elem = e.findElement();
          var direction = elem.hash == '#forward' ? '-1' : '1';
          if(direction == '-1'){ pos++ }else{ pos-- }
          if(pos < 0 || pos == total){pos = 0}
          
          var calc_pos = pos * width;
          var adjusted_amount = calc_pos;
          // console.log(adjusted_amount)

          scroller_list.morph('margin-left: -'+ adjusted_amount +'px');
        });
      });
    });
    
  },
  
  setup_map: function(){
    var map_link = $('map_view_link');
    var street_link = $('street_view_link');
    if(map_link && street_link){
      map_link.observe('click', function(e){$('street_view').morph('margin-top: -200px'); map_link.toggleClassName('active'); e.stop();})
      street_link.observe('click', function(e){$('street_view').morph('margin-top: 0px'); street_link.toggleClassName('active'); e.stop();})
    }
  },
  
  setup_bg: function(){
    
  },

  setup_font: function(){
    // $$("h1").each(function(h){
    //   h.observe("mouseover", function(e){Event.element(e).morph('color:#DF8719',{duration: 1, transition: 'easeOutQuad'})})
    //   h.observe("mouseleave", function(e){Event.element(e).morph('color:#222',{duration: 1, transition: 'easeOutQuad'})})
    // })
  },

  
  setup_archive: function(attribute){
    
  },
  
  nav_hover: function(){
    $$("#nav li a").each(function(tag){
      tag.old_title = tag.title;
      Event.observe(tag, 'mouseover', function() { tag.title="" }, true);
      Event.observe(tag, 'mouseout', function() { tag.title=tag.old_title }, true);
    })
  }
  
});

// Hover Behaviour for Nav etc.
var HoverBehavior = Class.create();
HoverBehavior.prototype = {
    initialize: function() {
        $A(document.styleSheets).each( function(stylesheet) {
        $A(stylesheet.rules).each( function(rule) {
        if( rule.selectorText.match(/:hover/i) ) {
            stylesheet.addRule( rule.selectorText.replace(/:hover/ig, '.hover'), rule.style.cssText );
        }
        });
    });

    $A(arguments).each( function(arg) {
        $$(arg).each( function(tag) {
            Event.observe(tag, 'mouseenter', function() { Element.addClassName(tag, 'hover'); }, true);
            Event.observe(tag, 'mouseleave', function() { Element.removeClassName(tag, 'hover'); }, true);
            });
        });
    }
};

Event.observe(window, 'load', function(){
  var nf = new NonFiction(); 
  if (Prototype.Browser.IE) {
    $$('#nav li:last-child').each( function(tag) { tag.addClassName('last-child'); });
    $$('.nav_band').each(function(tag){tag.observe('click', function(){window.location = this.down('a').href;})});
      new HoverBehavior('#nav li'); // keep at the end!
  }
  
});


