PWP = function () {
	var moduleData={},
		eventData={},
		debug=false,
		ec=0,
		createInstance = function ( moduleId ) {
			var instance = moduleData[moduleId].creator( new Sandbox() ),
				name, method;
			if( debug === true ) {
				for( name in instance ) {
					method = instance[name];
					if( typeof method == 'function' ) {
						instance[name] = function ( method, name ) {
							return function () {
								try { return method.apply( this, arguments ); }
								catch( err ) { PWP.log( moduleId + ' - ' + name + '(): ' + err.message ); }
							};
						}( method, name );
					}
				}
			}
			return instance;
		};
	return {
		register: function ( moduleId, creator ) {
			moduleData[moduleId] = {
				creator: creator,
				instance: null
			};
		},
		start: function ( moduleId ) {
			moduleData[moduleId].instance = createInstance( moduleId );
			moduleData[moduleId].instance.init();
		},
		stop: function ( moduleId ) {
			var data = moduleData[moduleId];
			if( !!data.instance ) {
				data.instance.destroy();
				data.instance = null;
			}
		},
		startAll: function () {
			var moduleId;
			for( moduleId in moduleData ) {
				if( moduleData.hasOwnProperty(moduleId) ) {
					this.start( moduleId );
				}
			}
		},
		stopAll: function () {
			var moduleId;
			for( moduleId in moduleData ) {
				if( moduleData.hasOwnProperty(moduleId) ) {
					this.stop( moduleId );
				}
			}
		},
		addListener: function ( events, handler, ctx, allowDupes ) {
			var l = events.length,
				i = 0;
			for( i; i < l; i++ ) {
				var j = 0,
					found=false,
					m;

				if( !eventData[events[i]] ) {
					eventData[events[i]] = [];
				}

				m = eventData[events[i]].length;

				for( j; j < m; j++ ) {
					if( handler == eventData[events[i]].handler && ctx == eventData[events[i]].ctx && !allowDupes ) {
						found = true;
					}
				}
				if( !found ) {
					eventData[events[i]].push({
						handler: handler,
						ctx: ctx
					});
				}
			}
		},
		dispatchEvent: function ( msgObj ) {
			var e = msgObj.e,
				data = [],
				i = 0,
				ev, l, prop;

			for( prop in msgObj.data ) {
				data.push( msgObj.data[prop] );
			}
			for( ev in eventData ) {
				if( ev == e ) {
					l = eventData[ev].length;
					for( i; i < l; i++ ) {
						var ce = eventData[ev][i];
						ce.handler.apply( ce.ctx, data );
					}
				}
			}
		},
		removeListener: function ( e, handler, ctx ) {
			var i = 0,
				l, ce;
			if( eventData[e] ) {
				l = eventData[e].length;
				for( i; i < l; i++ ) {
					ce = eventData[e][i];

					if( ce.handler == handler && ce.ctx == ctx ) {
						eventData[e].splice( i, 1 );
						break;
					}
				}
			}
		},
		clearEventType: function ( type ) {
			if( eventData[type] ) {
				eventData[type] = [];
			}
		},
		log: function ( msg ) {
			var ret, $c;
			ec++;
			ret = 'err' + ec + ': ' + msg;
			$c = $('#console');
			if( $c.length > 0 ) {
				var orig = $c.html();
				$c.html( orig + "\n" + ret );
			}
			else if( console ) {
				console.log( ret );
			}
			else {
				return ret;
			}
		}
	};
}();

Sandbox = function () {

	return {
		listen: function ( events, handler, ctx, allowDupes ) {
			events = typeof events == 'string' ? [events] : events;
			PWP.addListener( events, handler, ctx, allowDupes );
		},
		notify: function ( msgObj ) {
			PWP.dispatchEvent( msgObj );
		},
		kill: function ( e, handler, ctx ) {
			PWP.removeListener( e, handler, ctx );
		},
		killType: function ( e ) {
			PWP.clearEventType( e );
		},
		refresh: function () {
			PWP.stopAll();
			PWP.startAll();
		}
	};
};

PWP.register('adjust', function ( sandbox ) {
	var _;
	return {
		init: function () {
			_ = this;
			$(window).bind('resize',_.adjust);
			
		},
		adjust: function ( e ) {
			var w = $(window).width(),
				h = $(window).height();

			sandbox.notify({
				e: 'adjust',
				data: {
					width: w,
					height: h
				}
			});
			e.preventDefault();
		},
		destroy: function () {
			$(window).unbind('resize',_.adjust);
		}
	}
} );

