
/**
 * Arrow response reader
 */
ArrowResponseReader = {
		
	successCallback: null,
	exceptionCallback: null,
	returnErrorCallback: null,
	errorCallback: null,
	afterInteractionCallback: null,
		
	clearCallbacks:function(){
		ArrowResponseReader.successCallback = null;
		ArrowResponseReader.exceptionCallback = null;
		ArrowResponseReader.returnErrorCallback = null;
		ArrowResponseReader.errorCallback = null;
	},
	
	read:function( data ){
		ArrowResponseReader.clearCallbacks();
		var r = data.response;
		
		if(data.successCallback)
			ArrowResponseReader.successCallback = data.successCallback;
		if(data.exceptionCallback)
			ArrowResponseReader.exceptionCallback = data.exceptionCallback;
		if(data.returnErrorCallback)
			ArrowResponseReader.returnErrorCallback = data.returnErrorCallback;
		if(data.errorCallback)
			ArrowResponseReader.errorCallback = data.errorCallback;
		
		//if( r + "" == "[object XMLDocument]"){
		//obejscie dla uploadowania plikow
		if(r === null)
			ArrowResponseReader.successCallback(r);
		if( typeof( r ) == 'object' || typeof( r ) == 'Array' ){
			if( r.interaction ){
				if(ArrowResponseReader.afterInteractionCallback == null)
					ArrowResponseReader.afterInteractionCallback = ArrowResponseReader.successCallback;
				ArrowInteraction._do(r.interaction);
			}else if( r.errormsg ){
				//var msg = $("errormsg", r).text()+ "\n" + 
				//$("file", r).text() +" ("+
				if(ArrowResponseReader.exceptionCallback == null)
					alert(r.errormsg);
				else
					ArrowResponseReader.exceptionCallback( r.errormsg );
			}else if( r.error ){
				if(ArrowResponseReader.returnErrorCallback == null)
					alert(r.error);
				else
					ArrowResponseReader.returnErrorCallback(r.error);
			}else if( r.arrowerror ) {
				if( r.arrowerror.errormsg.msg != undefined ) {
					var err = r.arrowerror.errormsg.msg + "file: " + r.arrowerror.file + "\nline: " + r.arrowerror.line ;
				} else {
					var err = r.arrowerror.errormsg ;
				}
					if(ArrowResponseReader.returnErrorCallback == null)
						alert(err);
					else
						ArrowResponseReader.returnErrorCallback(err);
			}else if( r.arrowerr ) {
					var err = r.arrowerr ;
					if(ArrowResponseReader.returnErrorCallback == null){
						alert(r.arrowerr.msg + "\n" + r.file + "\nLine: " + r.line);
					}else
						ArrowResponseReader.returnErrorCallback(err);
			}else if( r.singleResponse && r.singleResponse['err-list'] ) {
				var err = $("singleResponse", r).text() ;
				var start = 0 ; 
				if( ( start = err.indexOf( 'err-list-' ) ) != -1 ) {
					err = err.substring( 9 );
					if(ArrowResponseReader.returnErrorCallback == null){
						alert(err);
					}else
						ArrowResponseReader.returnErrorCallback( "<pre>" + err + "</pre>" );
				}
			}else if( r.msg ) {
				alert(r);
				if(ArrowResponseReader.returnErrorCallback == null)
					alert(r.msg + "\nfield:" + r.field + "\nclass:" + r["class"] );
				else
					ArrowResponseReader.returnErrorCallback(r);
			}else{
				if(ArrowResponseReader.afterInteractionCallback != null)
					ArrowResponseReader.afterInteractionCallback(r);
				
				if(ArrowResponseReader.successCallback != null)
					ArrowResponseReader.successCallback(r);
			}
		}else{
			alert("error");
			if(ArrowResponseReader.errorCallback == null)
				alert(r);
			else
				ArrowResponseReader.errorCallback("<pre>" + r + "</pre>");
		}
		
	}
		
};


