
$(document).ready(function(){$("#progressbar").progressbar({value:0});$('#btnNext').click(function(){nextStep();return false;});$('#btnPrev').click(function(){prevStep();return false;});$('#btnRemove').click(function(){removeStep();return false;});$('#btnKeep').click(function(){keepStep();return false;});$('#btnDefineIter').click(function(){showIterationDialog($(this));return false;});$('#btnChooseAlternative').click(function(){onChooseAlternative($(this));return false;});$('.taskIterative').live("click",function(){gotoStep($(this));});$('.taskOptional').live("click",function(){gotoStep($(this));});$('.taskAlternatives').live("click",function(){gotoStep($(this));});$("#iterationDialog").dialog({autoOpen:false,modal:true,resizable:false,width:550});$('#iterationDialog').dialog('option','buttons',{"Cancel":function(){$(this).dialog("close");},"OK":addIterations});$("#dlgChooseAlternative").dialog({autoOpen:false,modal:true,resizable:false,width:550});$('#dlgChooseAlternative').dialog('option','buttons',{"Cancel":function(){$(this).dialog("close");},"OK":onAlternativeChosen});$('#btnWizFinish').click(function(){showName();return false;});$('#findQBtn').click(function(){whereAmI();return false;});$('#findQ').slideUp(0);$('.taskToggle').live('click',function(){toggleTask($(this,'fast'));});});function toggleTask(ele,speed)
{if(ele.hasClass('taskOpen'))
closeTask(ele,speed);else
openTask(ele,speed);}
function closeTask(ele,speed)
{ele.removeClass('taskOpen');ele.addClass('taskClosed');ele.parent().children('.task').slideUp(speed);}
function openTask(ele,speed)
{ele.addClass('taskOpen');ele.removeClass('taskClosed');ele.parent().children('.task').slideDown(speed);}
function getNextStep(){var tsks=$('.taskOptional,.taskIterative,.taskAlternatives');var returnNext=false;var i=0;if($('#tailorDlg').parent().hasClass('projecTailor'))
{returnNext=true;}
var pass=0;do
{var checkTask=tsks.eq(i);var isRemoved=checkTask.parent().hasClass('optionalRemoved');if((returnNext)&&(!isRemoved))
{return tsks.eq(i);}
if(checkTask.hasClass('curSelTask'))
{returnNext=true;}
i++;if(i>=tsks.length)
{i=0;pass++;}}while(pass<2);return null;}
function getPrevStep(){var tsks=$('.taskOptional,.taskIterative,.taskAlternatives');var returnNext=false;var i=tsks.length-1;if($('#tailorDlg').parent().hasClass('projecTailor'))
{returnNext=true;}
var pass=0;do
{var checkTask=tsks.eq(i);var isRemoved=checkTask.parent().hasClass('optionalRemoved');if((returnNext)&&(!isRemoved))
{return tsks.eq(i);}
if(checkTask.hasClass('curSelTask'))
{returnNext=true;}
i--;if(i==-1)
{i=tsks.length-1;pass++;}}while(pass<2);return null;}
function currentStep()
{var r=$('.taskOptional,.taskIterative,.taskAlternatives',$('#tailorDlg').parent());if(r.length==0)
return null;else
return r.eq(0);}
function setProgress()
{var totalTasks=$('.taskOptional,.taskIterative,.taskAlternatives');var tskParent=$('#tailorDlg').parent().children('.taskOptional,.taskIterative,.taskAlternatives');hideWizFinish();if((tskParent.length==0)||(totalTasks.length==0))
{$('#progressText').html('');showWizFinish();return 100;}
else
{var curPos=(totalTasks.index(tskParent)+1);var bc='';$('#tailorDlg').parents('.task').each(function()
{var sep='';if(bc!='')
sep=" &gt; ";bc=$(this).children('.taskName').text()+sep+bc;});$('#progressText').html(bc+' ('+curPos+'/'+totalTasks.length+')');$('#progressbar').progressbar('option','value',(curPos*100)/totalTasks.length);if(curPos==totalTasks.length){showWizFinish();}}}
function showWizFinish(){$('#btnNext').addClass('ui-priority-secondary');$('#btnWizFinish').css('display','');}
function hideWizFinish(){$('#btnNext').removeClass('ui-priority-secondary');$('#btnWizFinish').css('display','none');}
function whereAmI()
{var selStep=currentStep();if(selStep==null)
{$('#tailorDlg').css('display','none');}
else
{gotoStep(selStep);}}
function nextStep()
{var selStep=getNextStep();if(selStep==null)
{$('#tailorDlg').css('display','none');}
else
{gotoStep(selStep);}}
function prevStep()
{var selStep=getPrevStep();if(selStep==null)
{$('#tailorDlg').css('display','none');}
else
{gotoStep(selStep);}}
function gotoStep(selStep)
{if(selStep.parent().parent().hasClass('optionalRemoved'))
return;$('.taskHi').removeClass('taskHi');selStep.parent().addClass('taskHi');$('#projectTailorProcess .curSelTask').removeClass('curSelTask');selStep.addClass('curSelTask');$('#tailorDlg').insertAfter($('.taskName',selStep.parent()).eq(0));$('#tailorDlg').css('display','');selStep.parents('.task').each(function()
{var vClosed=$(this).children('.taskClosed');if(vClosed.length>0)
toggleTask(vClosed.eq(0),0);});setProgress();$('#tailorNotes').html($('.taskNotes',selStep.parent()).eq(0).html());if(selStep.hasClass('taskOptional'))
{$('#optStep').css('display','');}
else
{$('#optStep').css('display','none');}
if(selStep.hasClass('taskIterative'))
{$('#iterStep').css('display','');}
else
{$('#iterStep').css('display','none');}
if(selStep.hasClass('taskAlternatives')){$('#optAlternatives').css('display','');}
else{$('#optAlternatives').css('display','none');}
$(window).scrollTop(selStep.position().top-100);}
function keepStep()
{var selStep=currentStep();selStep.parent().removeClass('optionalRemoved');selStep.parents('.task').removeClass('optionalRemoved');$('.task',selStep.parent()).removeClass('optionalRemoved');nextStep();}
function removeStep()
{var selStep=currentStep();selStep.parent().addClass('optionalRemoved');$('.task',selStep.parent()).addClass('optionalRemoved');nextStep();}
function defineIter()
{}
var iterTaskId;function showIterationDialog(selStep)
{var stp=selStep.parents('.task').eq(0);iterTaskId=stp.attr('taskid');var prepopName=stp.children('.taskName').text();$(".task[itertaskid='"+iterTaskId+"']").each(function()
{prepopName+='\n';prepopName+=$(this).children('.taskName').text();});$('#txtIterations').val(prepopName);$('#iterationDialog').dialog('open');}
function addIterations()
{var iterText=$.trim($('#txtIterations').val());var iters=iterText.split('\n');var tsk=$(".task[taskid='"+iterTaskId+"']");var tskParent=tsk.parents('.task');$(".task[itertaskid='"+tsk.attr('taskid')+"']",tskParent).remove();for(i=iters.length-1;i>0;i--)
{var iter=$.trim(iters[i]);if(iter.length!=0)
{copyTask(tskParent,tsk,iter);}}
onAddedIterations(iters,tsk);setProgress();$(this).dialog("close");nextStep();}
var altTaskId;var altStep;function onChooseAlternative(selStep){altStep=selStep.parents('.task').eq(0);altTaskId=altStep.attr('taskid');var dParams={altGroup:altStep.attr('altGrp')};$.ajax({type:'POST',contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(dParams),url:_reposRoot+'/ProcessDefinition.asmx/GetAlternatives',success:function(alternatives){onGetAlternatives(alternatives);}});}
function onGetAlternatives(alternatives){var options='';for(var i=0;i<alternatives.d.length;i++){options+='<option value="'+alternatives.d[i].ProcessId+'">'+alternatives.d[i].ProcessTitle+'</option>';}
$('#listAlternatives').html(options);$('#lblAltGroup').text(altStep.attr('altGrp'));$('#dlgChooseAlternative').dialog('open');}
function onAlternativeChosen(){var selStep=currentStep();$.ajax({type:'POST',contentType:"application/json; charset=utf-8",dataType:"json",data:"{'processId':"+$('#listAlternatives').val()+"}",url:_reposRoot+'/ProcessDefinition.asmx/GetProcessTaskHtml',success:function(alternative){onLoadedAlternative(alternative,selStep);}});}
function onLoadedAlternative(data,selStep)
{var loadedData=$(data.d);selStep.parent().children('.task').remove();selStep.parent().append(loadedData.children('.task'));selStep.parent().attr('pid',loadedData.attr('pid'));$('#dlgChooseAlternative').dialog('close');$('a',selStep.parent().children('.taskName')).text($('#listAlternatives :selected').text());nextStep();}
function copyTask(tskParent,tsk,iterName)
{var newTsk=tsk.clone();$('.tailorDlg',newTsk).remove();newTsk.children('.taskIterative').attr('class','taskNoMarker');newTsk.removeClass('taskHi');newTsk.attr('taskid',createTaskId());newTsk.insertAfter(tsk);newTsk.children('.taskName').children('a').text(iterName);if(newTsk.children('.taskName').children('a').length==0)
newTsk.children('.taskName').text(iterName);$('.task',newTsk).each(function()
{$(this).attr('taskid',createTaskId());});newTsk.attr('itertaskid',tsk.attr('taskid'));}
function createTaskId()
{var ret=[];var idChars="0123456789abcdef";for(var i=0;i<32;i++)
{ret[i]=idChars.substr(Math.floor(Math.random()*16),1);}
return ret.join("");}
function toggleOptional()
{var p=$(this).parent();p.toggleClass('optionalRemoved');}
function finish()
{var proj=$('#projectTailorProcess').clone();$('.tailorDlg',proj).remove();$('.taskNotes',proj).remove();var rootTask=$('#projectTailorProcess').children('.task').eq(0);var dProject={planName:$('#txtProjectPlanName').val(),projectInfo:getTask(rootTask)};$.ajax({type:'POST',contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(dProject),url:_reposRoot+'/ProcessDefinition.asmx/SaveTailoredProject',success:function(plankey){finished(plankey);}});}
function finished(plankey){if(plankey.d!=null){$('#saveProgress').html("Your <a href='./"+plankey.d+"/Home'>project</a> has been created. You may want to <a href='./"+plankey.d+"/Roles'>assign roles</a> now.");$('#btnFinish').stop(true,true);$('#btnFinish').hide();$('#btnGoProjHome a').attr('href',"./"+plankey.d+"/Home");$('#btnGoProjHome').show();$('#btnGoProjAssign a').attr('href',"./"+plankey.d+"/Roles");$('#btnGoProjAssign').show();}
else{showName();checkProjectName();}}
function getTask(ele)
{var newStep={name:ele.children('.taskName').text(),taskId:ele.attr('taskid'),processId:ele.attr('pid'),stepId:ele.attr('stepid'),isOptional:ele.children('.taskOptional').length>0,isIterative:ele.children('.taskIterative').length>0,isConcurrent:ele.attr('concurrent')=='true',isRemoved:ele.hasClass('optionalRemoved'),basedOnTaskId:ele.attr('itertaskid'),altGroup:ele.attr('altGrp'),notes:ele.children('.taskNotes').html()};newStep.steps=[];var i=0;var childTasks=ele.children('.task');for(i=0;i<childTasks.length;i++)
{var chTask=getTask(childTasks.eq(i));newStep.steps[newStep.steps.length]=chTask;}
return newStep;}