var brandList = {
	init: function() {
		this.config = {};
		this.activeid = null;
		this.scrollUpBy = null;
		this.scrollDownBy = null;
	},
	open: function(id) {
		var self = this;
		var node = dojo.byId(id);
		console.log("open:", id);
		if (this.activeid) {
			self.close(this.activeid);
		}
		dojo.style(node, {
			top: "350px",
			left: "150px"
		});
		this.activeid = id;
	},

	close: function(id) {
		var self = this;
		var node = dojo.byId(id);
		console.log("close:", id);
		dojo.style(node, {
			top: "-5000px",
			left: "150px"
		});
	},
	scroll: function(id, dir) {
		if (this.config && this.config[id]) {
			console.log("config exists for id: ", id);
			if ((this.config[id].page == this.config[id].pages) && (dir == "up")) {
				return;
			}

			if ((this.config[id].page == 1) && (dir == "down")) {
				return;
			}
		} else {
			if (dir == "down") {
				return;
			}
			console.log("setting config[id] to false: ", id);
			this.config[id] = {};
		}

		var self = this;
		var node = dojo.byId(id);

		if (!this.scrollUpBy || !this.scrollDownBy) {
			var container = node.parentNode;
			this.scrollDownBy = dojo.style(container, "height") + 7;
			this.scrollUpBy = this.scrollDownBy * -1;
		}

		if (!this.config[id].page) {
			var listHeight = dojo.style(node, "height");
			this.config[id].pages = Math.ceil(listHeight / this.scrollDownBy);
			this.config[id].page = 1;
		}

		var dest = (dir == 'up') ? this.scrollUpBy : this.scrollDownBy;
		dojox.fx.slideBy({
			node: node,
			duration: 1000,
			top: dest,
			left: 0
		}).play();
		this.config[id].page = (dir == 'up') ? this.config[id].page+1 : this.config[id].page-1;
	},
	save: function(id) {
		var self = this;
		var cssClass = ".cb_" + id;
		var lid = id + "_brands_list";
		var pid = id + "_popup";
		var cid = id + "_checked";
		var uid = "form.step2.field." + id + "_brands_used";

		var nodes;
		if (this.config && this.config[lid] && this.config[lid].nodes) {
			this.config[lid].checked = [];
			nodes = this.config[lid].nodes;
		} else {
			if (!this.config[lid]) {
				this.config[lid] = {};
			}
			this.config[lid].checked = [];
			this.config[lid].nodes = dojo.query(cssClass);
			nodes = this.config[lid].nodes;
		}

		nodes.forEach(function(node, idx) {
			if (node.checked) {
				self.config[lid].checked.push(node.value);
			}
		});

		var asstring = self.config[lid].checked.join(",");
		dojo.byId(uid).value = asstring;
		dojo.byId(cid).innerHTML = asstring.substr(0, 80) + " ...";
		this.close(pid);
	}
};
brandList.init();


var checkBoxGroups = {
	init: function(cssClass) {
		this.groups = [];
		this.allNodes = [];
		this.groupClass = cssClass || "cb_group";

		this.findGroups();
	},
	findGroups: function() {
		var self = this;
		this.allNodes = dojo.query("." + this.groupClass);
		this.allNodes.forEach(function(node, i) {
			var group = dojo.attr(node, "name");
			if (self.groups[group] && self.groups[group].nodes) {
				self.groups[group].checked = node.checked ? node : self.groups[group].checked;
				self.groups[group].nodes.push(node);
			} else {
				self.groups[group] = {};
				self.groups[group].nodes = [];
				self.groups[group].checked = node.checked ? node : null;
				self.groups[group].nodes.push(node);
			}
			dojo.connect(node, "click", self, "clickHandler");
		});
	},
	clickHandler: function(e) {
		var group = dojo.attr(e.target, "name");
		var nodes = this.groups[group].nodes;
		if (this.groups[group].checked) {
			this.groups[group].checked.checked = false;
		}
		e.target.checked = true;
		this.groups[group].checked = e.target;
	}
};
dojo.addOnLoad(function() { checkBoxGroups.init(); });
