/* Copyright (c) 2008 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version: 1.0.3
 * Requires jQuery 1.1.3+
 * Docs: http://docs.jquery.com/Plugins/livequery
 */
(function(a){a.extend(a.fn,{livequery:function(f,e,d){var c=this,g;if(a.isFunction(f)){d=e,e=f,f=undefined}a.each(a.livequery.queries,function(h,j){if(c.selector==j.selector&&c.context==j.context&&f==j.type&&(!e||e.$lqguid==j.fn.$lqguid)&&(!d||d.$lqguid==j.fn2.$lqguid)){return(g=j)&&false}});g=g||new a.livequery(this.selector,this.context,f,e,d);g.stopped=false;g.run();return this},expire:function(f,e,d){var c=this;if(a.isFunction(f)){d=e,e=f,f=undefined}a.each(a.livequery.queries,function(g,h){if(c.selector==h.selector&&c.context==h.context&&(!f||f==h.type)&&(!e||e.$lqguid==h.fn.$lqguid)&&(!d||d.$lqguid==h.fn2.$lqguid)&&!this.stopped){a.livequery.stop(h.id)}});return this}});a.livequery=function(c,e,g,f,d){this.selector=c;this.context=e||document;this.type=g;this.fn=f;this.fn2=d;this.elements=[];this.stopped=false;this.id=a.livequery.queries.push(this)-1;f.$lqguid=f.$lqguid||a.livequery.guid++;if(d){d.$lqguid=d.$lqguid||a.livequery.guid++}return this};a.livequery.prototype={stop:function(){var c=this;if(this.type){this.elements.unbind(this.type,this.fn)}else{if(this.fn2){this.elements.each(function(d,e){c.fn2.apply(e)})}}this.elements=[];this.stopped=true},run:function(){if(this.stopped){return}var e=this;var f=this.elements,d=a(this.selector,this.context),c=d.not(f);this.elements=d;if(this.type){c.bind(this.type,this.fn);if(f.length>0){a.each(f,function(g,h){if(a.inArray(h,d)<0){a.event.remove(h,e.type,e.fn)}})}}else{c.each(function(){e.fn.apply(this)});if(this.fn2&&f.length>0){a.each(f,function(g,h){if(a.inArray(h,d)<0){e.fn2.apply(h)}})}}}};a.extend(a.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if(a.livequery.running&&a.livequery.queue.length){var c=a.livequery.queue.length;while(c--){a.livequery.queries[a.livequery.queue.shift()].run()}}},pause:function(){a.livequery.running=false},play:function(){a.livequery.running=true;a.livequery.run()},registerPlugin:function(){a.each(arguments,function(d,e){if(!a.fn[e]){return}var c=a.fn[e];a.fn[e]=function(){var f=c.apply(this,arguments);a.livequery.run();return f}})},run:function(c){if(c!=undefined){if(a.inArray(c,a.livequery.queue)<0){a.livequery.queue.push(c)}}else{a.each(a.livequery.queries,function(d){if(a.inArray(d,a.livequery.queue)<0){a.livequery.queue.push(d)}})}if(a.livequery.timeout){clearTimeout(a.livequery.timeout)}a.livequery.timeout=setTimeout(a.livequery.checkQueue,20)},stop:function(c){if(c!=undefined){a.livequery.queries[c].stop()}else{a.each(a.livequery.queries,function(d){a.livequery.queries[d].stop()})}}});a.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove");a(function(){a.livequery.play()});var b=a.prototype.init;a.prototype.init=function(d,f){var e=b.apply(this,arguments);if(d&&d.selector){e.context=d.context,e.selector=d.selector}if(typeof d=="string"){e.context=f||document,e.selector=d}return e};a.prototype.init.prototype=a.prototype})(jQuery);
/*
* Where do I fit in tool.
* Shows and hides companies acording to user's selections
*
*/
var noDiscipline				=	[ "Capital", "Construction", "Engineering", "Ground", "Major", "Mansell", "Parsons", "Rail", "Regional", "Utility", "Workplace", "LivingPlaces"];
var Architecture 				=	[ "Parsons" ];
var BuildingConstruction 		=	[ "Construction", "Ground", "Mansell", "Utility", "LivingPlaces"  ];
var BuildingServices 			=	[ "Construction", "Engineering", "Workplace", "LivingPlaces" ];
var BusinessRelated 			=	[ "Capital", "Construction", "Major", "Rail", "Regional", "Workplace", "LivingPlaces" ];
var ChemicalEngineering 		=	[ "Parsons" ];
var CivilEngineering 			=	[ "Construction", "Ground", "Major", "Parsons", "Rail", "Regional", "Utility", "LivingPlaces" ];
var ElectricalEngineering 		=	[ "Engineering", "Parsons", "Rail", "Utility", "Workplace", "LivingPlaces" ];
var EnvironmentalEngineering 	= 	[ "Parsons", "LivingPlaces" ];
var Geography 					=	[ "Parsons" ];
var Maths 						=	[ "Parsons" ];
var MechanicalEngineering 		=	[ "Engineering", "Parsons", "Rail", "Utility", "Workplace", "LivingPlaces" ];
var QuantitySurveying 			=	[ "Construction", "Engineering", "Ground", "Major", "Mansell", "Parsons", "Rail", "Regional", "Utility", "Workplace", "LivingPlaces"];
var TransportationPlanning 		= 	[ "Parsons" ];

