// CUSTOMIZE THIS :D
var
	thumbFadeSpeed = 250, // Hover fade transition speed in milliseconds
	viewerWidth = 350,
	viewerHeight = 260,
	moreVideosLinkText = 'More Videos',
	fewerVideosLinkText = 'Fewer Videos'
;

var isIE6 = ($.browser.msie && $.browser.version.substr(0,1)<7);


$(document).ready(function() {
	$('#products-tabs').codaSlider({
		dynamicArrows: false,
		dynamicTabs: true,
		dynamicTabsAlign: 'left',
		autoHeight: false
	});

	setViewerVideo($('#product-media-video li a')[0], 0);
	
	$('#product-media-video li a:not(.current) img').fadeTo(1, .8);
	$('#product-media-video li a')
		.hover(
			function() {
				if (!$(this).hasClass('current')) $(this).find('img').fadeTo(thumbFadeSpeed, 1);
			},
			function() {
				if (!$(this).hasClass('current')) $(this).find('img').fadeTo(thumbFadeSpeed, .8);
			}
		)
		.click(function() {
			$('#product-media-video li a.current')
				.removeClass('current')
				.find('img').fadeTo(thumbFadeSpeed, .8);
				
			$(this)
				.addClass('current')
				.find('img').fadeTo(1, 1);
				
			setViewerVideo(this, 1);

			if ($(window).scrollTop() > $('#product-media-viewer').offset()['top']) {
				$.scrollTo('#product-media-viewer', 1000);
			}
			
			return false;
		});

	$('a.more-fewer-videos').html(moreVideosLinkText);
	$('a.more-fewer-videos').click(function() {
		if ($(this).html() == moreVideosLinkText) {
			if (isIE6) {
				$('ul.more-videos-list').show();
				$('a.more-fewer-videos').html(fewerVideosLinkText);
			}
			else {
				$('ul.more-videos-list').show('slow', function() {
					$('a.more-fewer-videos').html(fewerVideosLinkText);
				});
			}
		}
		else {
			$(this).html(moreVideosLinkText);
			if (isIE6){
				$('ul.more-videos-list').hide();
			}
			else {
				$('ul.more-videos-list').hide('slow');
			}
		}
		
		return false;
	});

	$('#product-media-photo ul.photos-list a:not(.current) img').fadeTo(1, .8);
	$('#product-media-photo ul.photos-list a')
		.hover(
			function() {
				if (!$(this).hasClass('current')) $(this).find('img').fadeTo(thumbFadeSpeed, 1);
			},
			function() {
				if (!$(this).hasClass('current')) $(this).find('img').fadeTo(thumbFadeSpeed, .8);
			}
		)
		.click(function() {

			$('#product-media-photo ul.photos-list a.current')
				.removeClass('current')
				.find('img').fadeTo(thumbFadeSpeed, .8);
			
			$(this)
				.addClass('current')
				.find('img').fadeTo(1, 1);

			$('#product-media-viewer').html('<img class="product-photo" src="' + $(this).attr('href') + '" alt="Product Photo" />');

			if ($(window).scrollTop() > $('#product-media-viewer').offset()['top']) {
				$.scrollTo('#product-media-viewer', 1000);
			}

			return false;
		});

});

function setViewerVideo(a, autoPlay) {
	var vid, results, url = $(a).attr('href');
	if (url != null) {
		results = url.match("[\\?&]v=([^&#]*)");
		vid = ( results === null ) ? url : results[1];
		$('#product-media-viewer').html('<object wmode="transparent" width="' + viewerWidth + '" height="' + viewerHeight + '"><param name="movie" value="http://www.youtube.com/v/' + vid + '&hl=en_US&fs=1&autoplay=' + autoPlay + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + vid + '&hl=en_US&fs=1&autoplay=' + autoPlay + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + viewerWidth + '" height="' + viewerHeight + '"></embed></object>');
	}
}