// Equal height

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	if (tallest > 0) {
	  group.height(tallest);
	}
}

$(document).ready(function(){
	equalHeight($(".equal1"));
	equalHeight($(".equal2"));
});




$(document).ready(function() {  

	// Highlight active field
	
	$('.form1 input[type="text"], .form1 input[type="password"], .form1 textarea, .form1 select, .form2 input[type="text"], .form2 input[type="password"], .form2 textarea, .form2 select, .form3 input[type="text"], .form3 select').focus(function() {  
		$(this).parent("span").removeClass("field-wrap").addClass("field-wrap-active"); 
		
		if(this.value != this.defaultValue){  
		    if (this.select) {
			    this.select();  
			}
		} 
	});  
	$('.form1 input[type="text"], .form1 input[type="password"], .form1 textarea, .form1 select, .form2 input[type="text"], .form2 input[type="password"], .form2 textarea, .form2 select, .form3 input[type="text"], .form3 select').blur(function() {  
		$(this).parent("span").removeClass("field-wrap-active").addClass("field-wrap");  
	});  


	// Font replacements
    Cufon.replace('.tel');
    Cufon.replace('.h1-default');
    Cufon.replace('p.h1');
    Cufon.replace('.box1 h2');
    Cufon.replace('.box2 h2');
    Cufon.replace('.box3 h2');
    Cufon.replace('.subnav dt');
    Cufon.replace('.nqcc h2');
	
});



// Subnav
$(document).ready(function(){
    var nav_locations_hide_timerid = null; // this represents a timer that will hide the locations dropdown, we need to be able to cancel this

	$(".nav dd.nav_locations_dd").hover(
		function () {
            var locs = $("#nav_locations");
            locs.css('left', $(this).offset().left + 'px');
            locs.css('top', ($(this).offset().top+20) + 'px');
            locs.show();
		}, 
		function () {
		    nav_locations_hide_timerid = setTimeout(function () {
		        $("#nav_locations").hide();
		    }, 250);
		}
	);
	
	$("#nav_locations").hover(
	    function () {
	        if (nav_locations_hide_timerid!=null) {
	            clearTimeout(nav_locations_hide_timerid);
	            nav_locations_hidetimerid=null;
	        }
	    },
	    function () {
	        nav_locations_hide_timerid = setTimeout(function () { 
	            $('#nav_locations').hide();
	        }, 250);
	    }
	);
	});

// Subnav
$(document).ready(function() {
    var nav_employers_hide_timerid = null; // this represents a timer that will hide the employers dropdown, we need to be able to cancel this

    $(".nav dd.nav_employers_dd").hover(
		function() {
		    var locs = $("#nav_employers");
		    locs.css('left', $(this).offset().left + 'px');
		    locs.css('top', ($(this).offset().top + 20) + 'px');
		    locs.show();
		},
		function() {
		    nav_employers_hide_timerid = setTimeout(function() {
		        $("#nav_employers").hide();
		    }, 250);
		}
	);

    $("#nav_employers").hover(
	    function() {
	        if (nav_employers_hide_timerid != null) {
	            clearTimeout(nav_employers_hide_timerid);
	            nav_employers_hidetimerid = null;
	        }
	    },
	    function() {
	        nav_employers_hide_timerid = setTimeout(function() {
	            $('#nav_employers').hide();
	        }, 250);
	    }
	);
	});


	// Subnav
	$(document).ready(function() {
	    var nav_candidates_hide_timerid = null; // this represents a timer that will hide the candidates dropdown, we need to be able to cancel this

	    $(".nav dd.nav_candidates_dd").hover(
		function() {
		    var locs = $("#nav_candidates");
		    locs.css('left', $(this).offset().left + 'px');
		    locs.css('top', ($(this).offset().top + 20) + 'px');
		    locs.show();
		},
		function() {
		    nav_candidates_hide_timerid = setTimeout(function() {
		        $("#nav_candidates").hide();
		    }, 250);
		}
	);

	    $("#nav_candidates").hover(
	    function() {
	        if (nav_candidates_hide_timerid != null) {
	            clearTimeout(nav_candidates_hide_timerid);
	            nav_candidates_hidetimerid = null;
	        }
	    },
	    function() {
	        nav_candidates_hide_timerid = setTimeout(function() {
	            $('#nav_candidates').hide();
	        }, 250);
	    }
	);
	});


