var goal = {
  rules: {
    '.variety-selector' : function(details) {
      var productform = details.form;
      details.onchange = function() {
        var serializedForm = Form.serialize(productform);
        new Ajax.Updater('sku', '/catalog/sku', {asynchronous:true, parameters:serializedForm});
        return false;
      }
    },
      
    '.colour-swatch' : function(details) {
      details.onclick = function() {
        
        selected = document.getElementsByClassName('colour-swatch-selected');
        if (selected.length == 1) selected[0].className = 'colour-swatch';
        
        details.className = 'colour-swatch-selected';
        bits = details.id.split('-');
        $('colour-variety-' + bits[1]).value = bits[2];
        $('colour-variety-' + bits[1]).onchange();
        return false;
      }
    }
  }
}
Behaviour.register(goal.rules);

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 findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

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 popUp(URL)
{
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=800,height=600');");
}

