// JavaScript Document

$(document).ready(function(){
	
	if(jQuery.browser.msie && jQuery.browser.version <= 6){
		$('.overview_zoomIn').hide();
		$('.overview_zoomOut').hide();
	}	
	
	var defaultWidth = $('.overview_vehicle').width();
	var defaultHeight = $('.overview_vehicle').height();
	var defaultLeft = $('.overview_vehicle').position().left;
	var defaultTop = $('.overview_vehicle').position().top;
	
	var currentWidth;
	var currentHeight;
	var currentLeft;
	var currentTop;
	
	$('.overview_buttons').show();
	
	$('.overview_buttons').click(function(e){
														
		var myButtonId = $(e.target).attr("class");
		var animationSpeed = "fast";
		var animationOptions = null;
		
		updateCurrentPosition();
		
		switch(myButtonId){
			case "overview_up":
				if(currentTop > -currentHeight / 2){
					animationOptions = {top: currentTop - currentHeight / 4}	
				}
				break;
			case "overview_left":
				if(currentLeft > -currentWidth / 2){
					animationOptions = {left: currentLeft - currentWidth / 4}	
				}
				break;
			case "overview_center":
				animationOptions = {
					top: defaultTop,
					left: defaultLeft,
					width: defaultWidth,
					height: defaultHeight
				}
				break;
			case "overview_right":
				if(currentLeft < $('.overview_container').width() - currentWidth / 2 ){
					animationOptions = {left: currentLeft + currentWidth / 4}	
				}
				break;
			case "overview_down":
				if(currentTop < $('.overview_container').height() - currentHeight / 2 ){
					animationOptions = {top: currentTop + currentHeight / 4}	
				}
				break;
			case "overview_zoomIn":
				if(currentWidth < defaultWidth * 2){
					animationOptions = {
						width: currentWidth + defaultWidth / 2,
						height: currentHeight + defaultHeight / 2,
						left: currentLeft - defaultWidth / 4,
						top: currentTop - defaultHeight / 4
					}
				}
				break;
			case "overview_zoomOut":
				if(currentWidth >= defaultWidth * .9){
					animationOptions = {
						width: currentWidth - defaultWidth / 2,
						height: currentHeight - defaultHeight / 2,
						left: currentLeft + defaultWidth / 4,
						top: currentTop + defaultHeight / 4
					}
				}
				break;
			default:
				//
				break;
		}

		if(animationOptions){
			$('.overview_vehicle').attr('src', $('.overview_vehicle_small').attr('src'));
			$('.overview_vehicle').animate(animationOptions, animationSpeed, function() {
				updateCurrentPosition();
				if(currentWidth > defaultWidth){
					$('.overview_vehicle').attr('src', $('.overview_vehicle_large').attr('src'));
				}
			});
		}
	});
	
	function updateCurrentPosition(){
		currentWidth = $('.overview_vehicle').width();
		currentHeight = $('.overview_vehicle').height();
		currentLeft = $('.overview_vehicle').position().left;
		currentTop = $('.overview_vehicle').position().top;
	}
	
});
