jQuery(document).ready(function($) {

	// Add current class to the Library navigation menu
	$('#main_nav .library_navlink').addClass('current');


	
	
	
	
	

	// TOOLBAR FIXED
	if (!msie6) {
		var top = 40; //$('#main_content').offset().top;
		$(window).scroll(function (event) {

				// what the y position of the scroll is
				var y = $(this).scrollTop();
				// whether that's below the sidebar
				if (y >= top) {
					// if so, add show the fixed sidebar
					$('#toolbar_fixed').slideDown();
				} else {
					// otherwise hide it
					$('#toolbar_fixed').slideUp();
				}

		});
	}

	

	// QUICK PAGE FINDER
	$("#quick_page_finder").autocomplete({

		source: siteurl + "/wp-content/themes/learnwake/includes/autocomplete_library.php",
		minLength: 2,
		search: function() {
			$(this).addClass('autocomplete_loading');
		},
		open: function() {
			$(this).removeClass('autocomplete_loading');
		},
		select: function(event, ui) {
			if( ui.item.page_id != undefined){
				window.location.href = siteurl + '?page_id=' + ui.item.page_id;
			}
		}
		
	});

	$('.quick-find-button').click(function(e){
		$("#quick_page_finder_dialog").dialog({
			modal:true,
			resizable:false,
			draggable:false,
			width:450,
			height:105,
			position:["center",20],
			close: function(event, ui) {
				$("#quick_page_finder").val('');
			}

		});
		$("#quick_page_finder").focus();
		$(this).removeClass('ui-state-focus');
		e.preventDefault();
	});



	// Popeye	
	$('.jqzoom a').addClass('ignore_lightbox');
	$('.ppy a').addClass('ignore_lightbox');
	$('a[href$=png], a[href$=gif], a[href$=jpg], a[href$=jpeg], a[href$=PNG], a[href$=GIF], a[href$=JPG], a[href$=JPEG]').not('.ignore_lightbox').each(function(){
		var popeye_image = $(this).attr('href');	
		var popeye_thumb = $(this).find('img').attr('src');
		var popeye_caption = $(this).find('img').attr('title');
		var popeye_class = $(this).find('img').attr('class');
		var popeye_direction = 'right';
		if( $(this).find('img').hasClass('alignright') ){
			popeye_direction = 'left';
		}
		popeye_thumb = popeye_thumb.replace('100x100.', '150x150.');
		var popeyeHTML = '<div class="ppy single_popeye '+popeye_class+'" rel="'+popeye_direction+'"><ul class="ppy-imglist"><li><a href="'+popeye_image+'"><img src="'+popeye_thumb+'" /><span class="ppy-extcaption">'+popeye_caption+'</span></li></ul><div class="ppy-outer"><div class="ppy-stage"><div class="ppy-nav"><a class="ppy-prev" title="Previous image">Previous image</a><a class="ppy-switch-enlarge" title="Enlarge">Enlarge</a><a class="ppy-switch-compact" title="Close">Close</a><a class="ppy-next" title="Next image">Next image</a></div><div class="ppy-counter"><strong class="ppy-current"></strong> / <strong class="ppy-total"></strong></div></div></div><div class="ppy-caption"><span class="ppy-text"></span></div></div>';
		$(this).replaceWith(popeyeHTML);
	});

	$('.single_popeye').each(function(){
		$(this).popeye({caption:"permanent",zindex:10,direction:$(this).attr('rel')});
	});

	
	// Typo Submitter
	$('#library_page h3').each(function(){
		$('#typo_section').append('<option value="'+$(this).text()+'">'+$(this).text()+'</option>');
	});
	$('.typo-button').click(function(e){
		$("#typo_dialog").dialog({
			width:400,
			close: function(event, ui) {
				$("#typo_description").val('');
			}
		});
		$(this).removeClass('ui-state-focus');
		e.preventDefault();
	});
	$('.submit-fix-button').click(function(e){
		$('div.mouseout').remove();
		var votes = $(this).val();
		var $voteControls = $(this).parent().parent().parent();
		var requestID = $voteControls.attr('rel');
		var data_string = 'ajax=1&s=' + $('#typo_section').val() + '&d=' + $('#typo_description').val() + '&p=' + window.location;
		$.ajax({
			type: "POST",
			url: siteurl + '/wp-content/themes/learnwake/includes/typo_proc.php',
			data: data_string,
			success: function(response){
				$("#typo_description").val('');
				$("#typo_dialog").dialog("close");
				$.jGrowl(response, { life: 8000, theme: "success" });
			}
		});
		e.preventDefault();
	});


});

