function dialog_sendLink_showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

function dialog_sendLink_hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

var var_modalboxDomain = null;

/* this was previously attached to the close button, seems like it works also with hide(), leaving here for lookbacks in case of buggy behaviour */
//var hideObserver = Modalbox.hide.bindAsEventListener(Modalbox);

var nod_sendLink = document.getElementById('modalbox_sendlink');

function dialog_sendLink_setObservers ()
{
	$('modalbox_sendlink_send').observe('click', function() { sendLink(document.location); });
	$('modalbox_sendlink_close').observe('click', function() { Modalbox.hide(); dialog_sendLink_showFlash(); });
}
function dialog_sendLink_removeObservers ()
{
	$('modalbox_sendlink_send').stopObserving('click', function() { sendLink(document.location); });
	$('modalbox_sendlink_close').stopObserving('click', function() { Modalbox.hide(); dialog_sendLink_showFlash(); });
}

function dialog_sendLink ()
{
	dialog_sendLink_hideFlash();
	
	Modalbox.show(
		nod_sendLink,
		{
			title: 'Poslať linku e-mailom',
			afterLoad: dialog_sendLink_setObservers,
			beforeHide: dialog_sendLink_removeObservers
		}
	);
}

function sendLink (uri)
{
	var nod_sendLinkINotfilled = $('sendLinkINotfilled');
	var nod_sendLinkISuccess = $('sendLinkISuccess');
	var nod_sendLinkIFailure = $('sendLinkIFailure');
	var nod_sendLinkILoading = $('sendLinkILoading');
	var nod_sendLinkContent = $('modalbox_sendlink_content');
	
	var nod_sendLinkFrom = $('sendLinkFrom');
	var nod_sendLinkEmail = $('sendLinkEmail');
	var nod_sendLinkMessage = $('sendLinkMessage');
	
	nod_sendLinkINotfilled.hide();
	nod_sendLinkISuccess.hide();
	nod_sendLinkIFailure.hide();
	
	if(!nod_sendLinkFrom.value || !nod_sendLinkEmail.value || !nod_sendLinkMessage.value ){
		nod_sendLinkINotfilled.show();
		Modalbox.resizeToContent();
		return false;
	}
	
	nod_sendLinkILoading.show();
	
	Modalbox.resizeToContent();
	
	new Ajax.Request(
		var_modalboxDomain + '/xml/send_link',
		{
			method: 'post',
			parameters: {
				'uri': uri+'',
				'from_name': nod_sendLinkFrom.value,
				'to_email': nod_sendLinkEmail.value,
				'message': nod_sendLinkMessage.value,
			},
			onSuccess: function(transport){
				nod_sendLinkILoading.hide();
				
				try
				{
					//var response = transport.responseText || "no response text";
					//alert("Success! \n\n" + transport.responseXML);
				  
					var tmpResponse = transport.responseXML;
					if (!tmpResponse.documentElement && xmlhttp.responseStream) {
						tmpResponse.load(xmlhttp.responseStream);
					}else{
						tmpResponse.documentElement;
					}
				  
					tmpResponse = tmpResponse.firstChild;
				  
					if(tmpResponse)
					{
						var tmpResult = tmpResponse.getElementsByTagName("result")[0];
					 
						if(tmpResult.firstChild.nodeValue=='true')
						{
							nod_sendLinkISuccess.show();
							nod_sendLinkContent.hide()
							$('modalbox_sendlink_send').hide();
						}
						else
						{
							nod_sendLinkIFailure.show();
						}
					}
				}
				catch(e)
				{
					nod_sendLinkIFailure.show();
				}
				finally
				{
					Modalbox.resizeToContent();
				}
			},
			onFailure: function()
			{
				nod_sendLinkILoading.hide();
				
				nod_sendLinkIFailure.show();
				Modalbox.resizeToContent();
			}
		}
	);
}