

window.addEvent('load', function(){
 initValidateBoxes();
    $$(".checkbox").each(function(elem,index){
        var cb=new CheckBox(elem);
        elem.cb=cb;
       cb.addEvent("eventCheckBoxStateChanged",eventCheckBoxChanged);
    });
   // new UvumiScrollbar("scrollbar1");
    $('back-button').addEvent('click',eventBackClicked);
    $('next-button').addEvent('click',eventNextClicked);
    
    // prepare validate box for fadein ..
     $$(".Validate")[0].hide();
     $$(".Validate")[0].fade("out");
    //close validate box
     $$('.close-val').addEvent('click', function(){
        $$('.Validate').fade("out");
     });
     
     $$('.step1 .more').each(function (elem,index){
        var newFx=new  Fx.Slide(elem);
        elem.slider=newFx;
        newFx.hide();
     });
  placeFlashBG(); 
});

function runMain() {

    (function() {
        var topMenu = $('main-header').getElement('.top-menu');
        topMenu.set('opacity', 0);
        topMenu.show();
        topMenu.fade('in');
        var opaqBgLoader = new Asset.image("Resources/Global/Images/Bg/wizard_box_bg.png");
        var theBox = $('welcome-box');
        theBox.set('opacity', 0);
        theBox.show();
        theBox.fx = new Fx.Morph(theBox, { duration: 700, transition: Fx.Transitions.Sine.easeInOut });
        theBox.fx.start({ "left": 140, "opacity": 1 }).chain(function() {
            var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
            if (!IE6)
                $('welcome-box').addClass('withOpaque');
            var step1 = $('welcome-box').getElement(".step-holder.step1");
            step1.set('opacity', 0);
            step1.show();
            step1.fx = new Fx.Morph(step1, { duration: 700, transition: Fx.Transitions.Sine.easeInOut });
            step1.fx.start({ "opacity": 1 })
        });
    }).delay(1000);
}

function placeFlashBG(){

	var i = (location.href).lastIndexOf( ('/'));
	var currPath=location.href.substring(0, i);
    var flashvars = {};
	flashvars.path = currPath;
	flashvars.xmlPath = currPath+"/handler/GetCardsXml.ashx";
	var params = {};
	params.wmode="opaque";
	var attributes = {};
	attributes.id = "bg";
	swfobject.embedSWF(currPath + "/Resources/Global/Flash/hr.swf", "bg", 1000, document.body.clientHeight, "9.0.0", false, flashvars, params, attributes);

}

function eventCheckBoxChanged(cbTarget){
   var cb=cbTarget;
   var comboId=cb.id;
   
   // handle mangerial question updates on grid
   if ((comboId=="managerialYes" || comboId=="managerialNo")){
        //handle mangerial "radio Group"
        if (cb.getStatus()==true){
            if (comboId=="managerialYes"){
                $('managerialNo').cb.unCheck();
                $$("#step2 .checkbox.managerialYes").each(function(elem,index){
                    elem.cb.enableBox();
                });
                $$("#step2 .checkbox.managerialNo").each(function(elem,index){
                    if (!elem.hasClass('managerialYes'))
                        elem.cb.disableBox();
               });
            }
            if (comboId=="managerialNo"){
                $('managerialYes').cb.unCheck();
                 $$("#step2 .checkbox.managerialYes").each(function(elem,index){
                    elem.cb.disableBox();
                });
                $$("#step2 .checkbox.managerialNo").each(function(elem,index){
                    elem.cb.enableBox();
               });
            }
        }
        else
        {
         $$("#step2 .checkbox.managerialNo").each(function(elem,index){
            elem.cb.enableBox();
         });
         $$("#step2 .checkbox.managerialYes").each(function(elem,index){
            elem.cb.enableBox();
         });
        }
    return;
   }
   
   
   
   var moreCombos=$('cb_more_'+comboId);
  
   if (moreCombos){
        if(cb.getStatus()){
        // open more and check the first box (All items checkbox)
            moreCombos.slider.slideIn();
            moreCombos.getElements(".checkbox")[0].cb.check();
            }
        else{
            moreCombos.slider.slideOut();
            //remove all checks 
            moreCombos.getElements(".checkbox").each(function(elem,index){
            elem.cb.unCheck();
            });
            }
            
        }
        
}

function eventBackClicked(e){
    if (currentStep==1){
        return;
        }
    $$('.wizard-header')[0].removeClass('step'+currentStep);
    $$('.step'+currentStep)[0].addClass('hidden');
     removeScroller(currentStep);
    currentStep=currentStep-1;
     $$('.step'+currentStep)[0].removeClass('hidden');
    $$('.wizard-header')[0].addClass('step'+currentStep);
    
    if (currentStep==1)
         $('back-button').fade("out");
    
}

