function checkMaxLength(obj, limit) {
		var text = $(obj).val();
		//count the number of characters in the text
		var chars = text.length;

		//check if there are more characters then allowed
		if(chars > limit){
			//and if there are use substr to get the text before the limit
			var new_text = text.substr(0, limit);

			//and change the current text with the new text
			$(obj).val(new_text);
		}
}

function showRegistration() {
	$("#register").modal({
		opacity:80,
		overlayCss: {backgroundColor:"#666"},
		position: [50,'25%']
	});
}

$(document).ready(function ($) {
	$('.add_friend').live('click',function(e) {
		e.preventDefault();
		var obj = this;
		var uid = global_uid;
		var fuid = $(this).children('.uid').text();
		confirm("Send this user a friend request?  <br/>You can also add a message to send with your request:<br/><textarea id='friend_req_msg' style='width:98%;height:35px;'></textarea>", function () {

			var message = $('#friend_req_msg').val();
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/send_friend_request.php",
				 data: "uid="+uid+"&fuid="+fuid+"&message="+message,
				 success: function(msg){
					 plain_message("A request was sent.  You will be notified if the user accepts your friend request.",null);
					 $(obj).remove();
				 }
			 });
		});		
	});
	
	$('.add_fan_profile').live('click',function(e) {
		e.preventDefault();
		var obj = this;
		var uid = global_uid;
		var fuid = $(this).children('.uid').text();
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/add_fan_profile.php",
			 data: "uid="+uid+"&fuid="+fuid,
			 success: function(msg){
				 plain_message("Author was successfully added to your fan list. ",null);
				 $(obj).remove();
			 }
		 });
	});
	
	$('.add_fan').live('click',function(e) {
		e.preventDefault();
		var obj = this;
		var uid = global_uid;
		var fname = $(this).children('.fname').text();
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/add_fan.php",
			 data: "uid="+uid+"&fname="+fname,
			 success: function(msg){
				 plain_message("Author was successfully added to your fan list. ",null);
				 $(obj).remove();
			 }
		 });
	});
	
	// Add to wishlist
	$('.addToWishlist').live('click',function (e) {
		e.preventDefault();
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;
		var obj = this;
		
		if(uid == '') {
			promptToRegister();
		} else {
			if($(obj).hasClass('addtowishlist')) {
				confirm("Add this book to your wishlist?<br/><input type='checkbox' id='caddtowish' name='caddtowish'/> Make a top wish<br/><input type='checkbox' id='make_public' name='make_public' checked/> Show on my recent activity?<br/><input type='checkbox' id='post_to_fb' name='post_to_fb' /> Ask Facebook friends for it?", function () {
					if($('#caddtowish').attr('checked')) {
						var checked = 1;
					} else {
						var checked = 0;
					}
					if($('#make_public').attr('checked')) {
						var make_public = 1;
					} else {
						var make_public = 0;
					}
					if($('#post_to_fb').attr('checked')) {
						var post_to_fb = 1;
					} else {
						var post_to_fb = 0;
					}
					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/add_to_wishlist.php",
						 data: "uid="+uid+"&isbn="+isbn+"&top="+checked+'&make_public='+make_public+"&post_to_fb="+post_to_fb,
						 success: function(msg){
							 plain_message("Book has been added to your wishlist!",null);
						 }
					 });
					 $(obj).removeClass('addtowishlist').addClass('removefromwishlist');
					 $(obj).find('.tag').removeClass('green').addClass('red').text('-');

				});
			} else {
				$.ajax({
					 type: "POST",
					 url: "http://www.bookspy.net/controllers/add_to_wishlist.php",
					 data: "uid="+uid+"&isbn="+isbn+"&remove=1",
					 success: function(msg){
						 plain_message("Book has been removed from your wishlist.",null);
					 }
				 });				
				$(obj).removeClass('removefromwishlist').addClass('addtowishlist');
				$(obj).find('.tag').removeClass('red').addClass('green').text('+');

			}
			
		}
	});
	
	$('.addtobookshelf').live('click',function(e) {
		e.preventDefault();
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		confirm("Add this book to your bookshelf?<br/><table><tr><td style='width:150px;'><input type='checkbox' id='caddtobook' name='caddtobook'/> Make a favorite</td><td> <b>Read it?</b> <select class='reading_dropdown_addbookshelf' title='Have you read this book?'> <option value='0'>Haven't read yet</option> <option value='1'>Reading now</option> <option value='2'>Already read</option></select></td></tr><tr><td></td><td><b>Rate it: &nbsp;</b><select class='rating_dropdown_addbookshelf' title='Rate This Book'> <option value=''>Not rated</option> <option value='0'>0 / 5</option> <option value='1'>1 / 5</option> <option value='2'>2 / 5</option> <option value='3'>3 / 5</option> <option value='4'>4 / 5</option> <option value='5'>5 / 5</option> </select>				</td></tr><tr><td><input type='checkbox' id='make_public' name='make_public' checked/> Make public?	</td><td><input type='checkbox' id='post_to_fb' name='post_to_fb' /> Post to Facebook?	</td></tr></table>", function () {
			if($('#caddtobook').attr('checked')) {
				var checked = 1;
			} else {
				var checked = 0;
			}
			if($('#make_public').attr('checked')) {
				var make_public = 1;
			} else {
				var make_public = 0;
			}
			if($('#post_to_fb').attr('checked')) {
				var post_to_fb = 1;
			} else {
				var post_to_fb = 0;
			}
			var reading = $('.reading_dropdown_addbookshelf').val();
			var rating = $('.rating_dropdown_addbookshelf').val();
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_bookshelf.php",
				 data: "uid="+uid+"&isbn="+isbn+"&top="+checked+'&rating='+rating+'&reading='+reading+'&make_public='+make_public+"&post_to_fb="+post_to_fb,
				 success: function(msg){
					 //alert(msg);
					 plain_message("Book has been added to your bookshelf!",null);
				 }
			 });
			 $(obj).removeClass('addtobookshelf').addClass('removefrombookshelf');
			 $(obj).find('.tag').removeClass('green').addClass('red').text('-');

		});		
	});
	
	$('.removefrombookshelf').live('click',function(e) {
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/add_to_bookshelf.php",
			 data: "uid="+uid+"&isbn="+isbn+"&remove=1",
			 success: function(msg){
				 plain_message("Book has been removed from your bookshelf.",null);
			 }
		 });				
		$(obj).removeClass('removefrombookshelf').addClass('addtobookshelf');	
		$(obj).find('.tag').removeClass('red').addClass('green').text('+');

	});
	
	$('.favorite').live('click',function (e) {
		if($(this).hasClass('noclick')) {
			return;
		}
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		if($(this).hasClass('set')) {
			$(this).removeClass('set');
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_wishlist.php",
				 data: "uid="+uid+"&isbn="+isbn+"&top=0",
				 success: function(msg){
				 }
			 });
		} else {
			$(this).addClass('set');
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_wishlist.php",
				 data: "uid="+uid+"&isbn="+isbn+"&top=1",
				 success: function(msg){
				 }
			 });
		}
	});
	
	$('.favorite_bookshelf').live('click',function (e) {
		if($(this).hasClass('noclick')) {
			return;
		}
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		if($(this).hasClass('set')) {
			$(this).removeClass('set');
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_bookshelf.php",
				 data: "uid="+uid+"&isbn="+isbn+"&top=0",
				 success: function(msg){
				 }
			 });
		} else {
			$(this).addClass('set');
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_bookshelf.php",
				 data: "uid="+uid+"&isbn="+isbn+"&top=1",
				 success: function(msg){
				 }
			 });
		}
	});
	
	$('.delete,.delete_suggestion').live('click',function (e) {
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		if($(this).hasClass('delete_suggestion')) {
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/suggestion_not_interested.php",
				 data: "uid="+uid+"&isbn="+isbn+"&remove=1",
				 success: function(msg){
					 var rowIndex = $(this).parent('tr').prevAll().length+1;
					 oTable.fnDeleteRow( rowIndex );
				 }
			 });			
		} else {
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_wishlist.php",
				 data: "uid="+uid+"&isbn="+isbn+"&remove=1",
				 success: function(msg){
					 plain_message("Book has been removed from your wishlist.",null);
					 var rowIndex = $(this).parent('tr').prevAll().length+1;
					 oTable.fnDeleteRow( rowIndex );
				 }
			 });	
		}
	});
	
	$('.own').live('click',function (e) {
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;

		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/now_own.php",
			 data: "uid="+uid+"&isbn="+isbn,
			 success: function(msg){
				 plain_message("Book has been removed from your wishlist, and added to your bookshelf.",null);
				 var rowIndex = $(this).parent('tr').prevAll().length;
				 oTable.fnDeleteRow( rowIndex );
			 }
		 });	
		
	});
	
	$('.received').live('click',function (e) {
		var id = $(this).children('.id').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;

		$('#received_book').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container', 
			onShow: function (dialog) {
				var modal = this;
				// if the user clicks "yes"
				$('.yes', dialog.data[0]).click(function () {
					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/received.php",
						 data: "uid="+uid+"&id="+id+"&action=received",
						 success: function(msg){
							window.location.reload();
						 }
					 });	
				});
			}
		});
	});
	
	$('.not_received').live('click',function (e) {
		var id = $(this).children('.id').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;

		$('#not_received_book').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container', 
			onShow: function (dialog) {
				var modal = this;
				// if the user clicks "yes"
				$('.yes', dialog.data[0]).click(function () {
					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/received.php",
						 data: "uid="+uid+"&id="+id+"&action=not_received",
						 success: function(msg){
							window.location.reload();
						 }
					 });	
				});
			}
		});
	});
	
	
	$('.delete_bookshelf').live('click',function (e) {
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		
		if($(obj).hasClass('price_alert')) {
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/price_watch.php",
				 data: "uid="+uid+"&isbn="+isbn+"&action=remove_price_watch",
				 success: function(msg){
					 plain_message("Book has been removed from your price alerts.",null);
					 var rowIndex = $(this).parent('tr').prevAll().length;
					 oTable.fnDeleteRow( rowIndex );
				 }
			 });	
		} else {
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/add_to_bookshelf.php",
				 data: "uid="+uid+"&isbn="+isbn+"&remove=1",
				 success: function(msg){
					 plain_message("Book has been removed from your bookshelf.",null);
					 var rowIndex = $(this).parent('tr').prevAll().length;
					 oTable.fnDeleteRow( rowIndex );
				 }
			 });	
		}
	});
	
	$('.historical_data').live('click',function (e) {
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var nTr = this.parentNode.parentNode.parentNode;
		if($(this).hasClass('closed')) {
			$(this).removeClass('closed').addClass('opened');
			fnFormatDetails(oTable, nTr, isbn, uid);
		} else {
			$(this).removeClass('opened').addClass('closed');
			oTable.fnClose( nTr );
		}
		
	});
	
	$('.doSuggestFriend').live('click',function (e) {
		var isbn = $(this).children('.isbn').text();
		suggest_book_to_friends(isbn);
	});
	
	
	//Nice drop down menus
	
	$(".reading_dropdown").linkselect({change: function(li, value, text) {
		value = value.split(':');
		var val = value[0];
		var isbn = value[1];
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;	
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/change_read_status.php",
			 data: "uid="+uid+"&isbn="+isbn+"&val="+val,
			 success: function(msg){

			 }
		 });			
		 
	}}); 
	
	$(".rating_dropdown").linkselect({change: function(li, value, text) {
		value = value.split(':');
		var val = value[0];
		var isbn = value[1];
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;	
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/change_rating_status.php",
			 data: "uid="+uid+"&isbn="+isbn+"&val="+val,
			 success: function(msg){

			 }
		 });			
		 
	}});

	//Genre typeahead
	
	$('#genre_type').flexbox('http://www.bookspy.net/controllers/genre_type.php', {
		showArrow: false
	});
	
	// Date picker
	$(function() {
		$( "#bdatepicker" ).datepicker({ dateFormat: 'yy-mm-dd', changeYear: true, changeMonth: true, yearRange: '1900:2011' });
	});
	
	$('.btn-40').live("mouseover mouseout", function(event) {
		if ( event.type == "mouseover" ) {
			$(this).parent().children('.btn-flyc').show();
		} else {
			$(this).parent().children('.btn-flyc').hide();
		}
	});
	
	$('.wall-post').live("mouseover mouseout", function(event) {
		if ( event.type == "mouseover" ) {
			$(this).find('.uiCloseButton').css('display','inline-block');
		} else {
			$(this).find('.uiCloseButton').css('display','none');
		}
	});
	
	$('.uiCloseButton').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		

		
		var obj = this;	
		

		$(this).closest('.wall-post').fadeOut('slow');
		
		var msg_id = $(this).closest('.wall-post').children('.msg_id').text();
		
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/hide_wall_post.php",
			 data: "uid="+uid+"&msg_id="+msg_id,
			 success: function(msg){

			 }
		 });	
		 return false;
		
	});
	
	$('.pricealert_big').live("click", function(event) {
		event.preventDefault();
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		
		$('#add_price_watch').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container-300', 
			onShow: function (dialog) {
				var modal = this;
				

				//Load book preview
				$.ajax({
					 type: "POST",
					 url: "http://www.bookspy.net/controllers/price_watch.php",
					 dataType: "html",
					 data: "uid="+uid+"&isbn="+isbn+"&action=get_price",
					 success: function(msg){
						$('#price_watch_msg').html(msg);
					 }
				 });

				// if the user clicks "yes"
				$('.yes', dialog.data[0]).click(function () {
					var my_price = $('#price_watch_input').val();


					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/price_watch.php",
						 dataType: "html",
						 data: "uid="+uid+"&isbn="+isbn+"&action=set_price_watch&set_price="+my_price,
						 success: function(msg){
							$('#price_watch_msg').html("<div style='text-align:center;margin-left:auto;margin-right:auto;'><img src='http://www.bookspy.net/images/ajax-loader.gif'/><br/>Loading prices...</div>");
							modal.close(); // or $.modal.close();
							$(obj).removeClass('pricealert_big').addClass('pricealert_big_cancel');
							$(obj).find('.tag').removeClass('green').addClass('red').text('-');

						 }
					 });
				});
			}
		});
	
	});
	
	$('.pricealert_big_cancel').live("click", function(event) {
		event.preventDefault();
		var isbn = $(this).children('.isbn').text();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		
		confirm("Are you sure you want to remove the price watch on this book?", function () {
			var modal = this;
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/price_watch.php",
				 dataType: "html",
				 data: "uid="+uid+"&isbn="+isbn+"&action=remove_price_watch",
				 success: function(msg){
					$(obj).removeClass('pricealert_big_cancel').addClass('pricealert_big');
					$(obj).find('.tag').removeClass('red').addClass('green').text('+');
					modal.close(); // or $.modal.close();
				 }
			 });
		});
	
	});
	
	$('.vote_up').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var isbn = $(this).children('.isbn').text();
		var pid = $(this).children('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/group_list_vote.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&isbn="+isbn+"&vote=up",
		 success: function(msg){
			if(msg.reload) {
				window.location.reload();
			} else if (msg.ok) {
				$(obj).parent().html("<span style='color:green;'>You have voted (<a href='#' onclick=\"change_vote(this,'"+pid+"','"+isbn+"');\">change</a>)</span>");
				$(obj).parent().parent().children('.votes_left').html(msg.new_votes_left);
			} 
		 }
	 });
	
	});
	
	$('.banMember').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var banee = $(this).find('.uid').text();
		var pid = $(this).find('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/user_banning.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&ban="+banee+"&action=ban",
		 success: function(msg){
			$(obj).parent().parent().hide();
		 }
	 });
	
	});
	
	$('.unbanMember').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var banee = $(this).find('.uid').text();
		var pid = $(this).find('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/user_banning.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&ban="+banee+"&action=unban",
		 success: function(msg){
			$(obj).parent().parent().hide();
		 }
	 });
	
	});
	
	$('.approveMember').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var appr_uid = $(this).find('.uid').text();
		var pid = $(this).find('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/user_approve.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&approve="+appr_uid+"&action=approve",
		 success: function(msg){
			$(obj).parent().parent().hide();
		 }
	 });
	
	});
	
	$('.denyMember').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var appr_uid = $(this).find('.uid').text();
		var pid = $(this).find('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/user_approve.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&approve="+appr_uid+"&action=deny",
		 success: function(msg){
			$(obj).parent().parent().hide();
		 }
	 });
	
	});
	
	$('.vote_down').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var isbn = $(this).children('.isbn').text();
		var pid = $(this).children('.pid').text();
		
		$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/group_list_vote.php",
		 dataType: "json",
		 data: "uid="+uid+"&pid="+pid+"&isbn="+isbn+"&vote=down",
		 success: function(msg){
		 	if(msg.reload) {
		 		window.location.reload();
		 	} else if(msg.ok) {
				$(obj).parent().html("<span style='color:green;'>You have voted (<a href='#' onclick=\"change_vote(this,'"+pid+"','"+isbn+"');\">change</a>)</span>");
				$(obj).parent().parent().children('.votes_left').html(msg.new_votes_left);
			} 
		 }
	 });
	
	});
	
	$('.add_to_group').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		var isbn = $(this).children('.isbn').text();
		
		$('#add_to_group').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container', 
			onShow: function (dialog) {
				var modal = this;

				// if the user clicks "yes"
				$('.yes', dialog.data[0]).click(function () {
					var group_id = $('#book_groups_select').val();

					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/add_to_group.php",
						 dataType: "json",
						 data: "uid="+uid+"&group_id="+group_id+"&isbn="+isbn,
						 success: function(msg){
							if(msg.ok) {
								modal.close();
								alert(msg.msg);
							} else {
								modal.close();
								alert(msg.msg);
							}
							 // or $.modal.close();
						 }
					 });
				});
			}
		});
	
	});
	
	$('.invite_friends').live("click",function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;
		
		$('#invite_friends').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container-invite'
		});
	});
	
	$('.start_book_group').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var obj = this;

		$('#start_book_group').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container', 
			onShow: function (dialog) {
				var modal = this;

				// if the user clicks "yes"
				$('.yes', dialog.data[0]).click(function () {
					var group_name = $('#new_group_name').val();

					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/new_group.php",
						 dataType: "json",
						 data: "uid="+uid+"&title="+group_name,
						 success: function(msg){
							if(msg.ok) {
								var group_id = msg.id;
								window.location = "http://www.bookspy.net/book-groups/group/settings/"+group_id+"?justmade=1";
							}
							modal.close(); // or $.modal.close();
							$('#new_group_name').val('');
						 }
					 });
				});
			}
		});
	
	});
	
	$('.numbersOnly').live("keyup", function(event) {
			this.value = this.value.replace(/[^0-9\.]/g,'');

	});
	
	$('#new_group_name').live("keyup", function(event) {
		var len = $(this).val().length;
		if (len > 100) {
				this.value = this.value.substring(0, 100);
		}
		$("#dvLimit").text(100 - len + " characters left");
	});
	
	$('#group_description').live("keyup", function(event) {
		var obj = $(this);
		var text = $(this).val();
		var len = $(this).val().length;
		if (len > 100) {
				$(this).val(text.substr(0,5000));
		}
		obj.scrollTop(
				obj[0].scrollHeight - obj.height()
		);
	});
	
	$('#nice_url').live("keyup", function(event) {
		var obj = $(this);
		var text = $(this).val();
		var len = $(this).val().length;
		if (len > 30) {
				$(this).val(text.substr(0,30));
		}
		if(len > 0) {
			var rege = /[^A-Za-z0-9\-\_]+/;
			if(rege.test(text)) {
				$('#nice_url_err').removeClass('green').addClass('red');
				$('#nice_url_err').text("No good captain, try again!");
			} else {
				$('#nice_url_err').removeClass('red').addClass('green');
				$('#nice_url_err').text("Good to go! :)");
			}
		} else {
			$('#nice_url_err').text("");
		}
	});
	
	$(".composer").submit(function(){ 
		var data = $(this).serialize();
		$.ajax({
				//this is the php file that processes the data and send mail
				url: "http://www.bookspy.net/controllers/process_fb_comments.php",
				 
				//GET method is used
				type: "GET",

				//pass the data        
				data: data,    
				 
				//Do not cache the page
				cache: false,
				
				dataType:"html",
				 
				//success
				success: function (html) {             
						//if process.php returned 1/true (send mail success)
						if (html!=0) {                 
							
							$('#wall-posts').prepend(html);
								 
						//if process.php returned 0/false (send mail failed)
						} else {
							alert('Sorry, unexpected error. Please try again later.');      
						
						}
				}      
		});
		return false;
	});
	
	$(".post_a_reply").live("submit",function() {
		var data = $(this).serialize();
		var msg_id = $(this).attr('name');
		var cur_uid = global_uid;
		data = data+"&cur_uid="+cur_uid;
		$.ajax({
				url: "http://www.bookspy.net/controllers/post_fb_reply.php",
				 
				//GET method is used
				type: "GET",

				//pass the data        
				data: data,    
				 
				//Do not cache the page
				cache: false,
				
				dataType:"html",
				 
				//success
				success: function (html) {             
						//if process.php returned 1/true (send mail success)
						if (html!=0) {                 
							
							$('.reply_list_'+msg_id).append(html);
								 
						//if process.php returned 0/false (send mail failed)
						} else alert('Sorry, unexpected error. Please try again later.');              
				}      
		});
		return false;
	});

	$('.commit_to_get').live("click", function(event) {
		event.preventDefault();
		var uid = global_uid;	
		
		if(uid == '') {
			promptToRegister();
			return;
		} 
		
		var isbn = $(this).find('.isbn').text();
		var uid_to = $(this).find('.uid_to').text();
		var obj = this;
		

		//Next lets's see if target has verified their address
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/check_target_address.php",
			 dataType: "json",
			 data: "uid="+uid+"&isbn="+isbn+"&uid_to="+uid_to,
			 success: function(msg){
				if(!msg.ok) {
					return false;
				}
				
				if(msg.ebook == 1) {
					var ebook = "<input type='checkbox' name='lend_ebook' id='lend_ebook'/> <span style='font-size:11px;'>This user has an e-book reader. Do you want to lend an e-book?</span><div style='clear:both;'></div>";
				} else {
					var ebook = "";
				}
				
				
				if(msg.address_stat == 0) {
					//Prompt not to send until address is verified
					var address = msg.address;
					var verify_msg = "<div class='add_outer'>"+ebook+"<div class='add_in_left'><b>Address</b><p>"+address+"</p></div><div class='add_in_right'><b>Notice</b><p><b>This user has not verified their address yet</b>.  A message will be sent for them to do so.  Please do not send the book until you receive confirmation.  Your karma will not be affected if the user does not verify their address.</p></div><div style='clear:both;'></div></div><div class='get_btm'>Are you sure you want to commit to sending this book?  By clicking 'OK' you agree to ship the book as soon as possible.  Failure to do so may affect your karma!<br/><br/>To review how the karma system works, <a href='http://www.bookspy.net/karma'>click here</a></div>";
					
				} else {
					//address good to go
					var address = msg.address;
					var verify_msg = "<div class='add_outer'>"+ebook+"<div class='add_in_left'><b>Address</b><p>"+address+"</p></div><div class='add_in_right'><b>Notice</b><p>Please send the book to the address shown as soon as possible.  You can see this address, and enter any tracking information (optional), at any time on your 'Karma' tab.</p></div><div style='clear:both;'></div></div><div class='get_btm'>Are you sure you want to commit to sending this book?  By clicking 'OK' you agree to ship the book (pending verification of address, if applicable) as soon as possible.<br/><br/>To review how the karma system works, <a href='http://www.bookspy.net/karma'>click here</a></div>";
					
				}
				
				$('#commit_to_get').modal({
					closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
					position: ["20%",],
					overlayId: 'confirm-overlay',
					containerId: 'confirm-container-300', 
					onShow: function (dialog) {
						var modal = this;
						$('#commit_msg').html(verify_msg);
						// if the user clicks "yes"
						$('.yes', dialog.data[0]).click(function () {
							var send_ebook = $('#lend_ebook').attr('checked'); 
							if(send_ebook) {
								send_ebook = 1;
							} else {
								send_ebook = 0;
							}
							$.ajax({
								 type: "POST",
								 url: "http://www.bookspy.net/controllers/commit_send.php",
								 dataType: "json",
								 data: "uid="+uid+"&isbn="+isbn+"&uid_to="+uid_to+"&ebook="+send_ebook,
								 success: function(msg){
									modal.close(); // or $.modal.close();
									$('#commit_msg').html("<div style='text-align:center;margin-left:auto;margin-right:auto;'><img src='http://www.bookspy.net/images/ajax-loader.gif'/><br/>Loading...</div>");
									alert('Request sent!');
								 }
							 });
						});
					}
				});
				
			 }
		 });
		 
	
	});
	
	
});

