// JavaScript Document

$(function () { // this line makes sure this code runs on page load
	$("#profile_comment_submit").click(function (e) {
		e.preventDefault();
		var text = $('textarea#profile_comment_txtarea').val();
		var user = $('#profile_comment_user').val();
		$('#addcomment_status').show();
		$.ajax({
			type: "POST",
			url: "/ajax/ajax-misc.php",
			data: "action=addprofilecomment&user="+user+"&text="+text,
			success: function(msg){
				$('#addcomment_status').hide();
				$('#profile_comments_list').prepend(msg);
				$('#profile_comments_add').fadeOut();
				
			}
		});
		return false;
	});
	$("#profile_comment_addnew").click(function (e) {
		e.preventDefault();
		return false;
	});
	$('.checkall').click(function () {
		$(this).parents('table:eq(0)').find(':checkbox').attr('checked', this.checked);
	});
	$("#closeWindow").live('click', function(){
		$.fancybox.close(true)
	});
	$('.boxhoveritem').hover( 
		function () {
			$(this).addClass('boxshadowhover');
		}, 
		function () {
			$(this).removeClass('boxshadowhover');
		}
	);
	$('.add_link').die();
	$(".del_link").live('click', function(){
		var linker = $(this);
		linker.parent().remove();
		$.fancybox.update()
		return false;
	});
	
	
	
	
	
	// Deleting individual tradelist item
	$('.trpage_delete').live('click', function(e) {
		e.preventDefault();
		item_id = $(this).parent().parent().parent().attr("id");
		if (confirm("Are you sure you want to delete this item?") && item_id > 0){
			$.post("/ajax/ajax-cats.php", { delTradelistItem: item_id },
				function(data){
					if (data == item_id) { 
						$('#tr_'+item_id).fadeOut();
					}
				}
			);
		}
		return false;
	});
	
	// Moving individual tradelist item
	$('.trpage_move').live('click', function(e) { 
		e.preventDefault();
		var item_id = $(this).parent().parent().parent().attr("id");
		$.colorbox({transition:"none",innerWidth:"602px",href:"/ajax/ajax-moveitempopup.php?id="+item_id,opacity:".7",onLoad: function() {
			$('#cboxClose').remove();
		}});
	});
	
	// Launching tradelist photo upload window
	$('.trpage_photohandle').live('click', function(e) {
		e.preventDefault();
		var item_id = $(this).parent().parent().parent().attr("id");
		$.colorbox({transition:"none",innerWidth:"602px",href:"/ajax/ajax-uploadphotowindow.php?id="+item_id,opacity:".7",onLoad: function() {
			$('#cboxClose').remove();
		}});
	});
	
	// Showing the gear on hover
	$("ul.tradelist_items li").live({
		mouseenter: function() {
			$(this).css("background","#F1F1F1");
			$(this).find("span.gearholder").show();
		},
		mouseleave: function() {
			$(this).css("background","none");
			$(this).find("span.gearholder").hide();
		}
	});
	
	// Load tradelist photos
    $('a.tr_photolink').live('click', function(e) {
		e.preventDefault();
		var itemID = $(this).parent().parent().attr("id");
		if ($('#'+itemID+'_photos').hasClass('shown')) {
			$('#'+itemID+'_photos').toggle();
		}
		else { 
			$.post("/ajax/ajax-loadpictures.php", { record_id:itemID },
				function(data){
				if (data) {
					$(".fancybox").fancybox();
					$('#photoCell_' + itemID).html(data);
					$('#'+itemID+'_photos').show();
					$('#'+itemID+'_photos').addClass('shown');
				}
			});
		}
		return false;
    });
	
});
function escapeHtml(unsafe) {
  return unsafe
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}
