imgUrl="";
emailUrl="";
getalbumUrl="";
titlebarLabel="";
currentTip=-1;
currentInput='album title';

tips=new Array();
tips[0]="Click the 'advertise' box to advertise on 5byfive";
tips[1]="Follow us on Twitter <a href='http://twitter.com/5byfivecovers' target='_blank'>@5byfivecovers</a>";
tips[2]="Covers provided by Google Images";
tips[3]="Click the 'Get Album' button to buy the album";
tips[4]="Wrong cover? Try adding the artist after the album title";
tips[5]="Send us your feedback at <a href='mailto:support@5byfive.net'>support@5byfive.net</a>";
tips[6]="Click the 'Email Link' button to email a link to the cover";
tips[7]="The segmented bar below the cover shows the quality of the image";

$(document).ready(function(){
						   
	ticker();
	//monitorSearchField();
								   	
	$("#toolbar").fadeOut(0);
	$("#adbar").fadeOut(0);
	$("#titlebar").fadeOut(0);
	$("#qualitybar").fadeOut(0);
	$("#tooltip").fadeOut(0);
	$("#tips").fadeOut(0);
	$("#ac_wrapper").fadeOut(0);
	$("#cover_wrapper").fadeOut(0);
	
	if (navigator.appVersion.indexOf("Safari")==-1){
		$("#search_form_wrapper").css({"top":10});
	};

	$("#search_field").keyup(function() {
		$("#cover_image").attr({"src":""});
		if ($("#search_field").attr("value")==""){
			$("#search_box").animate({"left":222},{queue:false, duration:250});
			$("#search_box").animate({"top":249},{queue:false, duration:250});
			$("#logo").animate({"left":622},{queue:false, duration:250});
			$("#logo").animate({"top":249},{queue:false, duration:250});
			$("#search_edge").css({"cursor":"default"});
			$("#search_edge").css({"backgroundImage":"url(images/search_edge_search.png)"});
			$("#cover_wrapper").fadeOut(0);
			$("#tips").fadeOut(0);
			$("#ac_wrapper").css({"height":0});
			$("#ac_wrapper").fadeOut(0);
			};
		if ($("#search_field").attr("value")!=""){
			$("#search_box").animate({"left":65},{queue:false, duration:250});
			$("#search_box").animate({"top":289},{queue:false, duration:250});
			$("#logo").animate({"left":322},{queue:false, duration:250});
			$("#logo").animate({"top":234},{queue:false, duration:250});
			$("#search_edge").css({"cursor":"pointer"});
			$("#search_edge").css({"backgroundImage":"url(images/search_edge_clear.png)"});
			$("#cover_wrapper").fadeIn(250);
			$("#tips").fadeIn(250);
		};
		ii=-1;
		OnLoad();
	});
	
	$("#search_edge").click(function() {
		if ($("#search_field").attr("value")!="" && $("#search_field").attr("value")!="album title"){
			$("#search_box").animate({"left":222},{queue:false, duration:250});
			$("#search_box").animate({"top":249},{queue:false, duration:250});
			$("#logo").animate({"left":622},{queue:false, duration:250});
			$("#logo").animate({"top":249},{queue:false, duration:250});
			$("#search_edge").css({"cursor":"default"});
			$("#search_edge").css({"backgroundImage":"url(images/search_edge_search.png)"});
			$("#cover_wrapper").fadeOut(0);
			$("#qualitybar").fadeOut(0);
			$("#tips").fadeOut(0);
			$("#ac_wrapper").fadeOut(0);
			$("#search_field").val("album title");
			$("#search_field").css({"color":"#C0C0C0"});
		};
	});
	
	$("#search_field").focus(function() {
		if ($("#search_field").attr("value")=="album title"){
			$("#search_field").val("");
			$("#search_field").css({"color":"#555555"});
		};
	});
	$("#search_field").blur(function() {
		$("#ac_wrapper").animate({"height":0},200); 
		if ($("#search_field").attr("value")==""){
			$("#search_field").val("album title");
			$("#search_field").css({"color":"#C0C0C0"});
		};
	});
	$("#cover_wrapper").mouseenter(function() {
		$("#toolbar").fadeIn(150);
		$("#adbar").fadeIn(150);
		$("#qualitybar").fadeIn(150);
		//$("#titlebar").fadeIn(150);
	});
	$("#cover_wrapper").mouseleave(function() {
		$("#toolbar").fadeOut(250);
		$("#adbar").fadeOut(250);
		$("#qualitybar").fadeOut(250);
		//$("#titlebar").fadeOut(250);
	});
	
	$("#download_button").mousedown(function() {
		$("#toolbar").css({"backgroundImage":"url(images/toolbar_download.png)"});
	});
	$("#download_button").mouseup(function() {
		$("#toolbar").css({"backgroundImage":"url(images/toolbar.png)"});
		window.open(imgUrl);
	});
	$("#download_button").mouseenter(function() {
		$("#tooltip").html("Download");
		$("#tooltip").css({"left":105});
		$("#tooltip").fadeIn(150);
	});
	$("#download_button").mouseleave(function() {
		$("#tooltip").fadeOut(0);
	});
	
	$("#email_button").mousedown(function() {
		$("#toolbar").css({"backgroundImage":"url(images/toolbar_email.png)"});
	});
	$("#email_button").mouseup(function() {
		$("#toolbar").css({"backgroundImage":"url(images/toolbar.png)"});
		emailUrl = "mailto:recipient@email.com?subject=Album Art&body=Check out this album art: " + imgUrl;
		window.location=emailUrl;
	});
	$("#email_button").mouseenter(function() {
		$("#tooltip").html("Email Link");
		$("#tooltip").css({"left":165});
		$("#tooltip").fadeIn(150);
	});
	$("#email_button").mouseleave(function() {
		$("#tooltip").fadeOut(0);
	});
	
	$("#getalbum_button").mousedown(function() {
		$("#toolbar").css({"backgroundImage":"url(images/toolbar_getalbum.png)"});
	});
	$("#getalbum_button").mouseup(function() {
		getalbumUrl="";
		$("#toolbar").css({"backgroundImage":"url(images/toolbar.png)"});
		getalbumUrl = "http://astore.amazon.com/5byfive-20/search?node=1&keywords=" + $("#search_field").attr("value");
		window.open(getalbumUrl);
	});
	$("#getalbum_button").mouseenter(function() {
		$("#tooltip").html("Get Album");
		$("#tooltip").css({"left":230});
		$("#tooltip").fadeIn(150);
	});
	$("#getalbum_button").mouseleave(function() {
		$("#tooltip").fadeOut(0);
	});
	
	$("#ad1").click(function() {
		if ($("#ad1").css("backgroundImage")=="none"){
			window.location="mailto:support@5byfive.net?Subject=Regarding%20Advertising";
		}else {
			window.open("http://themeforest.net?ref=blabus");
		};
	});
	$("#ad2").click(function() {
		if ($("#ad2").css("backgroundImage")=="none"){
			window.location="mailto:support@5byfive.net?Subject=Regarding%20Advertising";
		}else {
			window.open("AD 2 URL HERE");
		};
	});
	$("#ad3").click(function() {
		if ($("#ad3").css("backgroundImage")=="none"){
			window.location="mailto:support@5byfive.net?Subject=Regarding%20Advertising";
		}else {
			window.open("AD 3 URL HERE");
		};
	});
	
	$(".ac_item").mouseenter(function() {
		$(this).children(".ac_album").css({"color":"#FFFFFF"});	
		$(this).children(".ac_album").css({"text-shadow":"0px -1px 0px #000000"});		
		$(this).children(".ac_artist").css({"color":"#CCCCCC"});	
		$(this).children(".ac_artist").css({"text-shadow":"0px -1px 0px #000000"});
		$(this).css({"color":"#CCCCCC"});	
		$(this).css({"text-shadow":"0px -1px 0px #000000"});								  
	});
	$(".ac_item").mouseleave(function() {
		$(this).children(".ac_album").css({"color":"#444444"});	
		$(this).children(".ac_album").css({"text-shadow":""});		
		$(this).children(".ac_artist").css({"color":"#777777"});	
		$(this).children(".ac_artist").css({"text-shadow":""});
		$(this).css({"color":"#777777"});	
		$(this).css({"text-shadow":""});								  
	});
	$(".ac_item").click(function() {
		$("#search_field").val($(this).text());
		$("#ac_wrapper").animate({"height":0},200); 
		OnLoad();
	});

});

