/** Portfolio JS
	Adjusts the height of the portfolio_holder to the size of the total rendered area to accurately display the dividers.
	 **/

var adjustHeight = function (){

	var adjusted_divs = $$('#portfolio_holder, #column1_down');
	
	if(adjusted_divs){
	
		adjusted_divs.setStyle('height','');
		adjusted_divs.setStyle('height',$('portfolio_contents').getScrollSize().y);
	
	}	

	
};

/*
|--------------------------------------------------------------------------
| --- IVP Portfolio JS > checkBox and verticalSorter Class
|--------------------------------------------------------------------------
|
| When instantiated, the verticalSorter creates a class named "checkBox" for every DOM input checkbox on the page.
| 
| 
| Each checkBox class includes:
|   The value of the checkbox it has been added to, unmodified. (value)
| 	The value of the checkbox it has been added to, hyphenated. (target)
| 	An Elements array of every DOM profileElement to which it's vertical or geographic location corresponds (members). 
|		-These are found by copmaring each element of type profileElement to the classes superceding "profileElement" in that element.
|  		-I.E, if a DOM element has class attribute "profileElement AMERICAS Network-Management", it is added to the AMERICAS and Network Management vertical_boxes.
|
|	The status of this checkBox. Each checkbox starts at false by default. Note: All is set to false by default in the view. All and shown have different functions.
| 
| As the verticalSorter adds a checkBox to every DOM input checkbox, it adds an eventListener to that DOM input checkbox. When toggled, the checkbox's eventListener
| fires an event which :
|
| First calls the verticalSorters' "hideAll" function, which toggles all of the profileElement's to have "display: none".
| Then toggles the status of the checkBox associated with the checkbox DOM element which fired the event i.e. from "visible" to "hidden".
|	- (checkBox.status.true and checkBox.status.false, respectively.)
| Then adds/removes the checkBox to/from an array of vertical_boxes which contains all currently "shown" checkboxes.
| Then iterates through the array of all "shown" vertical_boxes and sets each Elements array that it possesses to have "display: normal;"
| Then sets the height of the div 'portfolio_holder' to equal its new scrollSize.
| 
*/

		// This will remove all of the checks by default.
		// Added by Joey.
		window.addEvent('domready', function() {
								 
			if (portfolio == 1) {

				var checkbox = $$('input');

				checkbox.each(function(element) {
								   
					element.checked = false;

				});

				checkbox[2].checked = true;

			}
		});
		

var checkBox = new Class({
	value: $empty,
	target: $empty,
	
	all: $empty,
	active: $empty,
	
	all_AMERICAS: $empty,
	all_ASIA: $empty,
	all_EUROPE: $empty,
	active_AMERICAS: $empty,
	active_ASIA: $empty,
	active_EUROPE: $empty,
	
	status: false,
	initialize: function(target_value){
	
		this.value = target_value;
		this.target = target_value.replace(/[\s\\\/]/g,'-');
		
		this.all = $$('div.'+this.target);
		
		this.all_AMERICAS = this.all.filter(function(element){
			return element.hasClass('AMERICAS');
		});
		this.all_ASIA_PACIFIC = this.all.filter(function(element){
			return element.hasClass('ASIA_PACIFIC');
		});
		this.all_EUROPE = this.all.filter(function(element){
			return element.hasClass('EUROPE');
		});
		
		this.active = this.all.filter(function(element){
			return element.hasClass('Active');
		});
		
		this.active_AMERICAS = this.active.filter(function(element){
			return element.hasClass('AMERICAS');
		});
		this.active_ASIA_PACIFIC = this.active.filter(function(element){
			return element.hasClass('ASIA_PACIFIC');
		});
		this.active_EUROPE = this.active.filter(function(element){
			return element.hasClass('EUROPE');
		});

	},
	showElements: function(exclusive, inclusive){
	
		if(this[exclusive] != $empty){
			if(inclusive.length != 0){
				inclusive.each(function(inclusive_filter){
					
					this[exclusive+'_'+inclusive_filter].setStyle('display','block');
					
				},this);
			}
			else this[exclusive].setStyle('display','block');
		}
		
	}

});