PWP.register('home-carousel', function( sandbox ) {
	var _,
		MAX_HEIGHT = 0,
		MAX_WIDTH = 0,
		MIN_HEIGHT = 0,
		MIN_WIDTH = 0,
		container,
		offset,
		images,
		len,
		homeImage,
		captions,
		titles,
		arrowLeft,
		arrowRight,
		homeImage,
		arrowContainer,
		anim=false,
		currentIndex = 0,
		timer,
		duration,
		fade,
		imageOffset,
		footer,
		_w=0;

	return {
		init: function () {
			_ = this;

			homeImage = $('#home-image');

			if( !homeImage[0] ) return false;
			captions = $('.image-link');
			arrowContainer = $('.arrow-container');
			footer = $('#home-footer');
			container = $('.image-container').css('overflow','hidden');
			imageOffset = container.offset().top;
			MIN_WIDTH = 1050;
			MIN_HEIGHT = 600-imageOffset-70;
			duration = container.data('duration') || 5000;
			fade = container.data('fade') || 850;
			fade = container.data('fade') || 850;

			images = container.find('img');
			len = images.length;

			_.preload();

			sandbox.listen('adjust',_.adjust,_);

			arrowLeft=$('.image-link-arrow.left').css('z-index',9999).bind('click',_.prevItem);
			arrowRight=$('.image-link-arrow.right').css('z-index',9999).bind('click',_.nextItem);
			homeImage = $('#home-image');
			offset = homeImage.offset().left;

			_.timerAdvance();

			$(window).bind('adjust',_.adjust);
			$(window).bind('keydown',_.keydown);
		},
		preload: function () {
			var i,img;
			for( i=0; i<len; i++ ) {
				var img = new Image();
				img.src = images.eq(i).attr('src');
			}
		},
		timerAdvance: function () {
			timer = setInterval(_.nextItem, 5000);
		},
		keydown: function ( e ) {
			switch( e.keyCode ) {
				case 39:
					_.nextItem( e );
					break;
				case 37:
					_.prevItem( e );
					break;
			}
		},
		nextItem: function ( e ) {
			_.setCurrentIndex( currentIndex+1 );
			if( e ) {
				e.preventDefault();
				clearInterval(timer);
			}
		},
		prevItem: function ( e ) {
			_.setCurrentIndex( currentIndex-1 );
			if( e ) {
				e.preventDefault();
				clearInterval(timer);
			}
		},
		setCurrentIndex: function ( index ) {
			if ( currentIndex == index || anim == true ){
				return false;
			}
			anim = true;
			currentIndex = index;
			_.controlCurrentIndex();
			_.updateData();
		},
		controlCurrentIndex: function () {
			if( currentIndex >= len ) {
				currentIndex = 0;
			}
			else if ( currentIndex < 0 ) {
				currentIndex = len-1;
			}
		},
		updateData: function () {
			var current = images.filter('.active'),
				next = images.eq(currentIndex),
				currentCaption,nextCaption;
				
			current.css({
				zIndex: 0,
				position: 'absolute'
			});

			next.css({
				zIndex: 10,
				display: 'block',
				position: 'relative',
				opacity: 0,
				bottom: 0
			}).animate({
				opacity: 1
			}, 450, function () {
				current.css({
					zIndex: 1,
					display: 'none',
					position: 'relative',
					bottom: 0
				}).removeClass('active');
				next.css({
					zIndex: 1,
					position: 'relative'
				}).addClass('active');
				anim = false;
			} );
			
			currentCaption = captions.eq(current.parent().index()),
			nextCaption = captions.eq(next.parent().index());
			
			currentCaption.fadeOut(450, function(){
				$(this).removeClass('active')
			});
			nextCaption.fadeIn(450, function(){
				$(this).addClass('active')
			});
		},
		adjust: function ( w, h ) {
			MAX_WIDTH = w-100;
			MAX_HEIGHT = h-imageOffset-70;
			_w = w;
			//MAX_WIDTH = AX_WIDTH < MIN_WIDTH ? MIN_WIDTH : MAX_WIDTH;
			//MAX_HEIGHT = MAX_HEIGHT < MIN_HEIGHT ? MIN_HEIGHT : MAX_HEIGHT;
			images.each(_.resize);
		},
		resize: function (key, object) {
			var w, h, r;
			img = $(object);
			w = img.data('width');
			h = img.data('height');
			a = MAX_WIDTH/MAX_HEIGHT;
			ia = w/h;
			
			if( a<ia ) {
				img.css({
					width: MAX_WIDTH,
					height: 'auto'
				});
				footer.css({
					position: 'absolute',
					right: _w-MAX_WIDTH-50
				});
			}
			else {
				img.css({
					height: MAX_HEIGHT,
					width: 'auto'
				});
				r = MAX_HEIGHT/h;
				footer.css({
					position: 'absolute',
					right: _w-(w*r)-50
				});
			}
		},
		destroy: function () {
			
		}
	};
} );