function verify_delete(pid,url) {
		confirm("Are you sure you want to delete this group?  This action cannot be undone.", function () {
			window.location = url;
		});		
}

function update_set_price(uid,isbn) {
	var newPrice = $('.cur_'+isbn).val();
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/update_set_price.php",
		 dataType: "json",
		 data: "uid="+uid+"&isbn="+isbn+"&price="+newPrice,
		 success: function(msg){
			if(msg.ok) {
				$('#cur_updated_'+isbn).show().fadeOut(1000);
			}
		 }
	 });
}

function update_tracking(id) {
	var uid = global_uid;	
	
	if(uid == '') {
		promptToRegister();
		return;
	} 
	
	var tracking = $('.tr_'+id).val();
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/update_tracking.php",
		 dataType: "json",
		 data: "uid="+uid+"&id="+id+"&tracking="+tracking,
		 success: function(msg){

				$('#cur_updated_'+id).show().fadeOut(1000);
			
		 }
	 });
}

function accept_request(obj,uid1,uid2) {
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/accept_friend_request.php",
		 dataType: "json",
		 data: "uid1="+uid1+"&uid2="+uid2,
		 success: function(msg){
			$(obj).closest('.friend_r_outer').hide();
		 }
	 });
}

function deny_request(obj,uid1,uid2) {
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/deny_friend_request.php",
		 dataType: "json",
		 data: "uid1="+uid1+"&uid2="+uid2,
		 success: function(msg){
			$(obj).closest('.friend_r_outer').hide();
		 }
	 });
}

