jQuery(document).ready(function($) {
	var handleMatrixClick = function(functionClass, technologyClass, clickedElement) {
		
		
		if (!$(clickedElement).is(':checked')) {
			if ($('.functionLayers input:checked, .technologies input:checked').length == 0) {
				resetForm($);
				return;
			}
			activateSiblings(functionClass, technologyClass, clickedElement);
			if ($(clickedElement).hasParent(functionClass)) {
				highlightFunctionsForTechnology(functionClass, $(technologyClass + ' input:checked').attr('name'));
			} else {

				highlightTechnologiesForFunction(technologyClass, $(functionClass + ' input:checked').attr('name'));
			}
			return
		}
		// element is a function
		if ($(clickedElement).hasParent(functionClass)) {
			if ($(technologyClass).has('input:checked').length) {
				jumpToNode(functionClass, technologyClass);
			} else {
				highlightTechnologiesForFunction(technologyClass, $(clickedElement).attr('name'));
				deactivateSiblings(functionClass, technologyClass, clickedElement);
			}
		} else {
			// element is a technology
			if ($(functionClass).has('input:checked').length) {
				jumpToNode(functionClass, technologyClass);
			} else {
				highlightFunctionsForTechnology(functionClass, $(clickedElement).attr('name'));
				deactivateSiblings(functionClass, technologyClass, clickedElement);
			}
		}
	}
	
	var highlightTechnologiesForFunction = function(technologyClass, functionId) {
		$(technologyClass + ' input').parent().removeClass('deactivated');
		if (nodes['functions'][functionId]) {
			$(technologyClass + ' input').filter(function(index) {
				return nodes['functions'][functionId][$(this).attr('name')] == undefined;
			}).attr('disabled', true).parent().addClass('deactivated');
		} else {
			$(technologyClass + ' input').attr('disabled', true).parent().addClass('deactivated');
		}
	}
	
	var highlightFunctionsForTechnology = function(functionClass, technologyId) {
		$(functionClass + ' input').parent().removeClass('deactivated');
		if (nodes['technologies'][technologyId]) {	
			$(functionClass + ' input').filter(function(index) {
				return nodes['technologies'][technologyId][$(this).attr('name')] == undefined;
			}).attr('disabled', true).parent().addClass('deactivated');
		} else {
			$(functionClass + ' input').attr('disabled', true).parent().addClass('deactivated');
		}
	}
	
	var deactivateSiblings = function(functionClass, technologyClass, input) {
		$(input).parents(functionClass + ', ' + technologyClass ).find('input:not(:checked)').attr('disabled', true);
	}
	
	var activateSiblings = function(functionClass, technologyClass, input) {
		$(input).parents(functionClass + ', ' + technologyClass ).find('input').attr('disabled', false);
	}
	
	/**
	 * Jump to the node identified by the currently clicked items
	 */
	var jumpToNode = function(functionClass, technologyClass) {
		var technologyId = $(technologyClass + ' input:checked').attr('name');
		var functionId = $(functionClass + ' input:checked').attr('name');
		
		var nodeId = nodes['functions'][functionId][technologyId];
		
		openUrl('/' + urlProto + '&tx_tvmatrix_pi3[node]=' + nodeId);
		$(functionClass + ' input' + ', ' + technologyClass + ' input').attr('disabled', true).parent().addClass('deactivated');
	}
	
	

	
	var openUrl = function(url) {
		window.location = url;
	}
	

	
	
	// initialize
	$('.functionLayers input, .technologies input').click(function () {
		handleMatrixClick('.functionLayers', '.technologies', $(this));
	});
	
	$('.buttonLink').each(function() {
		var newButton = document.createElement('button');
		$(newButton).text($(this).text());
		var link = '/' + $(this).attr('href');
		$(newButton).click(function() {
			window.location = link;
		});
		
		$(this).replaceWith(newButton);
	});
	
	/*
	* Test whether argument elements are parents
	* of the first matched element
	* @return boolean
	* @param objs
	* 	a jQuery selector, selection, element, or array of elements
*/
$.fn.hasParent = function(objs) {
	// ensure that objs is a jQuery array
	objs = $(objs); var found = false;
	$(this[0]).parents().andSelf().each(function() {
		if ($.inArray(this, objs) != -1) {
			found = true;
			return false; // stops the each...
		}
	});
	return found;
}
});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

	// clean up all changes
	var resetForm = function($) {
		$('.functionLayers input, .technologies input').attr('disabled', false).attr('checked', false).parent().removeClass('deactivated');
	}
	
	var preselect = function($) {
		if (typeof technologyId != 'undefined' && typeof functionId != 'undefined') {
			$('.functionLayers input[name=' + functionId + ']').attr('checked', true);
			$('.technologies input[name=' + technologyId + ']').attr('checked', true);
			$('.functionLayers input:not(:checked), .technologies input:not(:checked)').attr('disabled', true).parent().addClass('deactivated');
		}
	}
