$(function() {
	
	// apply to all png images
	$('img[@src$=.png], #tooltip.pretty div').ifixpng();
	
	// Hide the preloaded images
	$('#preloadedImages').hide(); // IE doesn't like display: none; (of course)
	
	
	// $('div.optionResultContentContainer:first').show();
	
	
	/*
	* Home page functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	if ($('#homeBuilderSide')) 
	{
		var delay = 700;
		// Define the original BG color and image of the link
		var originalBG = '#ffffff';
		var originalBGImage = 'url(/assets/images/arrow-homeBuilder.gif)';
		// The color and image to fade too
		var fadeColor = "#d5e2bf"; 
		var fadeImage = "url(/assets/images/arrow-homeBuilder_o.gif)";
	
		if ($('body').attr('id') == 'home') {
			$('#homeBuilderSide ul li').each(function(e) {
				$('a', this).fadeTo(delay, 1).animate({backgroundColor: fadeColor}, {duration: 500, easing: "easeOutSine" }).animate({backgroundColor: originalBG}, {duration: 500, easing: "easeOutSine" });
	  			delay += 400;
			});
			// Home page bottom product tabs
			var tabContainers = $('#homeBuilderContent > div');
			tabContainers.hide().filter(':first').show();
	
			$('#homeBuilderSide ul li a').mouseover(function() {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('#homeBuilderSide ul li a').css({backgroundColor: originalBG, backgroundImage: originalBGImage});
				$(this).css({backgroundColor: fadeColor, backgroundImage: fadeImage});
				return false;
			});
		}
	}
	
	// Home bubble rollovers
	$('div.homeBubbleBox').hover(function() {
		$(this).find('a:first').removeClass();
		$(this).find('a:first').addClass('bubbleLinkActive');
	}, function() {
		$(this).find('a:first').removeClass();
		$(this).find('a:first').addClass('bubbleLink');
	});
	
    // Section home rollovers
	$('div#sectionImg img').hover(function() {
		$('#tipImg').hide();
		$('#tip').show();
	}, function() {
		$('#tip').hide();
		$('#tipImg').show();
	});
	
	// Preload all rollovers
	$('div.homeBubbleBox').each(function(){
		// Set the original src
		rollSrc = $('.imgOver').attr("src");
		rollSrcOn = rollSrc.replace(/.png$/ig,'_o.png');
		$('<img>').attr('src', rollSrcOn);
	});
	// Navigation rollovers
	$('div.homeBubbleBox').hover(function(){
		imageSrc = $(this).find('img').attr('src');
		imageMatches = imageSrc.match(/_o/);
		// Don't do the rollover if state is already ON
		if (!imageMatches) {
			imageSrcOn = imageSrc.replace(/.png$/ig,'_o.png'); // strip off extension
			$(this).find('img').attr('src', imageSrcOn);
		}
	}, function() {
	  	// Mouseout
		$(this).find('img').attr('src', imageSrc);
	});
  
	/*
	* Builder functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var builderBegin = $('div#builderBegin a');
	
	// Toggles builder options
	$('div#builderBegin a').toggle(function() {
		builderBegin.addClass('open');
		$('div#builderWrapper').slideDown(500);
		return false;
	}, function() {
		$('div#builderWrapper').slideUp(500, function() {
			builderBegin.removeClass('open');
		});
		return false;
	});
	
	// Opens the builder tool
	$('a#startBuilding').click(function() {
		// $('div#buildMyBlindsBox').show('slow', function() {
		// 			$('div#buildMyBlindsHeader a').fadeIn();
		// 			$('div#buildMyBlindsContentWrap').slideDown('normal');
		// 		});
		$('div#buildMyBlindsBox').show('fast');
		$('div#buildMyBlindsHeader a').show();
		$('div#buildMyBlindsContentWrap').show();
        return false;
	});
	
	// Hides the builder tool
	$('div#buildMyBlindsHeader a').click(function() {
		$('div#buildMyBlindsContentWrap').fadeOut('normal', function() {
			$('div#buildMyBlindsBox').hide();
		});
		return false;
	});

	// Toggles builder options
	$('a.optionToggle').toggle(function() {
		$(this).css('background-image','url(/assets/images/newWhiteArrowDown.gif)').next().slideDown(200);
		return false;
	}, function() {
		$(this).css('background-image','url(/assets/images/newWhiteArrowRight.gif)').next().slideUp(200);
		return false;
	});
	
	// Open builder option results
	$('div.optionResultItem').livequery('click', function() {
		var thisOption = $(this);
		// thisOption.next().slideDown('fast', function() {
		// 			thisOption.removeClass().addClass('optionResultItemActive');
		// 		});
		thisOption.removeClass().addClass('optionResultItemActive').next().slideDown(200);
		return false;
	});
	
	// Close builder option results
	$('div.optionResultItemActive').livequery('click', function() {
		var thisOption = $(this);
		thisOption.next().slideUp(200, function() {
			thisOption.removeClass().addClass('optionResultItem');
		});
		return false;
	});
	
	// Cell size selection
	$("#optionCellSize input[type='radio']").click(function() {
		var cellSize = $(this).attr('value');
		// Check this radio
		$(this).attr('checked', 'checked');
		// Add active class
		$('#optionCellSize label').removeClass();
		$(this).parent().addClass('shown');
		// Check this radio input
		$(this).attr('checked', 'checked');
		// Show the selection value
		$('#selectionCellSize').show();
		// Update thumbnail and selection text
		switch(cellSize) {
			case 'cell1':
				$('#optionCellSize img').attr('src', '/userimages/builder/CellSlatSize_100/HC_p07_0.75inch.jpg');
				$('#selectionCellSize em').text('3/8"');
			break;    
			case 'cell2':
		  		$('#optionCellSize img').attr('src', '/userimages/builder/CellSlatSize_100/HC_p07_0.375inch.jpg');
				$('#selectionCellSize em').text('3/4"');
		  	break;
			case 'cell3':
		  		$('#optionCellSize img').attr('src', '/userimages/builder/CellSlatSize_100/HC_p07_DOUBLE0.5inch.jpg');
				$('#selectionCellSize em').text('1/2"');
		  	break;
		}
	}).filter(':first').click();
	
	// Mount selection
	$("#optionMount input[type='radio']").click(function() {
		var whichMount = $(this).attr('value');
		// Check this radio
		$(this).attr('checked', 'checked');
		// Add active class
		$('#optionMount label').removeClass();
		$(this).parent().addClass('shown');
		// Check this radio input
		$(this).attr('checked', 'checked');
		// Show the selection value
		$('#selectionMount').show();
		// Update thumbnail and selection text
		switch(whichMount) {
			case 'inside':
				$('#optionMount #previewThumb').attr('src', '/userimages/builder/BMB_Options_100px/HONEYCOMBS_100/HC_InsideMount.jpg');
				$('#selectionMount em').text('Inside');
			break;    
			case 'outside':
		  		$('#optionMount #previewThumb').attr('src', '/userimages/builder/BMB_Options_100px/HONEYCOMBS_100/HC_OutsideMount.jpg');
				$('#selectionMount em').text('Outside');
		  	break;
		}
	}).filter(':first').click();
	
	// Initiate builder sliders
	$('#solutionsBlock').click(function() {
		$('div#slider1').slider({
			minValue: 0,
			maxValue: 3,
			startValue: 0,
			steps: 2,
			slide: function(e,ui) {
				$('#sliderValue1').val(parseInt(ui.value));
			}, change: function(e,ui) {
				$('#sliderValue1').val(parseInt(ui.value));
			}
		});
		$('div#slider2').slider({
			minValue: 0,
			maxValue: 3,
			startValue: 0,
			steps: 2,
			slide: function(e,ui) {
				$('#sliderValue2').val(parseInt(ui.value));
			}, change: function(e,ui) {
				$('#sliderValue2').val(parseInt(ui.value));
			}
		});
		$('div#slider3').slider({
			minValue: 0,
			maxValue: 3,
			startValue: 0,
			steps: 2,
			slide: function(e,ui) {
				$('#sliderValue3').val(parseInt(ui.value));
			}, change: function(e,ui) {
				$('#sliderValue3').val(parseInt(ui.value));
			}
		});
		$('div#slider4').slider({
			minValue: 0,
			maxValue: 3,
			startValue: 0,
			steps: 2,
			slide: function(e,ui) {
				$('#sliderValue4').val(parseInt(ui.value));
			}, change: function(e,ui) {
				$('#sliderValue4').val(parseInt(ui.value));
			}
		});
	});

	// Search options tabs
	var searchTabContainers = $('#thumbsCarousel > div');
	searchTabContainers.hide().filter(':first').show();
			
	$('div.optionContentContainer ul li a').click(function() {
		searchTabContainers.hide();
		searchTabContainers.filter(this.hash).show();
		$('div.optionContentContainer ul li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	/*
	* Product and Style functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var optionsContainers = $('div#customOptionsContainer > div');
	optionsContainers.hide();
	$('div#customOptions ul li a').click(function() {
		optionsContainers.hide();
		optionsContainers.filter(this.hash).show();
		$('div#customOptions ul li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});
	
	// Decorative customization tabs
	var decorativeTabContainers = $('div#decorativeHemsTrimsBlock > div');
	decorativeTabContainers.hide().filter(':first').show();
			
	$('div#decorativeHemsTrims ul li a').not('#previousTrim,#nextTrim').click(function() {
		decorativeTabContainers.hide();
		decorativeTabContainers.filter(this.hash).show();
		$('div#decorativeHemsTrims ul li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	// Decorative tapes customization tabs
	var decorativeTapesContainers = $('div#decorativeTapesBlock > div');
	decorativeTapesContainers.hide().filter(':first').show();
			
	$('div#decorativeTapes ul li a').click(function() {
		decorativeTapesContainers.hide();
		decorativeTapesContainers.filter(this.hash).show();
		$('div#decorativeTapes ul li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();

	// View more specs rollover
	$('ul#viewMore li').hover(function() {
		$(this).find('a').addClass('selected');
		$(this).children('ul').show();
		return false;
	}, function() {
		$(this).find('a').removeClass('selected');
		$(this).children('ul').hide();
		return false;
	});
	
	// Update the cassette headrail image
	$('a.updateHeadrail').mouseover(function() {
		var largeIMG = $(this).attr('href');
		var title = $(this).attr('title');
		$('img#cassetteThumb').attr('src', largeIMG);
    	$('div#cassetteChoose div.shownInfo span').text(title);
		$(this).click(function() {
    		return false;
    	});
	}).filter(':first').mouseover();
	
	/* Update the solid tape image and text */
	$('a.updateTapeSolid').mouseover(function() {
		var largeIMG = $(this).attr('href');
		var title = $(this).attr('title');
		// add selected class
		$('a.updateTapeSolid').removeClass('selected');
		$(this).addClass('selected');
		$('img#tapeSolidImg').attr('src', largeIMG);
    	$('div#tapeSolidInfo span').text(title);
		$(this).click(function() {
    		return false;
    	});
	}).filter(':first').mouseover();
	
	/* Update the pattern tape image and text */
	$('a.updateTapePattern').mouseover(function() {
		var largeIMG = $(this).attr('href');
		var title = $(this).attr('title');
		// add selected class
		$('a.updateTapePattern').removeClass('selected');
		$(this).addClass('selected');
		$('img#tapePatternImg').attr('src', largeIMG);
    	$('div#tapePatternInfo span').text(title);
		$(this).click(function() {
    		return false;
    	});
	}).filter(':first').mouseover();
	
	/*
	* Update swatch function
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var updateSwatch = function() 
	{
		$('a.updateSwatch').mouseover(function() {
			
			var img = new Image();
			var titleImg = $(this).attr('title');
			var newImg = $(this).attr('href');
			var curImg = $('img#swatchLarge').attr('src');
			var insertImg = '<img id="swatchLarge" src="' + newImg + '" alt="Thumb: ' + titleImg + '" width="250" height="250" border="0" />';
			
			// Add selected class
			$('a.updateSwatch').removeClass('selected');
			$(this).addClass('selected');
			
			// Find out if the images don't match. If not update new image
			if (curImg != newImg) {
				
				// Add loading class to container and remove the current image
				$('#aboutProductImg').addClass('loading').find('img').remove();

				// Load new image and append it
				$(img).load(function() {

					$(this).hide();
		            $('#aboutProductImg').removeClass('loading').append(insertImg).next().find('span').text(titleImg);
		            $(this).show();

		        }).error(function () {

		            // Notify the user that the image could not be loaded
		            alert('Error loading image.');

		        }).attr('src', newImg);
			}
			// If this link is clicked return it false
			$(this).click(function() { return false });
			
		}).filter(':first').mouseover();
 	}
		
	// Call the update swatch function
	updateSwatch();

 	/*
	* Slat options gallery functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var updateSlatContent = function(current) 
	{
		// Update the content based on current
		$('img#slatImageThumb').attr('src', slatSize[current][2]);
		$('#slatTitle').text(slatSize[current][1]);
		// Update swatches
		$.get('/show_me/slat-styles.php', {size: slatSize[current][0]}, function(data) 
		{
			$('#woodSwatchesLoad').html(data);
			// Bind the update swatch function
			updateSwatch();
		});
 	}
 	var nextSlatSize = function() 
	{
		if (curSlat >= slatsLength - 1) {
			var thisCurSlat = curSlat;
		} else {
			var thisCurSlat = curSlat += 1;
		}
		updateSlatContent(thisCurSlat);
		
		if (thisCurSlat >= slatsLength - 1) $('#nextSlatSize > img').attr('src', '/assets/images/whiteBlock.gif');
		if (thisCurSlat <= slatsLength - 1) $('#prevSlatSize > img').attr('src', '/assets/images/trimArrowLeft.gif');
 	}
 	var prevSlatSize = function() 
	{
		if (curSlat < 1) {
			var thisCurSlat = curSlat;
		} else {
			var thisCurSlat = curSlat -= 1;
		}
		updateSlatContent(thisCurSlat);
		
		if (thisCurSlat < slatsLength - 1) $('#nextSlatSize > img').attr('src', '/assets/images/trimArrowRight.gif');
		if (thisCurSlat <= 0) $('#prevSlatSize > img').attr('src', '/assets/images/whiteBlock.gif');
 	}

	$('#nextSlatSize').click(function() {
		nextSlatSize(); 
		return false;
	});
	$('#prevSlatSize').click(function() {
		prevSlatSize();
		return false;
	});
	
	/*
	* Cell options gallery functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var updateCellContent = function(current) 
	{
		// Update the content based on current
		$('img#slatImageThumb').attr('src', cellSize[current][2]);
		$('#slatTitle').text(cellSize[current][1]);
		// Update swatches
		$.get('/show_me/cell-styles.php', {size: cellSize[current][0], id: cellSize[current][3]}, function(data) {
			$('#woodSwatchesLoad').html(data);
			// Bind the update swatch function
			updateSwatch();
		});
 	}
 	var nextCellSize = function() 
	{
		if (curCell >= cellsLength - 1) {
			var thisCurCell = curCell;
		} else {
			var thisCurCell = curCell += 1;
		}
		updateCellContent(thisCurCell);
		if (thisCurCell >= cellsLength - 1) $('#nextCellSize > img').attr('src', '/assets/images/whiteBlock.gif');
		if (thisCurCell <= cellsLength - 1) $('#prevCellSize > img').attr('src', '/assets/images/trimArrowLeft.gif');
 	}
 	var prevCellSize = function() 
	{
		if (curCell < 1) {
			var thisCurCell = curCell;
		} else {
			var thisCurCell = curCell -= 1;
		}
		updateCellContent(thisCurCell);
		if (thisCurCell < cellsLength - 1) $('#nextCellSize > img').attr('src', '/assets/images/trimArrowRight.gif');
		if (thisCurCell <= 0) $('#prevCellSize > img').attr('src', '/assets/images/whiteBlock.gif');
 	}

	$('#nextCellSize').click(function() {
		nextCellSize(); 
		return false;
	});
	
	$('#prevCellSize').click(function() {
		prevCellSize();
		return false;
	});
	
	/*
	* Trim options gallery
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	var updateTrimContent = function(current) 
	{
		// Update the content based on current
		$('img#trimThumb').attr('src', trimStyle[current][2]);
		$('#trimTitle').text(trimStyle[current][1]);
		$('#trimStyleHeader').text(trimStyle[current][1]);
		// Update colors
		$.get('/show_me/trim-styles.php', {id: trimStyle[current][0]}, function(data) {
			$('#trimOptionsLoad').html(data);
		});
	}
	var nextTrim = function() 
	{
		if (curTrim >= trimsLength - 1) {
			var thisCurTrim = curTrim;
		} else {
			var thisCurTrim = curTrim += 1;
		}
		updateTrimContent(thisCurTrim);
		
		if (thisCurTrim >= trimsLength - 1) $('#nextTrim > img').attr('src', '/assets/images/whiteBlock.gif');
		if (thisCurTrim <= trimsLength - 1) $('#previousTrim > img').attr('src', '/assets/images/trimArrowLeft.gif');
	}
	var prevTrim = function() 
	{
		if (curTrim < 1) {
			var thisCurTrim = curTrim;
		} else {
			var thisCurTrim = curTrim -= 1;
		}
		updateTrimContent(thisCurTrim);
	
		if (thisCurTrim < trimsLength - 1) $('#nextTrim > img').attr('src', '/assets/images/trimArrowRight.gif');
		if (thisCurTrim <= 0) $('#previousTrim > img').attr('src', '/assets/images/whiteBlock.gif');
	}

	$('#nextTrim').click(function() {
		nextTrim(); 
		return false;
	});
	
	$('#previousTrim').click(function() {
		prevTrim();
		return false;
	});
	
	/*
	* Edge bindings gallery functions
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/		
	$('div#edgeBindingChoose ul li a').click(function() 
	{
		var thisId = $(this).attr('id');
		var bindingId = thisId.replace('edge', '');
		var thisTitle = $(this).attr('title');
		
		$('div#edgeBindingChoose ul li a').removeClass('selected');
		$(this).addClass('selected');
		
		$('#edgeBindingBlock').hide();
		$('div.edgeLoader').show();
		
		// Update edge binding content
		$.get('/show_me/binding-styles.php', {id: bindingId}, function(data) {
			$('#edgeBindingContent').html(data);
			$('div.edgeBindingOptionBlock h5 span').text(thisTitle);
			setTimeout('updateContent()', 600);
			updateEdgeBingingText();
		});
		return false;
	}).filter(':first').click();
	
	// $('a.updateEdgeBinding').livequery('mouseover',function() {
	// 		var updateText = $(this).attr('title');
	// 		var thisImg = $(this).attr('href');
	// 		$('a.updateEdgeBinding').removeClass('selected');
	// 		$(this).addClass('selected');
	// 		$('img#edgeBindImg').attr('src', thisImg);
	// 		$('div.shownInfoEdge span').text(updateText);
	// 		$(this).click(function() {
	// 			return false;
	// 		});
	// 		return false;
	// 	});
	
	var updateEdgeBingingText = function() 
	{
		$('a.updateEdgeBinding').mouseover(function() {
			var updateText = $(this).attr('title');
			var thisImg = $(this).attr('href');
			$('a.updateEdgeBinding').removeClass('selected');
			$(this).addClass('selected');
			$('img#edgeBindImg').attr('src', thisImg);
			$('div.shownInfoEdge span').text(updateText);
			$(this).click(function() {
    			return false;
    		});
			return false;
		}).filter(':first').mouseover();
 	}
	
	/*
	* Various linked events
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	// back button
	$('a[class*=back]').click(function() {
		history.go(-1);
		return false;
	});
	// print page
	$('a[class*=print]').click(function() {
		window.print();
		return false;
	});
	
	/*
	* Various form events
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	// Process the blingo login form * for IE6
	// $('#processLogin').click(function() { $('form#blingoLogin').submit() });
	
	
	/*
	* Home page sliders
	* ----------------------------------------------------------------------------------------------------------------------------------
	*/
	// products slider next
	$('a#productNext').click(function() {
		var sizeNext = '-285px';
		$('ul#productSliderBox').animate({ marginLeft: sizeNext }, 800, 'easeInOutExpo', function() {
			$('a#productPrev').css('background-image','url(/assets/images/arrowLeftHome.gif)');
			$('a#productNext').css('background-image','url(/assets/images/arrowRightLight.gif)');
		});
		return false;
	});
	// products slider previous
	$('a#productPrev').click(function() {
		var sizePrev = '0px';
		$('ul#productSliderBox').animate({ marginLeft: sizePrev }, 800, 'easeInOutExpo', function() {
			$('a#productNext').css('background-image','url(/assets/images/arrowRightHome.gif)');
			$('a#productPrev').css('background-image','url(/assets/images/arrowLeftLight.gif)');
		});
		return false;
	});
	
	// room slider next
	$('a#roomNext').click(function() {
		var sizeNext = '-190px';
		$('ul#roomSliderBox').animate({ marginLeft: sizeNext }, 800, 'easeInOutExpo', function() {
			$('a#roomPrev').css('background-image','url(/assets/images/arrowLeftHome.gif)');
			$('a#roomNext').css('background-image','url(/assets/images/arrowRightLight.gif)');
		});
		return false;
	});
	// room slider previous
	$('a#roomPrev').click(function() {
		var sizePrev = '0px';
		$('ul#roomSliderBox').animate({ marginLeft: sizePrev }, 800, 'easeInOutExpo', function() {
			$('a#roomNext').css('background-image','url(/assets/images/arrowRightHome.gif)');
			$('a#roomPrev').css('background-image','url(/assets/images/arrowLeftLight.gif)');
		});
		return false;
	});
});


function updateContent() 
{
	$('#edgeBindingBlock').show();
	$('div.edgeLoader').hide();
}
