var numberOfToCEntriesToDisplay = 8;

function hideToc() {
    $('#toc-content li:gt(' + (numberOfToCEntriesToDisplay - 1) +')').hide();
    $('.full-toc-button').show();
    $('.full-toc-button img').attr("src", "http://oreilly.com/images/oreilly/full_toc.png").unbind('click', hideToc).click(showToc);
}

function showToc() {
    // remove show callback when we fix videos
    $('#toc-content li:gt(' + (numberOfToCEntriesToDisplay - 1) +')').show();
    $('.full-toc-button img').attr("src", "http://oreilly.com/images/oreilly/close_toc.png").unbind('click', showToc).click(hideToc);
}

function hideFullDescription() {
    $('#fulldesc').hide();
    $('#fulldescButton').unbind('click', hideFullDescription).click(showFullDescription).removeClass("rolldown").addClass("rollup");
}

function showFullDescriptionButton() {
    var short = $('#short-description > div:first-child').text();
    var full = $('#fulldesc').text();
    var fullHtml = $('#fulldesc').html();
    var descriptionType = "";
    
    if ($.trim(short) == $.trim(full)) 
        var descriptionType = "same";
    if ($.trim(fullHtml) == "<div>None</div>")
        var descriptionType = "none";
    if ($.trim(fullHtml) == "")
        var descriptionType = "empty";
    
    if (descriptionType == "")
        $('#fulldescButton').css('display', 'block');
}

function showFullDescription() {
    $('#fulldesc').show();
    $('#fulldescButton').unbind('click', showFullDescription).click(hideFullDescription).removeClass("rollup").addClass("rolldown");
}


//Simplify Title Area
$(function() {
    if(simplifyTitle == true)
        $('.simplify').remove();
});

//Bring up Product Details
$(function() {
    if(detailsUp == true) {
        $('.test-details').append( $('#product-details') ).show();
    };
});

//Yellow and Gray Buy Buttons
$(function() {
    if(yellowBuy == true)
        $('.buybuttonswidget').addClass('yellow');
    if(grayBuy == true)
        $('.buybuttonswidget').addClass('gray').find('.em-option .shopping_cart_button_line').css('border-bottom', 'none');
    if(grayBuy == true || yellowBuy == true)
        {
            $('.yellow .gbr, .gray .gbr').remove();
            if($('.buybuttonswidget .shopping_cart_button_line').length == 1)
                {
                    $('.shopping_cart_button_line').css('border-bottom', 'none');
                    $('.buybuttonswidget .button_set').css('padding-bottom', '6px');
                    if($('.buybuttonswidget .shopping_cart_button_line').closest('.em-option').length == 0)  
                        {
                        if($('.em-option .shopping_cart_button_line, .em-option.shopping_cart_button_line').length == 0)
                            $('.save_a_tree, .shopping_cart_button_line').wrapAll('<div class="em-option" />');
                        }

                }
            if($('.em-option .shopping_cart_button_line, .em-option.shopping_cart_button_line').length == 0)
                {
                    $('.save_a_tree').unwrap();
                    $('.shopping_cart_button_line:contains("Print")').addClass('em-option');
                    $('.buybuttonswidget.gray .em-option').css('margin-top', '6px');
                }
            $('.buybuttonswidget')
                .filter('.yellow, .gray')
                .find('.product_buy_link img')
                .each(function() {
                    var el = $(this);
                    if(el.closest('.em-option').length == 0 && $('.product_buy_link img').length > 1)
                        $(this).attr('src', '/catalog/assets/images/btn_add_to_cart_small.png').css('padding-left', '24px');
                });
        }
});

//Push Offers Down
$(function() {
    if(offersDown == true) {
        $('.test-offers').append( $('#offers') ).show();
    };
});

$(function() {
    showFullDescriptionButton()
    hideFullDescription();
    
    // Play video from Watch Preview button
	$('#preview-button').click(function () { 
	   $('#video-preview embed')[0].sendEvent("play"); 
	});

   $('#editions-button').click(function() {
   var item = $('#work ol');
	   if(item.css('display') == 'none')
			{
			item.show();
			$('.button').addClass('open');
			}
	   else
	   		{
			item.hide();
			$('.button').removeClass('open'); 
			}	
		return false;
		
	});
	
	$('#work li:odd').addClass('odd');
	
    $('.etal-control').toggle(
    function () {
        $(this).prev('.etal').show();
        $(this).text(" (x)")
    }, function () {
        $(this).prev('.etal').hide();
        $(this).text(", et al.")
    });
}); 

// Hide/show the table of contents
$(function() {
	if($('#toc-content li').length > numberOfToCEntriesToDisplay + 6)
		hideToc();
		$('#toc-content').show();
});

//  Create Lightboxes
$(function() {
    $('.lightbox').lightBox({
        imageLoading:'/catalog/assets/images/lightbox-ico-loading.gif',
        imageBtnClose:'/catalog/assets/images/lightbox-btn-close.gif',
        imageBtnNext:'/catalog/assets/images/lightbox-btn-next.gif',
        imageBtnPrev:'/catalog/assets/images/lightbox-btn-prev.gif',
        imageBlank: '/catalog/assets/images/lightbox-blank.gif'
    });
});

$(function() {
    var lastButtonLine = $('.button_set .shopping_cart_button_line').length - 1;
    $('.shopping_cart_button_line').eq(lastButtonLine).css('border-bottom', 'none');
});

//Create Page ToC
$(function() {
    $('.page_toc_entry:visible').each(function (i) {
        if (i != 0) {
            $('#page-toc').append(" | ")
            }
        $('#page-toc').append("<a href=#" + this.id + ">" + $('.content-subtitle:first', this).text() + "</a>" )
    });
});
