var responseString;
var responseObject;
var lFlows;
var pim;
var work;

function startFlow(){
	setTimeout(function(){
		responseString = $("#formBpmLog\\:lFlowsResponseJson").val();
		responseObject = eval("("+responseString+")");
		lFlows = responseObject.flows;
		pim=lFlows;			            		
		work = document.getElementById("work");
		if(responseString.length>0){
			showActualStatus();	
	    } 
	},1000);
}
function removeSigns(){
    if(work.sign){
        for(var i in work.sign){
            work.removeChild(work.sign[i]);
        }
    }
    work.sign=new Array();
    if(work.done){
        for(var i in work.done){
            work.removeChild(work.done[i]);
        }  
    }
    work.done=new Array();
}
function showActualStatus(){
    var pim = lFlows;
    removeSigns();	            	            
    if(pim[0].variables.tasksMetaData){
        for(var i in pim[0].variables.tasksMetaData){
         try{
            manageDoneTask(pim[0].variables.tasksMetaData[i]);
            }catch(e){continue;}       
        }						
    }
    for(var i in pim){
        manageFlow(pim[i]);
    }      
}
function showFlowDispatcher(){
	showFlow([{name:'pMetaId',value:this.pMetaId}]);
}
function showTaskDispatcher(){
	showTask([{name:'taskName',value:this.taskName}]);
}
function manageDoneTask(pTask){		            			
    if(!pTask.name){
      return;
    }
    var status=self.document.createElement("div");
    status.style.position="relative";
    status.style.left=(pTask.x)+"px";
    var aux=0;
    for(var i in work.sign){
        aux+=work.sign[i].height;
    }		
    for(var i in work.done){
        aux+=work.done[i].height;
    }
    status.style.top=(pTask.y-($("#formBpmLog\\:imageFlow")[0].offsetHeight+aux))+"px";
    status.style.width=(pTask.w)+"px";
    status.style.height=(pTask.h)+"px";
    status.className="done common";
    var a=self.document.createElement("a");
    a.href="#";
    status.taskName=pTask.name;
    status.onclick=showTaskDispatcher;
    status.task=pTask.tasksMetaData;
    a.appendChild(status);
    status=a;
    status.height=pTask.h;    
    work.done[work.done.length]=status;
    work.appendChild(status);   
}

function manageFlow(pFlow){
    var status=self.document.createElement("div");
    status.style.position="relative";
    status.style.left=(pFlow.x)+"px";
    status.pMetaId = pFlow.pmetaid;
    var aux=0;
    for(var i in work.sign){
        aux+=work.sign[i].height;
    }
    for(var i in work.done){
        aux+=work.done[i].height;
    }
    status.style.top=(pFlow.y-($("#formBpmLog\\:imageFlow")[0].offsetHeight+aux))+"px";
    status.style.width=(pFlow.w)+"px";
    status.style.height=(pFlow.h)+"px";    
    var a=self.document.createElement("a");
    a.href="#";
    if(pFlow.sub.length>0){
        status.className="sub common";      
        status.onclick=showFlowDispatcher;
        status.flow=pFlow.sub;
        status.activity=pFlow.activity;
    }else{
        status.taskMetaData=pFlow.taskMetaData;
        status.className="act common";
        //status.onclick=$(CN()).temp.showPendingTask;
    }
    a.appendChild(status);
    status=a;      
    status.height=pFlow.h;
    work.sign[work.sign.length]=status;
    work.appendChild(status);
}