/**
 * 
 * Bike Gallery
 * 
 */

var bikeNextOff, bikeNextOn, bikePrevOff, bikePrevOn;
var bikeThumbnailCount, bikeCurrentPhoto, bikeThumbnailDisplayMax;
var bikeSlideDistance, bikeCurrentSlidePosition, bikeAnimationInterval, bikePhotoGalleryThumbTitleTopOffset;
var isInferior = (navigator.appName.indexOf('Microsoft') >= 0) && (parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf('MSIE')+5)) < 8);
var so, mpl;

$(document).ready(function() {

	bikeCurrentSlidePosition = 0;
	bikeCurrentPhoto = 1;
	
	// activate next button with correct state
	if (bikeThumbnailCount > bikeThumbnailDisplayMax) {
		$("#bikeGalleryNext img").attr("src", bikeNextOn); 
	} else {
		$("#bikeGalleryNext img").attr("src", bikeNextOff);
	}
	
	// activate off state of previous button
	$("#bikeGalleryPrev img").attr("src", bikePrevOff);
	
	// add onclick functionality
	$("#bikeGalleryPrev").click(function() { bikeSlideThumbnail(-1); $(this).blur(); return false; });
	$("#bikeGalleryNext").click(function() { bikeSlideThumbnail(1); $(this).blur(); return false; });
	
	// add image mouseover highlighting functionality
	$(".bikeGalleryItem p.image a img").hover(function() {
		$(this).parent().parent().siblings().addClass("highlight");
	},function() {
		$(this).parent().parent().siblings().removeClass("highlight");
	});

});

function bikeSlideThumbnail(x) {
	
	// do we need sliding at all?
	if (bikeThumbnailCount <= bikeThumbnailDisplayMax) {
		return;
	}
	
	if (x == 1) {
		// next
		// is this button active?
		if ($("#bikeGalleryNext img").attr("src") == bikeNextOn) {
			bikeCurrentSlidePosition = bikeCurrentSlidePosition + bikeSlideDistance;
			$("#bikeGalleryThumbs").animate({right: bikeCurrentSlidePosition}, bikeAnimationInterval);
			bikeCurrentPhoto++;
			bikeUpdateNextPrevious();
		}
		return false;
		
	} else {
		// previous
		if ($("#bikeGalleryPrev img").attr("src") == bikePrevOn) {
			bikeCurrentSlidePosition = bikeCurrentSlidePosition - bikeSlideDistance;
			$("#bikeGalleryThumbs").animate({right: bikeCurrentSlidePosition}, bikeAnimationInterval);
			bikeCurrentPhoto--;
			bikeUpdateNextPrevious();
		}
		return false;
	}
}

function bikeUpdateNextPrevious() {
	if ((bikeThumbnailCount - bikeThumbnailDisplayMax) >= bikeCurrentPhoto) {
		$("#bikeGalleryNext img").attr("src", bikeNextOn);
	} else {
		$("#bikeGalleryNext img").attr("src", bikeNextOff);
	}
	
	if (bikeCurrentPhoto > 1) {
		$("#bikeGalleryPrev img").attr("src", bikePrevOn);
	} else {
		$("#bikeGalleryPrev img").attr("src", bikePrevOff);
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function changeZoomImage(imageFileName, current_model_year) {
	var bikeImage = document.getElementById('bikeImage');
	var bc2 = document.getElementById('bc2');
	var bikeImageLarge = bc2.firstChild.firstChild;
	
	if (!bikeImageLarge) {
		bikeImageLarge = bc2.lastChild.firstChild;
	}
	
	bikeImage.src = '/images/bikes/'+current_model_year+'/large/'+imageFileName;
	bikeImageLarge.src = '/images/bikes/'+current_model_year+'/xl/'+imageFileName;

	// update selected item
	$(".bikeGalleryItem").removeClass("selected");
	idName = imageFileName.substring(0, (imageFileName.length - 4));
	$("#bikeGalleryThumbs #" + idName).addClass("selected");

	// update download image links
	$("a.download").attr("href", '/images/bikes/'+current_model_year+'/xl/'+imageFileName);
	return false;
}

function showVideo(videoFilename, subtitleFilename) {
	// update download link
	$("#downloadVideo").attr("href", videoFilename);
	
	so = new SWFObject('/flash/shared/player_42.swf','mpl','566','350','9');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	if (subtitleFilename) {
		so.addParam('flashvars','&file=' + videoFilename + '&autostart=true&skin=/flash/shared/stylish.swf');
		so.addParam('flashvars','&file=' + videoFilename + '&captions=' + subtitleFilename + '&skin=/flash/shared/stylish.swf&plugins=accessibility');
	} else {		
		so.addParam('flashvars','&file=' + videoFilename + '&autostart=true&skin=/flash/shared/stylish.swf');
	}
	so.addParam("wmode", "transparent");
	so.write('inplaceVideoPlayer');
	return false;
	
}

$().ready(function() {
	$("#bikeGalleryThumbs a").click(function() {
		stopAndCloseVideo();
	});
	$(".showBikeVideo").click(function() {
		pageTracker._trackPageview(document.location.toString() + "video/")
		$("#inplaceVideoPlayer").show();
		$("p#videoFunctions").show();
		$("#videoPlayerClose").show();
		$("p#zoomFunctions").hide();
		$(".bikeGalleryItem").removeClass("selected");
		$(this).addClass("selected");
		return false;
	});
	
	$("#videoPlayerClose").click(function() {
		stopAndCloseVideo();
		return false;
	});
});

function stopAndCloseVideo() {
	
	$("#inplaceVideoPlayer").hide();
	$("#bikeImage").show();
	$("p#zoomFunctions").show();
	$("p#videoFunctions").hide();
	$("#videoPlayerClose").hide();
	
	if (document.getElementById('mpl') != null) {
		mpl = document.getElementById('mpl');
		if (mpl.sendEvent) {
			mpl.sendEvent('STOP');
		}
	}
	
}
	