/*
 * @package Sj Jsjob Filter
 * @version 1.1.0
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @copyright (c) 2016 YouTech Company. All Rights Reserved.
 * @author YouTech Company http://www.smartaddons.com
 *
 */

function SjJSJOBShowDaterange(obj, range) {
    if(jQuery(obj).val() == 'range') {
        jQuery(range).show();
    } else {
        jQuery(range).hide();
    }
}

function sjJSJOBAjaxStart() {
    if(!jQuery('#jsjobajax-loading').length) {
        jQuery('body').append('<div id="jsjobajax-loading"></div>');
    }
    jQuery('#jsjobajax-loading').css({'display': 'block'});
}

function sjJSJOBResetSearch(moduleId, container, submitform)
{
    //var form = jQuery('#'+formId);
    if(typeof(container) == 'string') {
        container = jQuery('#'+container);
    }
	//reset input
    container.find('input[type=text], textarea').val('');
	
	container.find('input[type=radio]').prop('checked', false);

    //radio, checkbox
    container.find(':checked').each(function(item){
        jQuery(this).removeAttr('checked');
    });

    //select, multi-select
    container.find('select option:selected').each(function(item){
		jQuery(this).removeAttr('selected');
	});
	
	//reset range slider
    container.find('[name$="_sjcheck"]').each(function(el) {
        var sliders = jQuery('#slider_'+this.id.replace('_sjcheck', ''));
        if(sliders) {
            var val = this.value.split('|');
            sliders.slider('values', 0, val[0]);
            sliders.slider('values', 1, val[1]);
        }
    });

    //submit form?
    if(submitform) {
        if(container.prop('tagName').toLowerCase() != 'form') {
            var form = container.parents('form');
        } else {
            var form = container;
        }
        var autofilter = form.find('[name="btnSubmit"]').length;
        if(!autofilter) {
            if(typeof(form.submit) == 'function') {
                form.submit();
            }
        }
    }
}

function sjJSJOBAjaxSubmit(form, SitePath) {
	//if Container K2 does not exist, submit form to redirect to K2 Filter result page
    if(jQuery('#jsjobs-wrapper').length) {
        sjJSJOBAjaxStart();
        jQuery.ajax({
            type: "POST",
            url: jQuery(form).attr('action'),
            data: jQuery(form).serialize(),
            success: function(text){
                sjJSJOBProcessAjax(text, SitePath);
            }
        });
    } else {
        jQuery(form).find('input[name="tmpl"]').val('');
        jQuery(form).submit();
    }
}

function sjJSJOBAjaxPagination(container, SitePath) {
    var pages = container.find('ul.pagination li a');
    if(!pages.length) {
        pages = container.find('.pagination ul li a');
    }
    pages.each(function(){
        jQuery(this).click(function(event) {
            event.preventDefault();
            sjJSJOBAjaxStart();
            jQuery.ajax({
                type: "GET",
                url: jQuery(this).attr('href'),
                success: function(text){
                    sjJSJOBProcessAjax(text, SitePath);
                }
            });
            return false;
        });
    });
}

function sjJSJOBHighlight(container, searchword) {
    if(typeof(jQuery.fn.highlight) == 'function') {
        searchword = searchword.replace(/[<>#\\]/, '');
        //remove excluded words
        searchword = searchword.replace(/\-\s*(intitle\:|intext\:|inmetadata\:|inmedia\:)?\s*("[^"]"|[^\s]+)/g,'');
        //remove special keywords
        searchword = searchword.replace(/(intitle\:|intext\:|inmetadata\:|inmedia\:)/g,'');

        var pattern = /(?:"[^"]+"|[^\s]+)/gi;
        var matches = searchword.match(pattern);
        if(matches) {
            for(i=0; i<matches.length; i++) {
                var word = matches[i].replace(/"/g, '');
                if(word != '' && word != 'OR') {
                    container.highlight(word);
                }
            }
        }
    }
}

function sjJSJOBProcessAjax(text, SitePath) {
    // console.log(text);return false;
    var container = jQuery('#jsjobs-wrapper');
    var content = jQuery('<div>' + text + '</div>').find('#jsjobs-wrapper');
    if(content.length) {
        container.html(content.html());
        //paging
        sjJSJOBAjaxPagination(container, SitePath);

        //highlight search team in result
        sjJSJOBHighlight(container, jQuery('.sj_jsjob_filter_shopby input[name="jobtitle"]').val());
    } else {
        container.html('No Item found!');
    }

    jQuery('#jsjobajax-loading').css({'display': 'none'});
	jQuery('html, body').animate({scrollTop: container.offset().top}, 1000);
}