PWP.register('project-thumbnails', function ( sandbox ) {
	var _,
		items,
		lists,
		thumbnail;
	return {
		init: function () {
			_ = this;

			lists = $('.project-list');

			if( !lists[0] ) return false;

			thumbnail = $('<div class="floating-thumbnail" />').css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: 220,
				height: 125,
				zIndex: 99999,
				display: 'none',
				opacity: 0,
				overflow: 'hidden'
			}).appendTo('body');

			lists.bind({
				mouseenter: _.listEnter,
				mousemove: _.listMove,
				mouseleave: _.listLeave
			});

			items = $('.project-list-item').bind({
				mouseenter: _.updateThumbnail,
				mouseleave: _.fadeItemsIn
			});
		},
		listEnter: function ( e ) {
			var list = $(this),
				dx = list.offset().left + list.width() - 220,
				dy = e.pageY-125/2 < list.offset().top ? list.offset().top : e.pageY-125/2;

			thumbnail.css({
				display: 'block',
				opacity: 0,
				left: dx,
				top: dy
			}).stop().animate({
				opacity: 1
			});
		},
		listMove: function ( e ) {
			var list = $(this),
				top = list.offset().top+10,
				bottom = list.offset().top + list.height(),
				center = e.pageY-125/2,
				dy = (center < top) ? top : center;
			
			dy = dy > bottom ? bottom : dy;

			thumbnail.css({
				top: dy
			});

		},
		listLeave: function ( e ) {
			thumbnail.stop().animate({
				opacity: 0
			}, function () {
				thumbnail.css('display','none');
			});
		},
		updateThumbnail: function ( e ) {
			var item = $(this),
				img = item.find('img').clone().css({
					opacity: 0,
					display: 'block',
					position: 'absolute',
					top: 0,
					left: 0
				}).appendTo(thumbnail).animate({
					opacity: 1
				}, function () {
					img.prevAll('img').remove();
				} );
			_.fadeItemsOut( e, item );
		},
		fadeItemsOut: function ( e, item ) {
			var id = '#'+item.attr('id');
			items.not(id).stop().animate({
				opacity: .35
			});
			item.stop().animate({
				opacity: 1
			});
		},
		fadeItemsIn: function ( e ) {
			items.stop().animate({
				opacity: 1
			});
		},
		destroy: function () {
			
		}
	}
} );

PWP.register( 'project-thumb-rollover', function ( sandbox ) {
	var _,
		thumbs;

	return {
		init: function () {
			_ = this;
			return false;
			thumbs = $('.project-item').bind({
				mouseenter: _.thumbOver,
				mouseleave: _.thumbOut
			});
		},
		thumbOver: function ( e ) {
			var thumb = $(this);

			thumb.stop().animate({
				opacity: 1
			}).siblings().stop().animate({
				opacity: .35
			},150);
		},
		thumbOut: function ( e ) {
			var thumb = $(this);

			thumb.siblings().andSelf().stop().animate({
				opacity: 1
			},350);
		},
		destroy: function () {
			
		}
	}
} );

PWP.register( 'nav-fade', function ( sandbox ) {
	var _;
	return {
		init: function () {
			if( $.browser.msie && $.browser.version < 9 ) return false;
			_ = this;
			$('.nav-menu-item, .inner-nav-item').bind({
				mouseenter: _.onOver,
				mouseleave: _.onOut
			});
		},
		onOver: function ( e ) {
			var link = $(this);

			link.stop().animate({
				opacity: 1
			}).siblings().stop().animate({
				opacity: .35
			});
		},
		onOut: function ( e ) {
			var link = $(this);

			link.siblings().andSelf().stop().animate({
				opacity: 1
			});	
		},
		destroy: function () {
			
		}
	}
} );

