  $(document).ready(function() {
  	$(document).delegate('.results-item','click',function(){
			
			var id=$(this).attr('id');
			loadDetail(id);			
		})

  	getFeaturedTitles();
  	
  	$('body').delegate('.flowChart','click',function(){
  		$('#distro').trigger('click')
  	})
  	
  	$('body').delegate('.titles','click',function(){
  		$('#books').trigger('click')
  	})
  	
  	$('body').delegate('.faq','click',function(){
  		$('#faq').trigger('click')
  	})
  	
  	$('body').delegate('.contactUs','click',function(){
  		$('#contact').trigger('click')
  	})
    $('.slideshow').cycle({
		fx: 'scrollDown,scrollLeft,scrollRight,scrollUp',
		randomizeEffects: true,
		easing: 'easeInBack',
		pause: 1		
		
			});

	
	
	/*$('.slideshow a').live({
	
		mouseover:function(){
			var id = $(this).attr('id');
			$('.new'+id).show();
			
		},
		mouseout:function(){
			var id = $(this).attr('id');
			$('.new'+id).hide();
		
		}
		
		
	
	})*/
	
	$(document).delegate('.menu div','click',function(){
		var id = $(this).attr('id')
		$('.menu .current').removeClass('current')
		$(this).addClass('current')
		if($(this).attr('id')=="contact" || $(this).attr('id')=="store"){
		
		}else{
			$('.main').empty();
		if(id=="distro"){
		
		var html = '<h1>The four steps/criteria of book distribution</h1><div class="flowChart"><div id="flowCircle"><div class="book1number">1</div></div><div id = "book1"></div><div class="book2number">2</div><div id = "book2"></div><div class="book3number">3</div><div id = "book3"></div><div class="book4number">4</div><div id = "book4"></div></div>';
		$(html).prependTo('.main')
		
		}
		if(id=="marketing"){
			var html="<h1>Marketing</h1><p>We may be specialists in distributing your title, but not in marketing it. We do recognize that effective marketing is a vital and necessary part in the success and sale rate of your title, and we can recommend to you highly qualified professionals with proven ability that will work with us and you to achieve your titles utmost potential. Please contact us for our recommendations: info@danforthbooks.com</p>"
			$(html).prependTo('.main')
			
		}
		if(id=="home"){
		var html = '<div class="new1" style="display:none"><p>THE ALASKA PEN and its surrogate, PEN WHISPERS-125 issues in all, spanning the years 1934-1951, from the foundation upon which this latter-day history of Unga was built...Click book cover for more info</p><div class="chat-bubble-arrow-border"></div><div class="chat-bubble-arrow"></div></div><div class="new2" style="display:none"><p>Clearly written from a child\'s point of view that also honors the intelligence of the reader, &quot;Detective A: The Case of the Missing Silver Spoon&quot; is destined to be a classic. Curious, clever, and determined to solve his case, Detective A knows what to do... Click book cover for more info.</p> <div class="chat-bubble-arrow-border"></div><div class="chat-bubble-arrow"></div></div><div class="new3" style="display:none"><p>A compelling collection of personal, inspirational stories, Embracing the Edge recounts how author Neil Peterson overcame physical and learning disabilities from an early age to become a successful entrepreneur, corporate executive and public servant... Click book cover for more info.</p><div class="chat-bubble-arrow-border"></div><div class="chat-bubble-arrow"></div></div><div class="new4" style="display:none"><p>The Marco Polo Odyssey tracks the fantastic journey of an intrepid adventurer who spent ten years following the 13,000 mile overland route of Marco Polo from Venice to Israel, Turkey, Iran, Afghanistan, Pakistan, and finally crossing China to Beijing.... Click book cover for more info.</p><div class="chat-bubble-arrow-border"></div><div class="chat-bubble-arrow"></div></div><div class="new5" style="display:none"><p>An insiders view by a practicing family physician, Sacred Trust reveals the art and practice of medicine and opens a window into its soul. With wry humor, no-holds-barred candid writing style, Dr. Hollenbeck... Click book cover for more info.</p><div class="chat-bubble-arrow-border"></div><div class="chat-bubble-arrow"></div></div><h1 class="mainHeader">Welcome to Danforth Book Distribution</h1><p>Danforth Book Distribution is a family owned, independent book distributor based in Seattle Washington. Our goal is to assist the independently published author in getting your book to wholesalers that can promote and sell your book to the world</p><div class="slideshow"><a id ="1" href="http://www.danforthbooks.com/book%20pages/alaskapen.html"><img src="../images/book%20covers/alaskapen.jpg" alt="alaskapen" width="240" height="240" id="alaska"/></a><a id="2" href="http://www.danforthbooks.com/book%20pages/detecticea-goodisman.html"><img src="../images/book%20covers/detectivea.jpg" alt="detectivea" width="240" height="240" id="detective" /></a><a id="3" href="http://www.danforthbooks.com/book%20pages/embracing-peterson.html"><img src="../images/book%20covers/embracingtheedge.jpg" alt="embracingtheedge" width="240" height="240" id="edge"/></a><a id="4" href="http://www.danforthbooks.com/book%20pages/marcopolo-rutstein.html"><img src="../images/book%20covers/marcopolo.jpg" alt="marcopolo" width="240" height="240" id="marco" /></a><a id="5" href="http://www.danforthbooks.com/book%20pages/sacredtrust-hollenbeck.html"><img src="../images/book%20covers/sacredtrust.jpg" alt="sacredtrust" width="240" height="240" id="sacred"/></a></div><div class="newTitles"><h2><img src="images/featuredTitles.png"></h2></div><h2>Looking For Distribution?</h2><p>If you are an author with a great book you\'d like distributed, use this website as an educational tool to provide you with helpful insight about the innerworkings of independent book distribution.  Browse our <a href="#" class="flowChart">interactive flow chart</a> for an informative analysis of how we get your book into stores at Danforth Book Distribution. Don\'t forget to check out our list of <a href="#" class="titles">Titles</a>.  If you have any questions check out our <a href="#" class="faq">FAQ</a> section or <a href="#" class="contactUs">contact us</a> for a quote.</p>'
		
		$(html).prependTo('.main')
		  $('.slideshow').cycle({
		fx: 'scrollDown,scrollLeft,scrollRight,scrollUp',
		randomizeEffects: true,
		easing: 'easeInBack',
		pause: 1		
		
			});
		getFeaturedTitles();

}
if(id=="books"){
	var alpha = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
	var html = '<div class="mainHeader">Browse Our Catalog By Title</div><div class="search-container"><div class="alphabet-container">'
	$.each(alpha,function(i,ii){
		html+='<div class="letter" id = "'+ii+'">'+ii+'</div>';
	})
	html+='</div><div class="results-container"></div><div class="detail-container"><div class="title-container"</div></div></div>';
	$(html).prependTo('.main');
}
if(id=="faq"){
	var html = '<h1>FAQ :</h1><p>Q: whats the difference between a distributor and a wholesaler?</p> <p>A: A distributor maintains accounts with major wholesalers who in turn manage the relationship with the final retailer.  A wholesaler does not manage titles or store inventory.  A distributor manages titles on an individual basis tracking sales, shipments and payments from multiple wholesalers.</p><p>Q: What are your fees?</p><p>A: Storage Fee: 2 cents per book stored in our warehouse Shipping and Handling: 10% fee in addition shipping costs Commission: 15% on payments received from wholesalers.</p><p>Q: Which price is the Distributor commission based on?</p><p>A: The wholesale price (55% off of retail price)</p><p>Q: How many books should the distributor hold in inventory?</p> <p>A: There is no recommended inventory amount as each title is different.  The quantity of     books stored in our warehouse will need to correspond to individual authorÍs sales goals.  Inventory can always be adjusted based on how a book sells.</p> <p>Q: Does having my book in distribution enhance my sales ?</p><p>A: Working with a distributor is the only way to make sure your book is available to major wholesalers, so in that sense yes.  However, it is important to remember that Danforth Book Distribution is not a publishing house, and therefore provides no marketing or promotion services.  As a self-published author you are responsible for organizing events, and promoting your title.  We make sure that wholesalers and retailers can order your book easily and efficiently.</p>  <p>Q: What kind of information can I expect to receive regarding the activity of my title?</p> <p>A: Danforth Book Distribution provides a monthly statement outlining  account activity on the 15th of each month.  This statement includes information such as orders shipped, payments received, returns processed, and any changes in inventory, as well as fees charged for these various activities.  Activity is listed by wholesaler name, not bookstore, or other retailer.  If there is a balance owing, payment must be received within 30 days to avoid a $25.00 late fee.  If your account has a credit balance payment will be issued within 5 business days.  Balances (both debit or credit) of 40.00 or less will be automatically rolled over to the following monthÍs statement.</p><p>Q: How do I get paid? </p><p>A: Each wholesaler operates differently.  Legally they are obligated to pay invoices within 90 days of their date of sale.  This date can be somewhat ambiguous, and therefore it is not a hard and fast rule that you will be paid within 90 days.  Danforth Book Distribution processes payment as soon as remittances are received, and applies this credit to your account on your monthly statement.  </p><p>Q: How much is Shipping?</p> <p>A: Danforth Book Distribution uses the USPS media mail as its standard shipping.  This includes tracking and signature confirmation. Because orders include multiple titles, shipping costs vary.  The larger the quantity ordered of an individual title, the cheaper the shipping for that author, also, the more titles included in an order reduces the cost per author.</p> <p>Q: I went in to X bookstore and my book wasnÍt on the shelf, why?</p><p>A: While Danforth Book Distribution does fill orders to retailers directly when necessary, the majority of larger stores use one or more wholesalers to stock their shelves.  If your book is not on the shelf it is likely because it has not been ordered.  This is generally a marketing issue.  You as a self-published author are responsible for creating demand for your title either on your own or by hiring a marketing professional.  If you or your agent take an order from a retailer, or discover that a specific store has been unable to order your book, please relay this information to us, as we will otherwise be unaware of the issue.</p>  <p>Q: Why were some of my books returned? </p><p>A: Wholesalers can return books for a variety of reasons. Your title may have been overstocked, damaged, or unsold.</p>  Wholesalers rarely provide specific information as to the reason of return for an individual title.  </p>'
	
	$(html).prependTo('.main')


}
		
}		
	
	})

	
	//distro events
	$(document).delegate('#book1,.book1number','click',
 function(){
           /*$("#book1").animate({
             path : new $.path.arc({
                   center : [475,553.5],
                   
                   radius : 280.5,
                   start: 0,
                   end: 180
               })
        },500,function(){*/
        		var message1 ='<h1>step 1</h1><div class="modal">Getting started - once we confirm that your book meets distribution criteria we will begin setup and put your book into distribution.</div>'
        		$.fancybox({
				
					'content':message1,
					'width':'300px',
					'padding':'25px'
				
				})
        	
        }
        
        );
	//})

	$(document).delegate('#book2,.book2number','click',
		function(){
		var message ='<h1>step 2</h1><div class="modal">Listing with wholesalers - book wholesalers (define wholesaler: companies that have retail clientele that sell--at a discounted price--books that are ordered from Danforth Book Distribution)  will then list your book in their system for retail stores and libraries to order.</div>'	
		
		
		$.fancybox({
				
					'content':message,
					'width':'200px',
					'padding':'25px'
				
				})
	}
	)
	
	$(document).delegate('#book3,.book3number','click',
		function(){
		var message ='<h1>step 3</h1><div class="modal"> <p>Payment for books - Most wholesalers generally operate under 90 day payment terms for when they issue payment to us for books sold, with a wholesale discount of 55-60% (these terms are determined by each company and can be changed with our full compliance); once this remittance is received by us and closed out, we then credit the author\'s statement for that month\'s activity.</p><p>Example using 55% discount-</p><p>(No including shipping & handling)</p><p>Retail price of book: $20.00</p><p>To the wholesaler:</p><p> -55% (11.00) = $9.00</p><p>To the distributor: -15% (1.35), plus shipping</p><p>To the author: $7.65 </p></div>'	
		
		
		$.fancybox({
				
					'content':message,
					'width':'400px',
					'padding':'25px'
				
				})
			})
			
	$(document).delegate('#book4,.book4number','click',
		function(){
		var message ='<h1>step 4</h1><div class="modal">Returns - returns are an inevitable occurrence with wholesale book distributors. All wholesalers require that all titles ordered can be returned for up to 6 months  and in most cases up to 1 year, which includes damaged books. This can seem frustrating for some authors, but is necessary when conducting business with book wholesalers. Shipping costs for returns are charged by the wholesaler and forwarded to the author.</div>'	
		
		
		$.fancybox({
				
					'content':message,
					'width':'400px',
					'padding':'25px'
				
				})
			})
		
	
});

