$(document).ready(function() {
	
	// Home Button hover
	$('<div id="home_hover"></div>').appendTo('#navigation').hide();
	
	$('#navigation a#home_button').hover(function() {
		$('#home_hover').show();
	}, function() {
		$('#home_hover').hide();
	});
	
	
	// Album popup trigger
	$('#album_popup > a').click(function() {
		$('#album_module').fadeIn();
	});
	
	$('#album_module a.close').click(function() {
		$('#album_module').fadeOut();
	})
	
	$('#album_popup > a').hover(function() {
		$('.thought_bubble').fadeIn();
	}, function() {
		$('.thought_bubble').fadeOut();
	});
	
	
	// Listing Navigation - for Book/Art/School listings
	$('ul#arrow_nav li.prev a').hover(function() {
		$(this).stop().animate({ marginLeft : '-7px' }, 200);
	}, function() {
		$(this).stop().animate({ marginLeft : '0px' }, 200);
	});
	
	$('ul#arrow_nav li.next a').hover(function() {
		$(this).stop().animate({ marginRight : '-7px' }, 200);
	}, function() {
		$(this).stop().animate({ marginRight : '0px' }, 200);
	});
	
	
	// Configure scrollable book listings
	$('#listings').cycle({
		fx: 'scrollHorz',
		prev: 'ul#arrow_nav li.prev a',
		next: 'ul#arrow_nav li.next a',
		timeout: 0,
		easing: 'easeInOutBack',
		after: function onAfter(curr, next, opts) {
					var index = opts.currSlide;
					$('ul#arrow_nav li.prev a')[index == 0 ? 'fadeOut' : 'fadeIn']();
					$('ul#arrow_nav li.next a')[index == opts.slideCount - 1 ? 'fadeOut' : 'fadeIn']();
				}
	});
	
	if ( $('ul.list').length == 1 ) {
		$('ul#arrow_nav').hide();
	}

	
	// Custom Lightbox - configuration/settings for custom FancyBox
	$('#kids_module ul.list li a.popup, #kids_stuff.single a.popup').fancybox({
		'imageScale': true,
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.25,
		'callbackOnShow': function(itemCurrent) {
								var imgWidth = $('#fancy_content').width() +20;
								var centerTitle = ( $(window).width() / 2 ) - (imgWidth / 2) + 12;
								$('#fancy_title').width(imgWidth).css('left', centerTitle);
						  }
	});
	
	
	// Drag and Drop - configuration for the book listing page
	$('#book_module_big ul.list li').draggable({
		revert: true,
		revertDuration: 350,
		containment: '#content',
		start: function(event, ui) {
			$('#listings').addClass('visible');
		},
		stop: function(event, ui) {
			$('#listings').removeClass('visible');
		}
	});
	
	$('#drop_zone #look').droppable({
		hoverClass: 'drag',
		activeClass: 'active',
		tolerance: 'touch',
		drop: function(event, ui) {
			var bookDetailUrl = $('ul.list li.ui-draggable-dragging a.look').attr('href');
			setTimeout(function () { window.location.replace(bookDetailUrl); }, 400);
		}
	});
	
	$('#drop_zone #listen').droppable({
		hoverClass: 'drag',
		activeClass: 'active',
		tolerance: 'touch',
		drop: function(event, ui) {
				var bookAudioFile = $('ul.list li.ui-draggable-dragging a.listen').attr('href');
				var bookTitle = $('ul.list li.ui-draggable-dragging h2').text();
				
				/* Set up the player */
				$('#listen').addClass('drag');
				$('#player, #player_content').fadeIn(150);
				$('#player_content div.book_thumb, h4.book_title').empty();
				$('ul.list li.ui-draggable-dragging img').clone().appendTo('#player_content div.book_thumb').hide().fadeIn('slow');
				$('h4.book_title').text(bookTitle);
				$('#player_content a.download').attr('href', bookAudioFile);
				$('#volume_bar_value').height(60);
				
				/* Sets and plays audio file */
				$('#player').jPlayer('clearFile');
				startAudio(bookAudioFile);
			}
	});
	
	function startAudio(bookAudioFile) {
		$('ul#player_controls li#play').click(function() {
			$('#player').jPlayer('setFile', bookAudioFile).jPlayer('play');
		});
	}
	
	
	// Configuration for jPlayer on Book Listing pages
	$('#books #player').jPlayer({
		ready: function() {
			this.element.jPlayer('setFile','http://c1550162.cdn.cloudfiles.rackspacecloud.com/1269460436roar.mp3');
		},
		volume: 100,nativeSupport: false,
		swfPath: "/wp-content/themes/munsch/js",
		graphicsFix: false,
		customCssIds: true
	})
	.jPlayer("cssId", "play", "play")
	.jPlayer("cssId", "stop", "stop")
	.jPlayer("cssId", "volumeMin", "mute")
	.jPlayer("cssId", "volumeMax", "muted");
	
	/* Mute controls */
	$("ul#player_controls #mute a").toggle(function() {
		$(this).parent().addClass('muted');
		$("#player").jPlayer("volume", 0);
		$('#volume_bar').css('background-position','0 -100px');
	}, function() {
		$(this).parent().removeClass('muted');
		$("#player").jPlayer("volume", 100);
		$('#volume_bar').css('background-position','0 0');
	});
	
	/* Set the volume amount */
	$('#volume_bar').click(function(e) {
		var volumeNum = ($(this).height()) - (e.pageY - $(this).offset().top);
		var finalVolume = Math.floor(volumeNum / .7);
		var bgOffset = (100 - finalVolume) * .7;
		
		$('#player').jPlayer('volume', finalVolume);
		$('#volume_bar').css('background-position','0 '+bgOffset+'px');
	});
	
	// Configuration for the Book Details page
	$('ul#actions li.listen a').click(function() {
		var bookAudioFile = $('#player_content a.download').attr('href');
		var bookTitle = $('#content h2').text();
		
		$("#player").jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", bookAudioFile);
			},
			volume: 100,
			nativeSupport: false,
			swfPath: "/wp-content/themes/munsch/js",
			graphicsFix: false,
			customCssIds: true
		})
		.jPlayer("cssId", "play", "play")
		.jPlayer("cssId", "stop", "stop")
		.jPlayer("cssId", "volumeMin", "mute")
		.jPlayer("cssId", "volumeMax", "muted");
		
		$('#player, #player_content').fadeIn(150);
		$('h4.book_title').text(bookTitle);
		$('#volume_bar_value').height(60);
	});
	
	
	// Audio Player - configuration for the audio player pop-up
	$('#player_content a.close').click(function() {
		$('#player, #player_content, #player_help').fadeOut('fast');
		$("#player").jPlayer("clearFile");
		$('#listen').removeClass('drag');
	});
	
	$('#player_content a.help').click(function() {
		$('#player_help').css('top','175px').show().animate( { top:"75" }, 750);
	});
	
	
	// Book Listing (Homepage) - configuration/effects for books and info
	$('<div id="book_popup"></div>').appendTo('#content').hide();
	$('<div id="big_book"></div>').appendTo('#book_module').hide();
	
	$('#book_module ul.list li').live('click', function() {
		var getBookPos = $('ul.list li').index(this);
		var getBookImg = $(this).find('a').attr('rel');
		var getBookUrl = $(this).find('a').attr('href');
		
		if (getBookPos == 0 || getBookPos == 4) {
			$('#book_popup').css('background','url(wp-content/themes/munsch/img/bubble_large_1.png) no-repeat 0 0');
			$('#big_book').css('left','29px');
		} else if (getBookPos == 1 || getBookPos == 5) {
			$('#book_popup').css('background','url(wp-content/themes/munsch/img/bubble_large_2.png) no-repeat 0 0');
			$('#big_book').css('left','128px');
		} else if (getBookPos == 2 || getBookPos == 6) {
			$('#book_popup').css('background','url(wp-content/themes/munsch/img/bubble_large_3.png) no-repeat 0 0');
			$('#big_book').css('left','128px');
		} else if (getBookPos == 3 || getBookPos == 7) {
			$('#book_popup').css('background','url(wp-content/themes/munsch/img/bubble_large_4.png) no-repeat 0 0');
			$('#big_book').css('left','227px');
		}
		
		$('#big_book').empty().append('<img src="'+getBookImg+'" /><a class="more" href="'+getBookUrl+'">Find out more &raquo;</a><a class="close" href="#">Close</a> ').fadeIn();	
		$('#book_popup').empty().css('top','75px').fadeIn().animate({ top:"55px" }, 500);
		$(this).children('div.summary').clone().appendTo('#book_popup');
		return false;
	});
	
	$('#big_book a.close').live('click', function() {
		$('#book_popup, #big_book').fadeOut();
	});
	
	
	// Poems & Stories scroller
	$('ul#arrows li.up a').click(function() {
		$("ul#poemsstories_list").stop().scrollTo( '-=42', 500, {axis:'y'} );
	});
	
	$('ul#arrows li.down a').click(function() {
		$("ul#poemsstories_list").stop().scrollTo( '+=42', 500, {axis:'y'} );
	});
	
	
	// Assign class names to Something Special pagination
	$('#kids_stuff #pagination a').each(function() {
		var pagiName = $(this).text();
		$(this).addClass(pagiName);
	});
	
	
	// Redirect browser to selected option's URL
	$('#quick_find input[type="submit"]').click(function() {
		window.location = $(this).parent().find('select :selected').val();
	});
	
	$('#filter select').change(function() {
		window.location = $(this).val();
	});
	
	
	// Internet Explorer 6 message
	var warnIE6 = $.cookie('warnIE6');
	
	if (($.browser.msie && $.browser.version == 6) && !(warnIE6 == 'hidden')) {
		$('<div id="ie6_overlay"></div><div id="ie6_warning"></div>').appendTo('body').fadeIn();
		$('#ie6_warning').load('/wp-content/themes/munsch/ie6.php');
		
		var bodyHeight = $(window).height();
		var bodyWidth = $(window).width();
		var warnWidth = $('#ie6_warning').width();
		var wrapMiddle = (bodyWidth / 2) - (warnWidth / 1.5);
		
		$('#ie6_overlay').css({'width': bodyWidth, 'height': bodyHeight});
		$('#ie6_warning').css('left', wrapMiddle);
		$('#ie6_warning a.close').live('click', function() {
			$('#ie6_warning, #ie6_overlay').fadeOut();
			$.cookie('warnIE6', 'hidden');
		});
		
		$('ul#browsers li').live('mouseover', function() {
			$(this).find('img').stop().animate({ opacity: 1 });
			$(this).find('p').show();
		});
		
		$('ul#browsers li').live('mouseout', function() {
			$(this).find('img').stop().animate({ opacity: 0.5 });
			$(this).find('p').hide();
		});
	}


}); // End of $(document).ready