PWP.register('smooth-nav', function ( sandbox ) {
	var _,
		offsetNum,
		nav,
		nav,
		active,
		itemsActive,
		articles,
		re = new RegExp('^'+site_url+'(.+)$');

	return {
		init: function () {
			_ = this;
			nav = $('#side-nav');
			offsetNum = document.getElementsByTagName('body')[0].id == 'associate_book' ? 128 : 176;
			nav.find('.hijack').bind({
				click: _.hijack,
				mouseenter: _.fadeIn,
				mouseleave: _.fadeOut
			});

			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, _.route);
		},
		route: function ( e ) {
			var	id = e.path.replace(/^\/?\!\//,''),
				link = $('a[data-id="'+id+'"]'),
				a = $('article[data-id="'+id+'"]'),
				target = !!link[0] ? (a.parent().index() !== 0 ? a.children('a').eq(0).offset().top - offsetNum : 0) : false;
			link.parent().addClass('active').siblings().removeClass('active');

			if( target !== false ) {
				
				a.addClass('active').parent().siblings().children('a').removeClass('active');
				
				$('html,body').stop().animate({
					scrollTop: target
				}, 750);

			}
		},
		fadeIn: function ( e ) {
			var link = $(this).parent();

			link.stop().animate({
				opacity: 1
			}).siblings().stop().animate({
				opacity: .35
			});
		},
		fadeOut: function ( e ) {
			var link = $(this).parent();

			link.siblings().andSelf().stop().animate({
				opacity: 1
			});
		},
		hijack: function ( e ) {
			var a = $(this),
				hash = '#!/'+a[0].href.match(re)[1];
			
			window.location.hash = hash;

			e.preventDefault();
		},
		destroy: function () {}
	}
} );

