price_mods = {};
fourd = null;

window.addEvent('domready', function() {
	if (fourd = $$("select[name=drawers]"))
		fourd = fourd[0].options[2];

	updateDrawers($$("select[name=size]")[0]);
	updateWishList();
});
window.onload = function () {
	if (fourd = $$("select[name=drawers]")[0])
		fourd = fourd.options[2];

	updateDrawers($$("select[name=size]")[0]);
	updateWishList();
};

function toggleItemTotalPriceCombo(e) {
	// calculate total price

	if (!price_mods[e.name])
		price_mods[e.name] = 0;

	var m = e.options[e.selectedIndex].className.match(/_price_(\d+)/);
	if (!m) return;

	var totalPrice = parseFloat(document.getElementById('itemtotalprice').innerHTML);
	var price_mod = parseInt(m[1]) / 100;
	
	totalPrice -= price_mods[e.name];
	totalPrice += price_mod;
	
	document.getElementById('itemtotalprice').innerHTML = totalPrice.toFixed(2);
	price_mods[e.name] = price_mod;

	// calculate was price

	if (!price_mods[e.name + "_was"])
		price_mods[e.name + "_was"] = 0;

	var w = e.options[e.selectedIndex].className.match(/_pricewas_(\d+)/);
	if (!w) return;

	var was_price = parseFloat(document.getElementById('itemwasprice').innerHTML);
	var was_price_mod = parseInt(w[1]) / 100;	

	was_price -= price_mods[e.name + "_was"];
	was_price += was_price_mod;

	document.getElementById('itemwasprice').innerHTML = was_price.toFixed(2);
	price_mods[e.name + "_was"] = was_price_mod;

	var yousave = document.getElementById('you-save');
	if (!yousave) return;

	yousave.innerHTML = (was_price - totalPrice).toFixed(0);

	updateWishList();
}

function updateWishList() {
	var wl = document.getElementById('wish-list');
	if (!wl) return;

	var pid = wl.href.match(/^(.+?)\?(ProductID=\d+)/);
	var url = pid[1];

	url += "?" + pid[2];

	var s, f, d;
	if (s = $$("select[name=size]")[0]) {
		url += "&size=" + s.selectedIndex;
	}

	if (f = $$("select[name=firmness]")[0]) {
		url += "&firmness=" + f.selectedIndex;
	}

	if (d = $$("select[name=drawers]")[0]) {
		url += "&drawers=" + s.selectedIndex;
	}

	wl.href = url;

}

function updateDrawers(e) {
	var drawers = $$("select[name=drawers]")[0];
	if (!drawers) return;

	var bedsize = e.options[e.selectedIndex].value;

	var curdraw = drawers.options[drawers.selectedIndex].value;

	if (3 == bedsize) {
		if (4 == curdraw)
			drawers.selectedIndex = 1;
		//drawers.options[2].disabled = true;
		drawers.removeChild(fourd);
	} else {
		//drawers.options[2].disabled = false;
		drawers.appendChild(fourd);
	}
}

function SelectSubCat(e) {
}

window.addEvent('domready', function(){
	$$('.Tips1').each(function (e) {
		var m = e.id.match(/^tip-(\d+)$/);
		var id = m[1];
		if (!id) return;

		var content = "#tip-content-" + id;
		$(e).store('tip:title', $$(content + " div.tip-title").get('html'));
		$(e).store('tip:text', $$(content + " div.tip-text").get('html'));

		new Tips(e, {
			showDelay: 200,
			hideDelay: 400,
			fixed: true
		});
	});

// 	Tips1 = new Tips('.Tips1', {
// 		showDelay: 200,
// 		hideDelay: 400,
// 		fixed: true
// 	});



});

/*
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var pageTracker = _gat._getTracker("UA-5197893-2");
pageTracker._trackPageview();
*/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function changePic(newPic)
{
	var oldPic = document.getElementById('mainimg');
	var oldPicSrc = oldPic.src;
	
	var oldPicLink = document.getElementById('mainimglink');
	var oldPicHref = newPic.src;
	
	oldPicLink.href = newPic.src;
	oldPic.src = newPic.src;
	newPic.src = oldPicSrc;
}

window.addEvent('domready', function(){
	$$('.Tips1').each(function (e) {
		var m = e.id.match(/^tip-(\d+)$/);
		var id = m[1];
		if (!id) return;

		var content = "#tip-content-" + id;
		$(e).store('tip:title', $$(content + " div.tip-title").get('html'));
		$(e).store('tip:text', $$(content + " div.tip-text").get('html'));

		new Tips(e, {
			showDelay: 200,
			hideDelay: 400,
			fixed: true
		});
	});

});