function change_vote(obj,pid,isbn) {
	var uid = global_uid;	
	$(obj).parent().html("<div class='vote_up' alt='Vote NO' title='Vote NO'><div style='display:none;' class='isbn'>"+isbn+"</div><div style='display:none;' class='pid'>"+pid+"</div></div><div class='vote_down' alt='Vote YES' title='Vote YES'><div style='display:none;' class='isbn'>"+isbn+"</div><div style='display:none;' class='pid'>"+pid+"</div></div>");
}

function admin_approve(pid,isbn) {
	var uid = global_uid;	
	
	if(uid == '') {
		promptToRegister();
		return;
	} 
	
	var obj = this;

	$.ajax({
	 type: "POST",
	 url: "http://www.bookspy.net/controllers/group_list_vote.php",
	 dataType: "json",
	 data: "uid="+uid+"&pid="+pid+"&isbn="+isbn+"&admin_vote=1",
	 success: function(msg){
		if(msg.reload) {
			window.location.reload();
		} 
	 }
 });
}

function process_facebook_add_comment(commentID,href,parentCommentID) {
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/process_fb_comments.php",
		 data: "commentID="+commentID+"&href="+href+"&parentCommentID="+parentCommentID,
		 success: function(msg){
			//alert(msg);
		 }
	 });	
}

