if(typeof(pcmr) == 'undefined')
    var pcmr = {};

/* Historic function: many park city pages use this to register an onload
 * action. Now we can defer to jQuery's "on ready", which can start getting
 * things done when the DOM is fully loaded, but related resources such as
 * images may still be loading up */
pcmr.registerLoader = function(func) {
    jQuery(document).ready(func);
};

/* This is set on load to find all `<a class="targeted" ...>` tags. It uses
 * the target attribute to pop up a window. 
 * <a href="..." class="targeted" target="newpage-400x300">
 */
pcmr.attach_targeted_behavior = function() {
    var link = jQuery(this);
    var href = link.attr('href'),
        target = link.attr('target');

    var target_elements = target.split('-');
    // First two elements should be window name and dimensions:
    //      tour-400x600
    //      newpage-300
    //      news-400x600-resizable-menubar
    var window_name = target_elements.shift(),
        dimensions = target_elements.shift().split('x');
    
    // If the dimensions are only a single number, duplicate it for height.
    if(dimensions.length == 1)
        dimensions.push(dimensions[0]);
    
    // The remaining window options map to setting that option on the
    // window.open call.
    var window_options = jQuery.map(target_elements, function(option) {
        return option+'=1';
    });
    window_options.push('scrollbars=1');

    var option_string = "width="+dimensions[0]+",height="+dimensions[1];
    option_string += ','+window_options.join(',');
    
    link.attr('target', '')
        .click(function() {
            window.open(this.href, window_name, option_string);
            return false;
        });
};

jQuery(document).ready(function() {
    jQuery('a.targeted').each(pcmr.attach_targeted_behavior);
});

pcmr.resizer = (function($) {
    var default_config = {
        resize_container: '#text-resizer',
        button_selector: 'a.resize-link',
        button_highlight_class: 'btn-selected',
        button_ids: {
            small: 'resize-small',
            medium: 'resize-medium',
            large: 'resize-large'
        },
        font_sizes: {
            small: '10px',
            medium: '11px',
            large: '14px'
        },
        smalltype_sizes: {
            small: '9px',
            medium: '9px',
            large: '12px'
        },
        resizable_container: 'body',
        resizable_selectors: [],
        smalltype_selectors: []
    };
    
    var config = {};
    
    var configure = function(options) {
        jQuery.extend(config, default_config, options);

        $('#'+config.button_ids.small).click(function() {
            set_active_style('small', {reset: true, animate: true});
            return false;
        });

        $('#'+config.button_ids.medium).click(function() {
            set_active_style('medium', {reset: true, animate: true});
            return false;
        });

        $('#'+config.button_ids.large).click(function() {
            set_active_style('large', {reset: true, animate: true});
            return false;
        });
        
        load_default_style();
    };
    

    var highlight_resize_button = function(style_type) {
        var highlightable_id = config.button_ids[style_type];
        $(config.button_selector).each(function() {
            var button = $(this);
            button.removeClass(config.button_highlight_class);
            if(this.id == highlightable_id)
                button.addClass(config.button_highlight_class);
        });
    };
    
    var set_active_style = function(style_type, options) {
        options = jQuery.extend({reset: false, animate: true}, options || {});
        var font_size = config.font_sizes[style_type],
            small_font_size = config.smalltype_sizes[style_type],
            selectors = config.resizable_selectors.join(', '),
            small_selectors = config.smalltype_selectors.join(', ');
        
        if(!options.animate || jQuery.browser.msie) {
            $(config.resizable_container)
                .find(selectors)
                    .css({'font-size': font_size})
                    .end()
                .find(small_selectors)
                    .css({'font-size': small_font_size})
                    .end();
        } else {
            $(config.resizable_container)
                .find(selectors)
                    .animate({fontSize: font_size}, 500)
                    .end()
                .find(small_selectors)
                    .animate({fontSize: small_font_size}, 500)
                    .end();
        }
        
        if(options.reset)
            jQuery.cookie('psize', style_type, {expires: 365, path: '/'});
        
        highlight_resize_button(style_type);
    };
    
    var load_default_style = function() {
        var style_type = jQuery.cookie('psize');
        if((style_type == null) || (style_type == 'medium'))
            highlight_resize_button('medium');
        else
            set_active_style(style_type, {reset: false, animate: false});
    };

    /* Exported Names */
    return {
        'configure': configure,

        'configuration': config,
        'set_active_style': set_active_style
    };
})(jQuery);


/* jQuery.cookie */
if(typeof jQuery.cookie == 'undefined') {
  /*
   * @name $.cookie
   * @cat Plugins/Cookie
   * @author Klaus Hartl/klaus.hartl@stilbuero.de
   */
  jQuery.cookie = function(name, value, options) {
      if (typeof value != 'undefined') { // name and value given, set cookie
          options = options || {};
          if (value === null) {
              value = '';
              options.expires = -1;
          }
          var expires = '';
          if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
              var date;
              if (typeof options.expires == 'number') {
                  date = new Date();
                  date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
              } else {
                  date = options.expires;
              }
              expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
          }
          var path = options.path ? '; path=' + options.path : '';
          var domain = options.domain ? '; domain=' + options.domain : '';
          var secure = options.secure ? '; secure' : '';
          document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
      } else { // only name given, get cookie
          var cookieValue = null;
          if (document.cookie && document.cookie != '') {
              var cookies = document.cookie.split(';');
              for (var i = 0; i < cookies.length; i++) {
                  var cookie = jQuery.trim(cookies[i]);
                  // Does this cookie string begin with the name we want?
                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                      break;
                  }
              }
          }
          return cookieValue;
      }
  };  
}