function getFeaturedTitles(){
	var ids = [39,42,36,61,48,58,33,57,34,16];
	
	var randomnumber=Math.floor(Math.random()*9)+1
	var id = ids[randomnumber]
	$.getJSON('../php/featuredTitle.php',{
		id:id
	})
		
		.success(function(data){
		
				var count = 0;
			$.each(data.items,function(i,ii){
				count++;
				
				ii.cat_description = ii.cat_description.replace(/\\/g, '')
				
				var html = '<div class="featuredBook'+count+'"><div class ="featured-title"></div><div class="featured-picture"><img src="'+ii.cat_photoUrl+'" class="featured-pics"></div><div class="featured-description">'+ii.cat_description+'</div></div>';
				
				$(html).appendTo('.newTitles');
				$('.featured-pics').css('height',250)
				var title = ii.cat_title.replace(/\\/g, '');
					if(ii.cat_author != undefined){
						
						title+=' by '+ii.cat_author.replace(/\\/g, '');
					}
				$('.featured-title').text(title);
				
			})
			
		})


}
$(document).delegate('.letter','click',function(){
	var letter =$(this).attr('id');
	$('.results-container').empty();
	$('.detail_container').empty();
	$.getJSON('../ws/get_authors.php',
		{
			letter:letter
		}
	)
	.success(function(data){
		
		loadResults(data)
		
	})
	
})