var Capital = { 
	size :	  {  small :        1, large :    1 },
	ptype :   { consultant :    1, delivery : 1, technical: 1},
	training: { professional:   1, onthejob:  1},
	role: 	  { technicalRole:  0, people:    1, commercial:1}
}
var Regional = { 
	size :	  {  small :        1, large :    0 },
	ptype :   { consultant :    0, delivery : 1, technical: 1},
	training: { professional:   1, onthejob:  0},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Major 	= { 
	size :	  {  small :        0, large :    1 },
	ptype :   { consultant :    0, delivery : 1, technical: 1},
	training: { professional:   1, onthejob:  0},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Construction = { 
	size :	  {  small :        1, large :    1 },
	ptype :   { consultant :    1, delivery : 1, technical: 0},
	training: { professional:   1, onthejob:  0},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Engineering = Construction;
var Ground = Construction;
var Mansell = { 
	size :	  {  small :        1, large :    0 },
	ptype :   { consultant :    0, delivery : 1, technical: 0},
	training: { professional:   1, onthejob:  0},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Rail = { 
	size :	  {  small :        1, large :    1 },
	ptype :   { consultant :    1, delivery : 1, technical: 1},
	training: { professional:   1, onthejob:  1},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Parsons = { 
	size :	  {  small :        1, large :    1 },
	ptype :   { consultant :    1, delivery : 0, technical: 0},
	training: { professional:   1, onthejob:  0},
	role: 	  { technicalRole:  1, people:    1, commercial:1}
}
var Utility = Rail;
var Workplace = { 
	size :	  {  small :        1, large :    1 },
	ptype :   { consultant :    0, delivery : 0, technical: 1},
	training: { professional:   0, onthejob:  1},
	role: 	  { technicalRole:  0, people:    1, commercial:1}
}
var LivingPlaces = {
	size :	  {  small :        1, large :    0 },
	ptype :   { consultant :    0, delivery : 1, technical: 1},
	training: { professional:   0, onthejob:  1},
	role: 	  { technicalRole:  0, people:    1, commercial:1}
}

var selectDiscipline = "noDiscipline";
var size  		= "";
var ptype 		= "";
var training 	= "";
var role 		= "";
var showHide 	= 1;
var container	= "div#info";

function where(){
	$(document).ready(function (){
		
		$('form, .read-more').show();
		$('.ldesc').hide();
		$('.read-more').livequery("click", function (e){
			$(this).parent().siblings('.ldesc').toggle();
			if($(this).html() == 'Read more'){
				$(this).html('Read less');
			}
			else{
				$(this).html('Read more');
			}
			e.preventDefault();
			$("#outer").stop(true, true).animate({ height: $("#where-do-i-fit-in").height() }, 1000, "easeInOutExpo");
			return false;
		});
		
		$('input[type=radio]').livequery("click", function (){
			getChecked();
		});
		$('select').livequery("change", function(){
			selectDiscipline = $(this).attr("value");
			getChecked();
		});
		$('select').livequery("keyup", function(){
			selectDiscipline = $(this).attr("value");
			getChecked();
		});
		$('#startAgain').livequery("click",function (e){
			$('input:radio').each(function(){ if($(this).is(':checked')){ $(this).attr("checked",""); } });
			$('select').each(function(){ $(this).val($('option:first', this).val());});
			resetForm();
			e.preventDefault;
			return false;
		});
		/*
		* Creates variables acording with the checked radio buttons e.g.
		* size = "small"; ptype = "consultant"; training = "professional"; role = "commercial"; 
		*/
		function getChecked(){
			var variables = "";
			var tempDiscipline = eval(selectDiscipline);
			
			$('input[type=radio]').each(function (){ 
				if($(this).is(":checked")){
					variables += this.name + ' = "' + this.value + '"; ';
				}
			});
			if(variables !== "" ){
				eval(variables);
			}
			showByDiscipline(tempDiscipline);
		}
		/*
		* Check if the company should be displayed or hidden
		* If any value is 0 should be hidden, otherwise should be displayed.
		* Returns 1: show, 0: hide
		*/
		function checkOperatingCompanies(company){
			
			var variables 	= "";
			var sizeVal   	= 1;
			var ptypeVal  	= 1;
			var trainingVal = 1;
			var roleVal 	= 1;
			
			if(size.length > 0){
				variables = company + ".size." + size;
				sizeVal = eval(variables);
			}
			if(ptype.length > 0){
				variables = company + ".ptype." + ptype;
				ptypeVal = eval(variables);
			}
			if(training.length > 0){
				variables = company + ".training." + training;
				trainingVal = eval(variables);
			}
			if(role.length > 0){
				variables = company + ".role." + role;
				roleVal = eval(variables);
			}
					
			return sizeVal && ptypeVal && trainingVal && roleVal;
			
		}
		
		/*
		* Show/Hide only companies that belongs to a selected discipline
		* params: 
		*/
		function showByDiscipline(discipline){
	
			var objShow = new Array();
			var strShow = "";
			for(i=0; i< discipline.length; i++){
				showHide = checkOperatingCompanies(discipline[i]);
				if(showHide == 1 ){
					objShow.push("#"+discipline[i]);
				}
			}
			
			strShow = objShow.join();
			$(container+' div').not(strShow+' ,.spacer, .spacer-2').slideUp("normal", function(){
				if(objShow.length > 0){
					$(strShow).each( function() {
						if( !$(this).is(':visible') ){
							$(strShow).slideDown( '', function () {$("#outer").stop(true, true).animate({ height: $("#where-do-i-fit-in").height() }, 1000, "easeInOutExpo");});
						}
					});
				}
				else{
					$("#no-results").show('',function(){ });
				}
			});
			$("#outer").stop(true, true).animate({ height: $("#where-do-i-fit-in").height() }, 1000, "easeInOutExpo");
		}
		/*
		* Resets the form to its original state
		*/
		function resetForm(){
			selectDiscipline = "noDiscipline";
			size = "";
			ptype = "";
			training = "";
			role = "";
			$('#no-results').hide();
			$(container).children('div').not('#no-results').stop(true, true).show();
			$("#outer").stop(true, true).animate({ height: $("#where-do-i-fit-in").height() }, 1000, "easeInOutExpo");
		}
		
	});
}
