var findBookCoverImage = function (isbn) {
//document.title=isbn;
	if(findBookCoverImage.check==undefined) findBookCoverImage.check=new Array;
	if(findBookCoverImage.check[isbn]==undefined) findBookCoverImage.check[isbn]=0;
	else findBookCoverImage.check[isbn]++;
	var c=findBookCoverImage.check[isbn];
	if(c==0)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'_1.jpg';
	else if(c==1)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'_2.jpg';
	else if(c==2)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'x_1.jpg';
	else if(c==3)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'x_2.jpg';
	else if(c==4)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'_1.gif';
	else if(c==5)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'_2.gif';
	else if(c==6)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'x_1.gif';
	else if(c==7)
		var newurl='http://image.aladin.co.kr/cover/cover/'+isbn+'x_2.gif';
	else if(c==8) {
		var newurl='http://image.kyobobook.co.kr/images/book/large/'+isbn.substring(isbn.length-3,isbn.length)+'/l'+isbn+'.jpg';
	}
	else if(c==9)
		var newurl='http://bookimg.nate.com/cover0/02D/s1/'+isbn+'.jpg';
	else if(c==10)
		var newurl = 'http://images.amazon.com/images/P/'+isbn+'.01.MZZZZZZZ.gif';
	else if(c==11)
		var newurl = 'http://bookweb.kinokuniya.co.jp/imgdata/'+isbn+'.jpg';
	else return;
	$('#image_'+isbn).attr('src',newurl);


}

var putBookCoverImage = function(isbn,url) {
	if(findBookCoverImage.check==undefined) return;
	else if(findBookCoverImage.check[isbn]==undefined) return;
	$.post('/_do/putBookCoverImage',{isbn:isbn,url:url},function(data) {
	});
}

var libraryInit=function(){
	$('.bookItem').each(function(){
		if($(this).attr('inited')!=undefined) return;
		$(this).attr('inited','ok');

		$(this).mouseover(function(){
			var ing=$(this).attr('className').indexOf('Ing')>0?'Ing':'Done';
			$(this).attr('className','bookItem bookItem'+ing+' bookItemOn');
		});


		$(this).mouseout(function(){
			var ing=$(this).attr('className').indexOf('Ing')>0?'Ing':'Done';
			$(this).attr('className','bookItem bookItem'+ing);
		});


		$(this).find('.bookItemButtonDone').click(function(){
			var isbn=$(this).parent().parent().attr('id').replace('bookItem','');

			$.post('/_do/modifyBookIngStatus',{isbn:isbn,value:'done'},function(data) {
				if(data.result=='ok') {
					Alert('다 읽으셨군요!','축하해요. 다른 친구들에게도 이 책을 권해주세요^^');	
					$('#bookItem'+data.isbn).attr('className','bookItem bookItemDone bookItemOn');
				}
			},'json');
			return false;
		});

		$(this).find('.bookItemButtonIng').click(function(){
			var isbn=$(this).parent().parent().attr('id').replace('bookItem','');

			$.post('/_do/modifyBookIngStatus',{isbn:isbn,value:'ing'},function(data) {
				if(data.result=='ok') {
					Alert('읽고싶은 책이군요?','읽어가면서 틈틈히 어떤 책인지 이야기해주세요^^');
					$('#bookItem'+data.isbn).attr('className','bookItem bookItemIng bookItemOn');
				}
			},'json');
			return false;
		});

		$(this).find('.bookItemDelete a').click(function(){
			deleteBookItem( $(this).attr('id').replace('bookItemDelete','') );
			return false;
		});

		$(this).find('a.bookItemLink, div.bookItemInfo').click(function(){
			if($(this).parent().attr('id').indexOf('Search')>0) return false;
			var bookInfo={
			subject:$(this).parent().find('.bookItemTitleSubject').html(),
			author:$(this).parent().find('.bookItemTitleAuthor').html(),
			isbn:$(this).parent().attr('id').replace('bookItem',''),
			thumb:$(this).find('img').attr('src')
			};
			$.post('/_do/putBookSession',bookInfo,function(data,status){
				window.location.href='http://'+ENV.DOMAIN+'/'+(ENV.dFRIENDID!=''?ENV.dFRIENDID+'/':'')+'talk/book/'+bookInfo.isbn;
			},'json');
			return false;
		});
	});

}

var deleteBookItem=function(isbn){
	if(!confirm(LANG.confirm_delete) || !confirm(LANG.confirm_delete_again)) return;
        $.post('/_do/deleteBookItem',{isbn:isbn}, function(data) {
                if(data.result!=true) return;
                $('#bookItem'+data.isbn+' div').fadeTo('normal',0);
                $('#bookItem'+data.isbn).fadeTo('normal',0,function(){
                        $(this).hide();
                });
                $('#bookItem'+data.isbn+' div').attr('id','');
                $('#bookItem'+data.isbn).attr('id','');
        },'json');
}