function loadResults(data){
	
	var html;
	
	$.each(data.items,function(i,ii){
		html+='<div class="results-item" id = "'+ii.id+'">'+ii.title.replace(/\\/g, '')+'</div>';
	})
	html= html.replace('undefined','');
	
	$(html).prependTo('.results-container');
}

function loadDetail(id){
	$('.detail-container').empty();
	$.getJSON('../ws/get_bookDetail.php',
		{
			id:id
		}
	)
	.success(function(data){
		renderDetail(data)
	})
}

function renderDetail(data){
	$('.detail_container').empty();
	var html;
	var nameDiv = '<div class ="card-author">'+data.firstName+' '+data.lastName+'</div>';
	if(data.title.match(/, The/)){
				var title = data.title.split(',');
				var this_title = title[1].substring(1)+' '+title[0];
				this_title = this_title.replace(/\\/g, '')
			}else{
				var this_title = data.title.replace(/\\/g, '')
			}
			$('<div class="card-image"><img src="'+data.photo+'" height="240px" width="240px"></div>').appendTo('.detail-container')
			 $('<div class="card-">'+data.bookDescription.replace(/\\/g, '')+'</div>').appendTo('.detail-container')
			$('<div class = "card-link"><a href = "'+data.amazonLink+'">Buy this book!</div>').appendTo('.detail-container')

	$('<div class="title-container">'+this_title+'</div>').prependTo('.detail-container')
	$(html)
	$(nameDiv).insertAfter('.title-container');
}
