﻿
adecco.jobsearch = {
    //Variable to store variation path retrieved from the resource file
    variationLabelPath: {},
    
    getPreferedLocation: function() {
        var preferedLocation = getCookie(adecco.config.PREF_LOCATION_COOKIE_NAME);
        return preferedLocation;
    },

    setPreferedLocation: function(preferedLocation) {
        setCookie(adecco.config.PREF_LOCATION_COOKIE_NAME, preferedLocation, 365);
    },

    getPreferedCategory: function() {
        var preferedLocation = getCookie(adecco.config.PREF_CATEGORY_COOKIE_NAME);
        return preferedLocation;
    },

    setPreferedCategory: function(preferedLocation) {
        setCookie(adecco.config.PREF_CATEGORY_COOKIE_NAME, preferedLocation, 365);
    },

    pageDidFinishLoading: function() {

        // Register autocomplete to specific inputs
    $('input.job-titles').autocomplete(adecco.config.jobTitlesAutocompleteService, { minChars: adecco.config.jobTitlesAutocompleteMinChars, selectFirst: false });
        
	// autoFill set to true to automatically pick up the best match
        $('input.job-location').autocomplete(adecco.config.locationAutocompleteService, {
            cacheLength: 0
            , minChars: adecco.config.defaultAutocompleteMinChars, selectFirst: false 
        });

        // Add watermarks
        $('.job-search-toolbar input').watermark();
        //$('input.job-titles').watermark();
        
        // Register event handlers
        var js = adecco.jobsearch;
        $('.advanced-toggle').click(js.onAdvancedSearchClicked);
        $('.find-jobs-button').click(js.onSearchClicked);

        // Register event for enter key inside keywords and location text boxes
        //This is commented out to fix TFS issue 2807. Please delete this comment and commented out 
        //code after client reviews for initial launch. -Rob Reagan
        $('input.job-titles').keyup(js.onEnterKeyPressed);
        $('input.job-location').keyup(js.onEnterKeyPressed);    
        
        if (adecco.config.insertCssComponent) {
            ensureCssFile(adecco.config.cssComponent);
        }
	//Added for TFS#4203
        var itemCount = $('.searchListItem').length;
        var rds = adecco.config.defaultDistanceRadius.toString();
	if(getQuerystring('cnty') != '' )
	{
		var ctl = $('[name="job-search-ddlCountry"]');
		if(ctl !== null)
		{
			ctl.val(getQuerystring('cnty') );
		}
		
	}
        //TJF r9 10/6/09  Added t display advanced search is used from prior search TFS 2915
            if ((getQuerystring('cat') != '' ||
                getQuerystring('emp') != '' ||
                getQuerystring('edu') != '' ||
                getQuerystring('exp') != '' ||
                getQuerystring('rds', rds) != rds ) && itemCount != '0')
                    adecco.jobsearch.toggleAdvancedSearch();

    },
    
    toggleAdvancedSearch: function() {
        $('.advanced-search').slideToggle(adecco.config.animationSpeed);
    },
    
    onAdvancedSearchClicked: function(event) {
        adecco.jobsearch.toggleAdvancedSearch();
        event.preventDefault();
    },
    
    doSearch: function(searchWidget) {
        if (!searchWidget) {
            return;
        }
        
        var searchParams, targetUrl, hasErrors = false;
        
        searchParams = new SearchQuery();
        searchParams.keywords = $('.job-titles', searchWidget).val();
        searchParams.zipCode = $('.job-location', searchWidget).val();
	searchParams.country = $('[name="job-search-ddlCountry"]', searchWidget).val();
	        
        //AG:09/24/2010 assigning the default category value,if "----Category--" option 
        //is selected in the dropdown

        //searchParams.category = $('[name="job-search-category"]', searchWidget).val();

        var $options = $('option', $('[name="job-search-category"]', searchWidget));
        var CateogryCount = $options.size();
        var Category = $('[name="job-search-category"]', searchWidget).val();

        if ((Category == '') && (CateogryCount == 2)) {
            searchParams.category = $options[1].value;
        }
        else {
            searchParams.category = $('[name="job-search-category"]', searchWidget).val();
        }        
                
        //searchParams.category = $('[name="job-search-category"]', searchWidget).val();
        searchParams.employment = $('[name="job-search-employment"]', searchWidget).val();
        searchParams.education = $('[name="job-search-education"]', searchWidget).val();
        searchParams.experience = $('[name="job-search-experience"]', searchWidget).val();
        searchParams.radius = $('[name="job-search-radius"]', searchWidget).val();
        
        if (hasErrors) {
            
        } else {
            targetUrl = searchParams.toQueryString();
            if (targetUrl) {
                document.location = targetUrl;
            }
        }                               
    },
    
     onEnterKeyPressed: function(event) {
        if(event.keyCode == 13){    
            //$('.find-jobs-button').click(adecco.jobsearch.onSearchClicked);  
            var
                me = adecco.jobsearch,
                source = event.currentTarget
                ,searchWidget, location;
            
            // Find the ancestor that holds the whole widget.
            try {
                searchWidget = $(source).parents('.job-search-toolbar');
                
                // Get the location and save it.
                location = $('.job-location', searchWidget).val();
                me.setPreferedLocation(location);
                
                // Perform the actual search
                me.doSearch(searchWidget);            
            } finally {
                event.preventDefault();
            }                 
        }
    },
    
    onSearchClicked: function(event) {
        var
            me = adecco.jobsearch,
            source = event.currentTarget
            ,searchWidget, location;
        
        // Find the ancestor that holds the whole widget.
        try {
            searchWidget = $(source).parents('.job-search-toolbar');
            
            // Get the location and save it.
            location = $('.job-location', searchWidget).val();
            me.setPreferedLocation(location);
            
            // Perform the actual search
            me.doSearch(searchWidget);            
        } finally {
            event.preventDefault();
        }                       
    }
};

$(document).ready(adecco.jobsearch.pageDidFinishLoading);
