$(document).ready(function() {
	var contentExpanded = false;
	var searchDelay = null;
	var currentInput = null;
	var numImagesLoaded = 0;

	var api = new DiscogsAPI();
	api.delegate = function(method, properties) {
		if (contentExpanded) {
			hideNoResultsLabel();
			if (method == "didReceiveArtwork") {
				var primaryUrl = "proxy.php?url=" + encodeURIComponent(properties.artwork[0] + "?cb=" + Math.random());
				$("#primaryResult").attr("src", primaryUrl).load(function() {  
					numImagesLoaded++;
					if (numImagesLoaded == 4) {
						setTimeout(function() {
							hideActivityIndicator();
							showResults();
						}, 1000);
					};
				});
				var secondary1Url = "proxy.php?url=" + encodeURIComponent(properties.artwork[1] + "?cb=" + Math.random());
				$("#secondaryResult1").attr("src", secondary1Url).load(function() {  
					numImagesLoaded++;
					if (numImagesLoaded == 4) {
						setTimeout(function() {
							hideActivityIndicator();
							showResults();
						}, 1000);
					};
				});
				var secondary2Url = "proxy.php?url=" + encodeURIComponent(properties.artwork[2] + "?cb=" + Math.random());
				$("#secondaryResult2").attr("src", secondary2Url).load(function() {  
					numImagesLoaded++;
					if (numImagesLoaded == 4) {
						setTimeout(function() {
							hideActivityIndicator();
							showResults();
						}, 1000);
					};
				});
				var secondary3Url = "proxy.php?url=" + encodeURIComponent(properties.artwork[3] + "?cb=" + Math.random());
				$("#secondaryResult3").attr("src", secondary3Url).load(function() {  
					numImagesLoaded++;
					if (numImagesLoaded == 4) {
						setTimeout(function() {
							hideActivityIndicator();
							showResults();
						}, 1000);
					};
				});
			} else {
				hideActivityIndicator();
				showNoResultsLabel();
			};
		};
	};
	
	$("#searchBox").focus(function() {
		if ($(this).val() == $(this).attr("label")) {
			$(this).val("");
			currentInput = "";
			$(this).css("color", "#e0e0e1");
		};
	});
	$("#searchBox").blur(function() {
		if ($(this).val() == "") {
			$(this).val($(this).attr("label"));
			$(this).css("color", "#76787e");
		};
	});
	$("#searchBox").keyup(function() {
		if ($("#searchBox").val() != currentInput) {
			currentInput = $("#searchBox").val();
			clearTimeout(searchDelay);
			hideResults();
			numImagesLoaded = 0;
			if ($(this).val() == "") {
				collapseContent();
				hideActivityIndicator();
				hideNoResultsLabel();
			} else {
				if (!contentExpanded) {expandContent()};
				showActivityIndicator();
				hideNoResultsLabel();
				searchDelay = setTimeout(function() {
					api.getArtworkForQuery($("#searchBox").val());
				}, 500);
			};
		};
	});
	
	$(".secondaryResult").click(function() {
		swapPrimaryWithSecondary($(this).attr("index"));
	});
	
	function expandContent() {
		contentExpanded = true;
		$("#searchBoxWrapper").css({"-webkit-transition":"all .2s linear", "-moz-transition":"all .2s linear"});
		$("#searchBoxWrapper").css({"-webkit-transform":"translate3d(0px,220px,0px)", "-moz-transform":"translateY(220px)"});
		$("#footer").css({"-webkit-transform":"translate3d(0px,80px,0px)", "-moz-transform":"translateY(80px)"});
		setTimeout(function() {
			$("#searchBoxWrapper").css({"-webkit-transition":"none", "-moz-transition":"none"});
			$("#searchBoxWrapper").css({"-webkit-transform":"none", "-moz-transform":"none", "top":"450px"});
		}, 200);
	};
	function collapseContent() {
		contentExpanded = false;
		$("#searchBoxWrapper").css({"-webkit-transition":"all .2s linear", "-moz-transition":"all .2s linear"});
		$("#searchBoxWrapper").css({"-webkit-transform":"translate3d(0px,-220px,0px)", "-moz-transform":"translateY(-220px)"});
		$("#footer").css({"-webkit-transform":"translate3d(0px,0px,0px)", "-moz-transform":"translateY(0px)"});
		setTimeout(function() {
			$("#searchBoxWrapper").css({"-webkit-transition":"none", "-moz-transition":"none"});
			$("#searchBoxWrapper").css({"-webkit-transform":"none", "-moz-transform":"none", "top":"230px"});
		}, 200);
	};
	
	function hideActivityIndicator() {
		$("#activityIndicator").css("opacity", "0");
	};
	function showActivityIndicator() {
		$("#activityIndicator").css("opacity", "1");
	};
	
	function hideNoResultsLabel() {
		$("#noResultsLabel").css("opacity", "0");
	};
	function showNoResultsLabel() {
		$("#noResultsLabel").css("opacity", "1");
	};
	
	function showResults() {
		$("#primaryResult").css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$(".secondaryResult").css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$("#primaryResult").css({"-webkit-transform":"scale3d(1,1,1)", "-moz-transform":"scale(1,1)", "opacity":"1"});
		$(".secondaryResult").css({"-webkit-transform":"scale3d(1,1,1)", "-moz-transform":"scale(1,1)", "opacity":"1"});
	};
	function hideResults() {
		$("#primaryResult").css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$(".secondaryResult").css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$("#primaryResult").css({"-webkit-transform":"scale3d(.85,.85,1)", "-moz-transform":"scale(.85,.85)", "opacity":"0"});
		$(".secondaryResult").css({"-webkit-transform":"scale3d(.85,.85,1)", "-moz-transform":"scale(.85,.85)", "opacity":"0"});
	};
	
	function swapPrimaryWithSecondary(index) {
		$primaryResult = $("#primaryResult");
		var primarySrc = $primaryResult.attr("src");
		$secondaryResult = $(".secondaryResult[index='" + index + "']");
		var secondarySrc = $secondaryResult.attr("src");
		
		$primaryResult.css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$secondaryResult.css({"-webkit-transition":"all .15s linear", "-moz-transition":"all .15s linear"});
		$primaryResult.css({"top":$secondaryResult.position().top, "left":"445px", "width":"125px", "height":"125px"});
		$secondaryResult.css({"top":"0px", "left":"20px", "width":"400px", "height":"400px"});
		
		setTimeout(function() {
			$primaryResult.attr("src", secondarySrc);
			$secondaryResult.attr("src", primarySrc);
			$primaryResult.css({"-webkit-transition":"none", "-moz-transition":"none"});
			$secondaryResult.css({"-webkit-transition":"none", "-moz-transition":"none"});
			$primaryResult.css({"top":"0px", "left":"20px", "width":"400px", "height":"400px"});
			$secondaryResult.css({"left":"445px", "width":"125px", "height":"125px"});
			switch (parseInt(index)) {
				case 1:
					$secondaryResult.css("top", "0px");
				break;
				case 2:
					$secondaryResult.css("top", "137px");
				break;
				case 3:
					$secondaryResult.css("top", "275px");
				break;
			};
		}, 200);
	};
});



















