

/*	James

	File:		Layout JS
	Author:		James Brannon

	-------------------------------------------------------------------------------------------------------------------- */
	
	function Layout(construct,page) {this.init(construct,page);}
		Layout.prototype = {
			init: function(construct,page) {
				this.construct = construct;

				var layout = construct.layout,
					mask = '#'+layout.mask,
					holder = '#'+layout.holder,
					pages = layout.pages,
					nav = $('.'+layout.nav+' nav a.inc');
				
				var viewport = document.body.clientWidth,
				    padding = parseInt($('body').css('padding-left')),
				    width = viewport-(padding*2),
					slides = holder+' li.'+pages,
					pos = -(width*this.page(nav));
				
				//- adjust stage
				$(mask,slides).width(width);
				$(slides).width(width);
				$(holder).width((width*$(slides).length));
				$(holder).css({'margin-left': pos});
				$(slides).show();
				this.carousel();
			},
			page: function(arr){
				//- page lookup
				var id = $.address.value().replace('/', ''),
					request,
					items = [];
				for(var i=0;i<arr.length;i++){
					items.push($(arr[i]).attr('title').toLowerCase());
				};
				if(!id){id=items[0]};
				if($.inArray(id,items)<0){
					return 0;
				}else{
					return $.inArray(id,items);
				}
				
			},
			carousel: function(){
				
				//- get preview carousel
				var preview = document.getElementById('preview');
				
				//- action if present
				if(preview){
					var w = $(preview.getElementsByTagName('article')).width(),
						p = parseInt($('#carousel').css('paddingRight'))*2,
						m = parseInt($('#carousel').css('marginRight')),
						aw = w-p-m,		//- available space
						tw = aw * $('#carousel li').length;
					
					//- set the new image width
					$('#carousel li').width(aw);
					//- set carousel width
					$('#carousel ul').width(tw);
					
					//- now set the height to adjust accordingly
					var	ah = $('#carousel li img').height();
					if(!ah){
						//- fallback equation
						ah = Math.round(aw*(470/960));
					}
					//- set the new image height
					$('#carousel li').height(ah);
				
					//- calculate current slide from controls indicator position (constant)
					var viewport = document.body.clientWidth,
						offset = parseInt($('#controls li').css('marginLeft')),
						bpos = parseInt($('#controls .indicator').css('backgroundPosition')) - offset,
						bw = $('#controls li').width()+offset,
						cpos = bpos/bw,
						md = -(aw*cpos);
						
					//- adjust carousel offset
					//- only action if > 768 screen resolution
					if(viewport>=768){
						$('#carousel ul').css('marginLeft',md);
					}else{
						$('#carousel ul').css('marginLeft',0);
					}
					
				}
				
			}
	}