function process_facebook_remove_comment(commentID,href,parentCommentID) {

}

function fnFormatDetails ( oTable, nTr, isbn, uid )
{

	oTable.fnOpen( nTr, 'Loading', 'details' );

	var sOut = '<iframe src="http://www.bookspy.net/controllers/gen_price_graph.php?isbn='+isbn+'" scrolling="no" style="width:720px;height:400px;" frameborder="0"></iframe>';
	oTable.fnOpen( nTr, sOut, 'details' );

}

function add_like(obj,uid,msg_id) {
	var uidf = global_uid;
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/process_like.php",
		 data: {"uid":uid,"msg_id":msg_id,"uidf":uidf},
		 dataType: "json",
		 success: function(msg){
			if(msg.ok) {
				if(msg.addedlike) {
					//They just 'liked' it
					$(obj).text("Unlike");
					var new_num = parseInt($(obj).parent().children('.num_likes').children('.the_num').text()) + 1;
					$(obj).parent().children('.num_likes').children('.the_num').text(new_num);
				} else {
					//It was an unlike
					$(obj).text("Like");		
					var new_num = parseInt($(obj).parent().children('.num_likes').children('.the_num').text()) - 1;
					$(obj).parent().children('.num_likes').children('.the_num').text(new_num);					
				}
			}
		 }
	 });	
	 return false;
}


