var cal;

function toggleTwisty(twisty, target)
{
  twisty.innerHTML = (target.style.display == 'none') ? '&#9660;' : '&#9658;';
  new Effect[Element.visible(target) ? 'BlindUp' : 'BlindDown'](target, { duration:0.5, fps:50, queue:'end' });
}

function load_div(div, url)
{
  new Ajax.Updater(div, url, { method: 'get', evalScripts:true, onSuccess: function(){ new Effect.Highlight(div, { duration: 0.6 }); } });
}

function submit_form(div, url, form)
{
  var serializedForm = Form.serialize(form);
  new Ajax.Updater(div, url, {asynchronous:true, parameters:serializedForm, evalScripts:true, onSuccess: function(){ new Effect.Highlight(div, { duration: 0.6 }); }});
}

function bulkUpdate(obj, value)
{
  var fields = document.getElementsByClassName(obj);
  for (var i=0; i<fields.length; i++)
  {
    fields[i].value = value;
  }
}

function sanitiseNameForUrl(text)
{
	return text.replace(/[^a-zA-Z 0-9]+/g,'').replace(/[ ]+/g, '-').toLowerCase();
}

var do_reloadProductList;
var selectItemCallback;

function activateWYSIWYG(textarea)
{
  var oFCKeditor = new FCKeditor(textarea);
  oFCKeditor.Value = $F(textarea);
  oFCKeditor.ReplaceTextarea();
}

function deactivateWYSIWYG(textarea)
{
  var oFCKeditor = FCKeditorAPI.GetInstance(textarea);
  xhtml = oFCKeditor.GetXHTML();
  
  $(textarea + '___Frame').parentNode.removeChild($(textarea + '___Frame'));
  $(textarea).style.display = 'block';
  
  $(textarea).value = xhtml;
}

function select_variety(selector, target, div)
{
  var serializedForm = Form.serialize(selector.form);
  new Ajax.Updater(div, target, {asynchronous:true, parameters:serializedForm, evalScripts:true});
}


function batchTickbox(group_class, selector)
{
  boxes = document.getElementsByClassName(group_class);
  for (var i=0; i<boxes.length; i++)
    boxes[i].checked = selector.checked;
}

function jumpMenu(targ,selObj,restore)
{
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function productSliderLeft()
{
  if (hfp_index > 0)
  {
    hfp_index--;
    Element.show('featured-product-' + hfp_index);
    Element.show('fp-next');
  }
  if (hfp_index == 0) { Element.hide('fp-prev'); }
}

function productSliderRight()
{
  if (hfp_index + ($('featured-products-inner').getWidth() / 120) < hfp_total)
  {
    Element.hide('featured-product-' + hfp_index);
    hfp_index++; Element.show('fp-prev');
  }
  if (hfp_index == hfp_total - Math.floor($('featured-products-inner').getWidth() / 120)) { Element.hide('fp-next'); }
}

function lookupAddress(postcodecontent, address)
{	
	new Ajax.Request('/account/lookup', { parameters: { postcode: postcodecontent },
                                                      onSuccess: function(transport){
	var result = transport.responseText.evalJSON(true);

		$(address +'-city').value = result['_attributes']['city'];
		var premises = "<select name='address[address_1]' type='text' id='address-address_1'>";
		premises += "<option value=''>Select your property name</option>";
		
	  for (i=0; i<result['premises'].length; i++)
		  premises += "<option>" + result['premises'][i] + "</option>";
			
		premises += "</select>";
		$(address + '-premises-list').innerHTML = premises;
		
		if (address == 'trade_address')
		{
			$('trade_address-county').value = result['_attributes']['county'];
			$('trade_address-address_2').value = result['_attributes']['address_2'];
		}
	
	 }});
}

// Bens MegaMagick Animated Accordian Script (tm) (created on the train from 
// Melton to London Liverpool St, so any mistakes are the fault of leaves
// on the line or something similarly unlikely)
var Accordian = Class.create({
  element: null,
  accordianTitles: null,
  accordianPanels: null,
  currentAccordianSectionId: 0,
  initialize: function(id)
  {
    this.element = $(id);
    this.accordianTitles = Selector.findChildElements(this.element, ['H2.accordian-title']);
    this.accordianPanels = Selector.findChildElements(this.element, ['DIV.accordian-panel']);
    accordianPanelHeight = this.element.getHeight() - (this.accordianTitles[0].getHeight() * this.accordianTitles.length);
    for (i=0; i<this.accordianTitles.length; i++)
    {
      this.accordianPanels[i].style.height = accordianPanelHeight + 'px';
      this.accordianTitles[i].setAttribute('accordianSectionId',i);
      this.accordianTitles[i].accordian = this;
      Event.observe(this.accordianTitles[i], 'click', function() {
        if (this.accordian.currentAccordianSectionId != this.getAttribute('accordianSectionId'))
        {
          new Effect.Parallel([
            new Effect.BlindUp(this.accordian.accordianPanels[this.accordian.currentAccordianSectionId]),
            new Effect.BlindDown(this.accordian.accordianPanels[this.getAttribute('accordianSectionId')])
          ], { duration: 0.3 });
          this.accordian.currentAccordianSectionId = this.getAttribute('accordianSectionId');
        }
      });
      if (i > 0) this.accordianPanels[i].hide();
    }
  }
});
