/**
* To use the pop up box to change the status of the user mangashelf, add showBox into your element's class attribute
* an implementation example is in mangas/index
*/
$(document).ready(function(){
	
	//build and append to the document body
	var boxElement = $.DIV({id:'shelfAddBox'}, 
		$.SPAN({}, 'Add to your Mangashelf'),
		$.DIV({}, '(Select from list)'),
		$.INPUT({id:'MangashelfSeriesId', name:'data[Mangashelf][series_id]', type:'hidden'}),
		$.INPUT({id:'MangashelfId', name:'data[Mangashelf][id]', type:'hidden'}),
		$.UL({}, 
			$.LI({}, 
				$.IMG({src:'/img/bookmark_book.png'}),
				$.A({href:'#', Class:'shelf'}, 'want to read')
			),
			$.LI({}, 
				$.IMG({src:'/img/bookmark_book.png'}),
				$.A({href:'#', Class:'shelf'}, 'reading')
			),
			$.LI({}, 
				$.IMG({src:'/img/bookmark_book.png'}),
				$.A({href:'#', Class:'shelf'}, 'completed')
			),
			$.LI({}, 
				$.IMG({src:'/img/bookmark_book.png'}),
				$.A({href:'#', Class:'shelf'}, 'dropped')
			),
			$.LI({}, 
				$.IMG({src:'/img/bookmark_book.png'}),
				$.A({href:'#', Class:'shelf'}, 'paused')
			),
			$.LI({},
				$.A({id:'cancelnclose', href:'#'},'Cancel & Close')
			)		
		)
	);
	$('body').append(boxElement);
	$("#shelfAddBox").hide();

	//show the pop up box when clicking add to shelf or any other status like pending, want to read, etc.
	function showBox() {
		$("#MangashelfSeriesId").val(this.id.substring(5) );
		$("#MangashelfId").val(this.title);
		var offset = $('#' + this.id).offset();
		$("#shelfAddBox").hide().css({'left':offset.left + 'px', 'top':offset.top + 'px'}).show("slow");
		return false;
	}
	$(".showBox").click(showBox);
	
	$(".shelf").click(function() {
		$.post("/mangashelves/edit", {
			'data[Mangashelf][series_id]': $("#MangashelfSeriesId").val(),
			'data[Mangashelf][id]': $("#MangashelfId").val(),
			'data[Mangashelf][status]': $(this).text()
		}, function(data) {
			$("#shelf" + $("#MangashelfSeriesId").val() ).text(data.Mangashelf.status);
			$("#shelf" + $("#MangashelfSeriesId").val() ).attr('class', 'OtherStatus showBox');
			$("#shelf" + $("#MangashelfSeriesId").val() ).attr('title', data.Mangashelf.id);
			$("#shelfAddBox").hide();
		},
		'json');
		return false;
	});
	$("#cancelnclose").click(function() {
		$("#shelfAddBox").hide();
		return false;
	});
	//star ratings
	$('.star').rating({ 
		callback: function(value, link){
			$.post("/ratings/add", {
				'data[Rating][series_id]': this.id.substring(6), 
				'data[Rating][value]': $(this).val()
			});
		} 
	});
});	
function fb_connect_login() {
    $.post('/users/fb_connect_login', {}, function () {
            $('#ajaxLogin').load("/users/homeLogin");
    });
}
