/* 
Title:		Build Scripts

	Requires:
		
		
*/

	/* Initiate scripts */
	$(document).ready(function(){
	
	    $("#eddie").flash(
            {	src: '_resx/flash/eddie-v1.swf',
				wmode:'transparent',
                width: 240,
                height: 216 },
            {   version: 8 }
        );
		
		// *** Employers of the Month Nav ***
		var active = $(".employers .nav .active").text(); $("#selected").text(active) // Set active text
		$(".employers .nav a").hover(function(){ $("#selected").text($(this).text());},function(){$("#selected").text(active);});// Hover
		$(".employers .nav a").focus(function(){$("#selected").text($(this).text());});$(".employers .nav a").blur(function(){$("#selected").text(active);}); // Keyboard
		$(".employers .nav a").click(function(){$(".employers .nav a.active").removeClass("active");$(this).addClass("active");active = $(this).text();$("#selected").text(active);}); // Click
		
		//$('.quick-search .text, .refine-search .text').emptyonclick();
		$(".aside1 .toggle").show();
		$(".aside1 .section1 .inner div").addClass("hide");
		$(".aside1 .open .inner div").addClass("show");
		$(".aside1 .open .toggle").removeClass("expand").addClass("contract");
		
		equalHeight($(".featured-jobs .job"));
	});

function innerToggle(str) { 
	var div = ".aside1 .section1 ."+str+" div";
	var a = ".aside1 .section1 ."+str+" .toggle";
	if($(div).attr("class")=="hide") {
    	// expand
		$(div).removeClass("hide").addClass("show");
		$(a).removeClass("expand").addClass("contract");
		$.cookie("ResourcePanel_"+str, "open", { path: '/', expires: 30 });
	} else {
	    // collapse
		$(div).removeClass("show").addClass("hide");
		$(a).removeClass("contract").addClass("expand")
		$.cookie("ResourcePanel_"+str, "", { path: '/', expires: 30 });
	}
}

function ShowHide(id, display) {
	if(display=="block") { $(id).show(); } else { $(id).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);}

/* jQuery emptyonclick plugin - Created by Andreas Creten (andreas@madewithlove.be) on 2008-06-06. */
jQuery.fn.extend({emptyonclick: function(options) {return this.each(function() {new jQuery.EmptyOnClick(this, options);});}});jQuery.EmptyOnClick = function(element, options) {var defaultValue = $(element).val();$(element).bind("focus", function(e) {if(defaultValue == $(this).val())$(this).val('');}).bind("blur", function(e) {if(!$(this).val()) {$(this).val(defaultValue);}});$("form:has(#"+element.id+")").bind('reset', function(e) {$(element).val(defaultValue);$(element).removeClass(options.changeClass);}) .bind('submit', function(e) {if($(element).val() == defaultValue)$(element).val('');});};	

/* Clickable Area - target & href location */
function clickableArea(target,href) {href = href + ":first a:first";$(target).click(function() {window.location = $(this).find(href).attr("href");});$(target).hover(function() {$(this).addClass("hover");},function() {$(this).removeClass("hover");});}

// 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);
};


// ~/_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();
    }
};
