$(document).ready(function(){

	/**
	 * The basics
	 *
	 */
	// Beautify fonts where necessary
	Cufon.replace('h1, h2, footer dt, .cufon, #checkout-nav li, .heading, .panel-header h3');
	Cufon.replace('#checkout-row a, #continue-button, #login-submit, .arrow-button', {hover: true, hoverables: { a: true, button: true }});

	// Auto empty form fields
	$('#search input, .form-box input').emptyOnFocus();

	// External links in new window
	$('a[rel=external]').click(function(){
		url = $(this).attr('href');
		window.open(url);
		return false;
	});

	// Site navigation
	$('nav ul.sf-menu').superfish({
		autoArrows: false
	});

	// Pretty scroll bars on size panel content
	maxHeight = 220;
	if($('#size-header').next().find('ul').height() > maxHeight){
		$('#size-header').next().find('ul').css({ 'max-height': maxHeight + 'px', 'overflow': 'auto' }).jScrollPane();
	}

	/**
	 * Category list behaviour
	 *
	 */
	useClass = $('#cat-list li.use').attr('class');
	$('#cat-list li').hide();
	if(useClass !== undefined){
		classes = useClass.split(/\s+/);
		currentGender = classes[0];
		$('#cat-list li.' + currentGender).show().not(':first').addClass('indent');
	}
	$('#cat-list li.boys:first, #cat-list li.girls:first').show();

	/**
	 * Homepage
	 *
	 */
	if($('body#home').length > 0){
		// Banner cycle
		$('#banners').cycle({
			pager:  '#banner-nav'
		});

		// Featured products carousel
		$('#featured .items').jCarouselLite({
			btnNext: '.next',
			btnPrev: '.prev',
			visible: 4,
			scroll: 2,
			circular: false
		});
	}

	/**
	 * Catalog
	 *
	 */
	$('.tiny-product-list-item, .product-list-item').css('cursor', 'pointer').click(function(){
		window.location = $('a', this).attr('href');
	});

	/**
	 * Login page
	 *
	 */
	if($('body#login').length > 0){
		// Auto empty form fields
		if($('#login-email').val() == 'email address'){
			$('#login-email').emptyOnFocus();
		}

		$('#login-password-empty').show();
		$('#login-password').hide();

		$('#login-password-empty').focus(function() {
			$('#login-password-empty').hide();
			$('#login-password').show();
			$('#login-password').focus();
		});
		$('#login-password').blur(function() {
			if($('#login-password').val() == '') {
				$('#login-password-empty').show();
				$('#login-password').hide();
			}
		});

		setTimeout(function(){ if($('#login-password').val() !== '') {
			$('#login-password-empty').hide();
			$('#login-password').show();
		} }, 250);

	}

	/**
	 * Account pages
	 *
	 */
	$('#my-account-home-boxes div').css('cursor', 'pointer').click(function(){
		window.location = $('a', this).attr('href');
	});

});

/**
 * Functions & jQuery plugins
 *
 */
$.fn.emptyOnFocus = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			$(this).addClass('active');
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).removeClass('active');
		}
	});
};
