$(document).ready(function(){
	// Document is ready

	$("#artistresults").hide();

	// Flir
	FLIR.init( { path: './_scripts/' } );
	FLIR.replace( 'h2', new FLIRStyle({ cFont:'giza', mode:'wrap' }) );
	FLIR.replace( '.a_website', new FLIRStyle({ cFont:'giza', mode:'progressive' }) );	

	// Animate the signup envelope.
	$('#li_signup').hover(
      function () {
        $(this).animate( {top:"-100" },100 );
      }, 
      function () {
        $(this).animate( {top:"-110" },100 );
      }
    );


	//External links.
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

	// Country selection
	$('#country_list').hide();
	$('#countries').hover(
		function () {
			$('#country_list').fadeIn('fast');
			$('#country_selector').css('background-position' ,'-157px -16px');
		},
		function () {
			$('#country_list').hide();
			$('#country_selector').css('background-position' ,'0px -16px');
		}
	);
	
	var i = 0;
    $("div.overout").mouseover(function(){
      $("p:first",this).text("mouse over");
      $("p:last",this).text(++i);
    }).mouseout(function(){
      $("p:first",this).text("mouse out");
    });

	simple_tooltip("a.tip","tooltip");
	
	// Footer position
	// var footerwidth = parseInt($('#ul_footer').css('width'));
	// var margin = footerwidth/2;
	// $('#ul_footer').css('margin-left',-margin);

	// Artist live search
	$('#form_search').keydown(function(e){
		// alert(e.keyCode);
		if (e.keyCode == 40) {
			// Down key pressed
			var nextSelect = $('.selected').next();
			nextSelect.addClass('selected');
			nextSelect.prev().removeClass('selected');
			
		} else if (e.keyCode == 38){
			// Up key pressed
			$('.selected').prev().addClass('selected');
			$('.selected').next().removeClass('selected');
			
		} else if (e.keyCode == 13){
			// Enter key pressed
			var href = $('.selected a').attr('href');
			window.location = href;
			return false;
		} else {
			// Normal typing
			$("#artistresults").show();
			$.get("artistsearch.php?" + Math.random(),{query: $("#input_artist").val(), type: "quickcheck"}, function(data){
				$("#artistresults").html(data);
				artistLinkBind();
			});
		}
	});
	
	// When form clicked away from empty results.
	$('#input_artist').blur(function(){
		// Hide results
		setTimeout(	function (){
			$('#artistresults').empty();
			$('#artistresults').hide();
			// console.log('hiding');
			}, 200);
	});		

	
	// 	Makes the search button work.
	$('#artistsearch_btn').click(function(){
		$('#form_search').submit();
		return false;
	});
	
	// Now create a hover for the menu items.
	function artistLinkBind(){
		$(".artistlink").hover(
	      function () {
			$("#artistresults li").removeClass('selected');
			$(this).parent().addClass('selected');
	      }, 
	      function () {
			$(this).parent().removeClass('selected');
	      }
	    );
	}
	
	
	
	
	
	// End of doc ready
});

function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({ display:"none"}).fadeIn(100);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX -0, top:kmouse.pageY-30});
		}).mouseout(function(){
				my_tooltip.fadeOut(100);
		});
	});
}



