/*
GreenFoot JS file
*/

// Kill console.log
if(!window.console) (function() {
	var funcs = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
	window.console = {};
	for(var i=0; i<funcs.length; i++) {
		window.console[funcs[i]] = function(){};
	}
})();

// jQuery log function
$.fn.log = function() {
	console.log(this);
	return this;
};

// Put input type into class name
$(function() {
	$('input').each(function() {
		$(this).addClass(this.type);
	});
});

// IE ONLY Namespace
if($.browser.msie) $(function() {
	$(document.body).addClass('ie');
	
	// Sucker Fish Hover Replacement
	$('#top-nav li').addClass('hoverable');
});

// Non IE Namespace
else $(function() {
	$(document.body).addClass('not-ie');
});

function addAppQuestion() {
	$('.addAppQuestion').remove();
	$('.removeAppQuestion').show();

	$('#questions').append('<div><label for="newApplicantQuestion[]">' + $('#aqtext').text() + '</label> <input class="text" type="text" name="newApplicantQuestion[]" value=""/> <a href="javascript:void(0);" onclick="addAppQuestion();" class="addAppQuestion">Add another question</a> <a href="javascript:void(0);" onclick="removeQuestion(this);" class="removeAppQuestion hidden">Remove Question</a></div>');
}

function removeQuestion(element) {
	parent = $(element).parent();
	
	if(parent.children('a.addAppQuestion').length == 0) {
		parent.remove();
	}
}

function checkall(classname) {
	state = $('#checkallbox').attr('checked');

	if(classname) {
		$("input:checkbox."+classname).attr('checked', state);
	}
	else {
		$("input:checkbox").attr('checked', state);
	}
}

function new_logo() {
	$('#logo_new').show();
}

function goPage() {
	filter = $('#filter').val();
	page = $('#page').val();
	
	window.location = root + 'jobs/company/index/' + filter + '/' + page + '/';
	
	return false;
}

function updateUpgradeForm() {
	//featured jobs
	var num_f = $('#featuredJobs').val();
	var cost_f = $('#featuredCost').text();
	
	var total_f = num_f * cost_f;
	$('#featuredTotal').text(money_format(Number(total_f)));
		
	//standard jobs
	var num_s = Number($('#standardJobs').val());
	var dnum_s = Number($('#standardDiscountNum').text());
	
	if(num_s >= dnum_s) {
		var original_s = $('#standardCostOriginal').val()
		var t_cost_s = original_s - (original_s * ($('#standardDiscountAmount').text() / 100) );
		$('#standardCost').text(money_format(t_cost_s));
	}
	else {
		$('#standardCost').text($('#standardCostOriginal').val());
	}
	
	var cost_s = $('#standardCost').text().replace("$", '');
	
	var total_s = num_s * cost_s;
	$('#standardTotal').text(money_format(Number(total_s)));
	
	//resume searching
	var num_r = Number($('#resumeAccounts').val());
	var dnum_r = Number($('#resumeDiscountNum').text());
	
	if(num_r >= dnum_r) {
		var original_r = $('#resumeCostOriginal').val()
		var t_cost_r = original_r - (original_r * ($('#resumeDiscountAmount').text() / 100) );
		$('#resumeCost').text(money_format(t_cost_r));
	}
	else {
		$('#resumeCost').text($('#resumeCostOriginal').val());
	}
	
	var cost_r = $('#resumeCost').text().replace("$", '');
	
	var total_r = num_r * cost_r;
	$('#resumeTotal').text(money_format(Number(total_r)));
	
	//complete
	var total_c = 0;
	var complete = $('.complete:checked').val();
	if(complete) {
		num_c = $('#completePackage' + complete + 'Months').text().replace(',', '').replace('$', '');
		cost_c = $('#completePackage' + complete + 'Cost').text().replace(',', '').replace('$', '');
		
		total_c = parseInt(num_c) * parseInt(cost_c);
	}
	
	$('#unlimitedTotal').text(money_format(Number(total_c)));
	
	//total it all up
	grand_total = total_f + total_s + total_r + total_c;
	$('#total').text(money_format(grand_total));
}

$(function() {
	$('#upgrade input').change(function() {
		if($.browser.msie) setTimeout(updateUpgradeForm, 250);
		else updateUpgradeForm();
	});
});

