﻿jQuery.fn.tieredJobRole = function(roleData, options) {
	var settings = jQuery.extend({
		selectOneText: "-- select one --"
	}, options);

	var jobLevels = [
		{
			'color': 'pink',
			'level': ['C-Level'],
			'role': [],
			'focus': []
		},
		{
			'color': 'pink',
			'level': ['Vice President'],
			'role': ['CIO Global', 'CIO Line of Business/Divisional', 'CIO Shared Services'],
			'focus': []
		},
		{
			'color': 'green',
			'level': ['Manager', 'Director', 'Vice President'],
			'role': ['IT Support'],
			'focus': []
		},
		{
			'color': 'blue',
			'level': ['Manager', 'Director', 'Vice President'],
			'role': ['Infrastructure and Operations', 'Infrastructure Engineering and Operations'],
			'focus': []
		},
		{
			'color': 'yellow',
			'level': ['Manager', 'Director', 'Vice President'],
			'role': ['Applications'],
			'focus': []
		},
		{
			'color': 'purple',
			'level': ['Manager', 'Director', 'Vice President'],
			'role': ['IT Planning & Business Operations'],
			'focus': []
		},
		{
			'color': 'red',
			'level': ['Manager', 'Director', 'Vice President'],
			'role': ['IT Architecture'],
			'focus': []
		},
		{
			'color': 'grey',
			'level': ['IT Professional'],
			'role': ['IT Support'],
			'focus': []
		},
		{
			'color': 'gold',
			'level': ['IT Professional'],
			'role': ['Infrastructure and Operations'],
			'focus': []
		},
		{
			'color': 'ltgreen',
			'level': ['IT Professional'],
			'role': ['Applications'],
			'focus': []
		},
		{
			'color': 'orange',
			'level': ['IT Professional'],
			'role': ['IT Planning & Business Operations'],
			'focus': []
		},
		{
			'color': 'turquoise',
			'level': ['IT Professional'],
			'role': ['IT Architecture'],
			'focus': []
		},
		{
			'color': 'brown',
			'level': ['Business Professional'],
			'role': [],
			'focus': []
		},
		{
			'color': 'brown',
			'level': ['Vice President', 'Director', 'Manager', 'IT Professional'],
			'role': ['Non-IT', 'Student/Professor'],
			'focus': []
		}
	];
	var mapping = {
		'pink': ['AA', 'AB', 'AC', 'AD', 'AE', 'AG', 'AH', 'AI', 'AN', 'AO'],
		'green': ['AA', 'AE', 'AF', 'AH', 'AI', 'AK', 'AL', 'AM'],
		'blue': ['AA', 'AB', 'AC', 'AD', 'AE', 'AG', 'AH', 'AI', 'AJ', 'AO', 'AK', 'AN'],
		'yellow': ['AA', 'AB', 'AG'],
		'purple': ['AA', 'AD', 'AF', 'AI', 'AJ', 'AO', 'AK', 'AN'],
		'red': ['AA', 'AB', 'AC', 'AE', 'AK'],
		'grey': ['AR', 'AH', 'BP', 'BF', 'BG', 'BK', 'BE', 'AT', 'BB', 'AV', 'AU', 'BO', 'AW', 'BN'],
		'gold': ['AH', 'BM', 'BI', 'BH', 'AG', 'BA', 'BB', 'BQ', 'AY', 'AX', 'AN', 'AS', 'AV', 'AU', 'AZ', 'BO'],
		'ltgreen': ['AH', 'BP', 'BF', 'BK', 'AP', 'AG', 'AQ', 'BL', 'BD', 'AV', 'BO', 'AW', 'BN'],
		'orange': ['AR', 'BP', 'BL', 'AN', 'BC', 'BD', 'AV', 'AU', 'AW', 'BN'],
		'turquoise': ['AH', 'AP', 'BM', 'BI', 'AT', 'AS', 'AV', 'AU', 'AZ', 'AW'],
		'brown': ['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AO', 'AK', 'AL', 'AM', 'AN']
	};

	function contentMapping(level, role, focus, defaultColor) {
		var found_mappings = [];
		// NOTE: Since none of the mapping use the job focus that search is disabled

		found_mappings = $.grep(jobLevels, function(n, i) {
			return $.inArray(level, n.level) != -1 && $.inArray(role, n.role) != -1
		});

		if (found_mappings.length != 1)
			found_mappings = $.grep(jobLevels, function(n, i) {
				return $.inArray(level, n.level) != -1 && n.role && n.role.length == 0
			});

		if (found_mappings.length == 1)
			return mapping[found_mappings[0].color];
		else {
			if (defaultColor)
				return mapping[defaultColor];
			else
				return [];
		}
	};


	return this.each(function(index) {
		var $ = jQuery;
		var prePop = true;

		$(this).find('input[name$=JsEnabled]').val('1');
		var jobLevel = $(this).find('select');
		var jobRole_text = jobLevel.nextAll('input[name$=jobRole]').hide();
		var jobFocus_text = jobRole_text.nextAll('input[name$=jobFocus]').hide();

		var jobRole_select = $("<select id='jobRole" + index + "' disabled='disabled'><option value=''>" + settings.selectOneText + "</option></select>");
		var jobFocus_select = $("<select id='jobFocus" + index + "' disabled='disabled'><option value=''>" + settings.selectOneText + "</option></select>");

		jobLevel.change(function() {
			var selected = $(this).val();
			var level = getJobData(roleData, selected);

			if (!level)
				level = { JobRoles: null };

			populateDropDown(jobRole_select, level.JobRoles);
			jobRole_select.change();
		});

		jobRole_select.change(function() {
			var selected = $(this).val();
			if (!prePop)
				jobRole_text.val(selected);
			var level = getJobData(roleData, jobLevel.val());
			if (level)
				var role = getJobData(level.JobRoles, selected);

			if (!role)
				role = { JobFocuses: null };

			populateDropDown(jobFocus_select, role.JobFocuses);
			jobFocus_select.change();
		});

		jobFocus_select.change(function() {
			var selected = $(this).val();
			if (!prePop) {
				jobFocus_text.val(selected);

				filterContentPreferences();
			}
		});

		jobRole_text.after(jobRole_select);
		jobFocus_text.after(jobFocus_select);

		jobLevel.change();
		jobRole_select.val(jobRole_text.val());
		jobRole_select.change();
		jobFocus_select.val(jobFocus_text.val());

		prePop = false;
		filterContentPreferences();

		function populateDropDown(select, data) {
			select.empty();
			select.append("<option value=''>" + settings.selectOneText + "</option>");
			if (data && data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					select.append("<option value='" + data[i].Name + "'>" + data[i].DisplayName + "</option>");
				}
				select.removeAttr('disabled');
			}
			else
				select.attr('disabled', 'disabled');
		}

		function getJobData(data, name) {
			var d = $.grep(data, function(n, i) { return n.Name == name });
			if (d.length == 1)
				return d[0];
			else
				return null;
		}

		function filterContentPreferences() {
			var level = jobLevel.val();
			var role = jobRole_select.val();
			var focus = jobFocus_select.val();

			var question = $('span[questioncode=AOZ]');
			var answersToAlwaysShow = eval(question.attr('alwaysshow'));
			if (!answersToAlwaysShow)
				answersToAlwaysShow = [];
			var defaultColor = question.attr('defaultcolor');
			var answers = question.children('span[answercode]');
			var mapping = contentMapping(level, role, focus, defaultColor);

			var valid_answers = $.grep(answers, function(n, i) {
				var answercode = $(n).attr('answercode');
				return $.inArray(answercode, mapping) != -1 ||
					$.inArray(answercode, answersToAlwaysShow) != -1
			});

			answers.hide().next().hide();
			valid_answers = $(valid_answers);
			valid_answers.show().next().show();

			answers.find('input:checked:hidden').removeAttr('checked');
		}
	});
}
