(function ($) {

	$.extend($.expr[':'], {
		external: function (o) {
			return o.hostname !== window.location.hostname && o.hostname !== '';
		}
	});
	$.fn.external = function () {
		return this.filter(':external');
	};

	$(window).load(function () {
	
		$('#products-page .product').each(function (i, e) {

			var self = $(e),
			img = $('img', self),
			h = img.height(),
			w = img.width(),
			new_h = Math.ceil(h * 1.15),
			new_w = Math.ceil(w * 1.15),
			pos = img.position(),
			t = pos.top,
			l = pos.left,
			desc = $('.product-description', self);
			
			img.bind('mouseenter', function () {
				self.css({
					zIndex: 1000
				});			
				img.stop().css({
					zIndex: 1000
				}).animate({
					height: new_h,
					left: 0 - (new_w - w) / 2,
					top: 0 - (new_h - h) / 2,
					width: new_w
				}, function () {
					img.height('auto');
					desc.show();
				});
			});
			
			img.bind('mouseleave', function () {
				
				self.css({
					zIndex: 1
				});
				desc.hide();
				img.css({
					zIndex: 1
				}).animate({
					height: h,
					left: l,
					top: t,
					width: w
				}, function () {
					desc.hide();
				});
			});
		
		});
		
	});

	$(function () {
		
		//Placeholder
		if (!Modernizr.input.placeholder) {
			$('input').each(function () {
				var $this = $(this),
				placeholder = $this.attr('placeholder');
				$this.bind({
					focus: function () {
						if ($this.val() === placeholder) {
							$this.val('');
						}
					},
					blur: function () {
						if ($this.val() === '') {
							$this.val(placeholder);
						}
					}
				}).val(placeholder);
			});
		}
		
		$('.product:nth-child(3n)').addClass('last');
		
		//Home Page Images
		$('#home-page .product').cycle();
		$('#products-page #banner').cycle({
			slideExpr: 'img'
		});
		
		var nav_li = $('nav li');
		nav_li.filter(':first').addClass('first');
		nav_li.filter(':last').addClass('last');
		
		//CAPTCHA Info
		var captcha_info = $('#captcha-info');
		captcha_info.hide();
		$('#whats-this').hover(function () {
			captcha_info.show();
		}, function () {
			captcha_info.hide();
		});
		
		//Form Validation
		$('#contact-us').validate();
		
		//CAPTCHA
		$('#refresh').click(function (e) {
			var generator = '/lib/captcha/generate.php?' + Math.round(Math.random()*100);
			$('#captchacode').attr('src', generator);
			e.preventDefault();
		});
	
		//External links
		$('a:external').click(function (e) {
			window.open($(this).attr('href'));
			e.preventDefault();
		});
		
	});
}(jQuery));