function ticker() {
	currentTip=currentTip+1;
	if (currentTip>tips.length-1){
		currentTip=0;
	};
	if ($("#search_field").attr("value")!="" && $("#search_field").attr("value")!="album title"){
		$("#tips").fadeOut(250, function() {
			$("#tips").html(tips[currentTip]);
			$("#tips").fadeIn(250);
		});
	};
		var t=setTimeout("ticker()",6000);
};

function monitorSearchField() {
	if (currentInput != $("#search_field").attr("value") && $("#search_field").attr("value") != "album title") {
		currentInput = $("#search_field").attr("value");
		loadSuggestions($("#search_field").attr("value"));
	};
	if ($("#search_field").attr("value") == "album title" || $("#search_field").attr("value") == "") {
		$("#ac_wrapper").animate({"height":0},200); 
	};

	var t=setTimeout("monitorSearchField()",1500);
};

function loadSuggestions(qry) {
	qry=qry.replace(/ /g,"+")
	queryString="http://musicbrainz.org/ws/1/release/?type=xml&query="+qry+"*&limit=4";
	queryString="/proxy.php?url="+escape(queryString);
	 $.ajax({url: queryString, dataType: ($.browser.msie) ? "text" : "xml", success: function(data){
     var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     }
	 currentItm=0;
	 numItms=0;
	 $(xml).find("release").each(function(){
		numItms++;
	 });
	 $("#ac_wrapper").fadeIn(200);
	 if (numItms==0){
		//$("#ac_wrapper").animate({"height":0},200); 
	 };
	 if (numItms==1){
		$("#ac_wrapper").animate({"height":80},200); 
	 };
	 	 if (numItms==2){
		$("#ac_wrapper").animate({"height":120},200); 
	 };
	 if (numItms==3){
		$("#ac_wrapper").animate({"height":160},200); 
	 };
	 if (numItms==4){
		$("#ac_wrapper").animate({"height":200},200); 
	 };
     $(xml).find("release").each(function(){
	   currentItm++;
	   alb=$(this).find("title").text();
	   $(this).find("artist").each(function(){
			art=$(this).find("name").text();
	   });
	   htmlStr='<span class="ac_album">'+alb+'</span> <span class="ac_artist">'+art+'</span>';
	   iid="#"+"ac_item"+currentItm;
	   $(iid).html(htmlStr);
	 });
   }
 });
};

    google.load('search', '1');
    
    var imageSearch;
    
    function searchComplete() {
      $("#cover").fadeOut(0);
      // Check that we got results
      if (imageSearch.results && imageSearch.results.length > 0) {   
        // Loop through our results, printing them to the page.
        var results = imageSearch.results;
		numResults=results;
          // For each result write it's title and image to the screen
		ii=ii+1;
        var result = results[ii];
		var resultString = String(result.tbUrl);
		var finalUrl = resultString.substring(53);
		finalUrl = "http://"+finalUrl;
		imgUrl=finalUrl;
		var tempImg = new Image();
		tempImg.src = finalUrl;
		$("#cover_image").attr({"src":finalUrl});
		var displayUrl=finalUrl.substring(0,55) + "...";
      }
    }
	
	function loadTitle() {
		$("#cover").fadeIn(150);
		$("#title_label").html(document.getElementById('cover_image').naturalWidth + " x " + document.getElementById('cover_image').naturalHeight);
		if (document.getElementById('cover_image').naturalWidth<270 || document.getElementById('cover_image').naturalHeight<270) {
			searchComplete();
		};
		var ratio = document.getElementById('cover_image').naturalWidth / document.getElementById('cover_image').naturalHeight;
		if (ratio<0.8 || ratio>1.2) {
			searchComplete();
		};
		if (document.getElementById('cover_image').naturalWidth>270){
			$("#qualitybar").css({"backgroundImage":"url(images/quality_bar_1.png)"});
		};
		if (document.getElementById('cover_image').naturalWidth>350){
			$("#qualitybar").css({"backgroundImage":"url(images/quality_bar_2.png)"});
		};
		if (document.getElementById('cover_image').naturalWidth>450){
			$("#qualitybar").css({"backgroundImage":"url(images/quality_bar_3.png)"});
		};
		if (document.getElementById('cover_image').naturalWidth>499){
			$("#qualitybar").css({"backgroundImage":"url(images/quality_bar_4.png)"});
		};
	}
    
    function OnLoad() {
	  $("#cover").fadeOut(50);
	  $("#title_label").html("Loading...");
      // Our ImageSearch instance.
      imageSearch = new google.search.ImageSearch();
   
      // Restrict to medium images only
      imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                                 google.search.ImageSearch.IMAGESIZE_MEDIUM);
    
      // Here we set a callback so that anytime a search is executed, it will call
      // the searchComplete function and pass it our ImageSearch searcher.
      // When a search completes, our ImageSearch object is automatically
      // populated with the results.
      imageSearch.setSearchCompleteCallback(this, searchComplete, null);
    
      // Find me a beautiful car.
	  var searchString = $("#search_field").attr("value") + " album art";
      imageSearch.execute(searchString);
    }