function whereExit()
{
	if (tHolder[0])
	{
		clearTimeout(tHolder[0]);
	}
	tHolder[0]=setTimeout('hideWhereList();',1500);
}

function whereEnter()
{
	if (tHolder[0])
	{
		clearTimeout(tHolder[0]);
	}
}



function showWhereList()
{
	$('where_options').style.left=($('where_button').offsetLeft-50)+"px";
	$('where_options').style.top=($('where_button').offsetTop+20)+"px";
	Effect.Appear('where_options',{duration:0.25,to:0.95});
	setTimeout('whereOn=1',100);
	setTimeout('whereExit()',101);
}

function hideWhereList()
{
	Effect.Fade('where_options',{duration:0.25,queue: 'end'});
	whereOn=0;
}

function toggleWhereList()
{
	if (whereOn==1)
	{
		hideWhereList();
	}
	else
	{
		showWhereList();
	}
}

function whereUpdate()
{
	$('where_'+whereOpts[whereChoice]).className='selected';
}

function whereClicked(val)
{
	var end=whereOpts.length;
	var i=1;
	if ($('artist_input').value==aInputDefaultTxt[whereChoice])
	{
		$('artist_input').value=aInputDefaultTxt[val];
	}
	
	if ($('content').innerHTML==contentStartText[whereChoice])
	{
		$('content').innerHTML=contentStartText[val];
	}
	
	if (val>0 & val<=end)
	{
		while (i<end)
		{
			var temp=document.getElementById('where_'+whereOpts[i]);
			if (temp)
			{
				if (i==val)
				{
					temp.className='selected';
					whereChoice=val;
				}
				else
				{
					temp.className='';
				}
			}
			i++;
		}
	}
	cookieCut();
	hideWhereList();
}

function clearOpen()
{
	if (recentOn==1)
	{
		hideRecent();
	}
	if (whereOn==1)
	{
		hideWhereList();
	}
}



function hideLoader()
{
	Effect.Fade('loader',{ duration: 0.501, queue:'end'});
	loaderOn=0;
}

function showLoader()
{
	Effect.Appear('loader',{ duration: 0.25 });
	loaderOn=1;
	setTimeout("recentOn=1;hideRecent();",250);

}

function searchArtist(artist)
{
	if (artist!="" & artist!=aInputDefaultTxt[whereChoice] & searching!=1)
	{
		$('research').hide();
		Effect.Fade('content_container',{ duration: 0.5 });
		showLoader();
		$('clear_button').show();
		saveArtist(artist);
		artist=escape(artist);
		var url="http://www.monstore.info/reqInt.php?a="+artist+"&c="+country+"&am="+Amazon+"&e="+eMusic+"&i="+iTunes+"&ec="+eMusicComp+
		"&wh="+whereChoice+"&z="+Zunior;
		searching=1;
		var req=new Ajax.Request(url, {method: 'get',
		  onComplete: function(transport) {
			if (transport.status == 200)
			{
				output=transport.responseText;
    		}
			else
			{
				output="There was a problem retrieving your search results.";
			}
			holder=output;
			setTimeout("document.getElementById('content').innerHTML=holder",501);
			setTimeout("$('research').show();hideLoader()",512);
			setTimeout("Effect.Appear('content_container',{duration:0.5})",512);
			setTimeout("fixScroll()",1013);
			setTimeout("fixScroll()",1014);
			searching=0;
		      
		  }
		});
	}
}

function cutter(country,emusic,emusiccomp,amazon,zunior,recent,auto)
{
	var url="http://www.monstore.info/cookiecutter.php?c="+country+"&e="+emusic+"&ec="+emusiccomp+"&am="+amazon+"&rs="+escape(recent)+"&au="+auto+
	"&wh="+whereChoice+"&z="+zunior;
	var req=new Ajax.Request(url, {method: 'get',
	  onComplete: function(transport) {
	  }
	});
}

function searchFocus()
{
	if (aInputDefaultColor=="")
	{
		aInputDefaultColor=$('artist_input')['style'].color;
	}
	$('artist_input')['style'].color="black";
	$('artist_input')['style'].textAlign="left";
	if ($('artist_input').value==aInputDefaultTxt[whereChoice])
	{
		$('artist_input').value="";
	}
}

