﻿VacancySearchFacets = {};
VacancySearchFacets.htmlencode = function (str) {
    return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
};
VacancySearchFacets.load = function (heading, morelinktext, urlencodedparams, domid) {
    $(function(){
        $.getJSON(window.wwwroot + 'vacancy-search-facets.ashx?vsp=' + urlencodedparams, function (facets) {
            if (facets.length > 0) {
                var html = ['<div class="inner">'];
                html.push('<h2>');
                html.push(VacancySearchFacets.htmlencode(heading));
                html.push('</h2>');
                $.each(facets, function () {
                    html.push('<h3>');
                    html.push(this.FacetTypeName);
                    html.push('</h3>');
                    
                    html.push('<ul>');
                    
                    var showmore = false;
                                        
                    $.each(this.Facets, function () {
                        var indentlevel = this[0];
                        var linktext = this[1];
                        var hideundermore = this[2];
                        var url = this[3];
                    
                        html.push('<li');
                        if (hideundermore) {
                            html.push(' style="display:none" class="facetsmore"');
                            showmore = true;
                        }
                        html.push('><div style="padding-left:');
                        html.push(indentlevel);
                        html.push('em"><a href="');
                        html.push(VacancySearchFacets.htmlencode(url));
                        html.push('">');
                        html.push(VacancySearchFacets.htmlencode(linktext));
                        html.push('</a></div></li>');
                    });

                    if (showmore) {
                        html.push('<li class="showmore"><a href="javascript:;" onclick="$(\'.facetsmore\', $(this).parent().parent()).show(); $(this).hide(); return false">');
                        html.push(VacancySearchFacets.htmlencode(morelinktext));
                        html.push('</a></li>');
                    }
                                        
                    html.push('</ul>');
                });
                html.push('</div>');
				$('#' + domid).html(html.join(''));
            }
        });
    });
};

VacancyShortlistSummary = {};
VacancyShortlistSummary._toupdate = [];
VacancyShortlistSummary.update = function (njobs) {
   var l=window.VacancyShortlistSummary._toupdate.length;
   for (var i=0; i<l; i++) {
       var summary = window.VacancyShortlistSummary._toupdate[i];
       var summarydomid = summary[0];
       var emptytemplate = summary[1];
       var onetemplate = summary[2];
       var manytemplate = summary[3];
       var domidtohide = summary[4];
       var text;
       if (njobs <= 0) {
           text = emptytemplate.replace("[[count]]", "<strong>" + njobs + "</strong>");
       } else if (njobs == 1) {
           text = onetemplate.replace("[[count]]", "<strong>" + njobs + "</strong>");
       } else {
           text = manytemplate.replace("[[count]]", "<strong>" + njobs + "</strong>");
       }
       var $el = $('#' + summarydomid);
       $el.html(text);
       if (njobs == 0) {
           if (domidtohide) $('#' + domidtohide).hide();
       } else {
           if (domidtohide) $('#' + domidtohide).show();
       }
   }
};
VacancyShortlistSummary.register = function (summarydomid, emptytemplate, onetemplate, manytemplate, domidtohide) {
    VacancyShortlistSummary._toupdate.push(
        [summarydomid, emptytemplate, onetemplate, manytemplate, domidtohide]
    );
};

function ajaxVacancyShortlist(href,vacancyid,action) // used by VacancyShortlistHyperLink control
{                
  $.ajax({
    type:"POST",
    url:href + '&ajax=true',
    async:true,
    data:"dummy", // this gets around some weird Firefox issue where i'm getting a 411 Content Length not specified http error
    success: function(njobs){
      if (action == 1) {
        $("a[name='shortlistlink_remove_" + vacancyid + "']").show();
        $("a[name='shortlistlink_add_" + vacancyid + "']").hide();
      }
      if (action == -1) {
        $("a[name='shortlistlink_remove_" + vacancyid + "']").hide();
        $("a[name='shortlistlink_add_" + vacancyid + "']").show();
      }
      VacancyShortlistSummary.update(njobs);
    },
    error: function(){
      document.location.href=href;
    }
  });
  return false;
}

function showhint(element) { // used in VacancyAddEdit.ascx
	$(document).ready(function() {  
		$(element).focus(function () {
			hint = element+"-hint";
			$(hint).show();
		});
		$(element).blur(function () {
			hint = element+"-hint";
			$(hint).hide();
		});
	});
}

/* Equal Height Columns 
   http://www.cssnewbie.com/equal-height-columns-with-jquery/ */
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
	    thisHeight = $(this).height();
	    if(thisHeight > tallest) {
		    tallest = thisHeight;
	    }
    });
    group.height(tallest);
}

function setEqualHeights() {
    equalHeight($(".col1"));
    equalHeight($(".col2"));
    equalHeight($(".browse-jobs ul li"));		
}

/* Initiate scripts */
$(document).ready(function(){
    setEqualHeights();
});
