var textEffects = Class.create({
  initialize : function(){
    Cufon.set('fontFamily', 'copperplate').replace('#nav > ul > li > a');
    Cufon.set('fontFamily', 'copperplate2').replace('#nav > ul > li > a')('#content h1')(".biography h2");
    
    
    
    // surf-to-edit fix for banners
    $$('#banner-slideshow .pagecontent').each(function(div){
        div.setStyle({position:'relative', 'z-index':20 })
        div.down('div').setStyle({position:'absolute', 'z-index':50, left:'10px', top:'10px' })
    })
    
    this.adjust_navigation();
  },
  adjust_navigation : function(){
    //adjust widths of sub nav to longest item || max defined
    var last_subnav_items = $$('#nav ul li ul :last-child').invoke('addClassName','last');
    var second_tier_containers = $$('#nav > ul > li > ul').each(function(container){
      if(container.getWidth() < 160){
        container.setStyle({width:"190px"});
      }else{
        container.setStyle({width:"190px"});
        
        //container is same size as parent and > than 160px. Set class to remove top right rounded corner
        container.addClassName("squared");
      }
    })
  }
});




// 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);
      });
    });
  }
};


var tf = null;

document.observe('dom:loaded', function() {
  // Don't do tabs if in surf-to-edit
  if (!location.href.match(/\/nterchange/g)) {
    var t = new Tabs('#main .tab');
  }

  if(Prototype.Browser.IE){
    var navHover = new HoverBehavior("#nav li");
  }
  tf  = new textEffects();
   Cookie.init({name: 'trilogy'});
  if(Cookie.getData('fontsize') == 'larger'){
    $$('body')[0].addClassName("larger");
  }
  
  
  Event.observe($('font-resizer'),'click',function(){
    $$('body')[0].toggleClassName("larger");
    if(Cookie.getData('fontsize') == 'larger'){
      Cookie.setData('fontsize', "");
    }else{
      Cookie.setData('fontsize', "larger");
    }
  })
  
  //disable right clicks for fun, profit and great justice
  var context_menu=new Proto.Menu({
      selector: 'body', 
      className: 'menu desktop', 
      menuItems: [] 
    });

});


var Cookie = {
  data: {},
  options: {expires: 1, domain: "", path: "", secure: false},

init: function(options, data) {
  Cookie.options = Object.extend(Cookie.options, options || {});

  var payload = Cookie.retrieve();
        if(payload) {
            Cookie.data = payload.evalJSON();
        }
        else {
            Cookie.data = data || {};
        }
        Cookie.store();
    },
    getData: function(key) {
        return Cookie.data[key];
    },
    setData: function(key, value) {
        Cookie.data[key] = value;
        Cookie.store();
    },
    removeData: function(key) {
        delete Cookie.data[key];
        Cookie.store();
    },
    retrieve: function() {
        var start = document.cookie.indexOf(Cookie.options.name + "=");

        if(start == -1) {
            return null;
        }
        if(Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
            return null;
        }

        var len = start + Cookie.options.name.length + 1;   
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        } 
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
        var expires = '';

        if (Cookie.options.expires) {
            var today = new Date();
            expires = Cookie.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = Cookie.options.name + '=' + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires;
    },
    erase: function() {
        document.cookie = Cookie.options.name + '=' + Cookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    getOptions: function() {
        return (Cookie.options.path ? ';path=' + Cookie.options.path : '') + (Cookie.options.domain ? ';domain=' + Cookie.options.domain : '') + (Cookie.options.secure ? ';secure' : '');      
    }
};