var verticalSorter = new Class ({

	
	general_boxes: $empty,
	area_boxes: $empty,
	vertical_boxes: $empty,
	shown: $empty,
	exclusive_filter: $empty,
	inclusive_filters: [],
	all_box: $empty,
	initialize: function(){
	
		this.shown = new Elements();
		
		this.general_boxes = $$('input.exclusive_selector[type=checkbox]');
		this.area_boxes = $$('input.inclusive_selector[type=checkbox]');
		this.vertical_boxes = $$('input.additive_selector[type=checkbox]');
		
		this.general_boxes.each(function(element){
		
			element.addEvent('click',this.changeExclusive.bindWithEvent(this));
			
		},this);
		
		this.exclusive_filter = this.general_boxes.filter(function(element){
		
			return element.get('checked');		
		
		})[0];
		
		this.area_boxes.each(function(element){
		
			element.addEvent('click',this.changeInclusive.bindWithEvent(this));
		
		},this);
			
		this.vertical_boxes.each(function(element){
		
			var thisBox = new checkBox(element.get('value'));
			element.addEvent('click',this.handleFilter.bindWithEvent(thisBox,this));
				
		},this);
		
		this.all_box = new checkBox('portfolio_logo_box');
			
	},
	changeExclusive: function(event){
	
		var target = event.target;
		
		if(target != this.exclusive_filter){
			if(this.exclusive_filter){
				this.exclusive_filter.set('checked',false);
			}
			this.exclusive_filter = target;	
		}
		else{
			this.exclusive_filter.set('checked',true);
		}
		
		this.hideAll().showSelected();
		adjustHeight();
		
	},
	changeInclusive: function(event){
	
		var inclusive_toggle = event.target.get('value');
		if(inclusive_toggle == 'ASIA/PACIFIC') inclusive_toggle = 'ASIA_PACIFIC';
		 
		if(this.inclusive_filters.contains(inclusive_toggle)){
			this.inclusive_filters.erase(inclusive_toggle);
		}
		else{
			this.inclusive_filters.include(inclusive_toggle);
		}
		
		this.hideAll().showSelected();
		adjustHeight();
	},
	handleFilter: function(event,sorter){
	
		event = new Event(event);
		sorter.hideAll().toggle(this).showSelected();
		adjustHeight();	
		
	},
	toggle: function(thisBox){
	
		if(thisBox.status){
			thisBox.status = false;
			this.shown.erase(thisBox);			
		}else{
			thisBox.status = true;
			this.shown.include(thisBox);			
		}
	
		return this;
	
	},
	hideAll: function(){
	
		this.all_box.all.setStyle('display','none');
		return this;
	
	},
	showSelected: function(){
	
		var exclusive_filter = this.exclusive_filter.get('value');
		var inclusive_filters = this.inclusive_filters;
		
		if(this.shown.length != 0){
		
			this.shown.each(function(thisBox){
				
				thisBox.showElements(exclusive_filter,inclusive_filters);
			
			},this);
		}
		else{
		
			this.all_box.showElements(exclusive_filter,inclusive_filters);	
		
		}
		
		return this;
	}

});

var adjustBackdrop = function(){

	if( $('company_content').getSize().y < $('company_text').getSize().y ){
		if(Browser.Engine.trident){
			$('company_backdrop').setStyle('left',590);
		}
		else{
			$('company_backdrop').setStyle('left',592);
		}
	}
	
	$('company_backdrop').addEvent('mousewheel',function(event){
	
		event.stop();
	   
	    var company_content = $('company_content');
		company_content.scrollTo(0,company_content.getScroll().y - event.wheel*20);
	
	});

}

var addHighlights = function(){

	$$('div.portfolio_logo_box').addEvents({
	
		'mouseover': function(){
			$(this.getParent().get('id').replace(/_down/,'')).addClass('selected');
		},
		'mouseout': function(){
			$(this.getParent().get('id').replace(/_down/,'')).removeClass('selected');
		}
		
	});

}

var addHighlights2 = function(){

	$('company_list').getElements('li').addEvents({
	
		'mouseover': function(){
			$(this.get('class')).addClass('selected');
		},
		'mouseout': function(){
			$(this.get('class')).removeClass('selected');
		}
	
	});

}