function eventNextClicked(e){
    $$(".wizard .Validate")[0].setStyle('display','block');
    $$(".wizard .Validate")[0].fade("out");
    $$(".wizard .errMsg")[0].set('html',"");
    
    var idList=getCheckBoxIds('.step'+currentStep);
    
    if (idList=="")
        {
            if (currentStep==1)
                $$(".errMsg")[0].set('html',"Please choose at least one location.");
            if (currentStep==2)
                 $$(".errMsg")[0].set('html',"Please choose at least one category.");
            if (currentStep==3)
                 $$(".errMsg")[0].set('html',"Please choose at least one position.");
            $$(".wizard .Validate").show();
            $$(".wizard .Validate").fade("in");
            return false;
        }
        
     // now go to next step
    if (currentStep!=3)
        goToStep(idList,currentStep+1);
    else{
        var locationList=getCheckBoxIds('.step1');
        var categoryList=getCheckBoxIds('.step2');
        jumpToSearchResults(locationList,categoryList,idList);
        }
    return;
    
}

function jumpToSearchResults(locationList,categoryList,idList){
   location.href = "searchResults.aspx?locations="+ locationList+"&categories="+categoryList+"&jobIds="+idList;
}

function removeScroller(holderID){
 /*   $$('.step'+holderID+' .bar').each(function (elem,index){
        elem.destroy();
    });
    var stepHolder=$$('.step'+holderID+' .step-holder')[0];
    var barHolder=$$('.step'+holderID+' .scrollBar')[0];
    if (barHolder){
     barHolder.erase("style");
     barHolder.erase("class");
     barHolder.empty().adopt(stepHolder);
     barHolder.set("class","steps-scrollbar");
     barHolder.set("id","scrollbar"+holderID);
    }
    */
    //designed scroller has been disabled for now ...
    
}

function goToStep(idList,newStep){
    $$('.wizard-header')[0].removeClass('step'+currentStep);
    $$('.step'+currentStep)[0].addClass('hidden');
    currentStep=newStep;
    $$('.step'+currentStep)[0].removeClass('hidden');
    $$('.wizard-header')[0].addClass('step'+currentStep);
    $$('#step'+currentStep)[0].empty();
    ajaxLoadStep(idList,currentStep);
    $('back-button').fade("in");
}

var ajaxRequest;

function ajaxLoadStep(idList,newStep){
	var url = location.href;
	if(ajaxRequest)
	    {
	        try{
	            ajaxRequest.cancel();
	        }
	        catch(e){
	        }
	    }
	$$('#step'+currentStep)[0].set('html',"<div class='ajaxLoader'><img src='Resources/Global/Images/ajaxPreloader.gif' width='170' height='139'/></div>");
	
	//for the 2rd step - get the locations list as well
	var locationList="";
	var isManagerial=-1;
	if ($('managerialYes').cb.getStatus()==true)
	    isManagerial=1;
	if ($('managerialNo').cb.getStatus()==true)
	    isManagerial=0;
	if (newStep==3)
	    {
	        locationList=getCheckBoxIds('.step1');
	    }
	//alert(locationList+'--'+idList);
	ajaxRequest = new Request({
            method: 'get',
            url: url,
           data: { 'ajaxCall' : 'true','idList':idList,'step':newStep,'locationList':locationList,'managerial':isManagerial },
 //           onRequest: function() { alert('Request made. Please wait...'); },
            onComplete: eventStepLoaded
        }).send();
}

function eventStepLoaded(response){
//inject content to holder
$$('#step'+currentStep)[0].set('html',response);
//activate checkbox's
$$("#step"+currentStep+" .checkbox").each(function(elem,index){
        var cb=new CheckBox(elem);
        elem.cb=cb;
});
// new UvumiScrollbar("scrollbar"+currentStep);
}


function getCheckBoxIds(holder){
    var idList="";
    $$(holder+" .checkbox").each(function (elem,index){
        if(elem.cb && elem.cb.getStatus()==true && !isNaN(parseInt(elem.cb.id))){
            if (idList!="") 
                idList+=",";
            idList+=parseInt(elem.cb.id);
            }
    });
    return idList;
   
}

var currentStep=1;

function doCheckAll(){
    checkAll(".step"+currentStep);
}

function doUnCheckAll(){
    unCheckAll(".step"+currentStep);
}

function checkAll(holder){
    $$("#"+holder+" .checkbox").each(function(elem,index){
        var comboId=elem.cb.id;
        if (comboId=="managerialYes" || comboId=="managerialNo")
            return;
        elem.cb.check();
    });
}

function unCheckAll(holder){
    $$("#"+holder+" .checkbox").each(function(elem,index){
        var comboId=elem.cb.id;
        if (comboId=="managerialYes" || comboId=="managerialNo")
            return;
        elem.cb.unCheck();
    });
}
