$(document).ready(function() {

	// -- Pogil Header Buttons --
	// JB: Created a modified extension of pkUI for Pogil's header Bar. 
	// This injects .pk-header-btn.icon anchors with the .pk-b span so we can use CMS Icons in the dropdown
	$.each($('.pk-header-btn.icon'), function() { // inject extra markup for link styles
		txt = $(this).text();
		$(this).html("<span class='pk-b'>"+txt+"</span>");
   });



	pkUI('#pk-header-bar');



	// -- Pogil Top Bar Dropdown --
	var pkDrop = $('.pk-header-dropdown');
	pkDrop.parent().mouseenter(function(){
		$(this).addClass('open');
	});
	pkDrop.parent().mouseleave(function(){
		$(this).removeClass('open');
	});
	pkDrop.prev().click(function(event){
		event.preventDefault();
	});



	// -- Pogil Sidebar Filters --
	var filterToggles = $('.pogil-filter-sidebar h5');
	filterToggles.attr('title','Click to toggle');
	filterToggles.click(function(){
		$(this).parent().toggleClass('open');
	});
	$('.pogil-filter-sidebar a').prepend('<span class="close"></span>');
	
	

	// Single-option select elements with that option selected -> static text.
	// Select element sticks around, hidden but submitted
	pkSelectToStatic('body');



	// -- Use Input Type as Class Name (For IE) --
	// ieForms();
});

// This is extended from pkUI.js
function pkUIOpacity(uiOpacity)
{
	if (typeof uiOpacity == 'undefined') // If Not Set, use Default Value
	{
		uiOpacity = 1;
	}
	//Crossbrowser Opacity
	$('.pk-i, #the-apostrophe').fadeTo(0,uiOpacity); //Button Background Color
}

// Overrides function for extending pkUI
function pkOverrides() 
{

	// -- Pogil Form Error Class & Focus --
	if ($('.pk-form-error .error_list:first').length > 0)
	{
		$('.pk-form-error .error_list:first').parent().prev().addClass('has-errors'); // Applies error class to .pk-form-field
		$('.has-errors :input').focus();
	}

	// -- Pogil Textarea Autogrow
	$('.app-view .pk-form-row textarea').autogrow({minHeight: 30,	lineHeight: 16});		
}

// Convert 'Full Name (username)' to { username: 'username', fullname: 'Full Name' }
function pkParseUniqueName(name)
{
	var result = name.match(/^\s*(.*?)\s*\((\w+)\)\s*$/);
	if (result)
	{
		return { fullname: result[1], username: result[2] };
	}
	else
	{
		return false;
	}
}

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

		$('.viewable_toggle_lock').remove();

		var toggle = $('.viewable_toggle');
		var toggleRow = toggle.parents('.pk-form-row')

		toggleRow.hide(); // Hide the Checkboxes

		toggleRow.prev().find('.pk-form-field').append('<a href="#" class="viewable_toggle_lock">locked</a><p class="viewable_toggle_lock_text">This field is private. Other POGIL users will not be able to view the information above. Click the lock icon to make this field public.</p>'); // Inject the locks

		var lock = $('.viewable_toggle_lock');
		
		lock.each(function(){
			
			lockID = $(this).prev().attr('id')+'-lock';
			toggleID = $(this).parents('.pk-form-row').next().find('input[type="checkbox"]').attr('id');
			lockLabel = $(this).parents('.pk-form-row').children('label').html();
			$(this).attr({'id':lockID, 'rel':toggleID}); //We aren't using them but you never know. the locks get semantic ID and Rel tags

			var checkbox = $("#"+toggleID);
			var tooltipLocked = "Unlock "+lockLabel;
			var tooltipUnlocked = "Lock "+lockLabel;			
			
			// Initial setup on DOMReady
			if (!checkbox.attr('checked'))
			{
				$(this).addClass('locked').html('locked').attr('title',tooltipLocked);
				$(this).parents('.pk-form-field').addClass('locked');									
			}
			else
			{
				$(this).removeClass('locked').html('unlocked').attr('title',tooltipUnlocked);
				$(this).parents('.pk-form-field').removeClass('locked');				
			}

			// Flip states on Click
			$(this).click(function(event){
				event.preventDefault();

				if (!checkbox.attr('checked'))
				{
					checkbox.attr('checked',true);
					$(this).removeClass('locked').html('unlocked').attr('title',tooltipUnlocked);
					$(this).parents('.pk-form-field').removeClass('locked');
				}
				else
				{
					checkbox.attr('checked',false);
					$(this).addClass('locked').html('locked').attr('title',tooltipLocked);
					$(this).parents('.pk-form-field').addClass('locked');					
				}
			});
		})
		
	});
}