function searchBlur()
{
	if ($('artist_input').value=="")
	{
		$('artist_input')['style'].color=aInputDefaultColor;
		$('artist_input')['style'].textAlign="center";
		$('artist_input').value=aInputDefaultTxt[whereChoice];
		$('clear_button').hide();
	}
	else if($('artist_input').value!=aInputDefaultTxt[whereChoice])
	{
		$('clear_button').show();
	}
}

function searchPressed(event)
{
	if (checkEnter(event))
	{
		searchArtist($('artist_input').value);
	}
	else
	{
		$('clear_button').hide();
	}
}

function clearSearch()
{
	$('artist_input').value="";
	$('clear_button').hide();
	$('research').hide();
	$('artist_input').focus();
}

function checkEnter(e)
{
	var characterCode;
	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}
	if(characterCode == 13)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function showBlurBox()
{
	if (recentOn==1)
	{
		hideRecent();
	}
	Effect.Appear('blur_box',{to:0.75,duration:0.125,queue:'front'});
	blurOn=1;
}

function hideBlurBox()
{
	Effect.Fade('blur_box',{to:0.0,duration:0.125,queue:'end'});
	blurOn=0;
}

function toggleAuto(val)
{
	if (val)
	{
		au=1;
		if (!ajaxAutoload)
		{
			ajaxAutoload=new Ajax.Autocompleter("artist_input", "autolist", "./lookup.php", {
			paramName: "b", 
			callback: checkSearch,
			updateElement: updateArtist,
			minChars: 3
			});
		}
		ajaxAutoload.element=$('artist_input');
	}
	else
	{
		au=0;
		if (ajaxAutoload)
		{
			ajaxAutoload.element=$('nothing');
		}
	}
}

function showSettings()
{
	showBlurBox();
	new Effect.Appear('settings_box',{duration:0.1,queue:'end'});
	new Effect.Morph('settings_box', {style: 'width:33%; left:33%;', duration: 0.25,queue:'end'});
	new Effect.Morph('settings_box', {style: 'height:350px; top:110px;', duration: 0.3,queue:'end'});
	settingsOn=1;
}

function hideSettings(val)
{
	if (pageOn==1)
	{
		val=1;
	}
	if (val==2)
	{
		new Effect.Morph('settings_box', {  style: 'height:1px; top:200px;',  duration: 0.01, queue: 'front'});
		new Effect.Morph('settings_box', {  style: 'width:1%; left:50%;',  duration: 0.01, queue: 'front'});
		new Effect.Fade('settings_box',{duration:0.01, queue: 'front'});
	}
	else
	{
		new Effect.Morph('settings_box', {  style: 'height:1px; top:200px;',  duration: 0.3, queue: 'end'});
		new Effect.Morph('settings_box', {  style: 'width:1%; left:50%;',  duration: 0.25, queue: 'end'});
		new Effect.Fade('settings_box',{duration:0.1, queue: 'end'});
	}
	cookieCut();
	if (val!=1)
	{
		hideBlurBox();
	}
	settingsOn=0;
}

function setCountry(val)
{
	country=val;
	if (val==2 | val==3 | val==4 | val==5)
	{
		$('AmazonChk').disabled=false;
		$('AmazonFont').style.opacity="1.0";
	}
	else
	{
		$('AmazonChk').checked=false;
		Amazon=0;
		$('AmazonChk').disabled=true;
		$('AmazonFont').style.opacity="0.5";
	}
	if (val==1)
	{
		$('ZuniorChk').disabled=false;
		$('ZuniorFont').style.opacity="1.0";
	}
	else
	{
		$('ZuniorChk').checked=false;
		Zunior=0;
		$('ZuniorChk').disabled=true;
		$('ZuniorFont').style.opacity="0.5";
	}
}

function iTunesSet(on)
{
}

function eMusicSet(on)
{
	if (on)
	{
		eMusic=1;
		eMusicCompEnable(1);
	}
	else
	{
		eMusic=0;
		eMusicCompEnable(0);
	}
}

function eMusicCompEnable(on)
{
	if (on==1)
	{
		$('eMusicCompChk').disabled=false;
		$('eMusicCompFont').style.opacity='1';
	}
	else
	{
		eMusicComp=0;
		$('eMusicCompChk').checked=false;
		$('eMusicCompChk').disabled=true;
		$('eMusicCompFont').style.opacity='0.5';
	}
}

function eMusicCompSet(on)
{
	if (on)
	{
		eMusicComp=1;
	}
	else
	{
		eMusicComp=0;
	}
}

