$j = jQuery.noConflict();

$j(document).ready(function() {
	//External Links
	$j('a.product-details-thumbs').live('click', function() {
		$j('#big-image').fadeOut();
	   	$j('#zoom-btn').text('Zoom');
		$j('#image').fadeIn();
		var ImageUrl = $j(this).attr('href');
		$j('.cboxElement').attr('href', ImageUrl);
	   	$j('#img-link').attr('href', ImageUrl);
	   	var NewImage = new Image();
    	$j('#image, #zoom-btn').hide();
	   	var slimUrl = ImageUrl.split('.com');
	   	slimUrl = slimUrl[1];
	   	//slimUrl = '/shop/phpthumb/phpThumb.php?src='+slimUrl+'&q=99&h=451';
	   	NewImage.onload = function() {
	   		$j('#image').attr('src', slimUrl);
	   		new Effect.Appear('image', { duration: 0.5, queue: 'end'});
		   	new Effect.Appear('zoom-btn', { duration: 0.5, queue: 'end'});
			$j('#img-link,#zoom-btn').colorbox({href: $j('#img-link').attr('href')});
	   	}
    	NewImage.src = slimUrl;
		return false;
	});
	
	$j('li.subnav a:first').mouseenter(
	  function () {
	    $j(this).next('ul').show();
	    var cph = $j(document).height() - 99;
	    $j('#blackout').css('height',cph+'px').fadeIn(500);
	    $j(this).parent().addClass('selected');
	  }
	);

	$j('#blackout,.header-top-container').mouseenter(
	  function () {
	    $j('#blackout').hide();
	    $j('#nav li.selected a').next('ul').hide();
	    $j('#nav li.selected a').parent().removeClass('selected');
	  }
	);
	
	$j('#images').exposure({controlsTarget : '#controls',
		controls : { prevNext : true, pageNumbers : true, firstLast : false },
		visiblePages : 2,
		slideshowControlsTarget : '#slideshow',
		onThumb : function(thumb) {
			var li = thumb.parents('li');				
			var fadeTo = li.hasClass('active') ? 1 : 0.3;
			
			thumb.css({display : 'none', opacity : fadeTo}).stop().fadeIn(200);
			
			thumb.hover(function() { 
				thumb.fadeTo('fast',1); 
			}, function() { 
				li.not('.active').children('img').fadeTo('fast', 0.3); 
			});
		},
		onImage : function(image, imageData, thumb) {
			// Check if wrapper is hovered.
			var hovered = $j('.exposureWrapper').hasClass('exposureHover');
			
			// Fade out the previous image.
			$j('.exposureWrapper > .exposureLastImage').stop().fadeOut(500, function() {
				$j(this).remove();
			});
			
			// Fade in the current image.
			image.hide().stop().fadeIn(1000);
			
			var hasCaption = function() {
				var caption = imageData.find('.caption').html();
				var extra = imageData.find('.extra').html();
				return (caption && caption.length > 0) || (extra && extra.length > 0);
			}
			
			var showImageData = function() {
				imageData.stop().show().animate({bottom:0+'px'},{queue:false,duration:160});
			}
			var hoverOver = function() {
				$j('.exposureWrapper').addClass('exposureHover');
				// Show image data as an overlay when image is hovered.
				var hasCpt = hasCaption();
				
				if (hasCpt) {
					showImageData.call();
				}
			};
			
			var hideImageData = function() {
				var imageDataBottom = -imageData.outerHeight();
				imageData.stop().show().animate({bottom:imageDataBottom+'px'},{queue:false,duration:160});
			}
			var hoverOut = function() { 
				$j('.exposureWrapper').removeClass('exposureHover');
				// Hide image data on hover exit.
				if (hasCaption()) {
					hideImageData.call();
				}
			};
			
			$j('.exposureWrapper').hover(hoverOver,hoverOut);
			imageData.hover(hoverOver,hoverOut);
									
			if (hovered) {
				if (hasCaption()) {
					showImageData.call();
				} else {
					hideImageData.call();	
				}	
			}

			if ($j.exposure.showThumbs && thumb && thumb.length) {
				thumb.parents('li').siblings().children('img.selected').stop().fadeTo(200, 0.3, function() { $j(this).removeClass('selected'); });			
				thumb.fadeTo('fast', 1).addClass('selected');
			}
		},
		onPageChanged : function() {
			$j('.exposureThumbs li.current').hide().stop().fadeIn('fast', function() {
				var imageHeight = $j(this).find('img').height();
				if (imageHeight > 0) {
					$j(this).height(imageHeight);
				}
			});
		}
	});
		
	
});


