// Copyright 2008 Simon Celen. All rights reserved.

cookies = {
  set: function(name, value, options) {
    options = options || {};
    if (!options.expires)
    {
      options.expires = 365;  // 1 year default
    }
    var expires_date = new Date();
    expires_date.setDate(expires_date.getDate() + options.expires);
    if (!options.path)
    {
      options.path = '/';  // root is default, so cookies are site-wide
    }
    document.cookie = name + '=' + escape(value) +
      ((options.expires)? ';expires=' +expires_date.toGMTString() : '') + 
      ((options.path)?    ';path=' + options.path : '') +
      ((options.domain)?  ';domain=' + options.domain : '') +
      ((options.secure)?  ';secure' : '');
  },
  
  get: function(name) {
    var start = document.cookie.indexOf(name + '=');
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length)))
    {
      return null;
    }
    if (start === -1) { return null; }
    var end = document.cookie.indexOf(';', len);
    if (end === -1) { end = document.cookie.length; }
    var value = unescape(document.cookie.substring(len, end));
    if (value == 'true') { value = true; }
    if (value == 'false') { value = false; }
    return value;
  },
  
  remove: function(name, path, domain) {
    if (this.get(name))
    {
      document.cookie = name + '=' +
      ((path)? ';path=' + path : '') +
      ((domain)? ';domain=' + domain : '') +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    }
  },
  
  allowed: function() {
    this.set('checkCookie', 'test', 1);
    if (this.get('checkCookie'))
    {
      this.remove('checkCookie');
      return true;
    }
    return false;
  }
};
Ext.onReady(function() {
  // browsing by project image click
  if (next_project_name)
  {
    Ext.select('#project img').setStyle('cursor', 'pointer');
    Ext.select('#project img').on('click', function(e, target) {
      location.href = '/' + next_project_name + '/';
    });
  }
  
  // stop useless home click
  if (location.pathname == '/')
  {
    Ext.get('logo').setStyle('cursor', 'default');
  }
  Ext.get('logo').on('click', function(e, target) {
    if (location.pathname == '/')
    {
      e.stopEvent();
    }
  });
  
  // toggle info box
  var info_height = Ext.get('info').getHeight();
  var toggle_info = function(show, animate) {
    if (animate)
    {
      animate = {
        duration: 0.5,
        easing: (show)? 'easeIn' : 'easeOut'
      };
    }
    Ext.get('hide-info').update((show)? '<span class="arrow-up">HIDE INFO</span>' : '<span class="arrow-down">SHOW INFO</span>');
    Ext.get('info').setHeight((show)? info_height : 0, animate);
  };
  if (cookies.get('hide-info'))
  {
    toggle_info(false);
  }
  Ext.get('hide-info').on('click', function(e, target) {
    e.stopEvent();
    var show = cookies.get('hide-info');
    if (show === null)
    {
      // cookies are blocked
      show = (Ext.get('info').getHeight() !== info_height);
    }
    toggle_info(show, true);
    cookies.set('hide-info', !show);
  });
});