var InkFrog = {
	slideTimer: null,
	slides: null,
	pagination: null,
	slideWidth: null,

	init: function() {
		var $body = $('body');

		$body.addClass('js');

		this.initUserBox();
		this.initCarousel();
		this.initTabs($body);
		this.initVideo();
	},

	initUserBox: function() {
		var $user = $('#header .user');
		var $form = $user.find('form');
		var $inputs = $form.find('input[type=text], input[type=password]');

		$inputs.each(function() {
			$(this).prev('label').css('display', 'block');

			$(this).keypress(function() {
				$(this).prev('label').css('display', 'none');
			});

			$(this).blur(function() {
				if ($(this).val() == '') {
					$(this).prev('label').css('display', 'block');
				}
			});
		});

		var $toggle = $user.find('h1 a');

		$toggle.click(function() {
			$form.slideToggle('fast',function() {$('#username').focus(); });
			return false;
		});
	},

	initCarousel: function() {
		var $carousel = $('.carousel');

		this.pagination = $carousel.find('.pagination');

		this.slides = $carousel.find('.slides');

		var liCount = this.slides.find('li').length;

		this.slideWidth = this.slides.find('li').outerWidth();
		this.slides.css('width', this.slideWidth * liCount).css('left', 0);

		this.carouselInitInterval();

		this.slides.mouseenter(function() {
				clearInterval(InkFrog.slideTimer);
			})
			.mouseleave(function() {
				InkFrog.carouselInitInterval();
			});

		this.pagination.mouseenter(function() {
				clearInterval(InkFrog.slideTimer);
			})
			.mouseleave(function() {
				InkFrog.carouselInitInterval();
			});

		this.pagination.find('a').click(function() {
			InkFrog.changeSlide($(this).attr('page'));

			return false;
		});
	},

	carouselTimer: function() {
		var currentPage = parseInt(InkFrog.pagination.find('li.current a').attr('page'));
		var max = parseInt(InkFrog.pagination.find('li').length)
		var nextPage = (currentPage == max ? 1 : currentPage + 1);

		InkFrog.changeSlide(nextPage);
	},

	carouselInitInterval: function() {
		clearInterval(this.slideTimer);
		this.slideTimer = setInterval('InkFrog.carouselTimer();', 4000);
	},

	changeSlide: function(page) {
		var indent = -(this.slideWidth * (page - 1));

		this.slides.animate({'left' : indent}, 500, null);

		this.slides.find('li').removeClass('current');
		this.slides.find('li:nth-child(' + page + ')').addClass('current');

		this.pagination.find('li').removeClass('current');
		this.pagination.find('li a[page=' + page + ']').parent().addClass('current');
	},

	initTabs: function($b) {
		var $content = $('#content');

		if ($b.hasClass('home') || $b.hasClass('products')) {
			var $sidebar = $content.find('.sidebar');
			var $main = $content.find('.main');

			$sidebar.find('li a').click(function() {
				var target = $(this).attr('href').split('#')[1];
				var $tab = $main.find('li#' + target);

				if ($tab.length > 0) {
					$main.find('ul').children('.tab').css('display', 'none');
					$tab.css('display', 'block');

					$sidebar.find('li').removeClass('selected');
					$(this).parent().addClass('selected');
				}

				return false;
			});
		}
	},

	initVideo: function() {
		$('.watch-video').click(function() {
			$('#container').append('<div id="video"><div class="inner"><h1>Smart Lister Tour:</h1><h2>Empower your listings using inkFrog\'s Smart Lister</h2><div class="video"><div class="wrap"><object width="640" height="360" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="media/video/smartlister.mov"><param name="autoplay" value="true"><param name="controller" value="false"><embed src="media/video/smartlister.mov" width="640" height="360" autoplay="true" controller="false" pluginspage="http://www.apple.com/quicktime/download/"><a href="#"><img src="media/images/video/quicktime.jpg" width="640" height="360" alt="Download QuickTime"></a></embed></object></param></div><a href="/signup.php" class="button"><img src="media/images/buttons/sign-up-for-free2.png" width="243" height="43" alt="Sign up for free!"></a><a href="/products.html" class="link">close & view products</a></div></div>');

			var $video = $('#video');

			$video.click(function() {
				$(this).fadeOut('normal', function() {
					$(this).remove();
				});
			});

			$video.fadeIn();

			return false;
		});
	}
};

$().ready(function() {
	InkFrog.init();
});