function AmazonSet(on)
{
	if (on)
	{
		Amazon=1;
	}
	else
	{
		Amazon=0;
	}
}

function ZuniorSet(on)
{
	if (on)
	{
		Zunior=1;
	}
	else
	{
		Zunior=0;
	}
}

function loadScrolls()
{   
	scrollbar = new Control.ScrollBar('content','scrollbar_track');
	p_scrollbar = new Control.ScrollBar('page_contents','page_track');
}

function fixScroll()
{
	scrollbar.reset();
	scrollbar.recalculateLayout();
	scrollbar.scrollBy(0);
}

function fixPScroll()
{
	p_scrollbar.reset();
	p_scrollbar.recalculateLayout();
	p_scrollbar.scrollBy(0);
}

function cookieCut()
{
	cutter(country,eMusic,eMusicComp,Amazon,Zunior,searches.join(";"),au);
}

function autoLoad(val)
{
	if (val==1)
	{
		$('country').value=country;
		if (eMusic==1)
		{
			$('eMusicChk').checked=true;
		}
		if (eMusicComp==1)
		{
			$('eMusicCompChk').checked=true;
		}
		if (Amazon==1)
		{
			$('AmazonChk').checked=true;
		}
		if (Zunior==1)
		{
			$('ZuniorChk').checked=true;
		}
		if (au==1)
		{
			$('AutoChk').checked=true;
		}
		hideSettings(2);
	}
	else
	{
		showSettings();
	}
}

function showResults()
{
	if (settingsOn==1)
	{
		hideSettings(0);
	}
	
	if (pageOn==1)
	{
		hidePage(0);
	}
	
	if (blurOn==1)
	{
		hideBlurBox();
	}
	
	//document.getElementById('content').innerHTML=holder;
}

function kp()
{
	showResults();
	$('artist_input').value='Kick Ptarmigan';
	searchFocus();
	searchArtist('Kick Ptarmigan');
}

function showPage(val)
{
	var pageContents=document.getElementById('page_contents');
	if (!text[val])
	{
		$val="w";
	}
	if (settingsOn==1)
	{
		hideSettings(1);
	}
	pageContents.innerHTML=text[val];
	if (pageOn==1)
	{
		fixPScroll();
		setTimeout("fixPScroll()",1);
	}
	else
	{
		showBlurBox();

		new Effect.Appear('page_box',{duration:0.1,queue:'end'});
		new Effect.Morph('page_box', {style: 'width:33%; left:33%;', duration: 0.25,queue:'end'});
		new Effect.Morph('page_box', {style: 'height:300px; top:125px;', duration: 0.3,queue:'end'});
		setTimeout("fixPScroll()",1252);
		setTimeout("fixPScroll()",1253);
		pageOn=1;
	}

}

function hidePage(val)
{

	pageOn=0;
	new Effect.Morph('page_box', {  style: 'height:1px; top:200px;',  duration: 0.3,queue:'end'});
	new Effect.Morph('page_box', {  style: 'width:1%; left:50%;',  duration: 0.25,queue:'end'});
	new Effect.Fade('page_box',{duration:0.1,queue:'end'});
	if (val!=1)
	{
		hideBlurBox();
	}
}

function checkSearch(a,b)
{
	if (searching==1)
	{
		return "";
	}
	else
	{
		return b+"&whc="+whereChoice;
	}
}

function pageStart(auto,artist)
{
	loadScrolls();
	if (au==1)
	{
		ajaxAutoload=new Ajax.Autocompleter("artist_input", "autolist", "./lookup.php", {
			paramName: "b", 
			callback: checkSearch,
			updateElement: updateArtist,
			minChars: 3,
			indicator: 'loader_auto'
		});
	}
	else
	{
	}

	
	Effect.Fade('recent',{duration:0.0001}); //Can't hide recent??? Have to fade it or nothing works?!?
	$('autolist').hide();
	$('loader_auto').hide();
	$('where_options').hide();
	$('clear_button').hide();
	queue=Effect.Queues.get('global');
	queue.interval=10;
		
	whereUpdate();
	$('recent').style.background='white';
	$('autolist').style.background='white';
	$('autolist').style.left=($('search_pane').offsetLeft+60)+"px";
	$('autolist').style.top=($('search_pane').offsetTop+30)+"px";

	hidePage(1);
	
	if (auto!=1)
	{
		$('settings_box').style.zIndex='4';
	}
	
	$('artist_input').value=aInputDefaultTxt[whereChoice];
	$('content').innerHTML=contentStartText[whereChoice];
	if (artist!='')
	{
		autoLoad(1);
		searchArtist(artist);
		searchFocus();
		$('artist_input').value=artist;
	}
	else
	{
		autoLoad(auto);
	}
	$('research').hide();
	$('research').style.zIndex=2;
	$('research').style.top=($('artist_input').offsetHeight+$('artist_input').offsetTop+$('main').offsetTop-17)+"px";
	$('research').style.left=($('artist_input').offsetWidth+$('artist_input').offsetLeft+$('main').offsetLeft-22)+"px";
	setTimeout("$('page_box').style.zIndex='2';$('settings_box').style.zIndex='4';$('recent').style.zIndex='1';$('autolist').style.zIndex='5';$('where_options').style.zIndex=5;",750);
	hideLoader();
	eMusicCompEnable(eMusic);
	setCountry(country);
}

