var help_system;

document.observe('dom:loaded', function() {
  help_system = new HelpSystem();
});

var HelpSystem = Class.create({
  initialize: function() {
    if (!$('help-content')) this.add_help_links() // if we are displaying help content then we want to just use normal page loads
    $$('a').each(function(help_link) {
	  if (help_link.hash == "#instant_quote") help_link.href = link_to_new_cart; // set in top menu
    });
  },
  
  add_help_links: function(obj) {
  	var tthis = this;
	if (!obj) obj = '';
	$$(obj+' a.open-help-dialog').each(function(help_link) {
      help_link.observe('click', function(e) {
        tthis.show();
        tthis.load_page(help_link.href);
        e.stop();
      })
    });

  },
  
  open_page: function (href) {
  	this.show();
	this.load_page(href);
  },
  
  open_ele: function (ele, on_close_fn) {
  	this.show();
	this.dialog.setContent($(ele).innerHTML)
	this.dialog.observe('hiding', on_close_fn)
  },
  
  show: function() {
    if (this.dialog == null || !this.dialog.visible) {
      this.dialog = new UI.Window({ 
        theme:  "alphacube",
        shadowTheme: "mac_shadow",
        shadow: true,
		resizable: false,
        width:  940,
        height: 600
      }).
      setContent('<div id="help-system" class="popup-window"></div>');


    }
    
    this.dialog.show(true).center().activate();
	keepDialogSizedRight(this.dialog, ".page-body", function (element, window) {
	  $(element).style.height = window.getSize().height-91+"px"
	  $('left-menu').style.height = window.getSize().height-40+"px"
	  var contact_message = $$("#contact_message_text")
	  if (contact_message && contact_message.length > 0) $(contact_message[0]).style.width = window.getSize().width-300+"px"
      })
    this.adjust_dialog_bounds();
	var tthis = this;
	this.dialog.observe('hiding', function() {
		update_tracking('/popup_closed'); // rewrites original URL
		});
	Event.observe(this.dialog.windowManager.modalOverlay, "click", 
	  function(){if (confirm('Click okay to exit this dialog and return to the main screen.  \n\n'+
	                     'You may close dialog boxes at any time by clicking black X at the top right corner of the dialog.'
						 )) tthis.hide();
				})
  },
  
  adjust_dialog_bounds: function() {
  	return; // no longer used
    bounds = this.dialog.getBounds();
		if (bounds.top < 50) {
		  bounds.top = 50;
		  this.dialog.setBounds(bounds);
		}
  },
  
  hide: function() {
    if (this.dialog) {
		this.dialog.close(); // TODO for robustness should probably do a bit more checking...
	}
  },
  
  load_page: function(url) {
    tthis = this;
    
    new Ajax.Request(url+"?popup=true", { // popup=true causes controller not to render right bar
      method: 'get',
      onSuccess: function(transport) {
	  	if ($('help-system').length==0) return; //  this can happen if user hits close button before we finish loading page.
        $('help-system').empty();
        $('help-system').update(transport.responseText);
		update_tracking(url);
        new UI.Carousel("left-menu");
		$$('a').each(function(help_link) {
	  		if (help_link.hash == "#instant_quote") help_link.href = link_to_new_cart; // set in top menu
    		});
        // Hijack all of the page links to load via Ajax inside this window
        $$('#help-system a.page-link').each(function(link) {
          link.observe('click', function(e) {
            tthis.load_page(e.element().href);
            e.stop();
          });
        });
      }
    });
  }
});