EXP = {};

EXP.loadFunctions = function() {
	// if current page is books
	if ($('booksPage')) {
		// hide book descriptions and set up 'more' links to reveal them
		var moreLinks = $$('a.moreToggle');
		var moreDescriptions = $$('p.description');
		moreDescriptions.each(function(i){
			i.hide();
		}.bind(this));
		moreLinks.each(function(i, index){
			i.show();
			i.up().style.display = "inline";
			var hasBuyLinks = false;
			var listId = "list" + index;
			i.up().up().id = listId;
			var selectorString = "#" + listId + " li";
			$$(selectorString).each(function(j){
				if (j.className == "buyLinks") {
					hasBuyLinks = true;	
				}
			}.bind(this));
			var bullet = new Element('span');
			if (hasBuyLinks) {
				bullet.update(" &middot; ");
			}
			i.up().insert(bullet);
			var linkId = i.id;
			linkId = linkId.replace(/^more/, "#book");
			i.href = linkId;
			i.target = "_self";
			var descs = i.up().getElementsBySelector('p.description');
			var desc = descs[0];
			i.onclick = function() {
				desc.toggle();
				bullet.toggle();
			}.bind(this);
		}.bind(this));
		
		// hide links to online retailers and set up 'buy' links to reveal them
		var buyLinks = $$('a.buyToggle');
		var buyLists = $$('ul.buyList');
		buyLists.each(function(i){
			i.hide();
		}.bind(this));
		buyLinks.each(function(i){
			i.show();
			i.up().style.display = "inline";
			var linkId = i.id;
			linkId = linkId.replace(/^buy/, "#book");
			i.href = linkId;
			i.target = "_self";
			var lists = i.up().getElementsBySelector('ul.buyList');
			var list = lists[0];
			i.onclick = function() {
				list.toggle();
			}.bind(this);
		}.bind(this));
		
		// convert book features to two-column layout
		var bookPods = $$('div.bookPod');
		var floatValue = "right";
		bookPods.each(function(i, index){
			floatValue = (floatValue == "right" ? "left" : "right");
			i.style.width = "300px";
			i.style.cssFloat = floatValue;
			i.style.styleFloat = floatValue;
			if (floatValue == "right" || index == bookPods.length - 1) {
				var clearingDiv = new Element('div');
				clearingDiv.className = "clear";
				i.insert({
					after : clearingDiv
				});
			}
		}.bind(this));
		
		/*var bookPodsF09 = $$('div.w10');
		var floatValue = "right";
		bookPods.each(function(i, index){
			floatValue = index == 0 ? "left" : (floatValue == "right" ? "left" : "right");
			i.style.width = "300px";
			i.style.cssFloat = floatValue;
			i.style.styleFloat = floatValue;
			if (floatValue == "right" || index == bookPods.length - 1) {
				var clearingDiv = new Element('div');
				clearingDiv.className = "clear";
				i.insert({
					after : clearingDiv
				});
			}
		}.bind(this));
		
		var bookPodsF09 = $$('div.f09');
		var floatValue = "right";
		bookPods.each(function(i, index){
			floatValue = index == 0 ? "left" : (floatValue == "right" ? "left" : "right");
			i.style.width = "300px";
			i.style.cssFloat = floatValue;
			i.style.styleFloat = floatValue;
			if (floatValue == "right" || index == bookPods.length - 1) {
				var clearingDiv = new Element('div');
				clearingDiv.className = "clear";
				i.insert({
					after : clearingDiv
				});
			}
		}.bind(this));*/
		
	} // end books page if
}

Event.observe(window, "load", EXP.loadFunctions);