(function ($) {

	$.fn.defaultText = function () {
		return this.focus(function () {
			var $this = $(this);
			if ($this.hasClass('empty')) $this.removeClass('empty').val('');
		}).blur(function () {
			var $this = $(this);
			if (!$.trim($this.val())) {
				$this.addClass('empty').val($this.attr('title'));
			}
		}).blur();
	};

	$(function () {
		//on document
		$('.keywords').defaultText();
		$('.carousel').each(function () {
			var $this = $(this);
			var $nav = $this.find('> .nav');
			var $items = $this.find('.items .item');
			var $options_tmpl = $nav.find('.tmpl');
			var $current = null;
			var options_array = [];
			var animating = false;
			var $que = null;
			var interval_id = 0;
			function switchCarouselItemTo($next) {
				var $dir = 'left';
				var auto = false;
				if ($next == null) {
					if ($current.nextAll().size() > 0) {
						$next = $current.next();
					} else {
						$next = $($items.get(0));
					}
					auto = true;
				}
				if ($next.get(0) == $current.get(0)) return;

				if (!auto && $next.prevAll().prevAll().size() < $current.prevAll().size()) {
					$dir = 'right';
				}
				clearTimeout(interval_id);
				if (animating) {
					$que = $next;
					return;
				}
				animating = true;
				var n = $next.prevAll().size();
				$next.css({
					left: $dir == 'right' ? '-625px' : '625px',
					display: 'block'
				});
				$current.css({
					left: 0
				});

				$next.animate({ left: 0 }, 1000, function () {
					animating = false;
					if ($que) {
						switchCarouselItemTo($que);
						$que = null;
					} else {
						interval_id = setTimeout(function () { switchCarouselItemTo(); }, 5000);
					}
				});

				$current.animate({ left: $dir == 'right' ? '625px' : '-625px' }, 1000, function () {
					$(this).css({ display: 'none' });
				});

				$nav.find('a:eq(' + n + ')').addClass('selected').siblings().removeClass('selected');
				$current = $next;
			}

			$items.each(function (n) {
				if (n > 0) $(this).css({ display: 'none' });
				else $current = $(this);
				options_array.push({
					item_no: n
				});
			});

			$nav.append($options_tmpl.tmpl(options_array).each(function (n) {
				$(this).data('n', n);
				if (n == 0) $(this).addClass('selected');
				$(this).click(function (e) {
					e.preventDefault();
					var $item = $($items.get($(this).data('n')));
					switchCarouselItemTo($item);
				});
			}));
			interval_id = setTimeout(function () { switchCarouselItemTo(); }, 2000);
		});
		$('.subscribe form').submit(function (e) {
			e.preventDefault();
			var $form = $(this);
			var $sucess_message = $form.siblings('.success_message');
			var $fail_message = $form.siblings('.fail_message');
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			var email = $('#SubscribeEmail');
			var valid = true;
			if (!emailReg.test(email.val())) {
				email.addClass('error');
				valid = false;
			} else {
				email.removeClass('error');
			}
			if (valid) {
				$.getJSON('subscribe.asp', { email: email.val() }, function (data) {
					$form.animate({ opacity: 0 }, 500, function () {
						$(this).css({ display: 'none' });
					});
					if (data.status == "Success") {
						$sucess_message.fadeIn(500);
					} else {
						$fail_message.fadeIn(500);
					}
				});
			}
		});
	});

})(jQuery);