function getReplies(msg_id) {
	if($('.rp_'+msg_id).hasClass('shown')) {
		$('.rp_'+msg_id).hide();
		$('.rp_'+msg_id).removeClass('shown');
	} else {
		$('.rp_'+msg_id).show();
		$('.rp_'+msg_id).addClass('shown');
	}
	return false;
}

function leave_group(obj,pid) {
	var uid = global_uid;
	
	if(uid == '') {
		promptToRegister();
		return;
	} 
	
	var admin_uid = $(obj).children('aid').text();
	if(uid == admin_uid) {
		//Do nothing, they have to go to settings to delete
	} else {
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/remove_from_group.php",
			 data: "uid="+uid+"&pid="+pid,
			 success: function(msg){
				window.location="http://www.bookspy.net/book-groups/group/"+pid;
			 }
		 });	
	}
}

function promptToRegister() {
	plain_message("Please log in or register to use this feature.  Find out more by <a href='http://www.bookspy.net/?tour=1'>taking the tour!</a>.",null);
}

function confirm(message, callback) {
	$('#confirm').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;

			$('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});
}

function plain_message(message, callback) {
	$('#plain_message').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;

			$('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});
}

function suggest_book_to_friends(isbn) {
	$('#suggest_book_message').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container-300', 
		onShow: function (dialog) {
			var modal = this;
			

			
			//Load book preview
			$.ajax({
				 type: "POST",
				 url: "http://www.bookspy.net/controllers/suggest_book_preview.php",
				 data: "isbn="+isbn,
				 dataType: "html",
				 success: function(msg){
					$('#send_message_book_preview').html(msg);
				 }
			 });	
			
			//Enable type ahead
			$("#send_name_book_input").tokenInput("http://www.bookspy.net/controllers/friend_typeahead.php?my_uid="+global_uid, {
					hintText: "Start typing a name of a friend on Bookspy ",
					noResultsText: "No results",
					preventDuplicates: true,
					classes: {
							tokenList: "token-input-list-facebook",
							token: "token-input-token-facebook",
							tokenDelete: "token-input-delete-token-facebook",
							selectedToken: "token-input-selected-token-facebook",
							highlightedToken: "token-input-highlighted-token-facebook",
							dropdown: "token-input-dropdown-facebook",
							dropdownItem: "token-input-dropdown-item-facebook",
							dropdownItem2: "token-input-dropdown-item2-facebook",
							selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
							inputToken: "token-input-input-token-facebook"
					},
					onAdd: function (item) {
							recipients.push(item.id);
					},
					onDelete: function (item) {
							recipients.removeItem(item.id);
					},
					tokenLimit: 8
			});
		

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				if($('#make_public_suggest').attr('checked')) {
					var make_public = 1;
				} else {
					var make_public = 0;
				}
				if($('#share_book_fb').attr('checked')) {
					var fb_share = 1;
				} else {
					var fb_share = 0;
				}
				
				if(recipients.length > 0) {
					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/send_int_mail.php",
						 data: "suggest_book=1&uid_from="+global_uid+"&uids_to="+recipients+"&isbn="+isbn+"&make_public="+make_public+"&fb_share="+fb_share,
						 dataType: "json",
						 success: function(msg){
							recipients = new Array();
							$('#send_message_book_preview').html('');
							// close the dialog
							modal.close(); // or $.modal.close();
							//window.location.reload();
						 }
					 });	
				} else {
					recipients = new Array();
					$('#send_message_book_preview').html('');
					// close the dialog
					modal.close(); // or $.modal.close();
				}
			});
		}
	});
}

