$(document).ready(function() 
{
	// Intercept links to www.pharmacy4u.com.au, rework them to open the store selector instead
	$("#main").delegate("a","click",function(event)
	{
		var str= $(this).attr('href');
		var result = str.search("pharmacy4u.com.au");

		if(result != -1)
		{
			event.preventDefault();
			str = str.replace("http://www.pharmacy4u.com.au","");
			str = str.replace("http://pharmacy4u.com.au","");
			storeSelector(str);
		}
	})	
});

function storeSelector(url)
{
	// Fires off a lightbox with a store selector
	$.fn.colorbox(
	{
		inline:true,
		href:"#popup_selection",
		opacity: 0.7,
		onOpen: function(event)
		{
			// Reworks clicks from the lightbox to append the url we need to go to
			$('#popup_selection').delegate("a","click",function(event)
			{
				var linkUrl = $(this).attr('href');
				linkUrl += url;
				$(this).attr('href',linkUrl);
			});
		},
		onCleanup: function()
		{
			$('#popup_selection').undelegate();
		}
	});
}