function fixThings()
{
	$('research').style.top=($('artist_input').offsetHeight+$('artist_input').offsetTop+$('main').offsetTop-17)+"px";
	$('research').style.left=($('artist_input').offsetWidth+$('artist_input').offsetLeft+$('main').offsetLeft-22)+"px";
}

function html_entity_decode(str) 
{
	var re = /(<([^>]+)>)/gi;
	str=str.replace(re, "");

	var tarea=document.getElementById('nothingArea');
	tarea.innerHTML=str;
	if (tarea.value!="")
	{
		return tarea.value;
	}
	else
	{
		return str;
	}
}


function updateArtist(val)
{
	document.getElementById('artist_input').value="";
	var searchVal=html_entity_decode(val.innerHTML);
	searchArtist(searchVal);
	document.getElementById('artist_input').value=searchVal;	
	recentOn=1;
	hideRecent();
}

function showRecent()
{
	var end=searches.length;
	
	if (end>0)
	{
		var height=Math.round((searches.length+1)*20.75);
		var i=0;
		var text="";

		while (i<10)
		{
			if (i<end)
			{
				text=text+"<li onClick='recentLoad("+i+")'>"+searches[i]+"</li>";
			}
			else
			{
				text=text+"<li class='blank'>&nbsp;</li>";
			}
			i++;
		}
		text="<ul>"+text+"<li id='rClear' onClick='clearRecent()' style='text-align:center;color:#888888;'>Clear Recent Searches</li></ul>";
		var dur=(searches.length+1)*rDurMult;
	}
	else
	{
		var height=22;
		var text="<p style='margin:0px;padding-top:4px;width:99%;text-align:center;'><BR><BR><BR>No recent searches available.</p>";
		var dur=0.0625;
	}
	var dur=10*rDurMult;
	$('recent').innerHTML=text;
	$('recent').style.left=($('search_pane').offsetLeft+60)+"px";
	$('recent').style.top=($('search_pane').offsetTop+30)+"px";
	Effect.Appear('recent',{to:0.95,duration:dur});
	setTimeout("recentOn=1;",125);
	setTimeout("recentExit()",1000);
}

function hideRecent()
{
	if (recentOn==1)
	{
		var dur=10*rDurMult;

		Effect.Fade('recent',{to:0.0,duration:dur});
		recentOn=0;
	}
}

function recentExit()
{
	if (tHolder[1])
	{
		clearTimeout(tHolder[1]);
	}
	tHolder[1]=setTimeout('hideRecent();',1500);
}

function recentEnter()
{
	if (tHolder[1])
	{
		clearTimeout(tHolder[1]);
	}
}

function toggleRecent()
{
	if (recentOn==1)
	{
		hideRecent();
	}
	else
	{
		showRecent();
	}
}

function recentLoad(val)
{
	hideRecent();
	if (searches[val])
	{
		searchFocus();
		$('artist_input').value=searches[val];
		searchArtist(searches[val]);
	}
}

function saveArtist(artist)
{
	var i=0;
	var end=searches.length;
	var found=0;
	while (i<end)
	{
		if (searches[i]==artist)
		{
			found=1;
			i=end+2;
		}
		i++;
	}
	if (found==0)
	{
		var searchesCount=searches.unshift(artist);
		if (searchesCount>10)
		{
			searches.pop();
		}
		cookieCut();
	}
}

function clearRecent()
{
	searches=new Array();
	cookieCut();
	hideRecent();
}