/**
 * @author lukas
 */

if(typeof LUM == 'undefined') var LUM={};
if(typeof LUM['module'] == 'undefined') LUM.module={};


LUM.module.publicSendLinkToAFriend = function()
{
	
	var pub={}; // public interfae
	pub.temp={}; // temporary saves
	
	var _sUpdateLink; // private variable STRING	
	
	var _bBlocked=false; // privat "semafor"
	
	var _jDialog = false; // form dialog
	
	pub.init = function()
	{			
		_initDialog();
	};
			
	function _updatePartCompleteXml (xResponse)
	{
		// CHECK RESULT IF OK
		var sResult=$("response>result",xResponse).text();

		if(sResult!="1")
		{
			LUM.Common.Ajax.procesErrorXml($("response>error",xResponse));
			return;
		}
								
		// ALL
		var xDialog=$("response>data>all",xResponse);
		if(xDialog.size()!=0)
		{
			_jDialog.html(xDialog.text());
			pub.initFrienLinkForm();
		}

	}		
		
	//////////////////////////////////////////////////////////////
	// DIALOG
	/////////////////////////////////////////////////////////////
	
	function _initDialog()
	{
		if(!_jDialog)
		{
			_jDialog=$("#dialog-sendlink");

			_jDialog.dialog({
				resizable:false,
				modal:true,
				width:440,
				height:340,
				dialogClass: 'sendlink-dialog',
				autoOpen:false, 
				dialogopen:false,															
				close : _dialogClosed,
				bgiframe: true							
			});
		}
	}

	function _dialogClosed()
	{		
		_jDialog.html("");
	}

	pub.openDialog = function(title, width, height, url)
	{
		if(_bBlocked)
			return;

		_initDialog();

		_jDialog.html("<div>"+LUM.Common.Util.getLoadingPicHtml()+"</div>");
		_jDialog.dialog("open");


		_jDialog.data('title.dialog', 'Send link to a friend');
		if(width != 0) _jDialog.data("width.dialog", width);
		if(height != 0) _jDialog.data("height.dialog", height);
        _jDialog.data('position.dialog', 'center');

        var reqUrl = url;
        
        var oPostData = {
        	part : 'all'        	
       	}
                
		$.post(reqUrl, oPostData, _updatePartCompleteXml, "xml");		
	}
	
	//////////////////////////////////////////////////////////////
	// FORM SEND
	/////////////////////////////////////////////////////////////
		
	
	pub.initFrienLinkForm = function () {		
		
		var jForm = $('#application_friend_link');

		$('#seo_link').val(window.location.href);

		jForm.validate(
			 {
				rules: {
					sender : {
						required : true,
						email : true
					},
					recipient : {
						required : true,					
						email : true
					},
					subject : {
						required : true						
					},
					text : {
						required : true						
					}										
				},
				messages: {
					sender : {
						required : "Required",
						email : "Wrong format"
					},
					recipient : {
						required : "Required",
						email : "Wrong format"
					},
					subject : {
						required : "Required"
					},
					text : {
						required : "Required"
					}										
				},
				errorClass: "invalid",
				
				submitHandler : function(form)
				{
					if(_bBlocked)
						return;
						
					_bBlocked=true;
													
					var sUrl="/ibase.php?_server=luminis&_pack=luminis&_action=send_link_to_a_friend";
					
					var oOptions = { 
						url : sUrl, 
						dataType : 'json',
						type : 'POST',
						success : _frienLinkFormComplete
					};
					
					$(form).ajaxSubmit(oOptions);
					
					$("#application_friend_linkResponse").addClass('form-processing');
					$("#application_friend_linkResponse").html('<p>Sending form data.</p>');
				}
			}		
		);					
	}
	
	function _frienLinkFormComplete ( oResult ) {
		
		_bBlocked=false;
		
		$("#application_friend_linkResponse").removeClass("form-processing");
		
		if(oResult.result)
		{
			$("#application_friend_linkResponse").addClass('form-sent');
			$("#application_friend_linkResponse").html('<p>Form successfully sent.</p>');
			
		}else
		{
			$("#application_friend_linkResponse").addClass('form-error');
			$("#application_friend_linkResponse").html('<p>Sending form failed.</p>');							
		}
		
		window.setTimeout(function(){
			$("#application_friend_linkResponse").html("");
			$("#application_friend_linkResponse").removeClass("form-processing");
			$("#application_friend_linkResponse").removeClass("form-sent");
			$("#application_friend_linkResponse").removeClass("form-error");
		},5000);			
	}
	
	//////////////////////////////////////////////////////////////
	// RETURN
	/////////////////////////////////////////////////////////////
	
	return pub;

}();

