// JavaScript Document

jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $ as alias to jQuery

$(document).ready(function(){

$("#bookstore-carousel p").each(function(){
	var linkLabel = $(this).html();
	var titleLabel = $(this).prev("a").html();
	var titleLink = $(this).prev("a").attr('href');
	if (linkLabel.length + titleLabel.length > 200) {
	$(this).html(linkLabel.substring(0,200-titleLabel.length) + '... <a href="'+titleLink+'">Read more</a>');	
	}});

$("#book-list p").each(function(){
	var linkLabel = $(this).html();
	var titleLabel = $(this).prev("a").html();
	var titleLink = $(this).prev("a").attr('href');
	if (linkLabel.length + titleLabel.length > 200) {
	$(this).html(linkLabel.substring(0,200-titleLabel.length) + '... <a href="'+titleLink+'">Read more</a>');	
	}});



$("#bookstore-menu a").each(function(){
	var linkLabel = $(this).html();
	if (linkLabel.length > 25) {
	$(this).html(linkLabel.substring(0,25) + "...");	
	}});

// STARTS CAROUSEL
$('#bookstore-carousel').carousel();
	


$('#isbn_preview input[type="button"]').button({ disabled: false });
$('#isbn_preview input[type="submit"]').button({ disabled: false });

$('input[name="isbn_preview"]').click(function() {
	var book_isbn = $('input[name="book_isbn"]').val();
		var data ={
    		action: 'preview_add_isbn',
    		isbn: book_isbn
    		};
			$('input[name="isbn_preview"]').button({ disabled: true, label: 'Loading...' });
    	$.post('http://openokpyon.com/wp-admin/admin-ajax.php', data, function(response){
    		
    		//alert(response);
			$('#isbn_preview li:not(:first)').remove();
    		$('#isbn_preview').append(response);
			
			var autocompleteSource = $('#book-category').attr('source').split(',');
			$('#book-category').autocomplete({source: autocompleteSource});
			
			$('#isbn_preview .toggleImg').click(function(){$(this).next('img').toggle();return false;});
			$('#isbn_preview input[type="reset"]').button().css({'float':'right'}).click(function(){$('#isbn_preview li:not(:first)').remove();return false;});
			$('#isbn_preview .toggleImg').next('img').hide();
			
			$('input[name="date"]').datepicker({ 
			dateFormat: 'yy-mm-dd' 
			});

			
			$('#isbn_preview input[type="submit"]').button();
    		$('input[name="isbn_preview"]').button({ disabled: false, label: 'Preview' });
			
    	}); 
	
	//alert(book_isbn);
	return false;
	});

$('form[name="addISBN"]').hide();
$('form[name="addISBN"]').submit(function() {
	var book_isbn = $('input[name="book_isbn"]').val();
	var book_title = $('input[name="title"]').val();
	var book_author = $('input[name="author"]').val();
	var book_blurb = $('textarea[name="blurb"]').val();
	var book_date = $('input[name="date"]').val();
	var book_category = $('input[name="category"]').val();
	var book_image = $('form[name="addISBN"] .book_image').attr('src');
	var book_url = 'http://astore.amazon.com/omorphoscom-20/detail/' + book_isbn;
	//alert(book_category + ' ' +book_isbn + ' ' +book_title + ' ' +book_author + ' ' +book_blurb + ' ' +book_date + ' ' +book_image);
	var data ={
    		action: 'bookstore_add_book_isbn',
			title: book_title,
			isbn: book_isbn,
			author: book_author,
			blurb: book_blurb,
			url: book_url,
			date: book_date,
			category: book_category,
    		image: book_image
    		};
		//$('input[name="isbn_preview"]').hide();
    	$.post('http://openokpyon.com/wp-admin/admin-ajax.php', data, function(response){
    		if (response=='Success') {
				
				$('<div>Successfully Added</div>').dialog(
				{ modal: true,
				buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
				}
				});
				
				$('#isbn_preview li:not(:first)').remove();
				
			} else {
				$('#isbn_preview').html('Failed');
			}
    		//alert(response);
    		//$('#isbn_preview').html(response);
    		//$('input[name="isbn_preview"]').show();
    	}); 
	
	
	return false;
	});



$('.add-isbn-link').click(function(){
	$('form[name="addISBN"]').toggle();
	return false;
	});



	   
});
  
    
  });
})(jQuery);



var ourInterval;
(function ($) {
    $.fn.carousel = function () {
    var CC = $(this);
    // SET WIDTH / HEIGHTS
    var CCWidth = CC.width();
    var CCHeight = CC.height();
	CCInterval = 10000;
    CC.children("ul").children("li").css({
               "left"   : CCWidth,
               "width"  : CCWidth,
               "height" : CCHeight});
    
    // BEGIN FLIPPING
    CC.flipScreen(); 

    ourInterval = setInterval( function(){ CC.flipScreen(); }, CCInterval); /* Set the interval */
	
		// HOVER HOOK
		CC.hover(function() {
			clearInterval(ourInterval);
		});
		
		// MOUSEOUT HOOK
		CC.mouseout(function() {
			clearInterval(ourInterval); 
			ourInterval = setInterval(function(){ $(this).flipScreen(); }, CCInterval);
		});
		
		// MOUSEOVER HOOK
		CC.children().mouseover(function() {
			clearInterval(ourInterval);
		});
	
    }
    var nextPane;
	$.fn.flipScreen = function () {
    
        var CC = $(this); // CAROUSEL CONTAINER
        var CCWidth = CC.width();
        var CCHeight = CC.height();
		//alert(CC.attr('id'));
	
if (nextPane===undefined) { 
	CC.children("ul").children("li").hide().css({
		"left"   : CCWidth,
		"width"  : CCWidth,
		"top"	:CCHeight,
		"height" : CCHeight});
	nextPane = $("li:first", CC); }
//alert(nextPane.html());
	if (nextPane.length < 1) { // THERE IS NO NEXT PANE
	
	$("li:last", CC).show().animate({'left':-CCWidth,'top':-CCHeight}, function(e){ 
	$("li:first", CC).show().animate({left:0,top:0});
	$("li:last", CC).css({'left':CCWidth,'top':CCHeight}).hide();
	nextPane = $("li:first", CC);
	
	});
	
	// CAME TO END, TIME TO GO BACK
	 
	} 

	// FLIPPING
	nextPane.prev("li").animate({'left':-CCWidth,'top':-CCHeight}, function(e){ $(this).hide().css({'left':CCHeight,'top':CCHeight}); }); 
	nextPane.show().animate({left:'0',top:'0'}, 'slow');
	nextPane = nextPane.next("li");
    }
	


})(jQuery);









