var Deck = {
  initialize: function(deck){
	control=$(deck.id+"_control");
	Element.cleanWhitespace(control);

	num=control.childNodes.length;
	tabwidth=Math.floor(100000/num)/1000; // figure out percentage widths to 3 decimal places
	children = $A(control.childNodes);
	tabstotal=0;
	children.each(function(card){
//		if (card.nodeType == 1){ // make sure no whitespace/text nodes // redundant?
		tabstotal=tabstotal+tabwidth;
		thiscardwidth=(tabstotal>100)?tabwidth-(tabstotal-100):tabwidth;
		card.style.width = thiscardwidth+'%';
//		}
	});
	if (navigator.appVersion.match(/\bMSIE\b/)) // ie fix
		children[num-1].style.marginRight='-5%'; // doesn't seem to effect anything but stops intermittent wrapping in IE
  },
  topCard: function(deckdiv,topcard) {
	deckdiv = $(deckdiv);
	deck = $(deckdiv.id + "_deck");
	control = $(deckdiv.id + "_control");
	
	Element.cleanWhitespace(deck);
	children = $A(deck.childNodes);
	children.each(function(card){
//		if (card.nodeType == 1){ // make sure no whitespace/text nodes // redundant?
			if (card.id == topcard)
				Element.addClassName(card,"top");
			else
				Element.removeClassName(card,"top");
//		}
	});

//	Element.cleanWhitespace(control); // already done during initialization
	children = $A(control.childNodes);
	children.each(function(tab){
//		if (tab.nodeType == 1){ // make sure no whitespace/text nodes // redundant?
			if (tab.id == topcard+"_label")
				Element.addClassName(tab,"top");
			else
				Element.removeClassName(tab,"top");
//		}
	});
  }
}
BehaviourRules = {
	'div.deckdiv' : function(deck){
		Deck.initialize(deck);
	},
	'div.deckdiv ul.control li a' : function (lnk) {
		thedeck = Element.getParent(lnk,"div");
		var cardid = unescape(lnk.href.substring(lnk.href.indexOf("?")+1,lnk.href.length).toQueryParams()[thedeck.id]);
		var label = Element.getParent(lnk,"LI");
		label.c = cardid;
		lnk.c = cardid;
		function clickPage(d,c){
			card = $(d.id + "_" + c);
			Deck.topCard(d,card.id); // takes care of cleaning whitespace
			if ((Element.empty(card))||(Element.hasClassName(card,"nocache"))){
				var jsonobject = new Object;
				jsonobject["id"] = d.id;
				jsonobject["card"] = c;
				gotourl = getAjaxURL();
				var myAjax = new Ajax.Request(gotourl, {method: 'post', postBody: Object.toJSON(jsonobject), 
					onComplete: function(e){
						AJAX.showResponse(e);
	//					Behaviour.applyToId(tablediv.id);
						AJAX.complete(d);
					},
					onLoading: function(e){
						AJAX.loading(d);
					},
					onFailure: AJAX.reportError, contentType: 'text/plain; charset=UTF-8'} );
			}
		}
		Event.observe(lnk, "click", function(e){
			lnk.blur();
			clickPage(thedeck,lnk.c);
			Event.stop(e);
		});
		Event.observe(label, "click", function(e){
			label.blur();
			clickPage(thedeck,label.c);
			Event.stop(e);
		});
		Event.observe(label, "mouseover", function(e){
			Element.addClassName(label,"over");
		});
		Event.observe(label, "mouseout", function(e){
			Element.removeClassName(label,"over");
		});
	}
};
Behaviour.load(BehaviourRules);