$().ready(function(){
    //JobSearchCriteria.RegisterAjaxCombos();
    JobSearchResults.init();//RegisterAjaxCombos();
});

var defaultVar='';

JobSearchResults= {

    init:function(){
       JobSearchResults.setEventHandlers(); 
       //JobSearchResults.setEventHandlerPredicitive();
   },
   
   
   setEventHandlers:function(){
     //defaultVar=$(".JSCurrentSearch")[0].value; 
     
     $(".lnkEmailAlerts").unbind().click(JobSearchResults.clickAlertSubscribe);
     
      
     ///$(".JSSectors").unbind().click(JobSearchResults.showHideDiv);
     //$(".JSCurrentSearch").unbind().focus(JobSearchResults.hideDefaultValue);
     //$(".JSButtonGo").unbind().click(JobSearchResults.search);
     //$(".JSCurrentSearch").blur(JobSearchResults.showDefaultValue);
   },
   
   clickAlertSubscribe:function(){
        
        var _filters=JobSearchResults.getFilters();
        window.showModalDialog(this.href+"?"+_filters,'','dialogWidth:320px;dialogHeight:260px;center:1;status:yes;unadorned:no;scroll:no;');
        //window.open(this.href+"?"+_filters,'Alert','width=300px,height=250px,resizable=0,toolbars=0');
        return false;
   },

   showHideDiv:function(){
      $(".SectorsItemRepeater").toggle(200);
    },
   
   hideDefaultValue:function(){
        $(".JSCurrentSearch")[0].value="";
   },
   
   /*showDefaultValue:function(){
        $(".JSCurrentSearch")[0].value=defaultVar;
   }*/
   
   search:function(){
        var filters="";     
        var keywords=$(".JSCurrentSearch");
        filters+="keywords=";
        filters+=keywords[0].value;
        LoadResultsAjax(filters);
   },
   
   //Pass string as cookie to Alert PopUp
   getFilters: function() {
        var contractTypes=$(".contractType label input");
        var filtersSearch="";               	                	                
        var j=0;
        
        if($(".main")[0]==undefined){
            filtersSearch=window.location.search.substring(1);
            return filtersSearch;
        }
        
        //Check if exists contract types selected
        for (i=0;i<contractTypes.length;i++){
            if(contractTypes[i].checked){
                if (j==0){
                    filtersSearch="contractTypes=";
                }
                else{
                    filtersSearch+="+";
                }
                filtersSearch+=contractTypes[i].id;
                j++;
            }                   	                   
        }   
        
        //Check if exists any activity domain selected
        if ($(".main")[0].style.length!="0"){
            if (($(".main select").length!=0)&&($(".main select")[0].options[$(".main select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="activityDomain=" + $(".main select")[0].options[$(".main select")[0].selectedIndex].value;	                    	                                
                if($(".optional select")[0].selectedIndex>-1){
                    if (($(".optional select").length!=0)&&($(".optional select")[0].options[$(".optional select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="jobCategory=" + $(".optional select")[0].options[$(".optional select")[0].selectedIndex].value;
                    }
                }
            }                   	                                       
        }          	                
        else if (($(".editDomain")[0].innerHTML=="modifier")&&($(".domain label input").length!=0)){	                    
                if($(".domain label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="activityDomain=" + $(".domain label input")[0].id.replace("AD","");   	                       	                           	                           
                } 
                if($(".category label input").length!=0){
                    if($(".category label input")[0].checked){
                            if(filtersSearch!=""){
                                        filtersSearch+="&";
                            }
                            filtersSearch+="jobCategory=" + $(".category label input")[0].id.replace("JC","");
                    }
                }
       }           	                    	                    	                    	                                  
                   	                	                	                

        //Check if exists any region selected
        if ($(".mainSecond")[0].style.length!="0"){
            if (($(".mainSecond select").length!=0)&&($(".mainSecond select")[0].options[$(".mainSecond select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="region=" + $(".mainSecond select")[0].options[$(".mainSecond select")[0].selectedIndex].value;	                    	                                
                }
            
            if($(".optionalSecond select")[0].selectedIndex>-1){
                if (($(".optionalSecond select").length!=0)&&($(".optionalSecond select")[0].options[$(".optionalSecond select")[0].selectedIndex].value!=0)){	                    
                    if(filtersSearch!=""){
                        filtersSearch+="&";
                    }
                    filtersSearch+="department=" + $(".optionalSecond select")[0].options[$(".optionalSecond select")[0].selectedIndex].value;;
                }
            }	                    
        }          	                
        else if (($(".editLocation")[0].innerHTML=="modifier")&&($(".region label input").length!=0)){	                    
                if($(".region label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="region=" + $(".region label input")[0].id.replace("R","");  	                       	                           
                } 	
                if($(".department label input").length!=0){                               	                    	                    
                    if($(".department label input")[0].checked){
                            if(filtersSearch!=""){
                                        filtersSearch+="&";
                            }
                            filtersSearch+="department=" + $(".department label input")[0].id.replace("D","");	                           
                    }	                    	                                  
                }
        }
        
        //Check if exists any publication date selected	
        if ($(".mainThird")[0].style.length!="0"){
            if (($(".mainThird select").length!=0)&&($(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        if ($(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value == "5"){
                            var aux = $(".datePicker")[0].value.replace("/","-");
                            aux = aux.replace("/","-");
                            filtersSearch+="publicationDate=" + aux;
                        }
                        else{
                            filtersSearch+="publicationDate=" + $(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value; 
                        }        
                }                 
        }          	                
        else if (($(".editPublicationDate")[0].innerHTML=="modifier")&&($(".publicationDate label input").length!=0)){	                    
                if($(".publicationDate label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="publicationDate=" + $(".publicationDate label input")[0].id.replace("PD","");  	                       	                           
                } 	                               	                    	                    	                        	                    	                                  
        }               	                
        
        
        //Check if exists any keyword selected
        j=0;               	                	                
        var keywords=$(".keyword label input");
        for (i=0;i<keywords.length;i++){	                    
            if(keywords[i].checked){
                if (j==0){
                    if(filtersSearch!=""){
                        filtersSearch+="&";
                    }
                    filtersSearch+="keywords=";
                }
                else{
                    filtersSearch+="+";
                }
                filtersSearch+=keywords[i].value;
                j++;
            }                    	                   
        }
     
        
        return filtersSearch;                                              
    }
   
}

function LoadResultsAjax(filters)
{
   
   WSK.ClientServices.JobOfferServices.GetJobResults(filters,onResultsReturned.bind(this));
}

function onResultsReturned(res){
     //alert(res.value);
     //$(".wpJobSearchResults").html(res.value);
     document.getElementById("searchResultsPanel").innerHTML=res.value;
     JobSearchResults.setEventHandlers();
     //LoadLinkPagination();
     //refreshDisabledLinks();
    
     window.scrollTo(140,140);
}

