$(document).ready(function()
{	
	/* Confirm delete lightbox prompt */
	$( '.deleteLightBoxGroup' ).livequery ( 'click', function ( event )
	{
		// Get the query string from the link we've clicked
		// so we can pass the data to the lightbox content URL
		var str_url = $( this ).attr ( 'href' );
		var	int_qs	= str_url.indexOf ( '?' );	
		var str_qs	= str_url.substr ( int_qs );
			
		$( this ).htmlLightBox ( { contentURL: '/p/social/inc.group-confirm-delete.php' + str_qs, closeElementId: 'htmlLightboxClose' }); 
		return false;
	});
	
	/* Onload, show or hide select option clear buttons on selection change*/
	show_hide_select_clears("event_id","clear_event_ids");
	show_hide_select_clears("album_id","clear_album_ids");	
	show_hide_select_clears("club_group_notice_id","clear_notice_ids");		
	
	/* Show or hide select option clear buttons on selection change */
	$('#event_id, #album_id, #club_group_notice_id').change(function() 
	{
		var bln_show = false;
		var str_select_element_id;
		var str_clear_element_id;

		//Get the relevant element ids
		str_select_element_id = $(this).attr('id');

		if (str_select_element_id == "event_id")
		{
			str_clear_element_id = "clear_event_ids";
		}
		else if (str_select_element_id == "album_id")
		{
			str_clear_element_id = "clear_album_ids";			
		}
		else if (str_select_element_id == "club_group_notice_id")
		{
			str_clear_element_id = "clear_notice_ids";						
		}

		//Show or hide the clear buttons
		show_hide_select_clears(str_select_element_id ,str_clear_element_id);		
	});	
	
	/* Clear the select options and hide the clear button */
	$('#clear_event_ids, #clear_album_ids, #clear_notice_ids').click(function() 
	{		
		var str_select_element_id;
		
		//Get the relevant element ids		
		if ($(this).attr('id') == "clear_event_ids")
		{
			str_select_element_id = "#event_id";
		}
		else if ($(this).attr('id') == "clear_album_ids")
		{
			str_select_element_id = "#album_id";
		}	
		else if ($(this).attr('id') == "clear_notice_ids")
		{
			str_select_element_id = "#club_group_notice_id";
		}	
		
		//Clear select list
		$(str_select_element_id).each(function()
		{
			$(str_select_element_id + ' option').removeAttr("selected");  			
		}); 

		//Hide clear button		
		show_hide_element(false,$(this).attr('id'));		
	});	

	/* Shows or hides a clear button depending on if there are any options selected in a given select field */
	function show_hide_select_clears(str_select_element_id, str_clear_element_id)
	{
		var bln_show = false;
		
		//Check if there are any selected options
		$('#' + str_select_element_id  + ' :selected').each(function(i, selected)
		{		
			bln_show = true;		
			return false;
		});
	
		//Show or hide the clear button
		show_hide_element(bln_show,str_clear_element_id);		
	}

	/* Show or hide an element by id */
	function show_hide_element(bln_show,str_element_id)
	{
		if (bln_show)
		{
			$("#" + str_element_id).show();			
		}
		else
		{
			$("#" + str_element_id).hide();			
		}
	}
});
