$(function() { // document.ready
	// Draw drop shadows
	var shadows = '<div class="wrapperShadow" id="shadowLeft"></div>'
	            + '<div class="wrapperShadow" id="shadowRight"></div>'
	            + '<div class="wrapperShadow" id="shadowULeftCorner"></div>'
	            + '<div class="wrapperShadow" id="shadowLeftCorner"></div>'
	            + '<div class="wrapperShadow" id="shadowRightCorner"></div>'
	            + '<div class="wrapperShadow" id="shadowULeftCorner"></div>'
	            + '<div class="wrapperShadow" id="shadowURightCorner"></div>'
	            + '<div class="wrapperShadow" id="shadowBottom"></div>';
	$(shadows).prependTo('#wrapper'); // prepend for an automatic lower z-index
		
	if ($.browser.msie) {
		// IE6 needs help with the height of the shadows
		fixIE6ShadowHeight();
		// do it onload also to adjust for possible image size changes
		jQuery(window).bind('load', function() { fixIE6ShadowHeight(); });
		
		// IE needs help with the :before psuedo class on the breadcrumbs
		$('#breadCrumbs li:not(.first)').prepend('>&nbsp;');
	}
	
	if ($.browser.safari) {
		// Safari needs help positioning the bottom shadow once images without sizes have loaded
		// Just setting the same value over again doesn't work
		$('#shadowLeftCorner, #shadowRightCorner, #shadowBottom').css('bottom', '-11px');
		$(window).load(function() {
			$('#shadowLeftCorner, #shadowRightCorner, #shadowBottom').css('bottom', '-12px');
		});
	}
	
	// Add flash for home page
	if ($.fn.flash) {
		var $homeFlash = $('#homeFlash');
		$homeFlash.flash({
			src: '/themes/green/flash/433x488_175_tvl_hp.swf',
			width: 433,
			height: 488,
			update: false,
			flashvars: { 
				clickTag1: 'http://www.thomasville.com/Collection120/Soliloque.aspx', 
				clickTag2: 'http://www.thomasville.com/About-Us/Advertising.aspx'
			} 
		}, {
			version: 8,
			update: false
		}, function(htmlOptions) {
			$homeFlash
				.find('img')
					.remove()
				.end()
				.prepend($.fn.flash.transform(htmlOptions));
		});
	}
			
	 // setup datePicker
	if ($.fn.datePicker)
		$('#wPDate').datePicker();
		
	// Setup Browse module if one exists
	var expandables = $('#homeBrowse, #merchandiseBrowser, #collectionsBrowser, #upholsteryBrowser, #accessoriesBrowser, #roomBrowser'); // useing ID's for speed
	if (expandables.length == 1) expandables.find('> ul > li.expandable');
	if (expandables.length > 0) { // if it contains expandable nodes, prepare them
		var playing = false;
		expandables.find('> a')
			.click(function() {
				var li = $(this).parent('li');
				if (!li.is('.expanded') && !playing) { // ignore if already expanded or currently hiding
					//Reveal effect
					playing = true; // turn playing on to avoid over lap of animations
					expandables
						.filter('.expanded').find('ul')
							.find('li.selected div').each(function() { if($.browser.safari) $(this).hide(); }).end() // help safari with overflow issues on default selected description
							.animate({width: 'hide', opacity: 'hide'}, 'slow', function() {
								playing = false; // turn playing off after hide animation
								$(this)
									.css({ // remove styles animate left behind
											display: '',
											opacity: ''
										})
									.parent('li').removeClass('expanded');
								li.addClass('expanded').find('ul')
									.css('opacity', 0) // avoid flicker in firefox
									.animate({width: 'show', opacity: 1}, 'slow', function() {
										$('li.selected div', this).css('display', ''); // help safari with overflow issues on default selected description, only works after 2nd click
									});
							})
						.end().end(); // restore expandables state
				}
				this.blur(); // hide the focused border
				return false;
			})
		.end() // restore expandables state
		.find('li a') // setup rollovers for items under category
			.hover(function() {
				expandables
					.filter('.expanded').find('li').filter('.selected')
						.removeClass('selected')
					.end().end().end(); // restore expandables state to before .filter('.expanded')
				$(this).parent('li').addClass('selected');
			}, function() {})
		.end(); // restore expandables state
	}
	
	// Setup Product Detail Page Thumbs
	var largeImg = $('#prod_images > img');
	var thumbLinks = $('#thumbs a');
	thumbLinks
		.click(function() { // change image and .selected thumb on mouseover
			this.blur(); // remove dotted focus border
			var $this = $(this);
			if ($this.is('.selected')) return false; // short circuit, already selected, prevent default
			thumbLinks.removeClass('selected');
			$this.addClass('selected');
			
			// IE6 needs help with the height of the shadows ... AGAIN
			if ($.browser.msie && typeof XMLHttpRequest == 'function') {
				largeImg.bind('load', function() {
					fixIE6ShadowHeight();
				}).attr('src', $this.attr('href') + '?' + Math.floor(Math.random()*20)); // need to append random number so that img.onload fires properly
			} else 
				largeImg.attr('src', $this.attr('href'));
				
			return false; // prevent default
		});
	// Add Print Icon to Thumbs Listing
	$('<a href="#" class="printIcon" title="Print"><img src="/images/icon_print.png" width="22" height="29" class="png" /></a>')
		.appendTo('#thumbs')
		.click(function() {
			window.print();
			return false;
		});
	
	// Preload Product Detail Images
	thumbLinks.each(function() {
		var img = new Image();
		img.src = this.href;
	});
	
	// Setup Product Detail Swatches
	if ($('#swatches').length > 0) { // don't want to run this unless the swatches are present
		var swatchPopUp = $('<div id="swatchPopUp"><img id="largeSwatch" /><p class="swatchName"></p><p class="swatchNum"></p></div>').appendTo('#middle') // create swatch pop html
			swatchPopUp
				.hide() // hide it by default
				.css('top', $('#swatches a:first').offset().top - 130 + 'px'); // set to the top position based on the swatch link
		var swatchLinks = $('#swatches dd a'); // grab links
		swatchLinks
			.each(function() {
				$(this)
					.attr('metainfo', $(this).attr('title')) // copy title attribute to metainfo attribute
					.attr('title', ''); // remove title
			})
			.hover(function() {
				var $this = $(this);
				var src = $this.attr('href');
				var name = $this.attr('metainfo').split('#')[0];
				var num = '#' + $this.attr('metainfo').split('#')[1];
				swatchPopUp
					.show()
					.find('img')
						.attr('src', src)
					.end()
					.find('p.swatchName')
						.html(name)
					.end()
					.find('p.swatchNum')
						.html(num)
					.end();
			}, function() {
				swatchPopUp.hide();
			})
			.click(function() { this.blur(); return false; }); // Prevent default click action
	
		// Preload Product Detail Swatches
		swatchLinks.each(function() {
			var img = new Image();
			img.src = this.href;
		});
	}
	
	// Fix catalog listing when description is too long
	$('#middle dl.fourth').after('<div class="dr"></div>');
});

