

/*	James

	File:		Navigation JS
	Author:		James Brannon

	-------------------------------------------------------------------------------------------------------------------- */
	
	function Navigation(construct) {this.pages;this.work;this.jobs;this.init(construct);}
		Navigation.prototype = {
			init: function(construct) {
				var instance = this;
				this.construct = construct;
				this.gridObj = construct[4];
			
				var nav = $('.'+construct[0]+' nav a.inc'),
					holder = $('#'+construct[1]),
				    slides = $('#'+construct[1]+' li.'+construct[2]),
					content = [holder,slides];
				
				//- populate pages array
				this.pages=[];
				for(var i=0;i<nav.length;i++){
					this.pages.push($(nav[i]).attr('title').toLowerCase());
				};
					
				//- add listeners to site nav
				$(nav).click(function() {instance.click(this);});
				
				//- check url
				$.address.change(function(event) { instance.check(nav,content) });

				//- load work
				this.limit = '?limit='+construct[3];
				this.load($.address.baseURL()+'/inc/jobs.php'+this.limit,nav,content);
				
			},
			check: function(nav,content){

				//- define request and populate page pages array
				var id = $.address.value().replace('/', ''),
					request;

				//- if no request made default to work
				request = this.lookup(id,this.pages);

				//- action request
				if(request>=0){
					//- go to page
					this.action(request,content,nav,true);
					//- track request
					
					trackme('request','url',this.pages[request]);
				}else{
					//- default to home page
					this.action(0,content,nav);
					
					//- only action if JSON is loaded
					if(this.work){
						request = this.lookup(id,this.jobs);
						//- load job
						if(request>=0){
							var job = new Work(this.work[request],10000);
						}
						//- track request
						trackme('request','url',this.work[request].job.toLowerCase());
					}
				}
				
			},
			click: function(subject){
				$.address.value($(subject).attr('title').toLowerCase())
				//- track request
				trackme('navigation','click',$(subject).attr('title').toLowerCase());
			},
			lookup: function(id,arr){
				if(!id){id=arr[0]};
				return $.inArray(id,arr);
			},
			action: function(id,content,nav,collapse){
			
				//- define
				var holder = $(content[0]),
					offset = $(content[1]).width(),
					newPos = parseInt(-(offset*id));
					
				//- animate
				holder.stop().animate({'margin-left':newPos}, 500, 'easeOutCirc');
				
				//- remove preview if home/work selected
				if(collapse&&document.getElementById('preview')){
					$('html,body').stop().animate({scrollTop: 0}, 500, 'easeOutCirc');
					$('#preview').stop().animate({'height': 0,'padding':0}, 300, 'easeOutCirc', function(){
						window.clearInterval(interval);
						$('#preview').remove();
					});
					//- reset grid
					this.gridObj.check();
				}
				
				//- adjust nav button styles
				nav.removeClass('active');
				$(nav[id]).addClass('active');
			},
			load: function(url,nav,content){
				var instance = this;
				$.getJSON(url, function(work) {
					instance.output(work,nav,content);
				});
			},
			output: function(work,nav,content){
				var instance = this;
				if (work) {this.work = work};

				//- populate jobs array
				this.jobs=[];
				for(var i=0;i<work.length;i++){
					this.jobs.push(work[i].base);
				};
				
				//- run check for deep linking jobs
				instance.check(nav,content);
			
			}
	}
