window.addEvent('domready', function () {
	$$('a').each(function (e) {
		//form links
		if (e.hasClass('submitLnk')) {
			e.addEvent('click', submitForm.bindWithEvent(e, e));
		}
	});

	$ES('div.stores').each(function (e) {
		var img = $E('img.storePic', e);
		var links = $ES('div.storeThumbs a', e);
		if (img && links) {
			new ImageSwapper(img, links);
		}
	});

	//products images
	if ($('imgProdMain')) {
		var imgs = new Array();
		if ($('imgProd1')) imgs.push($('imgProd1'));
		if ($('imgProd2')) imgs.push($('imgProd2'));
		if ($('imgProd3')) imgs.push($('imgProd3'));
		if ($('imgProd4')) imgs.push($('imgProd4'));
		if (imgs.length > 0) {
			new ProductImageSwapper($('imgProdMain'), imgs);
		}
	}
	if ($('imgProdMain2')) {
		var imgs = new Array();
		if ($('imgProd5')) imgs.push($('imgProd5'));
		if ($('imgProd6')) imgs.push($('imgProd6'));
		if ($('imgProd7')) imgs.push($('imgProd7'));
		if ($('imgProd8')) imgs.push($('imgProd8'));
		if (imgs.length > 0) {
			new ProductImageSwapper($('imgProdMain2'), imgs);
		}
	}

	new Timeline('timeline');
});

var Timeline = new Class({
	initialize: function (element) {
		element = $(element);
		if (!element) {
			return;
		}

		this.layers = [];

		var regex = /#(entry.+)$/
		this.maps = $ES('#timelinemap area');
		this.maps.each(function (e, i) {
			var matches = regex.exec(e.href);
			if (matches) {
				this.layers.push($(matches[1]));
				e.addEvent('click', function(evt) {
					new Event(evt).stop();
					this.show(i);
				}.bind(this));
			}
		}, this);

		this.show(0);
	},
	show: function (j) {
		this.layers.each(function(e, i) {
			e.setStyles({
				display: (i == j) ? '' : 'none'
			});
		});
	}
});


var ProductImageSwapper = new Class({
	container: null,
	imgs: [],
	hideCurrent: true,
	initialize: function (containerImg, imgs) {
		this.container = $(containerImg);
		imgs.each(function(e) {
			if ($(e)) {
				this.imgs.push($(e));
				if ($(e).getParent().getTag() == 'a') {
					$(e).getParent().addEvent('click', function (evt, i) {
						this.swap(i);
						evt.stop();
					}.bindWithEvent(this, this.imgs.length - 1));
				}
			}
		}.bind(this));
	},
	swap: function(imgNb) {
		if (imgNb > this.imgs.length) return;

		this.container.setProperty('src', this.imgs[imgNb].getParent().getProperty('href'));
		if (this.hideCurrent) {
			var currentSrc = this.imgs[0].getProperty('src');
			var currentHref = this.imgs[0].getParent().getProperty('href');
			this.imgs[0].setProperty('src', this.imgs[imgNb].getProperty('src'));
			this.imgs[0].getParent().setProperty('href', this.imgs[imgNb].getParent().getProperty('href'));
			this.imgs[imgNb].setProperty('src', currentSrc);
			this.imgs[imgNb].getParent().setProperty('href', currentHref);
		}
	}
});

var ImageSwapper = new Class({
	links: [],
	current: 0,
	element: null,
	initialize: function (element, links) {
		this.links = links;
		this.element = element;
		links.each(function (e, i) {
			e.addEvent('click', function (evt) {
				var evt = new Event(evt);
				evt.stop();
				this.swap(i);
			}.bind(this))
		}, this);
	},
	swap: function (num) {
		this.element.setProperty('src', this.links[num].href);

		var src = this.links[0].getElement('img').src;
		var href = this.links[0].href;

		this.links[0].getElement('img').src = this.links[num].getElement('img').src;
		this.links[0].href = this.links[num].href;
		this.links[num].getElement('img').src = src;
		this.links[num].href = href;
	}
});


function submitForm(evt, e) {
	var form = null;

	// get the parent form
	$$('form').each(function (f) {
		if (f.hasChild(e)) {
			form = f;
			return;
		}
	});

	if (!form) {
		return;
	}

	if (form.onsubmit) {
		if (!form.onsubmit()) return;
	}

	if (e.getTag() == 'a') {
		evt.stop();
		//check get variables
		var hrefParts = e.href.split('?');
		if (hrefParts.length > 1) {
			var sep = (form.action.indexOf('?') != -1) ? '&' : '?';
			form.action += sep + hrefParts[1];
		}
	}
	else if (e.getTag() == 'select') {
		// make sure an item is selected
		var selected = 0;
		form.getElements('input[type=checkbox]').each(function (el) {
			if (el.checked) {
				selected++;
			}
		});
		form.getElements('input[type=radio]').each(function (el) {
			if (el.checked) {
				selected++;
			}
		});

		if (selected == 0) {
			alert('Please select at least one item.');
			e.selectedIndex = 0;
			return;
		}

		switch (e.value) {
			case '':
				return;
			case 'delete':
				if (!confirm('Are you sure you wish to delete this item?')) {
					e.selectedIndex = 0;
					return;
				}
				break;
		}
	}

	form.submit();
}



/*var submitted_forms = [];
function check_submitted(f) {
	var c = submitted_forms.length;
	for (var i = 0; i < c; i++) {
		if (submitted_forms[i] == f) {
			return false;
		}
	}
	submitted_forms[c] = f;
	return true;
}

function popup(url, width, height) {
	var width = width || 542;
	var height = height || 520;
	var title = 'White Label Clothing';

	var x = (640 - width)/2;
	var y = (480 - height)/2;
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}

	var properties = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',top=' + y + ',left=' + x
	var w = window.open(url,title,properties);
	w.focus();
}*/