Protoform = function (form, success) {
	this.form = $(form);
	this.onSuccess = success;

	this.highlightClassName = 'protoformHighlight';

	this.onSubmit = function (e) {
		if (e) { Event.stop(e); }
		this.clearHighlights();
		this.clearMessages();
		this.disableForm();
		this.lastRequest = this.form.request({onComplete:this.onComplete.bind(this)});
	};

	this.disableFormActual = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (!element.disabled)
			{
				element.disabled = true;
				element._protoform_disabled = true;
			}
		}
	};

	this.disableForm = function () {
		window.setTimeout(this.disableFormActual.bind(this), 1);
	};

	this.clearHighlights = function () {
		for (var i = this.form.elements.length; i--;)
			$(this.form.elements[i]).removeClassName(this.highlightClassName);
	};

	this.clearMessages = function () {
		var node;
		if (node = $(this.form.id +'_message'))
			node.update();
		for (var i = this.form.elements.length; i--;)
			if (node = $(this.form.id +'_'+ this.form.elements[i].name +'_message'))
				node.update();
	};

	this.onComplete = function (request) {
		this.enableForm();

		var response = request.responseText.evalJSON(true);
		if (response.success)
			this.onSuccess.call(this, request, response);
		else
			this.onFailure.call(this, request, response);
	};

	this.onFailure = function (request, response) {
		var node;

		if (response.message)
			if (node = $(this.form.id +'_message'))
				node.update(response.message)
			else
				alert(response.message);

		for (var i = this.form.elements.length; i--;)
		{
			var element = this.form.elements[i];
			var reaction = response.reactions[element.name];

			if (reaction && reaction.highlight)
				$(element).addClassName(this.highlightClassName);

			node = $(this.form.id +'_'+ element.name +'_message');
			if (reaction && node)
				node.update(reaction.message ? reaction.message : "");
		}
	};

	this.enableForm = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (element._protoform_disabled)
			{
				element.disabled = false;
				element._protoform_disabled = undefined;
			}
		}
	};

	this.form.observe('submit', this.onSubmit.bind(this));
};

var local = {};

local.leftNavCurrent = 0;	//	currently opened leftnav item
local.leftNavCount = 0;		//	count of leftnav items

local.openSequence = function (parent, children) {
	parent.removeClassName("leftNavCategoryClosed");
	parent.addClassName("leftNavCategoryOpen");
	children.setStyle({display: "block"});
};

local.closeSequence = function (parent, children) {
	parent.removeClassName("leftNavCategoryOpen");
	parent.addClassName("leftNavCategoryClosed");
	children.setStyle({display: "none"});
};

local.leftNavCloseCurrent = function () {
	var parent = $("leftNavCategory_"+ local.leftNavCurrent);
	var children = parent.down(".leftNavSubCategories");
	local.closeSequence(parent, children);
};

local.leftNavOpenCurrent = function () {
	var parent = $("leftNavCategory_"+ local.leftNavCurrent);
	var children = parent.down(".leftNavSubCategories");
	local.openSequence(parent, children);
	
	/*
	for (var i = 0; i < local.leftNavCount; i++)
	{
		var parent = $("leftNavCategory_"+ i);
		var children = parent.down(".leftNavSubCategories");
		
		if (i == local.leftNavCurrent)
		{
			//	open this leftnav item
			local.openSequence(parent, children);
		}
		else
		{
			//	close this leftnav item
			local.closeSequence(parent, children);
		}
	}
	*/
};

local.leftNavFindById = function (id) {
	for (var i = local.leftNavData.length; i--;)
		if (local.leftNavData[i].id == id)
			return i;
};

local.leftNavSetCurrent = function (current, open) {
	if (typeof this.blur == "function") { this.blur(); }

	local.leftNavCloseCurrent();	
	local.leftNavCurrent = current;
	if (open === undefined || open)
		local.leftNavOpenCurrent();
};

local.leftNavInit = function () {
	for (var i = 0; i < local.leftNavCount; i++)
	{
		var parentLink = $("leftNavCategory_"+ i).down("a");
		parentLink.observe("click", local.leftNavSetCurrent.bind(parentLink, i));
	}
	
	//local.leftNavOpenCurrent();
};

local.moveCopyGroupNav = function () {
	//	move copyGroupContainer to under leftNavCopyGroupDestination
	var nodeFrom = $("copyGroupContainer");
	var nodeTo = $("leftNavCopyGroupDestination");
	if (nodeFrom && nodeTo)
		nodeTo.appendChild(nodeFrom);
};

local.sifrInit = function () {
	if (typeof sIFR == "function")
	{
		sIFR.replaceElement(".sifr-Square721_BT-yellow", named({ sFlashSrc:"sifr/Square721_BT.swf", sColor:"#f4d50d", sBgColor:"#A39BC1", sWmode:"transparent" }));
	}
};

local.init = function () {
	local.moveCopyGroupNav();
	local.leftNavInit();
	local.sifrInit();
};

productLightBoxInit = function () {
	//	delay the call to let dom finish changing after lightbox is opened
	window.setTimeout(_productLightBoxInit, 1);
};

productLargeImageContainer = null;

productThumbClick = function (src, large) {
	productLargeImageContainer.innerHTML = '<a href="javascript:;" onclick="productLargeImageClick(\''+ src +'\');return false;"><img src="'+ large +'" alt="" width="300" height="240" /></a><br />';
};

productLargeImageClick = function (src) {
	window.open(src);
};

_productLightBoxInit = function () {
	productLargeImageContainer = $("productLargeImage");
};

FastInit.addOnLoad(local.init);