// javascript code for ~/_controls/HierarchicalCheckboxList.ascx control
var HierarchicalCheckboxList = {};
HierarchicalCheckboxList.hookupBehaviour = function (controlid) {
    $('#' + controlid + ' input').click(function () {
        var input = this;

        var rootul = document.getElementById(controlid);

        // make sure all descendents are ticked/unticked if a parent node is ticked
        $('input', $('#' + input.id).siblings()).each(function () { this.checked = input.checked; });

        if (input.checked) {
            // make sure the parent node is ticked if all its children are ticked
            var ul = $('#' + input.id).parent().parent();
            while (ul.get()[0] != rootul) { 
                var siblings = ul.children().children().filter('input');
                var allchecked = true;
                siblings.each(function () {
                    if (!this.checked) {
                        allchecked = false;
                    }
                });
                
                if (allchecked) {
                    ul.siblings().filter('input').get()[0].checked = true; // tick the parent
                } else {
                    break;
                }
            
                ul = ul.parent().parent();
            }
        } else {
            // make sure all ascendents are unticked if a node is unticked
            var ul = $('#' + input.id).parent().parent();
            while (ul.get()[0] != rootul) { 
                ul.siblings().filter('input').get()[0].checked = false;

                ul = ul.parent().parent();
            }
        }
    });
}
HierarchicalCheckboxList.selectAll = function (controlid) {
    $('#' + controlid + ' input').each(function () { this.checked = true; });
};
HierarchicalCheckboxList.deselectAll = function (controlid) {
    $('#' + controlid + ' input').each(function () { this.checked = false; });
};

// utility functions for use with Hotlizard.HLCheckboxList
var HLCheckboxList = {};
HLCheckboxList.selectAll = function (containingdivid) {
    $('#' + containingdivid + ' input').each(function () { this.checked = true; });
};
HLCheckboxList.deselectAll = function (containingdivid) {
    $('#' + containingdivid + ' input').each(function () { this.checked = false; });
};

// job basket
var JobBasket = {};
JobBasket.add = function (vacancyid, oncomplete) {
    oncomplete = oncomplete || function(){};

    $.ajax({
        type: 'GET',
        url: baseurl + 'jobs/basket.aspx?mode=ajax&addvacancy=' + vacancyid,
        success: oncomplete
    });
};
JobBasket.remove = function (vacancyid, oncomplete) {
    oncomplete = oncomplete || function(){};

    $.ajax({
        type: 'GET',
        url: baseurl + 'jobs/basket.aspx?mode=ajax&removevacancy=' + vacancyid,
        success: oncomplete
    });
};

// job listing javascript
var JobListingPage = {};
JobListingPage.addToBasket = function (vacancyid) {
    JobBasket.add(vacancyid, JobListingPage._oncomplete);
    $('#vacancyjobbasketlink_' + vacancyid)
        .text('Remove from Basket')
        .get()[0].onclick = function () { return JobListingPage.removeFromBasket(vacancyid) };
    
    return false;
};
JobListingPage.removeFromBasket = function (vacancyid) {
    JobBasket.remove(vacancyid, JobListingPage._oncomplete);
    $('#vacancyjobbasketlink_' + vacancyid)
        .text('Add to Basket')
        .get()[0].onclick = function () { return JobListingPage.addToBasket(vacancyid) };
        
    return false;
};
JobListingPage._oncomplete = function(responsetext) {
    if (/^\d+$/.test(responsetext)) {
        JobBasketControl.updateCount(parseInt(responsetext, 10));
    }
};
JobListingPage.compareJobs = function() {
    var checkedcheckboxes = $('.JobListingPageCompareINPUT:checked');
    
    if (checkedcheckboxes.length < 2) {
        alert('Please select at least 2 jobs to compare.')
    } else if (checkedcheckboxes.length > 4) {
        alert('You have selected too many jobs. Please select up to 4 jobs.')
    } else {
        location.href = 
            baseurl +
            'jobs/compare.aspx?vacancyids=' + 
            checkedcheckboxes.map(function () { return parseInt(this.value, 10); }).get().join('%2c')
    }
    
    return;
};

// job detail page javascript
var JobDetailPage = {};
JobDetailPage.addToBasket = function (vacancyid) {
    return JobListingPage.addToBasket(vacancyid);
};
JobDetailPage.removeFromBasket = function (vacancyid) {
    return JobListingPage.removeFromBasket(vacancyid);
};

// job compare page javascript
var JobComparePage = {};
JobComparePage.addToBasket = function (vacancyid) {
    return JobListingPage.addToBasket(vacancyid);
};
JobComparePage.removeFromBasket = function (vacancyid) {
    return JobListingPage.removeFromBasket(vacancyid);
};

// ~/_controls/JobBasket.ascx javascript
var JobBasketControl = {};
JobBasketControl.DOMID = null;
JobBasketControl.updateCount = function (newcount) {
    if (!JobBasketControl.DOMID) {
        return;
    }
    
    $('#' + JobBasketControl.DOMID + '_count').text(newcount + ' job' + (newcount!=1?'s':''));
    
    if (newcount==0) {
        $('#' + JobBasketControl.DOMID).fadeOut();
    } else {
        $('#' + JobBasketControl.DOMID).fadeIn();
    }
};

function rbc(bannerid)
{          
    // Call the request object method wrapper function
    var request_var = request_object();
    
    if(request_var)
    {
        
        var url = baseurl + "logbannerclick.ashx?bannerid=" + bannerid
        
        request_var.open("GET",url,true);    
        request_var.send(null);      
    }
    
    return false;
}

function request_object()
{
    try
    {
        return new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e)
    {
        try
        {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e)
        {
            return new XMLHttpRequest();
        }
    }
}