var Rules = null;

var enhancer = {
  
  // reference to flash object
  fo:null,
  current_location:null,
  pageWidth:null,
  pageHeight:null,
  section:'',
  sub_section:'',
  use_drivein:false,
  current_domain:'',
  member_check:'',
  experience:null,
  ajax_url:null,
  ajax_enabled_pages_patterns: [],
  current_domain_pattern: null,
  is_ajax_enabled: function(site_location){
    if (this.experience == "1"){
      return false;
    }
    if (this.ajax_enabled_pages_patterns.length == 0){
      return false;
    }
    return this.ajax_enabled_pages_patterns.any(function(page_url_pattern) {
      return page_url_pattern.test(site_location);
    });
  },

  navigation_load: function(site_location){
    if (site_location == '/' && this.is_ajax_enabled(this.current_location)){
      $('container').hide();
      return;
    }
    if (this.current_location == site_location){
      $('container').show();
      return;
    }

    var external_url = true;
    if (site_location.substring(0,4).toLowerCase() == 'http'){
      Logger.info('we have a full url http... etc');
      if (site_location.indexOf(this.current_domain) > -1){
        Logger.info('url is from this domain');
        external_url = false;
      }else{
        Logger.info('url is from an external domain');
        external_url = true;
      }
    }else{
      Logger.info("we don't have a full url, so it must be from this domain");
      external_url = false;
      if (site_location.charAt(0) == '/'){
        Logger.info('the url is absolute, prepend domain');
        site_location = 'http://www.' + this.current_domain + site_location;
      }else{
        Logger.info('the url is relative, lets have a go at working out previous url');
        var previous_url;
        if (this.ajax_url){
          Logger.info("we've loaded a url before, great... use previous");
          previous_url = this.ajax_url;
        }else{
          previous_url = window.location.href;
          Logger.info("okay, let's just use the current domain name" + previous_url);
        }
        if (site_location.charAt(0) == '?'){
          site_location = previous_url.substring(0, previous_url.indexOf('?')) +
            site_location;
          Logger.info("urls is of form '?param1=value1', grab everything before ?" +
            site_location);
        }else{
          site_location = previous_url.substring(0, previous_url.lastIndexOf('/') + 1) +
            site_location;
          Logger.info("assume url is 'news.php?uid=222', grab everything before last '/'" + 
            site_location);
        }
      }
    }

    if (external_url){
      document.location = site_location;
      return;
    }

    // goal is to end with a full absolute url and to know if using ajax or not
    Logger.info("checking for ajax with url: " + site_location);
    var use_ajax = this.is_ajax_enabled(site_location);
    if (use_ajax && this.is_ajax_enabled(this.current_location)){
      this.ajax_url = site_location;
      if (/\?/.test(this.ajax_url)){
        this.ajax_url = this.ajax_url + "&ajax=yes"
      }else{
        this.ajax_url = this.ajax_url + "?ajax=yes"
      }
      new Ajax.Updater('contentbox', this.ajax_url, {
        asynchronous:true, 
        method: 'get'
      });
      $("container").show();
    }else{
      document.location = site_location;
    }
  },

  hide_container: function(event){
    $("container").hide();
    if (event){
      Event.stop(event)
    }
  },
  reloadPageOnResize: function(){
    window.location.href = window.location.href;
  },

  resizeBackground: function(){
    try{

    var frameWidth;
    var frameHeight;
    var obj;

    if (self.innerWidth){
      frameWidth = self.innerWidth;
      frameHeight = self.innerHeight;
    }else if (document.documentElement && document.documentElement.clientWidth){
      frameWidth = document.documentElement.clientWidth;
      frameHeight = document.documentElement.clientHeight;
    }else if (document.body){
      frameWidth = document.body.clientWidth;
      frameHeight = document.body.clientHeight;
    }else{
      return;
    }

    // update local vars
    this.pageWidth = frameWidth;
    this.pageHeight = frameHeight;

    if ( (obj = $('background_image')) ){
      // reset to default position on resize/load
      obj.style.top = obj.style.left = 0;
      var ratio = 1140/600;
      var ratio_width = frameHeight * ratio;
      if (ratio_width < frameWidth){
      }
      obj.style.height = frameHeight.toString() + "px";
      obj.style.width = ratio_width.toString() + "px";
      $('background_image').show();
    }
    
    }catch (e){
    }
  },

  start: function(){
    Logger.initialize("logger", Logger.OFF, false); 
    /*
    if (this.experience == "3"){
      // the experience is not 'override' or 'degrade', so perform browser check
      var ua = navigator.userAgent.toUpperCase();
      // TODO: settle on minium browser requirements
      if ( ua.indexOf ("MAC") >= 0 && ua.indexOf ("MSIE 5") >= 0 ){
        location.replace ("/unsupported_browser.php");
      }
    }
    */

    this.current_location = document.location.href;
    use_ajax_on_current_page = this.is_ajax_enabled(this.current_location);
    try{
      this.section = $('section').innerHTML;
    }catch (e){}
    try{
      this.sub_section = $('subsection').innerHTML;
      if (this.sub_section == 'MAIN'){
        this.sub_section = '';
      }
    }catch (e){}
    try{
      //this.current_domain = 'redhotchilipeppers.dev:1111';
      // COMMENT THIS IN AND COMMENT THE ABOVE LINE OUT:   
      this.current_domain = $('current_domain').innerHTML;
    }catch (e){}
    try{
      this.member_check = $('member_check').innerHTML;
    }catch (e){}

    Rules = {
      'a:click': function(element, event) {
        if (element.onclick){
          return;
        }
        Event.stop(event);
        var target_url = element.getAttribute('href');
        enhancer.navigation_load(target_url);
        return false;
      },
      '.close_window_control:loaded': function(element, event){
        // only load if not already there
        Event.observe(element, 'click', enhancer.hide_container);
      }
    }

    if (use_ajax_on_current_page){
      if (this.current_location == '/'){
        $('container').hide();
      }
      EventSelectors.start(Rules);
      fo = new FlashObject("/media/environment.swf", "environment", "100%", "100%", "6", "#000000");
    }else{
      if (this.use_drivein){
        fo = new FlashObject("/media/degraded.swf", "environment", "100%", "100%", "6", "#000000");
      }else{
        fo = new FlashObject("/media/heavynav.swf", "environment", "240px", "768px", "6", "#000000");
      }
      // set background image
      try{
        $('background_image').hide();
        this.set_background();
      }catch (e){}
      this.resizeBackground();
      Event.observe(window, 'resize', this.resizeBackground);
    }
    fo.addParam("wmode", "transparent");
    fo.addVariable("SECTION", this.section);
    fo.addVariable("SUBSECTION", this.sub_section);
    fo.addVariable("memberCheck", this.member_check);
    fo.addVariable("currentDomain", this.current_domain);
    fo.write("environment_panel");
  },
  set_background: function(){
    var section_key = '';
    if (this.section == 'MAIN'){
      section_key = '';
    }
    if (this.section == 'NEWS' ||
        this.section == 'DISCOG' ||
        this.section == 'FLEAMAIL' ||
        this.section == 'LOGIN' ||
        this.section == 'TICKETS' ||
        this.section == 'HELP' ||
        this.section == 'EXCLUSIVES' ||
        this.section == 'VIRTUAL TICKET' ||
        this.section == 'SIGNUP' ||
        this.section == 'STORE' ||
        this.section == 'COMMUNITY'){
        section_key = this.section.toLowerCase().replace(' ', '_');
    }
	
    $('background_image').src = '/media/background_images/bg-js-' + section_key + '1140.jpg';
  }
}  


  
