// Remember the anchor's target, because we're going to wipe it out 
var popup_pharmacy_url;

$(function() {
	
	// Remember the anchor's href, since it's the target of the page load
	popup_pharmacy_url = $('a#popup_pharmacy_anchor').attr('href');
	
	// Turn off the regular click effects on the anchor
	$('a#popup_pharmacy_anchor').attr('href', '#').removeAttr('target');
		
	// Insert the popup div skeleton after the anchor 
	$('a#popup_pharmacy_anchor').after('<div id="popup_pharmacy" class="ns_popup"><a id="popup_pharmacy_reset">New Search</a><a id="popup_pharmacy_close">X</a><iframe id="popup_pharmacy_content"></iframe></div>');
		
	// On click of the anchor
	$('a#popup_pharmacy_anchor').click( function() { 

		// Hide all popup divs
		$('div.ns_popup').hide(); 
		
		// Scroll to the top of the site
		$('html, body').animate({scrollTop:0}, 'fast'); 
		
		// Clear the popup div
		$('iframe#popup_pharmacy_content').empty();
		
		// Load the content of the popup div
		display_pharmacy_search_form(); 
		
		// Show the popup div
		$('div#popup_pharmacy').show(); 
	});
	
	// Set up the close button's actions
	$('a#popup_pharmacy_close').click( function() {
		$('div#popup_pharmacy').hide(); 
	});
	
	// Set up the reset button's actions
	$('a#popup_pharmacy_reset').click( function() {
		display_pharmacy_search_form(); 
	});

});

/**
 * Fill the popup div's content with the search form
 * @return
 */
function display_pharmacy_search_form()
{	
	// Set the iframe src to force a refresh
	$('iframe#popup_pharmacy_content').attr('src', popup_pharmacy_url);
}