var postBookItem=function(isbn,isbn2,booktitle,bookauthor,bookpublisher,obj) {
        var bookimg=$('#image_'+isbn).attr('src');
        $.post('/_do/putBookItem',{isbn:isbn,isbn2:isbn2,booktitle:booktitle,bookauthor:bookauthor,bookpublisher:bookpublisher,bookimg:bookimg,Menu:ENV.MENU},function(data) {
                postBookItem.isbn=isbn;
                $('#boxItemsSearch').hide('blind',500,function(){
                        $('ul.bookItems:first').prepend(data.result_html);
                        $('#bookItem'+postBookItem.isbn).fadeTo(0,0);
                        $('#bookItem'+postBookItem.isbn).fadeTo('slow',1);
                        $('#boxItemsSearch').html('');
                        $('#boxSearchBarText').val('');
			libraryInit();
                });
		if(ENV.MENU=='interview' || ENV.MENU=='2ch' || ENV.MENU=='talk') {
			$('#boxPostBarBookBox').fadeOut(200,function(){
				$(this).remove();
				});

			attachBookView(isbn,booktitle,bookauthor,bookimg,bookpublisher);

		}
	
        },'json');
}


var initBookSearchBar=function(){
        $('#boxSearchBarText').focus(initBookSearchBar.focus);
}
initBookSearchBar.focus=function(){
	if(ENV.MYNO==0) {
		Alert('로그인해줘!','책을 담으려면 책장이 있어야해. 그러니 로그인을 해줘');
		$.openSignin();
		return false;
	}

        if($(this).attr('className')!='') return;
        $(this).attr('className','boxSearchBarClear');
        $(this).val('');
}

initBookSearchBar.submitDo=function(){

        if($('#boxSearchBarText').val()=='') return;
//        $('#boxSearchBarText').attr('disabled','true');
        $('#boxSearchBarSubmit').blur();
        if(initBookSearchBar.submitCheck==undefined) {
                initBookSearchBar.submitCheck=true;
                $('#boxSearchBarSubmit').before('<button id="boxSearchBarSaving"><img src="'+ENV.STATICPATH+'/images/loading2.gif"></button>');
        }
        $('#boxSearchBarText').attr('disabled','true');
        $('#boxItemsSearch').hide();
        $.post('/_do/getBookSearchResult',{text:$('#boxSearchBarText').val(),shareSocial:($('#shareSocial').attr('checked')?'yes':false)},function(data,status) {
                $('#boxSearchBarText').attr('disabled','');
                $('#boxSearchBarSaving').remove();
                if(data.result_html==null) {
                        $('#boxSearchBarText').val('');

                        Alert(data.lang_notfound,data.lang_notfound_detail);
                        $('#boxSearchBarText').focus();
                        return false;
                }
                $('#boxItemsSearch').html(data.result_html);
                $('#boxItemsSearch .bookItem').mouseover(function(){
                        $(this).attr('className','bookItem bookItemOn');
                });
                $('#boxItemsSearch .bookItem').mouseout(function(){
                        $(this).attr('className','bookItem');
                });
                $('#boxSearchBarText').attr('disabled','');
                $('#boxItemsSearch').show('blind',500);


        },'json');
        return false;
}
$.getme2day=function(){
	Alert('me2day에서 책을 가져올게!','잠시만 기다려줘...');
	$('a#getMe2day span').each(function(){$(this).attr('inited',$(this).html()).html('<img src="'+ENV.STATICPATH+'/images/loading1.gif">');});

	$.post('/me2day/getbook',{},function(data,status){
		$('a#getMe2day span').each(function(){$(this).html($(this).attr('inited'));} );

		if(data.result=='none') Alert('me2day에도 책이 없어','검색창에서 새 책을 담아줘');

		if(data.result=='ok') {
			$('ul.bookItems:last').prepend(data.result_html).show('blind','',700);
			$('a#getMe2day').remove();
			$('a.bookNone').remove();
			Alert(data.count+'권의 책을 가져왔어', '이미 읽은 것 맞지? 이제 책을 더 담아줘');
		}
	},'json');

}
$.nextBook=function(){
	$('a#nextButton span').each(function(){$(this).attr('inited',$(this).html()).html('<img src="'+ENV.STATICPATH+'/images/loading1.gif">');});
	if($.nextBook.page==undefined) $.nextBook.page=1; 
	$.nextBook.page++;
	$.post('/_do/getBookItems',{'page':$.nextBook.page},function(data,status){
		$('a#nextButton span').each(function(){$(this).html($(this).attr('inited'));});

                if(data.result=='ok') {
			$('<ul class="bookItems">'+data.result_html+'</ul>').insertAfter('ul.bookItems:last');
			$('ul.bookItems:last li').show('highlight','',1000);
			window.scrollTo(0,9999999);
			if(data.nextexist!='ok') $('a#nextButton').hide('blind','',function(){$(this).remove()});
			libraryInit();
                }
        },'json');

}
$(document).ready(function () {
	initBookSearchBar();
	libraryInit(); 
	$('a#nextButton').click(function(){
		$.nextBook();
		return false;
	});
	$('a#getMe2day').click(function(){
		$.getme2day();
		return false;
	});
	if(window.location.href.indexOf('#getme2daybook')>0) {
		window.location.href=window.location.href.substring(0,window.location.href.indexOf('#'));
		$.getme2day();
	}

	$('img.bookcoverimage').each(function(){
		var src=$(this).attr('src');
		if(src.indexOf('amazon')>0) {
			var isbn=$(this).attr('id').replace('image_','');
			findBookCoverImage(isbn);
		}
	});
});



