var loc = document.location.href;
var currentdiv=1;
if (loc.search(/#floor1-anchor/)>0)
{
	var l = location.href.replace(/#floor1-anchor/,'');
	document.location.href=l;
}	

// Work out anchor positions to make scrolling scroll to the bottom of the floor, not the top
function calcAnchors()
{	
	var tmpoffset=parseInt($(window).height()-$("#floor-shop").height());
	$("#floor1-anchor").css("margin-top",0-tmpoffset);
	
	/*var tmpoffset=parseInt($(window).height()-$("#floor-2").height());
	$("#floor2-anchor").css("margin-top",0-tmpoffset);

	var tmpoffset=parseInt($(window).height()-$("#floor-3").height());
	$("#floor3-anchor").css("margin-top",0-tmpoffset);
	
	var tmpoffset=parseInt($(window).height()-$("#floor-4").height());
	$("#floor4-anchor").css("margin-top",0-tmpoffset);

	var tmpoffset=parseInt($(window).height()-$("#floor-5").height());
	$("#floor5-anchor").css("margin-top",0-tmpoffset);
	
	var tmpoffset=parseInt($(window).height()-$("#floor-6").height());
	$("#floor6-anchor").css("margin-top",0-tmpoffset);

	var tmpoffset=parseInt($(window).height()-$("#floor-7").height());
	$("#floor7-anchor").css("margin-top",0-tmpoffset);
	
	var tmpoffset=parseInt($(window).height()-$("#basement").height());
	$("#basement-anchor").css("margin-top",0-tmpoffset);	*/
	
	return true;
}

// If the window is resized, recalculate anchor positions
$(window).resize(function()
{
	calcAnchors();		
});

function checkinput()
{
	var userinput = document.getElementById('isearchterm').value;
	userinput=userinput.replace(/<script/i,'');
	userinput=userinput.replace(/\/script>/i,'');
	userinput=userinput.replace(/>/g,'');
	userinput=userinput.replace(/</g,'');
	document.getElementById('isearchterm').value=userinput;
}

function sendtoFriend()
{
		$.ajax({
			type: "POST",
			url: "sendtofriend.php",
			data: 	"fromemail=" + document.getElementById("ifromemail").value +
					"&toemail=" + document.getElementById("itoemail").value +
					"&txtmessage=" + document.getElementById("itxtmessage").value +
					"&frmaction=sendtofriend",
			success: function(html){
				$("#sendForm").html(html);
				document.location.href='index.php#sendtofriendscreen';
			}
		});
		return false;
}

function lastfmSearch()
{
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"searchterm=" + document.getElementById("isearchterm").value +
					"&resultspage=" + document.getElementById("iresultspage").value +
					"&frmaction=search",
			success: function(html){				
				$("#albumResults").html(html);
				document.location.href='index.php#floor6-anchor';
			}
		});
		return false;

}

function submitVote()
{
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"albumids=" + document.getElementById("ialbumids").value +
					"&albumartists=" + document.getElementById("ialbumartists").value +
					"&albumnames=" + document.getElementById("ialbumnames").value +
					"&albumimages=" + document.getElementById("ialbumimages").value +
					"&frmaction=submitvote",
			success: function(html){			
				if (html.search(/Thanks for submitting/)>0)
				{
					$("#frmSearch").hide();
				}			
				$("#albumResults").html(html);				
			}
		});
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"frmaction=getuseralbums",
			success: function(html){				
				$("#userAlbums").html(html);
			}
		});	
		document.location.href='index.php#floor6-anchor';
		return false;

}


function gotoPage(searchTerm,pageId)
{
		$.ajax({
			type: "GET",
			url: "lastfmsearch.php",
			data: 	"searchterm=" + searchTerm +
					"&resultspage=" + pageId +
					"&frmaction=search",
			success: function(html){				
				$("#albumResults").html(html);
				document.location.href='index.php#floor6-anchor';
			}
		});
		return false;

}

function lastfmAdd()
{	
		var rad_val='';
		var artist_val='';
		var name_val='';
		var image_val='';
		for (var i=0; i < document.frmAdd.albumid.length; i++)
		{
			if (document.frmAdd.albumid[i].checked)
			{
				var rad_val = document.frmAdd.albumid[i].value;
			}
		}
		if (rad_val!='')
		{
			artist_val=document.getElementById("iartist"+rad_val).value;
			name_val=document.getElementById("iname"+rad_val).value;
			image_val=document.getElementById("iimage"+rad_val).value;
		}
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"albumid=" + rad_val +
					"&artist"+rad_val+"=" + artist_val +
					"&name"+rad_val+"=" + name_val +
					"&image"+rad_val+"=" + image_val +
					"&frmaction=addalbum",
			success: function(html){			
				if (html.search(/You have selected 10 albums for your top 10/)>0)
				{
					$("#frmSearch").hide();
				}			
				$("#albumResults").html(html);				
				
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"frmaction=getuseralbums",
			success: function(html){				
				$("#userAlbums").html(html);
			}
		});					
				
			}
		});	

		//document.location.href='index.php#floor6-anchor';
		return false;
}