var ArrowInteraction = {
	contextUrl: null,	
	pageMethod:function( funcName, params, callback ){
		//templateMethod
		var url = ArrowInteraction.contextUrl?ArrowInteraction.contextUrl:window.location.href;
		if(url.indexOf("#") != -1)
			url = url.split("#")[0];
		
		if(url.indexOf("?") == -1)
			url+="?templateMethod="+funcName;
		else
			url+="&templateMethod="+funcName;
		
		var sendData = {};
		for(i in params)
			sendData["methodCallData["+i+"]"] = params[i];
		$.post(url, sendData,
			function(res){
				ArrowResponseReader.read({response: res, successCallback: callback});
			},
			 "json"
		);
	},
		
	_do:function( message ){
		if(ArrowInteraction["_action_"+message.action]){
			ArrowInteraction["_action_"+message.action](message);
		}else{
			alert( "ArrowInteraction: '"+ message.action + "' not implemented" );
		}
	
	},
	
	postBack: function( message, data ){
		$.ajax({
			url: message.redirect,
			data: { "arrowInteractionData": data },
			success: function(res){
				ArrowResponseReader.read({response: res});
			},
			dataType: "json"
		});
	},

	_action_info:function(message){
		DialogRenderer.renderInfo(message.title, message.message, function(){  
			if(message['break'] == 1)
				return;
			ArrowInteraction.postBack(message, {});
		});

	},
	
	_action_error:function(message){
		DialogRenderer.renderError(message.title, message.message, function(){  
			if(message['break'] == 0)
				ArrowInteraction.postBack(message, {});
		});
	},
	
	_action_confirm:function(message){
		DialogRenderer.renderConfirm(message.title, message.message, function(result){  
			if(message['break'] == 1 && result == false)
				return;
			ArrowInteraction.postBack(message, result);
		});
	},
	
	_action_oneInput:function(message){
		DialogRenderer.renderOneInput(message.title, message.message, function(result){  
			ArrowInteraction.postBack(message, result);
		});
	},
	
	_action_refreshControl:function(message){
		CtrlAjax.reload(message.ctrlId);
		if(message['break'] == 0)
			ArrowInteraction.postBack(message, {});
	},
	
	_action_setControlStateVars:function(message){
		CtrlAjax.changeStateVars(message.ctrlId, message.stateVars, message.getVars );
		if(message['break'] == 0)
			ArrowInteraction.postBack(message, {});
	}	
	
	
		
} 

var DialogRenderer = {
		
	renderError:function(title, message, callback){
		var popup = DialogRenderer.renderBox(title);
		popup.addClass("error-popup");
		popup.find(".buttons-panel").append( '<div class="button dialog-ok">OK</div>' );
		popup.find('.content').append(message);
		popup.find(".dialog-ok").click(function(){ callback(); popup.parent().find(".error-shadow,.dialog-popup").remove() });
	},
	
	renderInfo:function(title, message, callback){
		alert(message);
		return;
		var popup = DialogRenderer.renderBox(title);
		popup.find(".buttons-panel").append( '<div class="button dialog-ok">OK</div>' );
		popup.find('.content').append(message);
		popup.find(".dialog-ok").click(function(){ callback(); popup.parent().find(".error-shadow,.dialog-popup").remove() });
	},
	renderConfirm:function(title, message, callback){
		var popup = DialogRenderer.renderBox(title);
		popup.find('.content').append(message);
		popup.find(".buttons-panel").append( '<div class="button dialog-cancel">Nie</div>' );
		popup.find(".buttons-panel").append( '<div class="button dialog-ok">Tak</div>' );
		popup.find(".dialog-ok").click(function(){ callback(true); DialogRenderer.hideBox(); });
		popup.find(".dialog-cancel").click(function(){ callback(false);  DialogRenderer.hideBox();  });
	},
	
	renderOneInput:function(title, message, callback){
		var popup = DialogRenderer.renderBox(title);
		popup.find('.content').append(message);
		popup.find('.content').append('<br /><br /><input type="text" class="single-input" />');
		popup.find(".buttons-panel").append( '<div class="button dialog-cancel">Anuluj</div>' );
		popup.find(".buttons-panel").append( '<div class="button dialog-ok">OK</div>' );
		popup.find(".dialog-ok").click(function(){ callback(popup.find(".single-input").val()); DialogRenderer.hideBox(); });
		popup.find(".dialog-cancel").click(function(){  DialogRenderer.hideBox();  });
	},
		
	hideBox:function(){
		$("body").find(".error-shadow,.dialog-popup").remove()
	},
	
	renderBox:function( title, content, callback ){
	 	var cont = $("body");
		if(cont.find(".error-shadow").length > 0 )
			cont.find(".error-shadow").remove();
		
		var shadow = $('<div class="error-shadow"></div>');
		shadow.css("background-image", "none");

		var width = cont.width();
		if( width != null )
			shadow.width(width);
		var height = cont.height();
		if( height != null )
			shadow.height(height);
		
		shadow.hide();
		cont.append( shadow );
		shadow.show();
		var titleTxt = "";
		if(title)
			titleTxt = '<div class="dialog-info">'+title+'</div>';
			
		var popup = $('<div class="dialog-popup">'+titleTxt+'<div class="content"></div><br /><br /><div class="buttons-panel" style="clear: both;" align="center"></div></div>').appendTo(cont).css({
			left: ((cont.width() - $(".dialog-popup").width()) /2)+"px",
			top: ((cont.height() - $(".dialog-popup").height()) /2)+"px"
		});
		
		
		return popup;
		
	}
		
}