PWP.register( 'project-images', function ( sandbox ) {
	var _,
		prjImage,
		images,
		prjNav,
		captions,
		MAX_HEIGHT = 0,
		MAX_WIDTH = 0,
		MAX_HEIGHT = 0,
		MAX_WIDTH = 0,
		imageOffset = 0,
		anim,
		re = new RegExp('^'+site_url+'(.+)$'),
		links,
		arrowLeft,
		arrowRight,
		wrap;

	return {
		init: function () {
			_ = this;

			anim = false;
			links = $('.image-gallery-link');

			if( !links[0] ) return false;

			prjImage = $('#project-image');
			imageOffset = prjImage.offset().top;
			MIN_WIDTH = 1050;
			MIN_HEIGHT = 600 - imageOffset - 70;

			images = prjImage.children('img').css({
				cursor: 'pointer'
			}).bind('click',_.fakeNext);

			captions = $('.caption-item');
			images.each(_.preload);
			images.not('.active').hide();
			
			//arrowLeft=$('.image-link-arrow.left').css('z-index',9999).bind('click',_.prevItem);
			//arrowRight=$('.image-link-arrow.right').css('z-index',9999).bind('click',_.nextItem);

			prjNav = $('#project-nav').children('.image-gallery-link').bind({
				click: _.hijack,
				mouseenter: _.onOver,
				mouseleave: _.onOut
			});

			wrap = $('#wrap');
			
			$('html').bind('keyup',_.keyup);

			SWFAddress.addEventListener(SWFAddressEvent.CHANGE,_.routeChange);
			sandbox.listen( 'adjust', _.adjust, _ );
		},
		prevItem: function ( e ){
			_.keyup (null,37);
			e.preventDefault();
		},
		nextItem: function ( e ){
			_.keyup (null,39);
			e.preventDefault();
		},
		fakeNext: function ( e ) {
			var obj = {};
			obj.keyCode = 39;
			obj.preventDefault = function() {
				return false;
			}
			_.keyup(obj);
		},
		keyup: function ( e, code ) {
			var trg;
			if( !e && !!code ) {
				e = {};
				e.keyCode = code;
			}
			switch( e.keyCode ) {
				case 37:
					trg = links.filter('.active').prev('.image-gallery-link');
					if( !trg[0] ) trg = links.eq(links.length-1);
					if(!code) e.preventDefault();
					break;
				case 39:
					trg = links.filter('.active').next('.image-gallery-link');
					if( !trg[0] ) trg = links.eq(0);
					if(!code) e.preventDefault();
					break;
			}
			if( trg) trg.trigger('click');
		},
		preload: function ( key, obj ) {
			var src = $(this).attr('src'),
				img = new Image();
			img.src = src;
		},
		onOver: function ( e ) {
			var ln = $(this);

			ln.stop().animate({
				opacity: 1
			}).siblings('.project-nav-child').stop().animate({
				opacity: .35
			});
		},
		onOut: function ( e ) {
			var ln = $(this);

			ln.siblings('.project-nav-child').andSelf().stop().animate({
				opacity: 1
			});
		},
		/*
		hijack: function ( e ) {
			var obj = $(this);
			var link = $(this).data('alias'),
				hash = '#!/'+link.match(re)[1];
			if( anim===true ) return false;
			window.location.hash = hash;
			obj.addClass('active').siblings('.image-gallery-link').removeClass('active');
			e.preventDefault();
		},
		*/
		hijack: function ( e ) {
			var obj = $(this);
			if( anim == true )
				return false;
			obj.addClass('active').siblings('.image-gallery-link').removeClass('active');
			index = obj.index();
			target = images.eq(index-1);
			if( target.is(':visible' ) )
				return false;
			_.updateData(target);
		},
		routeChange: function ( e ) {
			var index = e.path.replace(/!\/?/,'');
			prjNav.eq(index-1).trigger('click');
		},
		/*
		onSuccess: function ( data ) {
			var target=$('#i'+data.id);
			
			if( target.is(':visible') ) return false;
			links.eq(data.order).addClass('active').siblings('a').removeClass('active');
			captions.filter(':visible').fadeOut(850);
			captions.eq(data.order).fadeIn(850);
			_.updateData( target );
		},
		onError: function ( e, xhr, data ) {},
		*/		
		updateData: function ( next ) {
			var current = images.filter(':visible'),
			currentCaption,nextCaption;

		
/*		
			current.css({
				zIndex: 0,
				position: 'absolute'
			}).animate({
				opacity: 0
			});
			
			next.css({
				zIndex: 10,
				display: 'block',
				position: 'relative',
				opacity: 0
			}).animate({
				opacity: 1
			}, 450, function () {
				current.css({
					zIndex: 1,
					display: 'none',
					position: 'relative',
				}).removeClass('active');
				next.css({
					zIndex: 1,
					position: 'relative',
				}).addClass('active');
				anim = false;
			});
        
		*/	
			current.css({
				zIndex: -10,
				position: 'absolute'
			}).animate({
				opacity: 0
			});
		
			next.css({
				zIndex: 10,
				display: 'block',
				position: 'absolute',
				opacity: 0
			}).animate({
				opacity: 1
			}, 400, function () {
				current.css({
					zIndex: 1,
					display: 'none',
					position: 'absolute',
				}).removeClass('active');
				next.css({
					zIndex: 1,
					position: 'absolute',
				}).addClass('active');
				anim = false;
			});
			
			currentCaption = captions.eq(current.index()),
			nextCaption = captions.eq(next.index());
			
			currentCaption.removeClass('.active').fadeOut(400);
			nextCaption.addClass('.active').fadeIn(400);

			
		},
		adjust: function ( w, h ) {
			MAX_WIDTH = w-100;
			MAX_HEIGHT = h-imageOffset-70;
			prjImage.css('height',MAX_HEIGHT); //setting container to height so images can be displayed

			//MAX_WIDTH = MAX_WIDTH < MIN_WIDTH ? MIN_WIDTH : MAX_WIDTH;
			//MAX_HEIGHT = MAX_HEIGHT < MIN_HEIGHT ? MIN_HEIGHT : MAX_HEIGHT;
			
			images.each(_.resize);
		},
		resize: function( key, object ) {
			var w, h;
			img = $(object);
			w = img.data('width');
			h = img.data('height');
			a = MAX_WIDTH/MAX_HEIGHT;
			ia = w/h;

			if( a < ia ) {
				img.css({
					width: MAX_WIDTH,
					height: 'auto'
				});
			}
			else {
				img.css({
					width: 'auto',
					height: MAX_HEIGHT
				});	
			}
			
		},
		destroy: function () {
			
		}
	}
} );

PWP.register('stretch',function(sandbox) {
	var _,
		item,
		sideItem,
		container;
	return {
		init: function () {
			_ = this;
			item = $('.stretch');
			container = $('.header-bar');
			sideItem = $('.short');
			article = $('.firm-article-list');
			sandbox.listen('adjust', _.adjust, _);
		},
		adjust: function ( w, h ) {
			var containerWidth = container.width();
			var sideItemWidth = sideItem.outerWidth(true);
			var itemWidth = containerWidth-sideItemWidth;
			item.css('width',itemWidth);
		},
		destroy: function () {
			sandbox.kill('adjust', _.adjust, _);
		}
	}
} );

