/*
 * jQuery Extra Selectors - (c) Keith Clark freely distributable under the terms of the MIT license.
 * 
 * twitter.com/keithclarkcouk
 * www.keithclark.co.uk
 */

(function($) {
	function getNthIndex(cur, dir) {
		var t = cur, idx = 0;
		while (cur = cur[dir] ) {
			if (t.tagName == cur.tagName) {
				idx++;
			}
		}
		return idx;
	}

	function isNthOf(elm, pattern, dir) {
		var position = getNthIndex(elm, dir), loop;
		if (pattern == "odd" || pattern == "even") {
			loop = 2;
			position -= !(pattern == "odd");
		} else {
			var nth = pattern.indexOf("n");
			if (nth > -1) {
				loop = parseInt(pattern, 10) || parseInt(pattern.substring(0, nth) + "1", 10);
				position -= (parseInt(pattern.substring(nth + 1), 10) || 0) - 1;
			} else {
				loop = position + 1;
				position -= parseInt(pattern, 10) - 1;
			}
		}
		return (loop<0 ? position<=0 : position >= 0) && position % loop == 0
	}

	var pseudos = {
		"first-of-type": function(elm) {
			return getNthIndex(elm, "previousSibling") == 0;
		},
		"last-of-type": function(elm) { 
			return getNthIndex(elm, "nextSibling") == 0;
		},
		"only-of-type": function(elm) { 
			return pseudos["first-of-type"](elm) && pseudos["last-of-type"](elm);
		},
		"nth-of-type": function(elm, i, match) {
			return isNthOf(elm, match[3], "previousSibling");
		},
		"nth-last-of-type": function(elm, i, match) {
			return isNthOf(elm, match[3], "nextSibling");
		}		
	}
	$.extend($.expr[':'], pseudos);
}(jQuery));

/* ------ Allow jQuery to set CSS3 properties with vendor prefixes ------ */

(function($) {
	var cssName, propName, test, prefix, vendorTest
	var prefixes = {"Moz": "-moz", "O": "-o", "Webkit": "-webkit", "ms": "-ms", "Khtml": "-khtml"};
	var testElm = $("<div>");
	var tests = {
		"transform": "rotate(0)",
		"boxShadow": "0 0 0 #000"
	}
	
	for (test in tests) {
		cssName = test.replace(/[A-Z]/g, function(m) { return "-" + m.toLowerCase() });
		propName = test.charAt(0).toUpperCase() + test.substring(1);
		for (prefix in prefixes) {
			vendorTest = prefix + propName;
			testElm[0].style.cssText = prefixes[prefix] + "-" + cssName + ":" + tests[test];
			if (testElm.css(vendorTest)) {
				$.cssProps[test] = vendorTest;
				break;
			}
		}
	}
})(jQuery);

/* ----------------------------- Lightbox ------------------------------- */

(function ($) {

	var $modalOverlay = $("<div>").
		css({background: "rgba(0,0,0,0.85)", zIndex:200000}).
		attr("id", "modal").
		click( function() {
			$modalDialog.animate( { opacity: 0 }, function() {
				$modalDialog.detach()
				$modalOverlay.animate({ opacity: 0 }, function() {
					$modalOverlay.detach()
				})			
			})
		});
		
	var $modalDialog = $("<div>").
		css({
			padding: "10px",
			background: "#111",
			borderRadius: "8px",
			boxShadow: "0 0 5px #000"
		}).
		attr("id", "lightbox");
	/*
	var $close = $("<a>").css({
		position: "absolute",
		bottom:"-1.7em",
		width:"6em",
		background: "#111",
		left: "50%",
		marginLeft: "-3em",
		textAlign: "center",
		color: "#fff",
		borderRadius: "8px",
		padding: "0.5em 0 0.25em 0",
		opacity:0,
	}).text("Close").appendTo($modalDialog)
	*/
	var $image = $("<img>").
		load( function() {
			$image.css("display", "block");
			var x = {
					width: ($image.outerWidth()),
					height: ($image.outerHeight()),
					marginTop: -($image.outerHeight()/2) - ($modalDialog.innerHeight() - $modalDialog.height())/2,
					marginLeft: -($image.outerWidth()/2) - ($modalDialog.innerWidth() - $modalDialog.width())/2
				}
			$modalDialog.
				animate(x, function() {
					$image.
					animate({
						opacity: 1
					},250);
			});
		}).
		error( function() {
			if ($(this).attr("src")!="") {
				$image.attr("src", $image.attr("src").replace("-large.", "."));
			}
		}).
		click( function() {
			$modalDialog.stop().animate( { opacity: 0 }, function() {
				$modalDialog.detach()
				$modalOverlay.stop().animate({ opacity: 0 }, function() {
					$modalOverlay.detach()
				})			
			})
		}).
		appendTo($modalDialog);
	
	
	$.fn.lightbox = function () {
	
		return this.each(function () {
			
			$(this).click( function() {
				var $thumb = $(this)
				if ($modalOverlay.parent().length==0) {
					$image.css({opacity: 0, display:"none"}).attr("src","");
					$modalOverlay.
					css({
						position: "fixed",
						zIndex: 19998,
						top: 0,
						left: 0,
						height: "100%",
						width: "100%",
						opacity: 0
					}).
					appendTo( document.body ).
					animate( { opacity: 0.75 }, function() {
						$modalDialog.
						css({
							position: "fixed",
							zIndex: 19999,
							opacity: 0,
							left: "50%",
							top: "50%",
							height: "200px",
							width: "400px",
							marginLeft: "-200px",
							marginTop: "-100px"
						}).
						appendTo( document.body ).
						css({
							marginLeft: -($modalDialog.outerWidth()/2),
							marginTop: -($modalDialog.outerHeight()/2)
						}).
						animate({ opacity: 1 }, function() {
							$image.attr("src", $thumb.attr("src").replace(".", "-large."));
						});
					});
				}
			});
		});
	};
})(jQuery);


/* -------------------------- Scroll to anchors ------------------------- */

(function ($) {
	$.fn.scrollTo = function() {
		return this.click(function (e) {
			var st=0, et = $(this.hash).offset().top;
			e.preventDefault();
			$('html,body').each( function() {
				st=Math.max(this.scrollTop,st)
			}).animate({scrollTop: et}, 250+(Math.abs(st-et)/10));
		} )
	}
})(jQuery);
