$(document).ready(function() {


/*
function do_strike_out(idx)
{
//alert( $(this).css('width') );
  $(this).wrap("<span style='position:relative;left:0px;top:0px;'>").
    before( "<span style='margin-top:6px;border-top:1px solid #FF0000;"+
      "position:absolute;left:0;right:24px;width:100%;'></span>" ).
    wrap("<span style='position:relative;left:0px;top:0px;'>");
}

$(function(){
  $('.catalogue .section .price span').each(do_strike_out);
});
*/

	if ( $(".section").length )
		$(".section").last().addClass('last');

	/* 	SHOW/HIDE
		----------------------------------------------------------*/

		$("h5 a.accordion").parent().nextUntil("h5").hide();
		//$("h5 a.accordion:first").toggleClass("accordion_open");
		
		$("h5 a.accordion").click(function() {
			// Reference to currently clicked
			whichSection = $("h5 a.accordion").index(this);

			$("h5 a.accordion").each(function(){
				// Shut any open sections, as long they're not the one that's just been clicked
				if ($(this).is('.accordion_open') && $("h5 a.accordion").index(this) != whichSection )
				{
					$(this).parent().nextUntil("h5").slideToggle("fast");
					$(this).toggleClass("accordion_open");
				}
			}); /**/

			// Open all subsequent markup up to next h5, and add class for styling	
			// Or, if this section is already open, this will shut it instead	
			$(this).parent().nextUntil("h5").slideToggle("fast").addClass('accordion-text');

			// Flag as open (or closed if this section was already open)
			$(this).toggleClass("accordion_open");

			return false;
		});


               pageHash = window.location.hash;
                if(pageHash != ''){//we've got a hash in the URL
	               //alert(pageHash.substring(1) );
	               pageHash = pageHash.substring(1);
	              // $("li."+pageHash+" h5 a.accordion").parent().nextUntil("h5").slideToggle("fast");
	              // $("li."+pageHash+" h5 a.accordion").toggleClass("accordion_open");
	              $("h5."+pageHash+" a.accordion").parent().nextUntil("h5").slideToggle("fast");
	              $("h5."+pageHash+" a.accordion").toggleClass("accordion_open");
                }

		
		
	/* 	EXTERNAL LINKS
		-------------------------------------------------------------*/

		$(".external").click( function() {
			window.open( $(this).attr("href") );
			return false;
		});

	//###   Image Zoom   ###
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: true,
		allow_resize: false,
		theme:"light_square",
		counter_separator_label: " of "
	});

	/* ###   Book Slideshow   ### */
	$('#spread-frame #slideshow').cycle({
		fx:    'scrollLeft', 
		prev:    '.book_head p .prev',
		next:    '.book_head p .next',
		timeout: 0, /* Set to 0 to disable auto advance - was 5000 */
		speed:   500,
		after: function() {
			var currentIndex = $("#slideshow a").index( $("#slideshow a:visible") );
			$('#img-counter').html( currentIndex + 1 );
		}
	});
/*	function onSlideAfter() {
		imgCounter++;
		if (imgCounter > $("#slideshow img").length)
			imgCounter = 1;
		$('#img-counter').html(imgCounter);
	}; */

	/* ###   Homepage Featured Book Slideshow   ### */
	$('#main .feature #slideshow').cycle({
		fx:    'scrollLeft', 
		prev:    '#main .feature .prev',
		next:    '#main .feature .next',
		timeout: 7000, /* Set to 0 to disable auto advance */
		speed:   500
	});

	/* ###   Homepage Bundled books manual Slideshow   ### */
	$('.bundle-slideshow').each(function(index) {
		$(this).cycle({
			fx:    'scrollLeft', 
			prev:    '#bundle'+(index+1)+' .prev',
			next:    '#bundle'+(index+1)+' .next',
			timeout: 5000, /* Set to 0 to disable auto advance */
			speed:   500,
			after: function() {
				var currentIndex = $("#bundle"+(index+1)+" .cover").index( $("#bundle"+(index+1)+" .cover:visible") );
				$("#bundle"+(index+1)+" .img-counter").html( currentIndex + 1 );
			}
		});
	});

	/* ###   Desk Inspection Category Links   ### */
	$("#desk-inspection form.selector select").change(function () {
		//###   Dropdown box selection   ###
		if ($(this).val() != "") {
			top.location.href = "/books/disciplines/" + $(this).val();
		}
	});

	$("#content.bookinfo .review-container").cycle({
		fx:    'scrollLeft', 
		prev:    '.review-nav p .prev',
		next:    '.review-nav p .next',
		timeout: 0, /* Set to 0 to disable auto advance */
		speed:   500,
		after: function() {
			var currentIndex = $("#content.bookinfo .review-container blockquote").index( $("#content.bookinfo .review-container blockquote:visible") );
			$('.review-nav .img-counter').html( currentIndex + 1 );
			$("#content.bookinfo div.review-container").height( $("#content.bookinfo div.review-container").prev().height() + $("#content.bookinfo .review-container blockquote:visible").height() );
		}
	});
	if ($("#content.bookinfo .review-container").length) {
		if ($("#content.bookinfo .review-container blockquote").length == 1)
			$("#content.bookinfo .review-nav a").hide();
	}
});

