function myCreateElement (element) {
	if (document.createElementNS) {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	} else {
		return document.createElement(element);
	}
}

function laImportNode (node, allChildren) {
	/* find the node type to import */
	/* from alistapart, modified to support XHTML */
	switch (node.nodeType) {
		case document.ELEMENT_NODE:
			/* create a new element */
			var newNode = myCreateElement(node.nodeName);
			/* does the node have any attributes to add? */
			if (node.attributes && node.attributes.length > 0)
				/* add all of the attributes */
				for (var i = 0, il = node.attributes.length; i < il;)
					newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
			/* are we going after children too, and does the node have any? */
			if (allChildren && node.childNodes && node.childNodes.length > 0)
				/* recursively get all of the child nodes */
				for (var i = 0, il = node.childNodes.length; i < il;)
					newNode.appendChild(laImportNode(node.childNodes[i++], allChildren));
			return newNode;
			break;
		case document.COMMENT_NODE:
			return document.createTextNode("");
			break;
		case document.TEXT_NODE:
		case document.CDATA_SECTION_NODE:
			return document.createTextNode(node.nodeValue);
			break;
	}
};

var slideshows = [];

function updateText (slideshow) {
	while (slideshow.text.hasChildNodes()) {
		slideshow.text.removeChild(slideshow.text.firstChild);
	}
	var newText = document.createTextNode(" " + (slideshow.current + 1) + " / " + slideshow.totalItems + " ");
	slideshow.text.appendChild(newText);
}


function slidePrev () {
	if ( this.current == 0 ) {
		this.current = this.totalItems - 1; // last item index
	} else {
		this.current--;
	}

	YAHOO.util.Dom.removeClass( this.items, 'current' );
	YAHOO.util.Dom.addClass( this.items[this.current], 'current' );

	updateText(this);
}

function slideNext () {
	if ( this.current == (this.totalItems - 1) ) {
		this.current = 0;
	} else {
		this.current++;
	}

	YAHOO.util.Dom.removeClass( this.items, 'current' );
	YAHOO.util.Dom.addClass( this.items[this.current], 'current' );

	updateText(this);
}

// "conditional" in that slideshows aren't created unless there are at least 2 items in the slideshow list.
function conditional_create_slideshow(ulElement) {
	listItems = YAHOO.util.Dom.getChildrenBy( ulElement, function (thisNode) {
		if (thisNode.nodeName == "li" || thisNode.nodeName == "LI") {
			return true;
		} else {
			return false;
		}
	} );

	if ( listItems.length < 2 ) return; // see?

	YAHOO.util.Dom.removeClass( listItems, 'current' );
	YAHOO.util.Dom.addClass( listItems[0], 'current' );

	var slidenav = myCreateElement('p');
	YAHOO.util.Dom.addClass( slidenav, 'slidenav');

	var prev = myCreateElement('a');
	prev.setAttribute('href', '#');
	prev.onclick = function(){ return false; }
	prev.appendChild(
		document.createTextNode("<<")
	);

	var text = myCreateElement('span');
	text.appendChild(
		document.createTextNode(" 1 / " + listItems.length + " ")
	);

	var next = myCreateElement('a');
	next.setAttribute('href', '#');
	next.onclick = function(){ return false; }
	next.appendChild(
		document.createTextNode(">>")
	);

	slidenav.appendChild( prev );
	slidenav.appendChild( text );
	slidenav.appendChild( next );

	ulElement.insertBefore( slidenav, ulElement.firstChild );

	var thisSlideshow = {
		ulElement:	ulElement,
		items:		listItems,
		totalItems:	listItems.length,
		prev:		prev,
		next:		next,
		text:		text,
		current:	0
	};

	YAHOO.util.Event.addListener( prev, 'click', slidePrev, thisSlideshow, true );
	YAHOO.util.Event.addListener( next, 'click', slideNext, thisSlideshow, true );

	slideshows.push(thisSlideshow);
}

var cart_callback = {
	success: function(o) {
		cartDiv = YAHOO.util.Dom.get('cart');
		while (cartDiv.hasChildNodes()) {
			cartDiv.removeChild(cartDiv.firstChild);
		}
		cartDiv.appendChild( laImportNode(o.responseXML.firstChild, true) );
		
	},
	failure: function(o) {
	}
}

function purchase_form_submit(e, obj) {
	YAHOO.util.Connect.setForm(this);
	var trans = YAHOO.util.Connect.asyncRequest('POST', '/', cart_callback, "ajax=yes");
}

function cart_form_update(e, obj) {
	YAHOO.util.Connect.setForm(this);
	var trans = YAHOO.util.Connect.asyncRequest('POST', '/', cart_callback, "ajax=yes&update_cart=update_cart");
}

function init() { 
	// since the javascript is running, remove the header from each category (posters, shirts, etc)
	var elementsToHide = YAHOO.util.Dom.getElementsByClassName("hide-if-javascript");
	YAHOO.util.Dom.setStyle( elementsToHide, "display", "none");

	var slideshowElements = YAHOO.util.Dom.getElementsByClassName("slideshow");
	for ( var i = 0; i < slideshowElements.length; i++ ) {
		conditional_create_slideshow( slideshowElements[i] );
	}

	var purchaseForms = YAHOO.util.Dom.getElementsByClassName("purchase-form");
	for ( var i = 0; i < purchaseForms.length; i++ ) {
		purchaseForms[i].onsubmit = function(){return false;};
		YAHOO.util.Event.addListener(purchaseForms[i], "submit", purchase_form_submit );
	}

        /*
	var cartForm = YAHOO.util.Dom.get("cart-form");
	YAHOO.util.Event.addListener(cartForm, "change", cart_form_update ); */

	/* is this stuff defined? */
	/* from alistapart */
	if (!document.ELEMENT_NODE) {
		document.ELEMENT_NODE = 1;
		document.ATTRIBUTE_NODE = 2;
		document.TEXT_NODE = 3;
		document.CDATA_SECTION_NODE = 4;
		document.ENTITY_REFERENCE_NODE = 5;
		document.ENTITY_NODE = 6;
		document.PROCESSING_INSTRUCTION_NODE = 7;
		document.COMMENT_NODE = 8;
		document.DOCUMENT_NODE = 9;
		document.DOCUMENT_TYPE_NODE = 10;
		document.DOCUMENT_FRAGMENT_NODE = 11;
		document.NOTATION_NODE = 12;
	}

        selectors = YAHOO.util.Dom.getElementsByClassName('sku_selector', 'select');
	for(var i in selectors) {
	    var s = selectors[i];
	    var targId = 'buttonFor.' + selectors[i].id;
	    xkcdUpdateSelection(s, targId);
	}
} 

YAHOO.util.Event.onDOMReady(init); 
