function resizemiddle()
{
	if ($('#content').length>0) {
		var h = $('#footer').offset().top-$('#content').offset().top;
		$('#menu').css("height", h+"px").trigger("resize");
		$('#content').css("height", h+"px").trigger("resize");
	}
}

function setupsearchbar()
{
	$('#chktoggle').bind('click', function () {
		$(this).toggleClass('chkdisabled');
		$(this).toggleClass('chkenabled');
		pge = 0;
		ftnk();
	});
	
	$('#searchbox').bind('keyup', ftnk);
	resizemiddle();
}

function initsearch()
{
	$('span.hvscpath').html('<a href="http://hvsc.c64.org">HVSC Path</a>');
	//$('#songdetails').prepend('<li><a href="/order.php?src=sphp"><img border="0" src="/images/sphpadv.jpg" /></a><br /><br /></li>');
	$('a.play').click(function () {
		return ps($(this));
	});

	resizemiddle();	
	
	$('#middle').bind("resize", function(){
		resizemiddle();
	});
	
	if ($('#searchbarp').length>0) {
		$.ajax({type: "post",
			dataType: 'html',
			url: '/searchbar.php',
			success: function(data) {
				$('#searchbarp').html(data);
				setupsearchbar();
			}
		});
	}
	else {
		if ($('#searchbar').length>0) {
			setupsearchbar();
		}
	}
}

var tix = -1;
var lst = '';
var pge = 0;
var la = null;

function ftns(tixo)
{
	if (tixo>=0) {
		$("#searchlist a").eq(tixo).removeClass('seltune');
	}
	if (tix>=0) {
		$("#searchlist a").eq(tix).addClass('seltune');
	}
}

function ftnk(event)
{
	var key = 0;
	if (event) {
		key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	}
	if (key == 27) {
		$("#searchbox").get(0).value = '';
		$("#searchlist").hide();
		pge = 0;
		return false;
	}
	else
	if (key == 13) {
		if (tix>=0 && $("#searchlist a").size()>0) {
			ftnc(null, $("#searchlist a").get(tix));
		}
		return false;
	}
	else
	if (key == 40) {
		var tcnt = $("#searchlist a").size();
		if (tcnt>0) {
			if (tix<(tcnt-1)) {
				tix++;
				ftns(tix-1);
			}
		}
		return false;
	}
	else
	if (key == 38) {
		var tcnt = $("#searchlist a").size();
		if (tcnt>0) {
			if (tix>0) {
				tix--;
				ftns(tix+1);
			}
		}
		return false;
	}
	else {
		var tagstr = $("#searchbox").get(0).value;
		if (lst!=tagstr ||!event) {
			if (lst!=tagstr) {
				pge = 0;
				lst = tagstr;
			}
			if (tagstr!='') {
				var chk8580 = '';
				if ($('#chktoggle').hasClass('chkenabled')) {
					chk8580 = 'sv=8&';
				};
				var tsTimeStamp= new Date().getTime();
				
				if (la!=null) {
					la.abort();
				}
				
				la = $.ajax({type: "post",
					dataType: 'html',
					url: '/dyna/search.php',
					data: 'p='+pge+'&u='+tsTimeStamp+'&'+chk8580+'q=' + encodeURIComponent(tagstr.toLowerCase()),
					success: function(data) {
						if (data!='') {
							tix = -1;
							var sb = $("#searchbox");
							if (sb.get(0).value!='') {
								$("#searchlist").empty().prepend(data).show().css('left', sb.offset().left).css('top', sb.offset().top+sb.height()+8);
								$('#searchlist a.play').bind('click', function () {
									return ps($(this));
								});
							}
						}
						else {
							$("#searchlist").empty().hide();
						}
					}
				});				
				
				/*$.get('/dyna/search.php?p='+pge+'&u='+tsTimeStamp+'&'+chk8580+'q=' + encodeURIComponent(tagstr.toLowerCase()), function(data) {
					if (data!='') {
						tix = -1;
						var sb = $("#searchbox");
						if (sb.get(0).value!='') {
							$("#searchlist").empty().prepend(data).show().css('left', sb.offset().left).css('top', sb.offset().top+sb.height()+8);
							$('#searchlist a.play').bind('click', function () {
								ps($(this));
							});
						}
					}
					else {
						$("#searchlist").empty().hide();
					}
				});*/
			}
			else {
				$("#searchlist").empty().hide();
				pge = 0;
				lst = '';
			}
		}
	}
	return true;
}

function ftnc(event, mobj)
{
	var obj = null;

	if (event) {
		obj = event.srcElement?event.srcElement:event.target;
	}
	else if (mobj) {
		obj = mobj;
	}

	ps(null, obj);
}

function pgep(incr)
{
	pge+=incr;
	if (pge<0) {
		pge = 0;
	}
	ftnk();
}

function ps(obj, nobj)
{
	var h;
	var f;
	
	if (obj!=null) {
		h = obj.attr('id').substring(2);
		f = obj.attr('href');
	}
	else {
		h = nobj.id.substring(2);
		f = nobj.href;
	}
	
	if (h && h!='') {
		var pc = $('#player-control');
		if (plcs && pc.length==1) {
			preloadplayer(h, true);
		}
		else {
			var tsTimeStamp= new Date().getTime();

			$.ajax({type: "get",
				dataType: 'html',
				url: '/dyna/playlog.php',
				data: {'u': tsTimeStamp, 'h': h, 'f': f},
				success: function(data) {
					window.location.href = data;
				}
			});
		}
	}
	
	return false;
}