var $j = jQuery.noConflict();

$j(function() {

	if ($j("#folio").length > 0) folio(500, 0.6); // portfolio sleek image shade (timer, opacity)

	if ($j("div.item p.tags a", "#folio").length > 0) folioFilter(); // portfolio item filter

	
});

function folio(time, opacity) {
	$j("img", "#folio")
		.css("opacity", 1)
		.hover(
			function() {
				$j(this).stop().animate({ opacity: opacity }, time);
			},
			function() {
				$j(this).stop().animate({ opacity: 1 }, time);
			}
		);
}

function folioFilter() {
	var tags = getAllTags();
	var ph = $j(".placeholder", "#filter");
	$j(tags).each(function(i, tag) {
		ph.append(', <a href="#">' + tag + '</a>');
	});

	$j(".placeholder a", "#filter").click(function() {

		$j(".placeholder a.active", "#filter").removeClass("active");
		$j(this).addClass("active");

		folioClick($j(this));
		return false;
	});

	$j("#filter", "#folio").show();

	// select by the #hash in the url
	var hash = trim(window.location.hash.substring(1));
	if (hash.length > 0) {
		$j(".placeholder a:contains('" + hash + "'):first", "#filter").click();
	}
}

{ // folioFilter functions:

	function folioClick(obj) {
		var tag = obj.text();
		var rel = obj.attr("rel");

		folioHideAll();

		if (rel == "Start")
			folioShow();
		else
			folioShow(tag);

		folioClearer();
	}

	function folioClearer() {
		var visibleItems = $j("div.item:visible", "#folio");
		var lastIndex = visibleItems.length - 1;
		visibleItems.each(function(i, item) {
			if (i % 3 == 2 && i != lastIndex) $j('<div class="clear"> </div>').insertAfter(item);
		});
	}

	function folioShow(tag) {
		if (tag)
			$j("div.item:has(p.tags a:contains('" + tag + "'))", "#folio").show();
		else
			$j("div.item", "#folio").show();
	}

	function folioHideAll() {
		$j("div.item", "#folio").hide();
		$j("#folio > div.clear:not(:last)").remove();
	}

	function getAllTags() {
		var tags = [];
		$j("div.item p.tags a", "#folio").each(function() { tags.push(trim($j(this).text())); });
		return tags.unique().sort();
	}

}
