var wW;
var wH;
var bH; //body height
var imgSplit;
// 44 is the height of the user nav bar.

$(document).ready(function() {
	wW = $(window).width();
	wH = $(window).height();
	$("#artist_background").css("width", (wW-500) + "px");
	$("#artist_background").css("height", wH + "px");
	$("#right_navigation").css("height", wH + "px");
	$("#search_results").css("height", wH + "px");
	
	imgSplit = (wW-500) / 4;
	
	$(".bg_img").css("width", imgSplit + "px");
	$(".bg_img").css("height", imgSplit + "px");
	
	$("#artist").bind("focus",function() {
		$(this).val("");
		$("#search_button").css("color","#000");
		$("#artist").css("color","#000");
	});
	
	$("#artist").bind("blur",function() {
		
	});
	
	$("#close_search").bind("click",function() {
		$("object").css("visibility","hidden");
		
		$("#close_search").animate({"left": "-=" + 440 + "px"}, "slow", function() {
			$("#close_search").css("display","none");
		});
		
		$("#artist_background").animate({"left": "-=" + 440 + "px"}, "slow", function() {
			$("#search_results").css("display","none");
			$("#search_results").html('<div id="loader"><img src="' + BASE + '/img/5-0.gif" width="32" height="32" alt="5 0"></div>');
		});
	});
	
	$("#search_button").bind("click",function() {
		if ($("#search_results").css("display") == "block") {
			$("object").css("visibility","hidden");
			
			$("#close_search").animate({"left": "-=" + 440 + "px"}, "slow", function() {});
			
			$("#artist_background").animate({"left": "-=" + 440 + "px"}, "slow", function() {
				$("#close_search").animate({"left": "+=" + 440 + "px"}, "slow", function() {
				});

				$("#artist_background").animate({"left": "+=" + 440 + "px"}, "slow", function() {
					$("#search_results").css("display","block");
				});
				$("#search_results").css("display","none");
				$("#search_results").html('<div id="loader"><img src="' + BASE + '/img/5-0.gif" width="32" height="32" alt="5 0"></div>');
			});
		} else {
			$("#close_search").css("display","block");

			$("#close_search").animate({"left": "+=" + 440 + "px"}, "slow", function() {
			});

			$("#artist_background").animate({"left": "+=" + 440 + "px"}, "slow", function() {
				$("#search_results").css("display","block");
			});
		}
		
		$("#search_results").css("display","block");
		
		$.ajax({
			type: "POST",
			url: BASE + "/search/artist/" + $("#artist").val(),
			success: function(html){
				
				
				$("#search_results").html('<div id="loader"><img src="' + BASE + '/img/5-0.gif" width="32" height="32" alt="5 0"></div>');
				//$("#search_results").append(html);
				
				// Now, go fetch youtube
				$.ajax({
					type: "POST",
					url: BASE + "/search/queryYouTube/" + $("#artist").val(),
					success: function(html){
						$("object").css("visibility","visible");
						$("#loader").css("display","none");
						$("#search_results").append(html);
					}
				});
				
			}
		});
	});
	
	$("#artist").keypress(function (e) {
    	if (e.which == 13) {
			
			if ($("#search_results").css("display") == "block") {
				$("object").css("visibility","hidden");
				
				$("#close_search").animate({"left": "-=" + 440 + "px"}, "slow", function() {});
				
				$("#artist_background").animate({"left": "-=" + 440 + "px"}, "slow", function() {
					$("#close_search").animate({"left": "+=" + 440 + "px"}, "slow", function() {
					});

					$("#artist_background").animate({"left": "+=" + 440 + "px"}, "slow", function() {
						$("#search_results").css("display","block");
					});
					$("#search_results").css("display","none");
					$("#search_results").html('<div id="loader"><img src="' + BASE + '/img/5-0.gif" width="32" height="32" alt="5 0"></div>');
				});
			} else {
				$("#close_search").css("display","block");

				$("#close_search").animate({"left": "+=" + 440 + "px"}, "slow", function() {
				});

				$("#artist_background").animate({"left": "+=" + 440 + "px"}, "slow", function() {
					$("#search_results").css("display","block");
				});
			}
			
			$("#search_results").css("display","block");
			
			$.ajax({
				type: "POST",
				url: BASE + "/search/artist/" + $("#artist").val(),
				success: function(html){
					
					
					$("#search_results").html('<div id="loader"><img src="' + BASE + '/img/5-0.gif" width="32" height="32" alt="5 0"></div>');
					//$("#search_results").append(html);
					
					// Now, go fetch youtube
					$.ajax({
						type: "POST",
						url: BASE + "/search/queryYouTube/" + $("#artist").val(),
						success: function(html){
							$("object").css("visibility","visible");
							$("#loader").css("display","none");
							$("#search_results").append(html);
						}
					});
					
				}
			});
    	}
	});
	
});

$(window).bind("resize", function() {
	wW = $(window).width();
	wH = $(window).height();
	$("#artist_background").css("width", (wW-500) + "px");
	$("#artist_background").css("height", wH + "px");
	$("#right_navigation").css("height", wH + "px");
	$("#search_results").css("height", wH + "px");
	
	imgSplit = (wW-500) / 4;
	
	$(".bg_img").css("width", imgSplit + "px");
	$(".bg_img").css("height", imgSplit + "px");
	
});

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function hideFlash() {
	$("#flashMessage").slideUp();
}


