$(function() {
	
	var $last_depth = 0;
	
	$('select').selectBox();
	
	$('#slider').nivoSlider({
		directionNav: false,
		controlNav: false,
		keyboardNav: false,
		pauseOnHover: false
	});
	
	// Trustwave append.
	$trustwave = $('#trustwave').detach();
	$trustwave.find('#trustwaveSealImage').appendTo($('#secured'));
	
	$('#keywords').focus(function() {
		if($(this).val() == 'Keywords')
			$(this).val('');
	}).blur(function() {
		if($(this).val() == '')
			$(this).val('Keywords');
	});
	
	$('#submit_keywords').click(function(e) {
		e.preventDefault();
		$(this).parents('form').submit();
		return false;
	});
	
	$('#search_category, #search_product').change(function() {
		
		if($(this).val() == '')
			return false;
		
		window.location = $(this).val();
	});
	
	$('#header h1, #header h2').hover(function() {
		$(this).css({ cursor: 'pointer' });
	}).click(function(e) {
		e.preventDefault();
		window.location = '/';
	});
	
	$('#nav li ul').parent().addClass('has-children');
	
	//$('#nav > li.has-children a')
	$('#nav li.has-children a').live('mouseenter', function() {
		
		$(this).parents('li.has-children').addClass('activated');
		$this_depth = $(this).parents('li.has-children').length;
		$target_uls = $(this).parent().find('> ul');
		$list_depth = $(this).parents('li').length;
		
		switch($this_depth) {
			case 1: {
				$target_uls.slideDown('fast');
				break;
			}
			case 2: {
				
				$target_uls.not(':visible').show('slide', { direction: 'left' }, 'fast');
				
				// Earmark parents
				$target_uls.parents('li').addClass('activated');
				
				break;
			}
		}
		
	});
	
	var $remove_class_hover = function() { $(this).parents('li.has-children').eq(0).removeClass('activated'); };
	
	$('#nav li.has-children').mouseleave(function() {
		
		// Find nearest
		$pp = $(this).find('ul:visible');
		$dp = $(this).parents('li').length;
		
		switch($dp) {
			case 0: {
				$pp.slideUp('fast', '', $remove_class_hover);
				break;
			}
			case 1: {
				$pp.hide('slide', { direction: 'left' }, 'fast', $remove_class_hover);
				break;
			}
			default: {
				return;
			}
		}
	});
	
	// Run though and position dropdowns in the right place.
	$('#nav li.has-children ul li.has-children ul').each(function(i) {
		$li_height = $(this).find('li').actual('outerHeight');
		$parent_ul_wh = $(this).parent().actual('width');
		$parent_index = $(this).parent().index();
		$(this).css({
			position: 'absolute',
			top: $li_height * $parent_index,
			left: $parent_ul_wh
		});
	});
	
	$('a.light-box').fancybox({
		'overlayColor'			: '#000',
		'overlayOpacity'		: 0.75,
		'hideOnContentClick'	: true,
		'onStart'				: function() {
			$('.qtip').hide();
		}
	});
	
	// Readonly switch.
	$('#change-address').click(function(e) {
		
		e.preventDefault();
		$fields = $(this).parents('form').find('input.readonly-field')
		$fields.each(function() {
			$(this).attr('readonly', false);
			$(this).removeClass('readonly-field');
		});
		
		$fields.eq(0).focus();
		$(this).fadeOut(250);
	});
	
	// Print page.
	$('.print-page').click(function() {
		window.print();
	});
	
	$('input.stars').rating({ 
		split: 2,
		callback: function(v) {
			$('#actual_rating').html('<span class="rating">' + v + '</span> / <span class="total">' + $('input.stars').length + '</span>').addClass('rated');
		}
	});
	
	$('a.tip').qtip({
		show: {
			event: 'click'
		},
		content: {
			attr: 'alt'
		}
	});
	
	$('input.add').click(function(e) {
		
		// AJAX.
		$form = $(this).parents('form');
		$data = $form.serialize() + '&ajax=1';

		$.ajax({
			type: 'post',
			url: $form.attr('action'),
			data: $data,
			success: function(data) {

				switch(data.status_code) {
					case 'OK': {
						
						// Update basket totals.
						$('#basket .items').html(data.new_items);
						$('#basket .value').html(data.new_value);
						$('#tooltip').html(data.tooltip_html);
						
						break;
					}
					default: {
						
						return;
					}
				}
			},
			error: function(data) {
				$('#tooltip').html('Error. Check JSON response');
			},
			dataType: 'json'
		});

		e.preventDefault();
		
	}).qtip({
		show: {
			event: 'click'
		},
		hide: false,
		content: {
			text: $('#tooltip')
		},
		position: {
			my: 'bottom center',
			at: 'top center',
			target: $('input.add'),
			adjust: {
				y: -5
			}
		},
		style: {
			classes: 'ui-tooltip-majestic'
		}
	});
	
	$('span.close a').live('click', function(e) {
		$('input.add').qtip('hide');
		e.preventDefault();
	});
	
	$('.productInfo a.light-box').qtip({
		content: {
			text: 'Due to technological differences with computer displays, colours shown may differ from the actual towel colours. If in doubt please ask for actual towel swatches in the colour(s) you have selected.'
		},
		position: {
			my: 'bottom center',
			at: 'top center',
			target: false,
			adjust: {
				y: -5
			}
		},
		style: {
			classes: 'ui-tooltip-majestic ui-tooltip-majestic-text'
		}
	});
	
});
