// Do this on ready
$(document).ready(function() {

	// Add auto-postback to country drop down
	$("#set_country").change(function(){$.cookie('countrycode', $("#set_country").val(), { path: '/' }); $("#set_location").submit();});
	
	// Add events to hovermenu link
	$('#hoverlink a').mouseenter(function(){$('#hovernav').addClass('show');});
	$('#hoverlink a').mouseleave(function(){$('#hovernav').removeClass('show');});
	$('#hovernav').mouseenter(function(){$('#hovernav').addClass('show');});
	$('#hovernav').mouseleave(function(){$('#hovernav').removeClass('show');});
	
	// Add destination to postage info
	$('.destination').text($('#set_country :selected').text());
	
	// Loop through each listing
	$('.listing').each(function() {
		var mydialog = $(this).find('.dialog');
		mydialog.dialog({autoOpen: false});
		var mainlink = $(this).find('.lpl a:first');
		$(this).find('.lpl a').click(function() {
			$('.dialog').each(function() {$(this).dialog('close')}); // Close any other dialogs
			var offset = $(mainlink).offset();
			var newpos = new Array(1);
			newpos[0] = offset.left;
			newpos[1] = offset.top + $(this).height() - $(window).scrollTop();
			mydialog.dialog('option', 'position', newpos);
			mydialog.dialog('open');
		});
	});
	
});