function money_format(num) {
	var AmountWithCommas = num.toLocaleString();
	var arParts = String(AmountWithCommas).split('.');
	var intPart = arParts[0];
	var decPart = (arParts.length > 1 ? arParts[1] : '');
	decPart = (decPart + '00').substr(0,2);

	result = intPart + '.' + decPart;

	return "$" + result;
}

function upgradeChange(amount, input) {
	var n = Number($('#'+input).val());
	n+=amount;
	
	if(n < 1) {
		n = 0;
	}
	
	$('#'+input).val(Number(n));
	
	updateUpgradeForm();
}

// Add class on focus
$(function() {
	$('form.format .field :input')
		.focus(function() {
			$(this)
				.parent('.field')
				.addClass('focus');
		})
		.blur(function() {
			$(this)
				.parent('.field')
				.removeClass('focus');
		})
});

// Set focus to element on load
$(function() {
	$('.field.error :input, .focus-on-load :input')
		.filter(':first')
		.focus()
});

// Show/Hide a field description value on focus/blur
$(function() {
	var isSubmit = false;
	$(':input.description-toggle')
		.each(function() {
			this.description = this.value;
		})
		.focus(function() {
			if(this.description == this.value) this.value = '';
		})
		.blur(function() {
			if(this.value == '' && !isSubmit) this.value = this.description;
		})
	$('form')
		.submit(function() {
			isSubmit = true;
			$(this)
				.find(':input.description-toggle')
					.each(function() {
						if(this.value == this.description) this.value = '';
					})
		})
});

// Add a hover class to anything that is hoverable
$(function() {
	$('.hoverable')
		.hover(function() {
			$(this)
				.addClass('hover')
				// IE6 Hack
				.find('.hover-action')
					.css({opacity: 1})
		},function() {
			$(this)
				.removeClass('hover')
				// IE6 Hack
				.find('.hover-action')
					.css({opacity: 0})
		})
});

// Toggles the enable class when clicked
$(function() {
	$('.enableable').click(function() {
		$(this).toggleClass('enabled');
	});
});

// Add functions using class
$(function() {
	$('.action').click(function() {
		var that = this;
		$.map(this.className.split(' '), function(className) {
			var classNames = className.split('-');
			var action = classNames.shift();
			var targets = classNames.join('-');
			var elems = targets == 'this' ? $(that) : $('.'+targets);
			
			switch(action) {
				case 'show': elems.slideDown(); break;
				case 'hide': elems.slideUp(); break;
				case 'toggle': elems.slideToggle(); break;
				case 'submit': elems.submit(); break;
				case 'focus': elems.filter(':first').focus(); break;
				case 'checkall': elems.attr('checked', true); break;
				case 'uncheckall': elems.attr('checked', false); break;
				case 'activate': elems.addClass('active'); break;
				case 'deactivate': elems.removeClass('active'); break;
			}
		});
	});
});

// Check all required form fields for values
$(function() {
	$('form.format').submit(function() {
		updateRTEs();
		
		var allValid = true;
		$(this)
			.find(':input')
				.blur()
		$(this)
			.find('.required :input')
				.each(function() {
					if(this.offsetHeight === 0 && this.offsetWidth === 0) return; // Check if visible
					var valid = $.trim($(this).val()).length > 0;
					if(!valid) $(this).parent('.field').addClass('error');
					allValid = allValid && valid;
				});
		
		if(!allValid) {
			alert('You must fill out all required fields');
			$(this)
				.find('.error :input')
				.filter(':first')
					.focus()
		}
		return allValid;
	});
});

$(function() {
	$('.jobpost-accordian').click(function() {
	
		var classnames = this.className.split(" ");
		
		var show = '';
		for(i=0;i<classnames.length;i++) {
		
			if(classnames[i].substring(0,5) == 'show-') {
				show = classnames[i].substring(5);
			}
		}
		
		var display = $('#' + show).css('display');
		
		if(display == 'none') {
			$('.jobpost').slideUp();
			$('#' + show).slideDown();
			$('.jobpost-show').show();
			$('#' + show + '_header .jobpost-show').hide();
		}
	});
});