var colors = {
	
	obj : null,
	
	init : function() {
		this.changeItem();
		global.makeTips({x:35,y:-5}, '.make-tip-multiple', 'header-tool');
		this.filter();
	},
	
	filter : function() {
		if (global.selected == 'colorizer' ) this.begin();
		if (global.selected == 'ram_in_action') {
			if(userData.bandwidth == 'low') document.location = global.site;
		}
	},
	
	
	begin : function() {
		// set colorizer obj
		this.obj = nav.obj.selected
		
		// show intro video logic
		this.cookie();
		
		// remove gallery scroll buttons
		$('gallery_button_right').remove();
		$('gallery_button_left').remove();
		
		// move swatches into colorizer
		this.move();
		
		// build vehicle switcher
		this.build();
		
		// add get updates button
		this.addGetUpdates();
	},
	
	
	cookie : function() {
		if ( Cookie.get('colors_intro') == false ) {
			Cookie.set('colors_intro', true);
			global.colors_intro = false;
		}
		
		else {
			global.colors_intro = true;
		}
	},
	
	
	changeItem : function() {
		var page_nav = $('page_nav_list');
		var colorizer = page_nav.getElement('li.colorizer');
		var clone = colorizer.clone();
			
			clone.injectTop(page_nav);
			colorizer.remove();
			
			var color_list = clone.getElement('ul');
			var color_items = color_list.getElements('li');
			
			var color = nav.obj.parents['colorizer'] = new Object();
				color['element'] = color_list;
				color['gParent'] = "colorizer";
				color['height'] = color_list.getAttribute('name').toInt();
				
				color_items.each( function(item) {
						var name = item.className;
						var child =	color[name] = new Object();
							child['element'] = item;
				});
				
				var type = (colorizer == nav.obj.selected) ? true : '';
				nav.assignHandler(['colorizer'], type);
	},
	
	
	move : function() {	
		var container = new Element('div', { 'id' : 'swatch_container' });
			container.injectInside( $('page_gallery') );
			
		var description = new Element('span');
			description.injectInside(container);
		
		var trims = global.build( this.obj );		
		trims.each( function(trim) {
			
			if ( trim.hasClass('invisible') ) {
				var swatches = trim.getElements('li');
				var vehicle = new Element('ul', {
						'class' : trim.className + ' multiple'
				});
				
				// move swatches into new <ul>'s
				// display swatch name on mouseover
				swatches.each( function(swatch) {
						var swatch_description = swatch.getAttribute('name');
						if (swatch_description.indexOf('/') != -1) {
							swatch_description = swatch_description.split('/')[0];
						}
						
						swatch.addEvent('mouseenter', function() {
								description.setText(swatch_description);
						});
						
						swatch.addEvent('mouseleave', function() {
								description.setText('');
						});
						
						swatch.injectInside(vehicle);
				});
				
				// place new <ul>'s into 'swatch_container' <div>				
				vehicle.injectInside(container);
				vehicle.removeClass('invisible');
				
				var parents = global.innerOBJ( gallery.obj.parents, true);
				parents.each( function(parent) {
						if ( trim.hasClass(parent) ) {
							nav.obj.parents[parent]['element'] = vehicle;
						}
				});
				
				trim.remove();
			}
		});
	},
	
	
	build : function() {
		// Primary Trim
		var hero_desc = new Element('p', {
				'id' : 'hero_desc',
				'class' : global.parent
		});
		
		// Secondary Trim
		var switcher = new Element('div', {
				'id' : 'colorizer_switch',
				'class' : global.parent
		});		
		var button = new Element('a', {
			'events' : {
				'click' : function() {
					colors.switchTrims();
				}
			},
			'href' : '#'
		});		
		var info = new Element('p').setProperty('id', 'colorizer_info');
		
			// Primary Trim
			hero_desc.injectInside( $('page_gallery') );
		
			// Secondary Trim
			button.injectInside(switcher);
			info.injectInside(switcher);
			switcher.injectInside( $('page_gallery') );
			
			// initially highlight active child in colorizer subnav
			nav.obj.parents[global.selected][global.parent]['element'].addClass('selected');
		
		this.setDescriptions();
	},
	
	
	switchTrims : function() {
		global.parent = (global.parent == 'laramie') ? 'sport' : 'laramie';
		global.newParent(true);
		this.highlight();
		this.setDescriptions();
	},
	
	
	highlight : function() {
		var parents = global.innerOBJ( gallery.obj.parents, true );
			parents.each( function(nav_child) {
					var child = nav.obj.parents[global.selected][nav_child]['element'];
						if ( child.hasClass('selected') ) child.removeClass('selected');
			});
			
			nav.obj.parents[global.selected][global.parent]['element'].addClass('selected');		
	},
	
	
	setDescriptions : function() {
		// Primary Trim
		var hero_desc = $('hero_desc');
		var swatch = nav.obj.parents[global.parent][global.child]['element'].getAttribute('name');
		
		// Secondary Trim
		var switcher = $('colorizer_switch').getElement('a');
		var info = $('colorizer_info');
		
			if (switcher.hasClass(global.parent) == false) switcher.className = '';
			switcher.addClass(global.parent);
		
			switch( global.parent ) {
				
				case 'laramie':
					switcher.setStyles({
						'width' : '205px',
						'height' : '110px'
					});
					info.setText('Colorize Ram Sport');
					hero_desc.setHTML('Ram Crew 1500 Larmie in <span>' + swatch + '</span>');
					break;
					
				default:
					switcher.setStyles({
						'width' : '230px',
						'height' : '120px'					
					});
					info.setText('Colorize Ram Laramie');
					hero_desc.setHTML('Ram Crew 1500 Sport in <span>' + swatch + '</span>');
			}
	},
	
	
	addGetUpdates : function() {
		var feature_GU_btn = $('get_updates');
		var containers = $$('div.content');
		
		if (feature_GU_btn) {			
			containers.each( function(div) {
					var button = new Element('a');
						button.set({
							'class' : 'get_updates_btn',
							'href' : feature_GU_btn.getAttribute('href'),
							'title' : feature_GU_btn.getAttribute('title'),
							'name' : '&lid=get_updates&lpos=content'
						});
						button.setText(feature_GU_btn.getText());
						button.injectInside(div);
					
			});
		}
	}
};


onload_register('colors.init()');
