/* Declare a namespace for the site */
var site = window.site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {

  //same as $(document).ready();
  $(function() {
  
    /**
     * Doesn't work in all browsers :(
     */
    //site.handleActions();
  
  });


  $(window).bind("load", function() {

  });

})(jQuery);

// iaip actions id
site.IAIP_API_ID = 46;

// attach vars to Site
site.actions = [];

/**
 * handle cookies
 */
site.cookie = {

  set: function(name, value, days) {
    if(days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    } else {
      var expires = "";
    }
    document.cookie = 'exp_'+name+"="+value+expires+"; path=/";
  },
  
  get: function(name, _default) {
    var nameEQ = 'exp_'+name+"=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while(c.charAt(0) == ' ') {
        c = c.substring(1, c.length);
      }
      if(c.indexOf(nameEQ) == 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return _default;
  },
  
  remove: function(name) {
    this.set('exp_'+name, '', -1);
  }
  
};

// @deprecated
site.getParam = function(name, _default) {
  return site.request.get(name, _default);
};

/**
 * site.request
 */
site.request = {
  
  get: function(name, _default) {
    
    if (_default==null) {
      _default="";
    }
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+name+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null) {
      return _default;
    } else {
      return qs[1];
    }
    
  },
  
  post: function(name, _default) {
    // TODO: implement
    alert('site.request.post() not implemented.');
  }
  
};

/**
 * site.handleActions()
 *
 * Actions are cool, nuff said.
 */
site.handleActions = function() {
  
  for(var action in this.actions) {
    var function_name = this.actions[action];
    window[function_name]();
  }
  
};

/**
 * Site.modal.
 */
site.modal = {
  
  show: function(options) {
    
    var options_default = {
      type: 'default',
      data: '',
      selector: false,
      width: 500,
      height: 400,
      auto_close: false,
      timer: 3000,
      callback: false,
      onClosed: false
    };
    
    if (typeof options == 'object') {
      options = $.extend(options_default, options);
    } else {
      options = options_default;
    }
    
    switch(options.type) {
      case 'inline':
        $.colorbox({open:true, width: options.width, height: options.height, html: options.data, onClosed: options.onClosed});
      break;
      default:
        var path = options.data;
        if(options.selector) {
          path += ' ' + options.selector;
        }
        options.open = true;
        options.href = path;
        $.colorbox(options);
      break;
    }
    
    if(options.callback) {
      
      options.callback();
      
    } else {
      
      if(options.auto_close) {
        setTimeout(function() {
          site.modal.close();
        }, options.timer);
      }
      
    }
    
  },
  
  open: function(type, value, selector, options) {
    this.show(type, value, selector, options);
  },
  
  hide: function() {
    $.colorbox.close();
  },
  
  close: function() {
    this.hide();
  },
  
  success: function(options) {
    
    var options_default = {
      type: 'inline',
      heading: 'Success',
      width: 300,
      height: 200,
      auto_close: true,
      timer: 5000
    };
    
    if (typeof options == 'object') {
      options = $.extend(options_default, options);
    } else {
      options = options_default;
    }
    
    options.data = '<h3 class="status">'+options.heading+'</h3><p class="status">'+options.data+'</p>';;
    this.show(options);
    
  },
  
  error: function(options) {
    
    var options_default = {
      type: 'inline',
      heading: 'Error',
      width: 300,
      height: 200,
      auto_close: true,
      timer: 5000
    };
    
    if (typeof options == 'object') {
      options = $.extend(options_default, options);
    } else {
      options = options_default;
    }
    
    options.data = '<h3 class="error">'+options.heading+'</h3><p class="error">'+options.data+'</p>';;
    this.show(options);
    
  }
  
};

