function notify(message){var bar=$('#notification'),content=$('#notification-message');if(bar.is(':visible')){bar.hide();content.html(message);bar.slideDown('slow');}else{content.html(message);bar.slideDown('slow');}
setTimeout(function(){bar.slideUp(200);content.empty();},5000);}
$(function(){$('#switch li a').click(function(e){e.preventDefault();$('.current').removeClass('current');$(this).addClass('current');var type=$(this).attr('id');$.post('/load',{homepage:'true',type:type},function(data){if(data!=''){$('#latest').html(data);}});});$('#get-started').click(function(e){e.preventDefault();$('#email').focus();});$('a[rel*=popup]').facebox();$(document).pngFix();$('#query').defaultvalue('Search DVDs...');$('.borrower').defaultvalue('Borrower\'s Name');$('#mobile').mask('(999) 999-9999');$('.what_is').tipsy({gravity:'w',fade:true});$('#back-to-top').click(function(e){e.preventDefault();$('html,body').animate({scrollTop:0},'slow');$(this).slideUp(200);window.location.hash='';});$('#new_password').keyup(function(){if($(this).val()){$('#change_password').show();}else{$('#change_password').hide();}});$('#close_account').click(function(e){e.preventDefault();if(confirm('Are you sure you want to close your account?\n\nYour shelf will be emptied and your account will be closed. It cannot be recovered.\n\n')){$('#close_account_form').submit();}});$('#empty_shelf').click(function(e){e.preventDefault();if(confirm('Are you sure you want to empty your shelf?\n\nAll DVDs on your shelf as well as any loans will be removed. They cannot be recovered.\n\n')){$.post('/session',{method:'empty_shelf'},function(data){notify('Your shelf has been emptied!');$('#shelf-count').text('0');});}});$('#empty_wish_list').click(function(e){e.preventDefault();if(confirm('Are you sure you want to empty your wish list?\n\nAll DVDs on your wish list will be removed. They cannot be recovered.\n\n')){$.post('/session',{method:'empty_wish_list'},function(){notify('Your wish list has been emptied!');$('#wish-count').text('0');});}});$('#notification').click(function(e){e.preventDefault();$(this).slideUp(200);$('#notification-message').empty();});if($('#notification').is(':visible')){setTimeout(function(){$('#notification').slideUp(200);},5000);}
$('.cancel').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),borrower=$('#'+parent+' .enter-borrower'),tags=$('#'+parent+' .enter-tags'),actions=$('#'+parent+' .disc-actions');borrower.add(tags).hide();actions.show();});$('.add-wish').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),wish_count=$('#wish-count'),wish_id=$('#'+parent+' .metadata-wish'),actions=$('#'+parent+' .disc-actions'),url=$('#'+parent+' .metadata-url').text(),title=$('#'+parent+' .metadata-title').text(),image=$('#'+parent+' .metadata-image').text();$(actions).empty();$.post('/disc',{method:'add_wish',url:url,title:title,image:image},function(data){notify(title+' was added to your wish list!');wish_count.text(parseInt(wish_count.text())+1);wish_id.text(data);actions.html('<button class="button add-disc">Add Disc</button> <a href="#" class="remove-wish tip" title="Remove '+title+' from your Wish List.">- Wish List</a>');});});$('.remove-wish').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),wish_count=$('#wish-count'),wish_id=$('#'+parent+' .metadata-wish'),actions=$('#'+parent+' .disc-actions'),title=$('#'+parent+' .metadata-title').text(),wish=$('#'+parent+' .metadata-wish').text();$(actions).empty();$.post('/disc',{method:'remove_wish',wish:wish},function(data){notify(title+' was removed from your wish list.');wish_count.text(parseInt(wish_count.text())-1);wish_id.empty();actions.html('<button class="button add-disc">Add Disc</button> <a href="#" class="add-wish tip" title="Add '+title+' to your Wish List.">+ Wish List</a>');});});$('.add-disc').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),shelf_count=$('#shelf-count'),actions=$('#'+parent+' .disc-actions'),dvd=$('#'+parent+' .metadata-dvd'),wish_count=$('#wish-count'),wish_id=$('#'+parent+' .metadata-wish'),url=$('#'+parent+' .metadata-url').text(),title=$('#'+parent+' .metadata-title').text(),image=$('#'+parent+' .metadata-image').text(),wish=$('#'+parent+' .metadata-wish').text();$(actions).empty();$.post('/disc',{method:'add_disc',url:url,title:title,image:image,wish:wish},function(data){notify(title+' was added to your shelf!');shelf_count.text(parseInt(shelf_count.text())+1);dvd.text(data);if(wish){wish_count.text(parseInt(wish_count.text())-1);wish_id.empty();}
actions.html('<button class="button remove-disc">Remove Disc</button> <button class="button loan-disc">Loan Disc</button> <a href="#" class="tag-disc">+/- Tags</a>');});});$('.remove-disc').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),shelf_count=$('#shelf-count'),dvd_id=$('#'+parent+' .metadata-dvd'),loan_id=$('#'+parent+' .metadata-loan'),actions=$('#'+parent+' .disc-actions'),tags=$('#'+parent+' .disc-tags'),temp_loan=$('#'+parent+' .temp-loan'),loan_status=$('#'+parent+' .loan-status'),url=$('#'+parent+' .metadata-url').text(),title=$('#'+parent+' .metadata-title').text(),dvd=$('#'+parent+' .metadata-dvd').text(),loan=$('#'+parent+' .metadata-loan').text();$(actions).empty();$.post('/disc',{method:'remove_disc',dvd:dvd,loan:loan},function(data){notify(title+' was removed to your shelf!');dvd_id.add(loan_id).empty();shelf_count.text(parseInt(shelf_count.text())-1);actions.html('<button class="button add-disc">Add Disc</button> <a href="#" class="add-wish tip" title="Add '+title+' to your Wish List.">+ Wish List</a>');tags.add(temp_loan).add(loan_status).remove();});});$('.tag-disc').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),borrower=$('#'+parent+' .enter-borrower'),actions=$('#'+parent+' .disc-actions'),tags=$('#'+parent+' .enter-tags');borrower.add(actions).hide();tags.show();});$('.save-tags').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),sidebar_tags=$('#tags_list'),disc_tags=$('#'+parent+' .disc-tags'),actions=$('#'+parent+' .disc-actions'),tag_input=$('#'+parent+' .enter-tags'),url=$('#'+parent+' .metadata-url').text(),title=$('#'+parent+' .metadata-title').text(),dvd=$('#'+parent+' .metadata-dvd').text(),tags=$('#'+parent+' .tags').val();$.post('/disc',{method:'save_tags',url:url,title:title,dvd:dvd,tags:tags},function(data){notify('Tags saved!');var tag=$.parseJSON(data);sidebar_tags.html(tag.sidebar);if(tag.disc!=null){if(disc_tags.length){disc_tags.html('Tagged: '+tag.disc);}else{actions.before('<p class="disc-tags">Tagged: '+tag.disc);}}else{disc_tags.remove();}
tag_input.hide();actions.show();});});$('.loan-disc').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),actions=$('#'+parent+' .disc-actions'),tags=$('#'+parent+' .enter-tags'),borrower=$('#'+parent+' .enter-borrower');actions.add(tags).hide();borrower.show();});$('.save-loan').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),loan_count=$('#loan-count'),actions=$('#'+parent+' .disc-actions'),loan_id=$('#'+parent+' .metadata-loan'),loan_input=$('#'+parent+' .enter-borrower'),url=$('#'+parent+' .metadata-url').text(),title=$('#'+parent+' .metadata-title').text(),dvd=$('#'+parent+' .metadata-dvd').text(),image=$('#'+parent+' .metadata-image').text(),borrower=$('#'+parent+' .borrower').val();$.post('/disc',{method:'save_loan',url:url,title:title,dvd:dvd,image:image,borrower:borrower},function(data){notify(title+' was loaned to '+borrower+'!');loan_count.text(parseInt(loan_count.text())+1);actions.before('<p class="temp-loan">'+borrower+' borrowed this DVD just now.</p>');loan_id.text(data);loan_input.hide();actions.show().html('<button class="button remove-disc">Remove Disc</button> <button class="button return-loan">Return Loan</button> <a href="#" class="tag-disc">+/- Tags</a>');});});$('.return-loan').live('click',function(e){e.preventDefault();var parent=$(this).parent().parent().attr('id'),loan_status=$('#'+parent+' .loan-status'),temp_loan=$('#'+parent+' .temp-loan'),loan_id=$('#'+parent+' .metadata-loan'),loan_count=$('#loan-count'),actions=$('#'+parent+' .disc-actions'),title=$('#'+parent+' .metadata-title').text(),dvd=$('#'+parent+' .metadata-dvd').text(),loan=$('#'+parent+' .metadata-loan').text();$(actions).empty();$.post('/disc',{method:'return_loan',dvd:dvd,loan:loan},function(data){notify(title+' was returned to your shelf!');loan_status.add(temp_loan).remove();loan_id.empty();loan_count.text(parseInt(loan_count.text())-1);actions.html('<button class="button remove-disc">Remove Disc</button> <button class="button loan-disc">Loan Disc</button> <a href="#" class="tag-disc">+/- Tags</a>');});});});
