$(document).ready(function() {
	$('#cartSummary').position({
		my : "right bottom",
		at : "right top",
		of : '.myCart',
		offset : '0 3'
	});

	// image overlay
	$('.thumbHover').hide();
	$('.detail-image').mouseenter(function() {
		$('.thumbHover').show();
		var margin = (185 - $('.detail-image img').width()) / 2;
		$('.overlay').css('margin-left', margin);
		$('.overlay').css('width', $('.detail-image img').width());
		$('.overlay').css('height', $('.detail-image img').height());
		$('.overlay').css('background-color', '#000000');
		$('.detail-image img').css('opacity', '0.5');

	});

	$('.detail-image').mouseleave(function() {
		$('.thumbHover').hide();
		$('.overlay').css('background-color', '#ffffff');
		$('.detail-image img').css('opacity', '1');
	});

	// image overlay
	$('.viewdetails').hide();
	$('.image-container').mouseenter(function() {
		$(this).find('.viewdetails').show();
	});

	$('.image-container').mouseleave(function() {
		$('.viewdetails').hide();
	});

	if (jQuery.browser.safari && document.readyState != "complete") {
		setTimeout(arguments.callee, 100);
		return;
	}

	if ($('.detail-image img').height() < 130) {
		// if we're here, we want to add a top margin to the image so that it slides
		// down over the overlay
		offsetHeight = 147 - $('.detail-image img').height();
		$('.thumbHover').css('margin-top', -1 * (offsetHeight / 2));
	}

	$('.productimage').each(function() {
		var maxWidth = 185;
		var width = $(this).width();
		if (width < maxWidth) {
			leftMargin = ((maxWidth - width) / 2);
			$(this).css('left', leftMargin);
		}
	});

	// image resizing so that they look more uniform
	$('.product_image').each(function() {
		var maxWidth = 185; // Max width for the image
		var maxHeight = 185; // Max height for the image
		var ratio = 0; // Used for aspect ratio
		var width = $(this).width(); // Current image width
		var height = $(this).height(); // Current image height

		// Check if the current width is larger than the max
		if (width > maxWidth) {
			ratio = maxWidth / width; // get ratio for scaling image
			$(this).css("width", maxWidth); // Set new width
			$(this).css("height", height * ratio); // Scale height based on ratio
			height = height * ratio; // Reset height to match scaled image
		}

		// Check if current height is larger than max
		if (height > maxHeight) {
			ratio = maxHeight / height; // get ratio for scaling image
			$(this).css("height", maxHeight); // Set new height
			$(this).css("width", width * ratio); // Scale width based on ratio
			width = width * ratio; // Reset width to match scaled image
		}
	});

	// front page banner
	$('.flexslider').flexslider({
		controlNav : true,
		directionNav : false,
		slideshowSpeed : 7000,
		animationDuration : 600,
		animation : 'show'
	});

	$('.imageThumb').hover(function() {
		$(this).children('img').css('border-bottom', '2px solid #231f20');
		$(this).children('img').css('padding-bottom', '3px');
	}, function() {
		$(this).children('img').css('border', '0');
	});

	// thumbnails going to main panel
	$('.imageThumb').click(function(e) {
		e.preventDefault();
		$src = $(this).children('img').attr('src');
		$('.product_image').attr('src', $src);
		$('a.lightbox').attr('href', $src.replace('med_', ''));
	});

	// lightbox for product images
	$(".lightbox").lightbox({
		fitToScreen : 'true'
	});

	//Form text effects
	$('#inputName').defaultvalues({
		focusaction : "clear"
	});
	$('#inputEmail').defaultvalues({
		focusaction : "clear"
	});
	$('.contactFormInput').defaultvalues({
		focusaction : "clear"
	});
	$('.contact2').defaultvalues({
		focusaction : "clear"
	});
	$('.cms_textfield').defaultvalues({
		focusaction : "clear"
	});
	$('.promo').defaultvalues({
		focusaction : "clear"
	});
	$('.password').defaultvalues({
		focusaction : "clear"
	});

	//hack to get textarea/captcha form color to change properly. 
	$('.text').css('color', '#515151');
	$('.cms_textfield').css('color', '#838383');
	$('.cms_dropdown').css('color', '#515151');

	$("ul.dropdown li").hover(function() {

		$(this).addClass("hover");
		$('ul:first', this).css('visibility', 'visible');

	}, function() {

		$(this).removeClass("hover");
		$('ul:first', this).css('visibility', 'hidden');

	});

	$("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

	/* Open, close shopping cart */
	$("a.cartlink").click(function(e) {
		e.preventDefault();
		if (!$('a.cartlink').hasClass('active')) {
			$('a.cartlink').addClass("active");
			$('.myCart').css('background-color', '#fffdf0');
			$('.cartImage').addClass('active');
			$('#cartSummary').show().position({
				my : "right bottom",
				at : "right top",
				of : '.myCart',
				offset : '0 3'
			});
		} else {
			$('#cartSummary').hide();
			$("a.cartlink").removeClass("active");
			$('.myCart').css('background-color', '#ffffff');
			$('#cartSummary').position({
				my : "right bottom",
				at : "right top",
				of : '.myCart',
				offset : '0 3'
			});
		}
	});

	$("#cartSummary a.close").click(function() {
		$('#cartSummary').hide();
		$("a.cartlink").removeClass("active");
		$('.myCart').css('background-color', '#ffffff');
		$('#cartSummary').position({
			my : "right bottom",
			at : "right top",
			of : '.myCart',
			offset : '0 3'
		});

	});

	$('.myCart').hover(function() {
		$('span.cartImage').addClass('active');
	}, function() {
		if ($('#cartSummary').css('display') != 'block') {
			$('.cartImage.active').removeClass('active');
		}
	});

	$("#tab-1, #tab-2, #tab-3, #tab-4, #tab-5, #tab-6, #tab-7").hide();
	$("#tab-1").show();

	$("#continue-2").click(function() {
		$('#tab-1').hide();
		$('#tab-2').show();
	});

	$("#continue-3").click(function() {
		$('#tab-2').hide();
		$('#tab-3').show();
	});

	$("#continue-4").click(function() {
		$('#tab-3').hide();
		$('#tab-4').show();
	});

	$("#continue-5").click(function() {
		$('#tab-4').hide();
		$('#tab-5').show();
	});

	$("#continue-6").click(function() {
		$('#tab-5').hide();
		$('#tab-6').show();
	});

	$("#continue-7").click(function() {
		$('#tab-6').hide();
		$('#tab-7').show();
	});

	$("#back-1").click(function() {
		$('#tab-1').show();
		$('#tab-2').hide();
	});

	$("#back-2").click(function() {
		$('#tab-2').show();
		$('#tab-3').hide();
	});

	$("#back-3").click(function() {
		$('#tab-3').show();
		$('#tab-4').hide();
	});

	$("#back-4").click(function() {
		$('#tab-4').show();
		$('#tab-5').hide();
	});

	$("#back-5").click(function() {
		$('#tab-5').show();
		$('#tab-6').hide();
	});

	$("#back-6").click(function() {
		$('#tab-6').show();
		$('#tab-7').hide();
	});

	$('.collapsableBlock').hide();

	// about us content
	$('.collapsableHeading1').click(function() {
		$('.collapsableContent1').slideToggle('slow');
	});

	$('.collapsableHeading2').click(function() {
		$('.collapsableContent2').slideToggle('slow');
	});

	$('.collapsableHeading3').click(function() {
		$('.collapsableContent3').slideToggle('slow');
	});

	$('.collapsableHeading4').click(function() {
		$('.collapsableContent4').slideToggle('slow');
	});

});
/*


 $(window).load(
 function() {
 $('.productimage').each(function(){
 var maxWidth = 185;
 var width = $(this).width();
 if (width < maxWidth){
 leftMargin = ( (maxWidth - width) / 2 );
 $(this).css('left', leftMargin);
 }
 });
 /*
 if ($('.detail-image img').height() < 100){
 // if we're here, we want to add a top margin to the image so that it slides
 // down over the overlay
 offsetHeight = 147 - $('.detail-image img').height();
 $('.detail-image').css('padding-top', offsetHeight/2);
 }
 }
 );


 */