PWP.register('mini-player',function(sandbox) {
	var _,
		vimeos,
		cheech,
		currentVideo,
		currentClose;
	return {
		init: function () {
			_ = this;
			vimeos= $('.video');
			cheech = $('#video-cheech');
			vimeos.find('img,.play-button').css('cursor','pointer').bind('click',_.initPlayer);
		},
		initPlayer: function ( e ) {
			var img = $(this),
				item = img.parent().parent('.news-item'),
				p = item.parent('.news-list'),
				video, anchor;
			
			video = document.createElement('iframe');
			video.width = 460;
			video.height = 260;
			video.src = 'http://player.vimeo.com/video/'+item.data('vimeo');

			$(video).css({
				position: 'absolute',
				top: p.offset().top,
				left: img.offset().left,
				zIndex: 9999999
			});

			currentVideo = video;

			anchor = document.createElement('a');

			anchor.href = "#";
			anchor.innerHTML = 'Close';
			anchor.className = "vimeo-close";

			$(anchor).css({
				position: 'absolute',
				top: p.offset().top - 12,
				left: img.offset().left + p.innerWidth() - 29,
				zIndex: 9999999
			}).bind('click',_.closeVideo);
			currentClose = anchor;

			cheech.stop().css({
				display: 'block',
				opacity: 0
			}).animate({
				opacity: 1
			}).bind('click',function(e){
				_.closeVideo(undefined,$(anchor));
			});

			p.css({
				zIndex: 99999
			});

			$('body').prepend(anchor,video);

			e.preventDefault();
		},
		closeVideo: function ( e,anchor ) {
			var btn = e !== undefined ? $(this) : anchor,
				data = btn.data('hold'),
				parent = btn.parent('.news-list');
			
			parent.css({
				zIndex: 1,
				position: 'relative',
				overflow: 'hidden'
			});

			$(currentVideo).remove();
			currentVideo = undefined;
			$(currentClose).remove();
			currentClose = undefined;
			cheech.stop().animate({
				opacity: 0
			}, function () {
				cheech.css('display','none');
			})
			
				
			e.preventDefault();
		},
		destroy: function () {
			
		}
	}
} );

PWP.register( 'google-map', function ( sandbox ) {
	var self,
		map,
		lat,
		lng,
		canvas,
		location,
		mapStyle = [{
			featureType: 'all',
				stylers: [
					{saturation: -100},
					{gamma: 0.50},
					{lightness: 25}
				]
			},
			{
				featureType: 'all',
				elementType: 'labels',
				stylers: [
					{lightness: -10},
					{color: 000000}
				]
			}],
		opts = {
			mapTypeControlOptions: {
				mapTypeIds: ['Greyscale']
			},
			mapTypeControl: false,
			zoom: 15,
			mapTypeId: 'Greyscale'
		},
		marker,
		markerHeight,
		markerWidth,
		size,
		origin,
		anchor,
		point,
		styledMapType,
		scaled;
	return {
		init: function () {
			self = this;

			if( !document.getElementById('pwp-map') ) return false;
			styledMapType = new google.maps.StyledMapType(mapStyle, {name:'Greyscale'});

			map = $('#pwp-map');

			lat = map.data('latitude');
			lng = map.data('longitude');
			marker = map.data('marker');
			markerWidth = map.data('markerwidth');
			markerHeight = map.data('markerheight');

			
			size = new google.maps.Size( markerWidth, markerHeight );
			origin = new google.maps.Point( 0, 0 );
			anchor = new google.maps.Point( markerWidth/2, markerHeight/2 );
			scaled = new google.maps.Size( markerWidth, markerHeight );

			markerImage = new google.maps.MarkerImage( marker, size, origin, anchor, scaled );

			opts.center = new google.maps.LatLng(lat,lng);

			self.load();
		},
		load: function () {
			canvas = new google.maps.Map( document.getElementById('pwp-map'), opts );
			canvas.mapTypes.set('Greyscale', styledMapType);

			var marker = new google.maps.Marker({
				position: new google.maps.LatLng(lat,lng),
				map: canvas,
				icon: markerImage
			});

		},
		destroy: function () {

		}
	}
} );

