function lcgInit()
{
	$$('.hidden').invoke('hide');
	$$('.searchHint').each(addSearchHint);
	$$('.showMore').invoke('observe', 'click', showMore);
	$$('.toggle').invoke('observe', 'click', toggle);
	$$('.appear').invoke('observe', 'click', function (x){$(this.getAttribute('appear')).appear({duration: 0.5});})
	$$('.linkSubmit').invoke('observe', 'click', linkSubmit);
	$$('.modal').each(initModal);
	$$('.tooltip').each(createToolTip);	
	$$('ul.tabs').each(function (link){new Control.Tabs(link)});	
	$$('.carousel').each(function (scroller){new Carousel(scroller)});	
	$$('.auto_carousel').each(function (scroller){new Carousel(scroller, {auto: 1})});	
}

if(no_lcg_init != true)
	document.observe("dom:loaded", lcgInit);

function dwSubmitForm(form, action, secure)
{
	form.appendChild(new Element('input', {type: 'hidden', name: action, value: 'x'}));

	if(secure)
		form.action = $('secureForm').action;
		
	form.submit();

	return false;
}

function addSearchHint(hint)
{
	hint.observe('focus', searchHintFocus);
	hint.observe('blur', searchHintBlur);
}

function searchHintFocus(event)
{
	if(this.value == this.defaultValue)
		this.value = '';
}

function searchHintBlur(event)
{
	if(this.value == '')
		this.value = this.defaultValue;
}

function showMore(event)
{
	this.hide();
	Effect.BlindDown(this.rel);
	event.stop();
}

function toggle(event)
{
	Effect.toggle(this.getAttribute('toggle'), 'appear', {duration: 0.5, afterFinish: function (e){var ta=e.element.down('textarea'); if(ta) ta.focus();}});
	if("href" in this)
		event.stop();
}

function linkSubmit(event)
{
	if(this.rev != null)
		$(this.rel).appendChild(new Element('input', {type: 'hidden', name: this.rev, value: 'x'}));
		
	if(this.hash == '#secure' || this.protocol == 'https:')
		$(this.rel).action = $(this.rel).action.replace('http:', 'https:');

	$(this.rel).submit();
	event.stop();
}

function initModal(link)
{
	var dialogvar = link.getAttribute('dialogvar');
	if(dialogvar)
	{
		eval(dialogvar + '= new Control.Modal(link);');
		var modalFocus = null;
		modalFocus = link.getAttribute('focusvar');
		if(modalFocus != null)
			eval(dialogvar + '.observe(\'afterOpen\', function (){$(modalFocus).focus();})');
    if (dialogvar != 'taxDialog')
    {
		  eval(dialogvar + '.observe(\'beforeOpen\', lcg.hideSelect)');
  		eval(dialogvar + '.observe(\'afterClose\', lcg.showSelect)');
    }
	}
	else
	{
		modal = new Control.Modal(link, {afterOpen: function(){modal = this}});
		var modalFocus = null;
		modalFocus = link.getAttribute('focus');
		if(modalFocus != null)
			modal.observe('afterOpen', function (){$(modalFocus).focus();});
		modal.observe('beforeOpen', lcg.hideSelect);
		modal.observe('afterClose', lcg.showSelect);
	}
	
}

function createToolTip(link)
{
	var text = link.getAttribute('text');
	if(text == null)
		text = $(link.rel) ? $(link.rel) : link.rel;

	if(link.getAttribute('delay') != null)
		new Control.ToolTip(link, text, {className: link.rev ? link.rev : 'tip', delay: link.getAttribute('delay')});
	else
		new Control.ToolTip(link, text, {className: link.rev ? link.rev : 'tip'});
}

var lcg = {
  hideSelect: function(id) {
    if (Prototype.Browser.IE) {
      id = id ==  null ? "" : "#" + id + " ";
      $$(id + 'select').each(function(element) {
        if (typeof(element.oldVisibility) == "undefined" || element.oldVisibility == null) {
          element.oldVisibility = element.style.visibility ? element.style.visibility : "visible";
          element.style.visibility = "hidden";
        }
      });
    }
  },
  
  showSelect: function(id) {
    if (Prototype.Browser.IE) {
      id = id ==  null ? "" : "#" + id + " ";
      $$(id + 'select').each(function(element) {
        if (typeof(element.oldVisibility) == "undefined" || element.oldVisibility == null) {
          // Why?? Ask IE
          try {
            element.style.visibility = element.oldVisibility;
          } catch(e) {
            element.style.visibility = "visible";
          }
          element.oldVisibility = null;
        }
        else {
          if (element.style.visibility)
            element.style.visibility = "visible";
        }
      });
    }
  }
}

// TODO:
//
// Auto insert iframe and bind with CSS selectors

var iframeshim = {

  hide: function() 
  {
    if(this.ifele)
    	this.ifele.hide();
	this.curele = null;
    return this;
  },


  positionUnder: function(elementx, topoffset) {

	if(this.ifele == null)
	{
		this.ifele = new Element('iframe', {
		style: 'position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=1);',
		src: 'javascript:false;',
		frameborder: 0
		});
		$(document.body).insert(this.ifele);
	}	
	
	if(this.curele == elementx || $(elementx) == null)
		return this;

	this.curele = elementx;

	var top_o = topoffset ? topoffset : 0;
	
    var ele = $(elementx),
        offset = ele.cumulativeOffset(),
        dimensions = ele.getDimensions(),
        style = {
          left: offset[0] + 'px',
          top: (offset[1] + top_o) + 'px',
          width: dimensions.width + 'px',
          height: dimensions.height + 'px',
          zIndex: ele.getStyle('zIndex') - 1
        };
    this.ifele.setStyle(style).show();
    return this;
  },

  curele: null,
  
  ifele: null
};

function backButtonOverride()
{
  // Work around a Safari bug
  // that sometimes produces a blank page
  setTimeout("backButtonOverrideBody()", 1);

}

function backButtonOverrideBody()
{
  // Works if we backed up to get here
  try {
    history.forward();
  } catch (e) {
    // OK to ignore
  }
  // Every quarter-second, try again. The only
  // guaranteed method for Opera, Firefox,
  // and Safari, which don't always call
  // onLoad but *do* resume any timers when
  // returning to a page
  setTimeout("backButtonOverrideBody()", 500);
}