function write_message(p,data) {
	$('#send_message').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;
			
			//Enable type ahead
			if(p == 1) {
				recipients.push(data.id);
				//Prepopulating from profile page perhaps
				$("#send_name_input").tokenInput("http://www.bookspy.net/controllers/friend_typeahead.php?my_uid="+global_uid, {
						hintText: "Start typing a name of a friend on Bookspy",
						noResultsText: "No results",
						preventDuplicates: true,
						classes: {
								tokenList: "token-input-list-facebook",
								token: "token-input-token-facebook",
								tokenDelete: "token-input-delete-token-facebook",
								selectedToken: "token-input-selected-token-facebook",
								highlightedToken: "token-input-highlighted-token-facebook",
								dropdown: "token-input-dropdown-facebook",
								dropdownItem: "token-input-dropdown-item-facebook",
								dropdownItem2: "token-input-dropdown-item2-facebook",
								selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
								inputToken: "token-input-input-token-facebook"
						},
						onAdd: function (item) {
								recipients.push(item.id);
						},
						onDelete: function (item) {
								recipients.removeItem(item.id);
						},
						tokenLimit: 8,
						prePopulate: [data]
				});			
			} else {
				$("#send_name_input").tokenInput("http://www.bookspy.net/controllers/friend_typeahead.php?my_uid="+global_uid, {
						hintText: "Start typing a name of a friend on Bookspy",
						noResultsText: "No results",
						preventDuplicates: true,
						classes: {
								tokenList: "token-input-list-facebook",
								token: "token-input-token-facebook",
								tokenDelete: "token-input-delete-token-facebook",
								selectedToken: "token-input-selected-token-facebook",
								highlightedToken: "token-input-highlighted-token-facebook",
								dropdown: "token-input-dropdown-facebook",
								dropdownItem: "token-input-dropdown-item-facebook",
								dropdownItem2: "token-input-dropdown-item2-facebook",
								selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
								inputToken: "token-input-input-token-facebook"
						},
						onAdd: function (item) {
								recipients.push(item.id);
						},
						onDelete: function (item) {
								recipients.removeItem(item.id);
						},
						tokenLimit: 8

				});
			}

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				if(recipients.length > 0) {
					var message = $('#send_message_text').val();
					$.ajax({
						 type: "POST",
						 url: "http://www.bookspy.net/controllers/send_int_mail.php",
						 data: "uid_from="+global_uid+"&uids_to="+recipients+"&message="+message,
						 dataType: "json",
						 success: function(msg){
							recipients = new Array();
							$('#send_message_text').val('');
							// close the dialog
							modal.close(); // or $.modal.close();
							window.location.reload();
						 }
					 });	
				} else {
					recipients = new Array();
					$('#send_message_text').val('');
					// close the dialog
					modal.close(); // or $.modal.close();
				}
			});
		}
	});
}