PWP.register( 'ajax-search', function ( sandbox ) {
	var _,
		hook,
		field,
		form,
		submit,
		items;
	return {
		init: function () {
			_ = this;
			hook = $('input[name="ajax"]');
			form = hook.parent('form');
			items = $('.project-list-item');
			field = hook.siblings('input[type="text"]').bind('keyup',_.update);
			submit = hook.siblings('input[type="submit"]').bind('click',_.disable);
		},
		update: function ( e ) {
			var call = {};
			call.url = site_url+'_resources/search/';
			call.type = 'post';
			call.dataType = 'json';
			call.data = form.serialize();
			call.success = _.dataReceived;
			call.error = _.dataError;
			call.timeout = 5000;
			$.ajax(call);
		},
		dataReceived: function ( data ) {
			var targets;
			if( !!data ) {
				targets = data.join(',');
				$(targets).show();
				items.not(targets).hide();
			}
		},
		dataError: function ( e, xhr, data ) {},
		disable: function ( e ) {
			e.preventDefault();
		},
		destroy: function () {
			
		}
	}
} );

PWP.register( 'mini-carousels', function ( sandbox ) {
	var _,
		carousels;
	return {
		init: function () {
			_ = this;
			carousles = $('.mini-carousel-wrapper').miniCarousel();
		},
		destroy: function () {
			
		}
	}
} );

PWP.register( 'news-adjust', function (sandbox ) {
	var _,
		container,
		items,
		itemWidth,
		numItems;

	return {
		init: function () {
			_ = this;
			container = $('#news');
			items = $('.news-list').css('height','auto');
			itemWidth = items.eq(0).width() + parseFloat(items.eq(0).css('margin-right'),10);
			numItems = items.length;
			sandbox.listen('adjust', _.adjust, _);
		},
		adjust: function ( w, h ) {
			var containerWidth = container.width(),
				itemsPerRow = Math.floor(containerWidth / itemWidth),
				i;
			for( i=0; i<numItems; i+=itemsPerRow ) {
				var maxHeight=0,
					endOfRow = i+itemsPerRow,
					j;
				for( j=i; j<endOfRow; j++ ) {
					itemHeight = items.eq(j).children('.news-item').innerHeight();
					if( itemHeight > maxHeight )
						maxHeight = itemHeight;
				}
				for( j=i; j<endOfRow; j++ ) {
					items.eq(j).css('height', maxHeight);
				}
				console.log( maxHeight, itemsPerRow );
			}
		},
		destroy: function () {
			sandbox.kill('adjust', _.adjust, _);
		}
	}
} );

(function($){
	$.fn.miniCarousel = function ( opts ) {
		var defaults = {};
		
		$.extend(defaults, opts);

		return this.each( function ( key, obj ) {
			var _ = this,
				obj = $(this),
				images = obj.find('img'),
				count = images.length,
				timer = null,
				currentIndex=0,
				next = obj.find('.right'),
				prev = obj.find('.left');

			_.setCurrentIndex = function ( index ) {
				currentIndex = index;
				_.controlCurrentIndex();
				_.updateData();
			};

			_.controlCurrentIndex = function () {
				if( currentIndex >= count ) {
					currentIndex = 0;
				}
				else if( currentIndex < 0 ) {
					currentIndex = count-1;
				}
			};

			_.updateData = function () {
				var current = images.filter(':visible'),
					next = images.eq(currentIndex);
				current.css({
					'z-index': 0
				});
				next.css({
					zIndex: 10,
					opacity: 0,
					display: 'block'
				}).animate({
					opacity: 1
				}, function () {
					current.css({
						opacity: 0,
						zIndex: 0,
						display: 'none'
					});
					next.css({
						zIndex: 0
					});
				} );
			};

			_.nextItem = function (e) {
				_.setCurrentIndex(currentIndex+1);
				if( e ) {
					clearTimeout(timer);
					timer = null;
					e.preventDefault();
				}
			}

			_.prevItem = function (e) {
				_.setCurrentIndex(currentIndex-1);
				if( e ) {
					clearTimeout(timer);
					timer = null;
					e.preventDefault();
				}
			}

			timer = setInterval( _.nextItem, 5000 );

			next.bind('click',_.nextItem);
			prev.bind('click',_.prevItem)
		} );
	};
})(jQuery);

$(function(){
	$('html').removeClass('no-js');
	PWP.startAll();
	$(window).trigger('resize');
	$(window).trigger('scroll');
});