function lastfmRemove(id)
{
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"albumid=" + id +
					"&frmaction=removealbum",
			success: function(html){	
				$("#frmSearch").show();
				$("#albumResults").html(html);
				//document.location.href='index.php#floor6-anchor';
			}
		});	
		$.ajax({
			type: "POST",
			url: "lastfmsearch.php",
			data: 	"frmaction=getuseralbums",
			success: function(html){				
				$("#userAlbums").html(html);
			}
		});	
		return false;	
}

function gotoStamp(stampid)
{
		$('a#linkstamp').click();
		scrollfunc = "$('.horizontal-scroller').scrollTo('#stamp"+stampid+"',1000,{axis:'x'})";
		setTimeout(scrollfunc,3000);
		currentdiv=stampid;
		if (currentdiv==10)
		{
			$('#btnnext').html('<img src="images/stamps-next-off.gif" width="60" height="122" alt="Next" />');
		}
		if (currentdiv>1)
		{
			$('#btnprev').html('<a href="#stamp1" class="prev" onclick="previousSlide();return false" onfocus="blur()"><img src="images/stamps-previous.gif" width="77" height="122" alt="Previous" /></a>');
		}		
		if (currentdiv==1)
		{
			$('#btnprev').html('<img src="images/stamps-previous-off.gif" width="77" height="122" alt="Previous" />');
		}
		if (currentdiv<10)
		{
			$('#btnnext').html('<a href="#stamp2 class="next" onclick="nextSlide();return false" onfocus="blur()"><img src="images/stamps-next.gif" width="60" height="122" alt="Next" /></a>');
		}		
}

function sendtofriendClose()
{
	
		$("#sendtofriend").hide();
		return false;
}

function showPacks(whichstamp)
{
	
		$("#view-packpopup").show();
		document.location.href='index.php#ppack-large';
		return false;
}

function hidePacks()
{
	
		$("#view-packpopup").hide();
		return false;
}

	function nextSlide()
	{
		if (currentdiv<10)
		{
			currentdiv++;			
		}
		if (currentdiv==10)
		{
			$('#btnnext').html('<img src="images/stamps-next-off.gif" width="60" height="122" alt="Next" />');
		}
		if (currentdiv>1)
		{
			var prevdiv=currentdiv-1;
			$('#btnprev').html('<a href="index.php?frmaction=changestamp&whichstamp='+prevdiv+'" class="prev" onclick="previousSlide();return false" onfocus="blur()"><img src="images/stamps-previous.gif" width="77" height="122" alt="Previous" /></a>');
		}
		if(currentdiv<10)
		{
			var nextdiv=currentdiv+1;
			$('#btnnext').html('<a href="index.php?frmaction=changestamp&whichstamp='+nextdiv+'" class="next" onclick="nextSlide();return false" onfocus="blur()"><img src="images/stamps-next.gif" width="60" height="122" alt="Next" /></a>');				
		}
		$(".horizontal-scroller").scrollTo('#stamp'+currentdiv,1000,{axis:'x'});  
		return false;		
	}

	function previousSlide()
	{
		  if (currentdiv>1)
		  {
			currentdiv--;	   
		  }
		  if (currentdiv==1)
		  {
			$('#btnprev').html('<img src="images/stamps-previous-off.gif" width="77" height="122" alt="Previous" />');
		  }
		  if (currentdiv<10)
		  {
			  var nextdiv=currentdiv+1;
			  $('#btnnext').html('<a href="index.php?frmaction=changestamp&whichstamp='+nextdiv+'" class="next" onclick="nextSlide();return false" onfocus="blur()"><img src="images/stamps-next.gif" width="60" height="122" alt="Next" /></a>');
		  }
		  if (currentdiv>1)
		  {
			var prevdiv=currentdiv-1;
			$('#btnprev').html('<a href="index.php?frmaction=changestamp&whichstamp='+prevdiv+'" class="prev" onclick="previousSlide();return false" onfocus="blur()"><img src="images/stamps-previous.gif" width="77" height="122" alt="Previous" /></a>');			  
		  }
 
		  $(".horizontal-scroller").scrollTo('#stamp'+currentdiv,1000,{axis:'x'});  
		  return false;		
	}

$(document).ready(function()
{
	calcAnchors();

	$('.scroll-pane').jScrollPane({showArrows:true});
	
	$('#sendtofriendlink').click(function()
	{
		$("#sendtofriend").show();
		return false;
	});
	
	//Escape key hides send to friend
	$(document).keyup(function(event) 
	{ 
		if (event.keyCode == 27) 
		{	 
			$('#sendtofriend').hide(); 
			$('#view-packpopup').hide(); 
		}		
	});
	
	var defaultsendtofriend=$("#sendtofriend").html();
	
	$(".rollover").hover(
  		function ()
		{
    		$(this).find("img").attr('src','/images/'+$(this).attr('id')+'-on.jpg');
		}, 
  		function ()
		{
			$(this).find("img").attr('src','/images/'+$(this).attr('id')+'.jpg');
		}
	);

});