function connect_fb_account(uid) {
	FB.login(function(response) {
		if (response.session) {
			window.location = "http://www.bookspy.net/?fb_connect="+uid;
		} else {
			// user is not logged in
		}
	}, {perms:'publish_stream,offline_access'});
}

//Functions to load book rows

function navPrev(obj,node,offset,uid,type) {
	//don't do anything if button disabled
	if($(obj).hasClass('qSlider-navPrevDisabled')) {
		return;
	}
	if(offset <= 0) {
		$(obj).addClass('qSlider-navPrevDisabled');
	} 
	if(offset > 0) {
		$(obj).removeClass('qSlider-navPrevDisabled').addClass('qSlider-navPrev');
	}
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/load_mrow.php",
		 data: {"node":node,"offset":offset,"type":"next","uid":uid,"select":type},
		 dataType: "html",
		 success: function(msg){
			$('#row-'+node).html(msg);
		 }
	 });		
}

function navNext(obj,node,offset,uid,type) {
	//don't do anything if button disabled
	if($(obj).hasClass('qSlider-navNextDisabled')) {
		return;
	}
	if(offset >= 8) {
		$(obj).addClass('qSlider-navNextDisabled');
	} else {
		$(obj).removeClass('qSlider-navNextDisabled').addClass('qSlider-navNext');
	}
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/load_mrow.php",
		 data: {"node":node,"offset":offset,"type":"next","uid":uid,"select":type},
		 dataType: "html",
		 success: function(msg){
			$('#row-'+node).html(msg);
		 }
	 });		
}

