$(document).ready(function(){
	// Add friends link
	$('a.addfriendlink').click(function(){
		idnum = $(this).next().html();
		$(this).html("");
		$.ajax({
        type: "GET",
        url: "addfriends.ajax.php",
        data: "friend_id="+idnum	
      	});	
	});
	
	// Remove friends link
	$('a.removefriendlink').click(function(){
		idnum = $(this).next().html();
		$(this).html("");
		$.ajax({
        type: "GET",
        url: "removefriends.ajax.php",
        data: "friend_id="+idnum,
        success: function(msg) {
        	if(msg!="error") {
        		$('span#remove'+msg).parent().parent().fadeOut('slow');
        	}
        }	
      	});	
	});
		
		// makes the buy item tooltips appear
    $("a.buyitem").tooltip({
			track: true,
			showURL: false,
			delay: 0,
			showBody: "::"
		});
		
		// confirm link for buying location items
		$('a.buyitem').click(function(event) {
   		location.href = $(this).attr('href');
		}).confirm({
				dialogShow:'fadeIn',
				msg: "Buy This?<br />",
				timeout: 3000,
				buttons: {
  				cls: "confirmlinks",
  				separator: " / "
				}
			});
			
		
			
		//ajax load function to show new locations
		$('#showlocations a').click(function(){
			$("#showlocations").html("Please wait...")
			$("#buyalocation").hide()
			$("#buyalocation").load('showlocations.php', function(){
						$("#showlocations").hide()
						$("#buyalocation").slideDown("slow");
						
						// confirm link for buying locations	
        		$('a.locationbuy').click(function(event) {
           		location.href = $(this).attr('href');
        		}).confirm({
        				dialogShow:'fadeIn',
        				msg: "Set up here?<br />",
        				timeout: 3000,
        				buttons: {
          				cls: "confirmlinks",
          				separator: " / "
        				}
        			});
			})
		});

		//hides any messages after 3 secs
		setTimeout("$('.msghide').hide('slow')",3000);
		
		//sliders
		$("#example").slider({
			max: 1000,
			stepping: 50,
			change: function(e,ui){
				var currentLoan = $("#loanamtfield").val();
				var sliderValue = $("#example").slider('value',0);
				
				if(currentLoan<sliderValue){
				$('#loanamt').html("<p>Increase your loan to: <strong>$"+sliderValue+"</strong> <a href='bankloan.php?amt="+sliderValue+"' class='bankloanconfirm'>Confirm</a></p>")
				}
				if(currentLoan>sliderValue){
				var pay = (currentLoan-sliderValue)*1.5;
				$('#loanamt').html("<p>Decrease your loan to: <strong>$"+sliderValue+"</strong> and pay <strong>$"+pay+"</strong> <a href='bankloan.php?amt="+sliderValue+"' class='bankloanconfirm'>Confirm</a></p>")
				}
				if(currentLoan==sliderValue){
				$('#loanamt').html("");
				}
				
				// confirm link for changing bank loan
    		$('a.bankloanconfirm').click(function(event) {
       		location.href = $(this).attr('href');
    		}).confirm({
    				dialogShow:'fadeIn',
    				msg: "Are you sure? ",
    				timeout: 5000,
    				buttons: {
      				separator: " / "
    				}
    			});	
				}
		});
		$("#example").slider("moveTo",$("#loanamtfield").val(),0);
		
		
		// confirm link for researching item
    $('a.researchthis').click(function(event) {
    	location.href = $(this).attr('href');
    }).confirm({
    	dialogShow:'fadeIn',
    	msg: "Are you sure? ",
    	timeout: 5000,
    	buttons: {
    		separator: " / "
    	}
    });	
		
		$('a.edit').livequery('click',function(){
			$(this).parent().hide();
			$(this).parent().siblings('.input').show();
			$(this).parent().siblings('.input').children('.priceinput').select();
		});
		
		$('a.close').livequery('click',function(){
			$(this).parent().hide();
			$(this).parent().siblings('.price').show();
		});
		
		
		$('a.save').livequery('click',function(){
			var newprice = $(this).siblings('.priceinput').val();
			var idnum = $(this).parent().siblings('.id').html();
			
			var chars = "abcdefghiklmnopqrstuvwxyz";
    	var string_length = 4;
    	var rand = '';
    	for (var i=0; i<string_length; i++) {
    		var rnum = Math.floor(Math.random() * chars.length);
    		rand += chars.substring(rnum,rnum+1);
    	}
    
    			$(this).parent().parent().html("Loading...").addClass(rand);
			
			$.ajax({
        type: "GET",
        url: "inventoryajax.php",
        data: "value="+newprice+"&id="+idnum,
        success: function(msg){
          if(msg!="Error"){
						$('.'+rand).html(msg);
						var newid = $('.'+rand).children('.id').html();
      			$('.'+rand).siblings('.quantitytd').children('.id').html(newid);
						$('.'+rand).removeClass(rand);		
					}
					else{
						alert("Error: Your price must be a valid amount under $5");
						location.reload();
					}
        }
				
      });
					
		});
	
		$('a.qbuy').livequery('click',function(){
			$(this).parent().hide();
			$(this).parent().siblings('.qinput').show();
			$(this).parent().siblings('.qinput').children('.quantityinput').select();
			$(this).parent().siblings('.total').show();
		});
		
		$('a.qclose').livequery('click',function(){
			$(this).parent().hide();
			$(this).parent().siblings('.total').hide();
			$(this).parent().siblings('.quantity').show();
		});
		
		$('input.quantityinput').livequery('keyup',function(){
			var quantity = $(this).val();
			var cost = $(this).parent().siblings('.cost').html();
			var total = $(this).parent().siblings('.total');
			multiplier=1;
			if(quantity>=500) multiplier=.75;
			else if (quantity>=200) multiplier=.90;
			else if (quantity>=100) multiplier=.95;
			var sum = (quantity*cost)*multiplier;
			var sum = sum.toFixed(2);
			total.html("($"+(sum)+")");
		});
		
		$('a.qsave').livequery('click',function(){
			var quantity = $(this).siblings('.quantityinput').val();
			var idnum = $(this).parent().siblings('.id').html();
			var cost = $(this).parent().siblings('.cost').html();
			
			var chars = "abcdefghiklmnopqrstuvwxyz";
    	var string_length = 4;
    	var rand = '';
    	for (var i=0; i<string_length; i++) {
    		var rnum = Math.floor(Math.random() * chars.length);
    		rand += chars.substring(rnum,rnum+1);
    	}
    	$(this).parent().parent().html("Loading...").addClass(rand);
			
			$.ajax({
        type: "GET",
        url: "inventoryajax2.php",
        data: "quantity="+quantity+"&id="+idnum,
        success: function(msg){
          if(msg!="Error"){
						$('.'+rand).html(msg);
						var newid = $('.'+rand).children('.id').html();
						$('.'+rand).siblings('.pricetd').children('.id').html(newid);	
						$('.'+rand).removeClass(rand);			
					}
					else{
						alert("Error: Make sure you have sufficient funds and enter a valid number between 1 and 999");
						location.reload();
					}
					$.ajax({
						type: "GET",
						url: "getmoney.php",
						success: function(msg){
							$(".moneyamt").html(msg);
						}
					});
        }
				
      });			
		});
		
});

//png fix
jQuery(function($) {
     $("a").pngfix();
		 $("img").pngfix();
		 $("span").pngfix();
});