(function ($){
	var $this = null;
	var $options = {};

	observe = function(){
		$this.find(".defaulttext").each(function(i, obj){
			$('#subNav').append($(obj));
		});
		$($(".defaulttext")).show();
		$this.find(".scenetext").each(function(i, obj){
			$('#subNav').append($(obj));
		});

		$this.find(".nav a").each(function(i, obj){
			$(obj).click(function(){
				$($this).find(".nav li").removeClass("selected");
				$($this).find(".nav li").removeClass("first-selected");
				$(obj).parent().addClass("selected");
				if(i == 0){
					$(obj).parent().addClass("first-selected");
					$($this.find(".default")).hide();
					$($this.find(".scene")).hide();
					$($this.find(".default")).fadeIn(500);
					$($this.find(".default")).show();

					$($(".scenetext")).hide();
					$($(".defaulttext")).hide();
					$($(".defaulttext")).fadeIn(500);
					$($(".defaulttext")).show();
				}else{
					$($(".defaulttext")).hide();
					$($(".scenetext")).hide();
					$($(".scenetext")[i-1]).fadeIn(500);
					$($(".scenetext")[i-1]).show();
					showScene(i-1);
				}
				resizeInner();
				return false;
			});

			$(obj).hoverIntent({
				over: function(){
					if($(obj).parent().hasClass("selected")) return false;
					$($this).find(".nav li").removeClass("selected");
					$($this).find(".nav li").removeClass("first-selected");
					$(obj).parent().addClass("selected");
					if(i == 0){
						$(obj).parent().addClass("first-selected");
						$($this.find(".default")).hide();
						$($this.find(".scene")).hide();
						$($this.find(".default")).fadeIn(500);
						$($this.find(".default")).show();

						$($(".scenetext")).hide();
						$($(".defaulttext")).hide();
						$($(".defaulttext")).fadeIn(500);
						$($(".defaulttext")).show();
					}else{
						$($(".defaulttext")).hide();
						$($(".scenetext")).hide();
						$($(".scenetext")[i-1]).fadeIn(500);
						$($(".scenetext")[i-1]).show();
						showScene(i-1);
					}
					resizeInner();
					return false;				
				},
				timeout: 500,
				out: function(){}
			});
		});

		$this.find('.scene').each(function(i, scene){
			$(scene).find('.stage a[rel="hotspot"]').each(function(i, obj){
				$(obj).click(function(){
					showHotspot(scene, i);
					return false;
				});

				$(obj).hoverIntent({
					over: function(){
						showHotspot(scene, i);
						return false;
					},
					timeout: 500,
					out: function(){}
				});
			});
			$(scene).find('.hotspotnav a[rel="hotspot"]').each(function(i, obj){
				$(obj).click(function(){
					showHotspot(scene, i);
					return false;
				});
			});
		});
	};

	showScene = function(i){
		$($this.find(".default")).hide();
		$($this.find(".scene")).hide();
		$($this).find('.stage a[rel="hotspot"]').animate({ width: 24, height: 24, marginTop: -6, marginLeft: -6 }, 1);
		$($this).find('.stage a[rel="hotspot"] img').animate({ width: 12, height: 12, marginTop: 6, marginLeft: 6 }, 1);
		$($this).find('.stage a[rel="hotspot"]').removeClass("hotspot-active");
		if($this.find(".scene")[i]){
			$($this.find(".scene")[i]).fadeIn(500);
			$($this.find(".scene")[i]).show();

			$($this.find(".scene .overview")).show();
			$($this.find(".scene div.hotspot")).hide();
		}
	};

	showHotspot = function(scene, i){
		$($(scene).find(".overview")).hide();
		$($(scene).find("div.hotspot")).hide();
		$(scene).find('.stage a[rel="hotspot"]').animate({ width: 25, height: 25, marginTop: -6, marginLeft: -6 }, 75);
		$(scene).find('.stage a[rel="hotspot"] img').animate({ width: 12, height: 12, marginTop: 6, marginLeft: 6 }, 75);
		$(scene).find('.stage a[rel="hotspot"]').removeClass("hotspot-active");
		if($(scene).find('.stage a[rel="hotspot"]')[i]){
			$($(scene).find('.stage a[rel="hotspot"]')[i]).animate({ width: 24, height: 24, marginTop: -6, marginLeft: -6 }, 100);
			$($(scene).find('.stage a[rel="hotspot"]')[i]).find("img").animate({ width: 24, height: 24, marginTop: 0, marginLeft: 0 }, 100);
			$($(scene).find('.stage a[rel="hotspot"]')[i]).addClass("hotspot-active");
		}
		if($(scene).find("div.hotspot")[i]){
			$($(scene).find("div.hotspot")[i]).fadeIn(500);
			$($(scene).find("div.hotspot")[i]).show();
		}
	};

	$.fn.productfinder = function(options){
		$this = $(this);
		$options = $.extend({}, $.fn.productfinder.defaults, options);
		observe();
	};

	$.fn.productfinder.defaults = {};
}(jQuery));

$(document).ready(function(){  
	$("#productfinder2").productfinder();
	resizeInner();
});  