function not_interested(obj,node,isbn,uid,type) {
	//$(obj).closest('.agMovie').html("<br/<br/><br/><img src='http://www.bookspy.net/images/ajax-loader.gif'/>");
	if(typeof(node) == 'undefined') {
		node = '';
	}
	if(typeof(type) == 'undefined') {
		type = '';
	}
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/not_interested.php",
		 data: {"uid":uid,"isbn":isbn,"node":node,"type":type},
		 dataType: "html",
		 success: function(msg){
			$(obj).closest('.agMovie').html(msg);
		 }
	 });
}

// Mail functions

function read_more(obj,msg_id) {
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/read_more.php",
		 data: {"msg_id":msg_id},
		 dataType: "html",
		 success: function(msg){
			$(obj).parent().parent().children('.summary').text(msg);
		 }
	 });
}

function reply(obj) {
	if($(obj).parent().parent().children('.post_reply').css('display') == 'none') {
		$(obj).parent().parent().children('.post_reply').show();
	} else {
		$(obj).parent().parent().children('.post_reply').hide();
	}
}

function post_reply(obj,from,to,parent) {
	var reply = $(obj).parent().children('.reply_msg').val();
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/controllers/post_reply.php",
		 data: {"parent":parent,"to":to,"from":from,"reply":reply},
		 dataType: "html",
		 success: function(msg){
			window.location.reload();
		 }
	 });	
}

function load_more_messages(obj,parent) {
	$(obj).parent().parent().children('.p_'+parent).show();
	$(obj).parent().hide();
}

function get_mrows(uid) {
	$.ajax({
		 type: "POST",
		 url: "http://www.bookspy.net/mrows.php",
		 data: {"uid":uid},
		 dataType: "html",
		 success: function(msg){
			$('#content-left-top').html(msg);
		 }
	 });	
}

function mail_select_all(obj) {
	if($(obj).attr('checked')) {
		//uncheck all
		$('#pmform input:checkbox').each(function() {
			$(this).attr('checked',true);
		});
		$(obj).attr('checked',true);
	} else {
		$('#pmform input:checkbox').each(function() {
			$(this).attr('checked',false);
		});
		$(obj).attr('checked',false);
	}

}

function set_notify_feed(obj,uid,author) {
	if($(obj).attr('checked')) {
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/feed_suscribe.php",
			 data: {"uid":uid,"author":author,"action":"suscribe"},
			 dataType: "html",
			 success: function(msg){

			 }
		 });	
		$(obj).attr('checked',true);
	} else {
		$.ajax({
			 type: "POST",
			 url: "http://www.bookspy.net/controllers/feed_suscribe.php",
			 data: {"uid":uid,"author":author,"action":"delete"},
			 dataType: "html",
			 success: function(msg){

			 }
		 });	
		$(obj).attr('checked',false);
	}
}

function fb_bg_publish(mName,mCaption,mDesc,mLink,mImg) {
	FB.api('/me/feed', 'post', {
				 message: '',
				 name: mName,
				 caption: mCaption,
				 description: (mDesc),
				 link: mLink,
				 picture: mImg,
				 actions: [{
						name: 'Send A Gift!',
						link: mLink
					}]
			 
		 },
		 function(response) {
				if (!response || response.error) {
					//alert('Error occured');
				} else {
					//alert('Gift shared!');
				}
			}
	); 
}

function fb_publish(mName,mCaption,mDesc,mLink,mImg) {
	FB.ui(
	 {
		 method: 'feed',
		 name: mName,
		 link: mLink,
		 picture: mImg,
		 caption: mCaption,
		 description: mDesc,
		 message: ''
	 },
	 function(response) {
		 if (response && response.post_id) {
			 //alert('Post was published.');
		 } else {
			 //alert('Post was not published.');
		 }
	 }
	);
}

function learn_more() {
		$('#preview').modal({
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'confirm-overlay',
			containerId: 'confirm-container-900', 
			onShow: function (dialog) {
				//Show Banner
				$(".main_image .desc").show(); //Show Banner
				$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
			 
				//Click and Hover events for thumbnail list
				$(".image_thumb ul li:first").addClass('active'); 
				$(".image_thumb ul li").click(function(){ 
					//Set Variables
					var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
					var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
					var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
					var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
					
					if ($(this).is(".active")) {  //If it's already active, then...
						return false; // Don't click through
					} else {
						//Animate the Teaser				
						$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
							$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
							$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
						});
					}
					
					$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
					$(this).addClass('active');  //add class of 'active' on this list only
					return false;
					
				}) .hover(function(){
					$(this).addClass('hover');
					}, function() {
					$(this).removeClass('hover');
				});
						
				//Toggle Teaser
				$("a.collapse").click(function(){
					$(".main_image .block").slideToggle();
					$("a.collapse").toggleClass("show");
				});
			}
		});
}
