jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
    	jQuery("<img>").attr("src", arguments[i]);
	}
}

// what was the last page to be ajax-ed in.
var lastContent = '';
var running = true;

function rotate() {
	if(!running) return;
	next();
}
function next(){
	var $active = $('#slideshow DIV.active');
	var $active_dot = $('#dots A.current');
	
	if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
	if ($active_dot.length == 0) $active_dot = $('#dots A:last');	

	var $next =  $active.next().length ? $active.next() : $('#slideshow DIV:first');
	var $next_dot =  $active_dot.next().length ? $active_dot.next() : $('#dots A:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})

	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
		$active.css('opacity', '0');
		$active_dot.removeClass('current');
		$active_dot.css('background-position', '0 0')
	});

	$next_dot.css('background-position', '0 -16px')
	.addClass('current')
}
function backwards(){
	running = false;	
	$('.toggle').attr('src', 'js/img/play.gif');	
	var $active = $('#slideshow DIV.active');
	var $active_dot = $('#dots A.current');
	
	if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
	if ($active_dot.length == 0) $active_dot = $('#dots A:last');	

	var $prev =  $active.prev().length ? $active.prev() : $('#slideshow DIV:last');
	var $prev_dot =  $active_dot.prev().length ? $active_dot.prev() : $('#dots A:last');

	$active.addClass('last-active');

	$prev.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
		$active.css('opacity', '0');
		$active_dot.removeClass('current');
		$active_dot.css('background-position', '0 0')
	});

	$prev_dot.css('background-position', '0 -16px')
	.addClass('current')
}

$(function() {
    setInterval( "rotate()", 4000 );
});


$(document).ready(function(){

	for(i=0; i<$('#slideshow > DIV').size(); i++){
		$('#dots').append('<a href="#"></a>');
	}
	$('#dots :first').addClass('current');
	$('.control-panel').css('display','block').css('margin-top', '');
	$('#keywords').css('margin-top', '-35px');
	$('#footer').css('height', '15px');
	$('#dots').click(function(){
		
	});

	$('.toggle').click(function(){
		running = !running;
		if(running == true){
			$(this).attr('src', 'js/img/pause.gif');					
		}
		if(running == false){
			$(this).attr('src', 'js/img/play.gif');					
		}
	});	
	$('.forward').click(next);
	$('.backward').click(backwards);

    $.preloadImages(
		'/images/frame.gif',
		'/images/content/about.jpg',

		'/images/content/connaught-tea-ware.jpg',	
		'/images/content/fairmont-express-tea-ware.jpg',
		'/images/content/fairmont-tea-ware.jpg',
		'/images/content/fm-manson-tea-ware.jpg',
		'/images/content/goring-tea-ware.jpg',
		'/images/content/queen-mary-tea-ware.jpg',

		'/images/content/fm-giftware.jpg',
		'/images/content/diamond-wedding-giftware.jpg',
		'/images/content/stgeorges-giftware.jpg',
		'/images/content/highgrove-giftware.jpg',
		'/images/content/loving-cups-giftware.jpg',
		'/images/content/noel-giftware.jpg',

		'/images/content/hotelware-goring.jpg',
		'/images/content/hotelware-trinidad.jpg',
		'/images/content/hotelware-claridges.jpg',
		'/images/content/hotelware-rac.jpg',
		'/images/content/hotelware-burj.jpg',
		'/images/content/hotelware-ritz.jpg',
		'/images/content/hotelware-connaught.jpg',

		'/images/content/zoob_01.jpg',
		'/images/content/stickley_01.jpg',
		'/images/content/janconstantine-collection.jpg'
	);
	
	$('#menu li').attr('class','');

	$('#menu a').click(function(e){
		$('#menu a').css('background-color', '#271F1C').css('color','#B5B5B3');
		$(this).css('background-color', '#B5B5B3').css('color','#271F1C');
		e.preventDefault();
		if(lastContent != $(this).attr('href')){
			$('#content_container #js_content').html('<p style="text-align:center; margin-top:100px;"><img src="/images/loading.gif" alt="loading..." /></p>');
			$.ajax({
				url: $(this).attr('href'),
				success: function(content){
					$('.imagelist').html(content);			 
					$('#dots').html('');
					for(i=0; i<$('#slideshow > DIV').size(); i++){
						$('#dots').append('<a href="#"></a>');
					}
					$('#dots :first').addClass('current');
					if( $('#slideshow > DIV').size() == 0 ){
						$('.control-panel').css('display','none');
						$('#dots').css('display','none');											
					}
					else{
						$('.control-panel').css('display','block');	
						$('#dots').css('display','block');
						$('#address').css('display', 'none');
						$('#phone').html('<p>+44 (0)1782 838 000</p>');
					}	
					if($('#about').length) {
						$('#keywords').css('display', 'none');
						$('#phone').html('<p>+44 (0)1782 838 000</p>').css('margin-top', '-160px');
					}	
					else{
						$('#keywords').css('display', 'block');
						$('#phone').css('margin-top', '0px');
					}
					if($('#directions').length){
						$('#phone').html('<p id="address">ANDERTON WORKS<br />PORT STREET<br />MIDDLEPORT<br />STOKE-ON-TRENT<br>ST6 3PF<br /><br />+44 (0)1782 838 000</p>');
						$('#keywords').css('display', 'none');
						$('#phone').css('margin-top', '0px');
                        
					}
					
				}
				
			});
			lastContent = $(this).attr('href');
		}
	});




	
	
});