// helper function for fixing shadow height in IE 6
function fixIE6ShadowHeight() {
	if ($.browser.msie && typeof XMLHttpRequest == 'function') {
		var height = $('#wrapper')[0].offsetHeight; 
		if (height%2 == 0) height += 1; // Even height values need an extra pixel
		$('#shadowLeft, #shadowRight').css('height', height + 'px');
		$('#shadowBottom, #shadowLeftCorner, #shadowRightCorner').css('bottom', '-13px'); // ughhh ... need to set it to -13
		$('#shadowBottom, #shadowLeftCorner, #shadowRightCorner').css('bottom', '-14px'); // and then back to -14 so it believes there has been a change
	}
};
var ver3 = (navigator.appVersion.charAt(0) <= "3") ? 	true : false;
var dom = (document.getElementById) ? 	true : false;
var ie4 = (document.all && !document.getElementById) ? 	true : false;
var ns4 = (document.layers) ? 	true : false;
var ie5 = (document.all && document.getElementById) ? 	true : false;
var n6 = (document.getElementById && !document.all) ?  true : false;
var msie = (document.all) ?true:false;
 
function FindObject(objekt) {
  var blok;
  	if (dom)
  	{ 
		var obj;
  		obj = document.getElementById(objekt);
  		if (null != obj)
			blok = obj.style;
	}
  	else if (ie4) 
  	{ 
		var obj;
		obj = document.all[objekt].style;
  		if (null != obj)
			blok = obj.style;
	}
  	else if (ns4) 
		blok = document.layers[objekt];
  	return blok;
}
  
function Collapse(objekt){
  var blok = FindObject(objekt);
  if (null != blok)
    blok.display="none"
}
 
function ShowMenu(objekt,onoff){
  HideAll();
  var blok = FindObject(objekt);
  if (null != blok)
    blok.display=(onoff) ? "block" : "none";
}
function HideAll(){
	for (nr=1;nr<20;nr++)
		Collapse('menu'+nr)
}


