function getExchange(url, currency, target)
{
	jQuery(document).ready(function()
	{
		jQuery.ajax({
			url: url,
			type: 'POST',
			data: 'currency='+currency,
			beforeSend: function(XMLHttpRequest)
			{
				jQuery('#'+target).html('<img src="scripts/indicator.gif" />');
			},
			complete: function(XMLHttpRequest, textStatus)
			{
				if(XMLHttpRequest.responseText.length > 0)
					jQuery('#'+target).html(XMLHttpRequest.responseText);
				else
					jQuery('#'+target).html('Not Found');
			}
		});
	});
}

jQuery(document).ready(function()
{
	jQuery('body').mouseleave(function()
	{
		createPopupDialog('/popup.html', {
			width: '300px',
			height: '255px'
		});
	});
});

/**
* 
*/

function createPopupDialog(url, params)
{
	if(!popupDialogCookie()) return false;
	
	var time = (new Date()).getTime();
	var _dimmer = 'nhDimDialog_'+time;
	var _dialog = 'nhDialog_'+time;
	
	jQuery.ajax({
		url: url,
		cache: false,
		success: function(data, textStatus, jqXHR)
		{
			jQuery('body').append('<div id="'+_dimmer+'" style="display: none;"></div>');
			jQuery('#'+_dimmer).css({
				position: 'fixed',
				width: '100%',
				height: '100%',
				backgroundColor: '#fff',
				left: 0,
				top: 0,
				zIndex: 999
			});
			
			jQuery('#'+_dimmer).after('<div id="'+_dialog+'" style="display: none;"></div>');
			
			jQuery('#'+_dialog).html(data).css(params);
			jQuery('#'+_dialog).css({
				position: 'absolute',
				top: '50%',
				left: '50%',
				marginTop: -(jQuery('#'+_dialog).height()/2),
				marginLeft: -(jQuery('#'+_dialog).width()/2),
				border: '1px solid #999',
				backgroundColor: '#fff',
				overflow: 'auto',
				zIndex: 1000,
				boxShadow: '0 5px 20px #333',
				mozBoxShadow: '0 5px 20px #333',
				webkitBoxShadow: '0 5px 20px #333',
				borderRadius: '6px',
				width: jQuery('#'+_dialog).width() + 0,
				height: jQuery('#'+_dialog).height() + 4,
				padding: '6px'
			});
		},
		complete: function(jqXHR, textStatus)
		{
			jQuery('#'+_dimmer).fadeTo(300, 0.9, function()
			{
				jQuery('#'+_dialog).show();
			});
		}
	});
}

function closePopupDialog(button)
{
	var dialog = jQuery(button).parentsUntil('div[id^="nhDialog_"]').parent();
	var uniqueID = dialog.attr('id').split('_');
	uniqueID = uniqueID[uniqueID.length-1];
	
	jQuery('#nhDialog_'+uniqueID).hide().queue(function()
	{
		jQuery('#nhDimDialog_'+uniqueID).fadeOut('slow', function()
		{
			jQuery(this).remove();
		});
		
		jQuery(this).dequeue().remove();
	});
	
	return false;
}

function popupDialogCookie()
{
	//	return true, display popup
	
	var popupCookieName = 'nhPopupDialog';
	var cookies = document.cookie.split(';');
	var cookieFound = false;
	
	//	try to get cookie first. If found, return false
	for(var index in cookies)
	{
		var cookie = cookies[index];
		var cookieName = cookie.substr(0, cookie.indexOf('='));
		var cookieValue = cookie.substr(cookie.indexOf('=')+1);
		cookieName = cookieName.replace(/^\s+|\s+$/ig, '');
		
		if(cookieName == popupCookieName)
		{
			cookieFound = true;
			break;
		}
	}
	
	if(!cookieFound)
	{
		//	set the new cookie
		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate()+1);
		var cookieValue = escape('enable') + '; expires=' + expireDate.toUTCString();
		document.cookie = popupCookieName + '=' + cookieValue;
		
		return true;
	}
	
	return false;
}

function clearPopupDialogCookie()
{
	var popupCookieName = 'nhPopupDialog';
	var expireDate = new Date();
	expireDate.setDate(expireDate.getDate()-365);
	var cookieValue = escape('disable') + '; expires=' + expireDate.toUTCString();
	document.cookie = popupCookieName + '=' + cookieValue;
}

