$(document).ready(function() 
{
/*
	$([
	'images/bg.jpg',
	'images/main.png',
	'images/menu/over/home.png',
	'images/menu/over/bedlinen.png',
	'images/menu/over/bedspreads.png',
	'images/menu/over/pillows.png',
	'images/menu/over/rugs.png',
	'images/menu/over/towels.png',
	'images/menu/over/other.png',
	'images/menu/over/about.png',
	'images/menu/over/about.png',
	'images/menu/over/retailers.png',
	'images/menu/over/contact.png'
	]).preload();
*/

	var hash = window.location.hash.substr(1);
	
	if ( hash != 'home' && hash != '' && hash != 'about' && hash != 'retailers' && hash != 'contact' && hash != 'press' )
	{
		toggleCollections( );	
	}
	
	if ( hash != '' )
	{
		var href = $('#menu a').each(function()
		{
			var href = $(this).attr('href');
			if(hash==href.substr(0,href.length-4))
			{
				var toLoad = hash+'.html #content > *';
				$('#content').load(toLoad);
			}											
		});
	}
	
	$('#container a').click(function()
	{
		var href = $(this).attr('href');
		if ( href == '#' ) return false;
		var toLoad = href.substr(0,href.length-4)+'.html #content > *';
		$("#content").hide('');
		$("#content").html('');
		$("#content").load(toLoad, function(){ $("#content").show();});
		
		
		
		/*
		$('#content').text('');
		$('#content').hide('fast',loadContent);
		$('#load').remove();
		$('#contentwrapper').append('<span id="load"></span>');
		$('#load').fadeIn('normal');
		window.location.hash = href.substr(0,href.length-4);
		function loadContent() 
		{
			$('#content').load(toLoad,'',showNewContent());
		}
		function showNewContent() 
		{
			$('#content').show('fast', hideLoader());
		}
		function hideLoader() 
		{
			$('#load').fadeOut('fast');
		}
		*/
		return false;
		
	});
	
	$('#submenu a').live('click', function()
	{
		var href = $(this).attr('href');
		if ( href == '#' ) return false;
		var toLoad = href.substr(0,href.length-4)+'.html #content > *';
		$('#content').text('');
		$('#content').hide('fast',loadContent);
		$('#load').remove();
		$('#contentwrapper').append('<span id="load"></span>');
		$('#load').fadeIn('normal');
		window.location.hash = href.substr(0,href.length-4);
		function loadContent() 
		{
			$('#content').load(toLoad,'',showNewContent());
		}
		function showNewContent() 
		{
			$('#content').show('fast',hideLoader());
		}
		function hideLoader() 
		{
			$('#load').fadeOut('fast');
		}
		return false;
		
	});
	
	$("#smallpic a").live('click', function() 
	{	
		var mainImage = $(this).attr("href"); //Find Image Name
		$("#largepic img").attr({ src: mainImage });
		return false;		
	});
	
	$('#retailers ul > li > ul > li a').live('click', updateStores);
	
});


function changePic()
{
	$("#smallpic a").click(function() 
	{	
		var mainImage = $(this).attr("href"); //Find Image Name
		$("#largepic img").attr({ src: mainImage });
		return false;		
	});
}

$.fn.preload = function() 
{
    this.each(function()
    {
        $('<img/>')[0].src = this;
    });
}

function toggleCollections() 
{
    $('#collections').toggle(400);
    return false;
}

function showCollections() 
{
    $('#collections').show( );
    return false;
}

function switchImage(num, imgname) 
{
	document[imgname].src = img[num].src;
}

function updateInfo(content) 
{
	$("#info").hide();
	$("#infocontent").text('');
	$("#infocontent").append(content);
	$("#info").show();
}

function updateStores() 
{
	$("#info").hide();
	$("#stores").hide();
	$("#storescontent").text('');
	$("#infocontent").text('');
	var content = '';
	$(this).next( ).children().children('a').each(function()
	{
		content += '<a href="#" onclick=\'updateInfo("'+$(this).next().children().text()+'")\'>' + $(this).text( ) + '</a><br/>';
	});
	$("#storescontent").append(content);
	$("#stores").show();
}

