var portfolio = {
	openedItem: null,
	init: function (){
		var items = $('portfolio').$$('li');
		for (var i=0, li; li = $(items[i]); i++) {
			var div = $(li.$$('div')[0]);
			var a = $(div.$$('a')[0]);
			a.addEvent ('click',function(e){
				(typeof this.parentNode.expanded == 'undefined' || !this.parentNode.expanded) ?
					portfolio.expand(this.parentNode) :
					portfolio.contract(this.parentNode);
				What.Event.preventDefault(e);
				return false;
			});
		}
	},
	expand: function (elm){
		if (!elm.busy) {
			var ani = new What.Animation.CSS (elm,'height',272,10);
			ani.ease = 'out';
			ani.onAnimationStart = function (){
				elm.busy = true;
				/* fix awkward IE rendering bug */
				$(elm.parentNode.parentNode).addClass('iefix-portfolio-image');
				if (portfolio.openedItem) {
					portfolio.contract (portfolio.openedItem);
				}
			};
			ani.onAnimationEnd = function (){
				portfolio.openedItem = elm;
				elm.expanded = true;
				elm.busy = false;
			};
			ani.init();
		}
	},
	contract: function (elm){
		if (!elm.busy) {
			var ani = new What.Animation.CSS (elm,'height',100,10);
			ani.ease = 'in';
			ani.onAnimationStart = function (){
				elm.busy = true;
			};
			ani.onAnimationEnd = function (){
				elm.expanded = false;
				elm.busy = false;
			};
			ani.init();
		}
	}
};

/*
pullquote function by Roger Johansson, http://www.456bereastreet.com/
*/
var pullquote = {
	init : function() {
	// Check that the browser supports the methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oPullquote, oPullquoteP, oQuoteContent, i, j;
	// Find all span elements with a class name of pullquote
		var arrElements = document.getElementsByTagName('span');
		var oRegExp = new RegExp("(^|\\s)pullquote(\\s|$)");
		for (i=0; i<arrElements.length; i++) {
	// Save the current element
			oElement = arrElements[i];
			if (oRegExp.test(oElement.className)) {
	// Create the blockquote and p elements
				oPullquote = document.createElement('blockquote');
				oPullquote.className = oElement.className
				oPullquoteP = document.createElement('p');
				oPullquoteP.appendChild(document.createTextNode('"'));
	// Insert the pullquote text
				for(j=0;j<oElement.childNodes.length;j++) {
					oPullquoteP.appendChild(oElement.childNodes[j].cloneNode(true));
				}
				oPullquoteP.appendChild(document.createTextNode('"'));
				oPullquote.appendChild(oPullquoteP);
	// Insert the blockquote element before the span elements parent element
				oElement.parentNode.parentNode.insertBefore(oPullquote,oElement.parentNode);
			}
		}
	}
};

pullquote.init();

if ($('portfolio'))
	portfolio.init();
