( function( $ ) {
	
	/**************************************************************************
	 * initialize site-wide configuration
	 **************************************************************************/

	adj.siteConfig.init( {
		productionHost: "briandillard.com",
		googleId: "UA-11645831-2",
		typekitId: "nxg5gzm"
	} );
	
	/**************************************************************************
	 * initialize adj.pages package for page-specific behaviors
	 **************************************************************************/

	adj.register( "adj.pages" );
	
	adj.pages.dashboard = function() {
		
		/**
		 * Create the jQuery Tools scrollable
		 */
		$( "div.scrollable" ).scrollable( { 
		    vertical: true,
			clickable: false,
		    size: 1,
			speed: 100
		} );
		
	};

	adj.pages.homepage = function() {
		
		/**
		 * Load and process FriendFeed JSONP
		 */
		adj.ff.load();

		/**
		 * Build canvas background
		 */
		
		adj.pages.homepage.bgCanvas = new adj.dom.Canvas(

			//canvas id
			'bgCanvas',

			//drawing callback
			function( ctx, width, height, canvas ) {
				
				if ( !this.params.strokeStyle1 || !this.params.strokeStyle2 ) {
					return;
				}
				
				ctx.strokeStyle = this.params.strokeStyle1;
				
				ctx.lineCap = "round";
				ctx.lineWidth = 425;

				ctx.beginPath();  
				ctx.moveTo( 0, 150 );
				ctx.bezierCurveTo( canvas.width + 100, canvas.height + 100, canvas.width - 50, canvas.height, canvas.width + 400, canvas.height );
				ctx.stroke();

				ctx.beginPath();  
				ctx.moveTo( 25, 150 );
				ctx.bezierCurveTo( canvas.width + 100, canvas.height + 100, canvas.width + 250, canvas.height, canvas.width + 400, canvas.height  + 50 );
				ctx.stroke();

				ctx.beginPath();  
				ctx.moveTo( 50, 150 );
				ctx.bezierCurveTo( canvas.width + 100, canvas.height + 100, canvas.width - 400, canvas.height, canvas.width + 400, canvas.height + 150 );
				ctx.stroke();

				ctx.lineWidth = 1;
				ctx.strokeStyle = this.params.strokeStyle2;
				ctx.fillStyle = this.params.strokeStyle2;
				
				ctx.beginPath();
				ctx.arc( 30, 55, 16, 0, Math.PI * 2, false );
				ctx.closePath();
				ctx.stroke();
				ctx.fill();

				ctx.beginPath();
				ctx.arc( 60, 75, 18, 0, Math.PI * 2, false );
				ctx.closePath();
				ctx.stroke();
				ctx.fill();

				ctx.beginPath();
				ctx.arc( 65, 45, 22, 0, Math.PI * 2, false );
				ctx.closePath();
				ctx.stroke();
				ctx.fill();
				
			},

			//options
			{},

			//callback params
			{
				strokeStyle: null
			}

		);

		/**
		 * build dynamic nav from existing html
		 */
		$( 'ul#nav' ).nav();

		var sections = [];

		/**
		 * build magicmenu config object for the magicmenu plugin
		 */
		var visibility = $.fn.magicmenu.buildMenuSection(
			//properties
			{
				domId: 'menus',
				label: 'this menu',
				defaultIndex: 0,
				help: '',
				choices: [
					{ value: 'visible', text: 'show always' },
					{ value: 'hover', text: 'show on hover' }
				]
			},

			//callbacks
			{}
		);

		//sections.push( visibility );

		/**
		 * build link config object for the magicmenu plugin
		 */
		var links = $.fn.magicmenu.buildMenuSection(

			//properties
			{
				domId: 'popup',
				label: 'open links in',
				defaultIndex: 0,
				help: '',
				choices: [
					{ value: 'here', text: 'this window' },
					{ value: 'window', text: 'new window' },
					{ value: 'lightbox' }
				]
			},

			//callbacks
			{}

		);

		//sections.push( links );

		/**
		 * build themes config object for the magicmenu plugin
		 */
		var themes = $.fn.magicmenu.buildMenuSection(

			//properties
			{
				domId: "theme",
				defaultIndex: 3,
				help: '',
				choices: [
					{
						value: 'none'
					},
					/*
					{
						value: '1996'
					},
					{
						value: 'iphone'
					},
					*/
					{
						value: 'bold',
						text: 'bright',
						canvasParams: {
							strokeStyle1: '#7f8',
							strokeStyle2: '#eed'
						}
					},
					{
						value: 'w_on_b',
						text: 'black',
						canvasParams: {
							strokeStyle1: '#252525',
							strokeStyle2: '#000'
						}
					},
					{
						value: 'greyscale',
						text: 'grey',
						canvasParams: {
							strokeStyle1: '#cecece',
							strokeStyle2: '#dedede'
						}
					},
					{
						value: 'b_and_w',
						text: 'white',
						canvasParams: {
							strokeStyle1: 'white',
							strokeStyle2: 'white'
						}
					},
					{
						value: 'ghostly',
						canvasParams: {
							strokeStyle1: '#ded1de',
							strokeStyle2: '#ade'
						}
					}

				]
			},

			//callbacks
			{
				before_init: function( section, $list ) {

					//set the current theme in styleswitcher

					section.styleswitcher = adj.dom.styleswitcher.getInstance(
						section.current, section.choiceValues, "asset/css/site/theme/"
					);
					
					//redraw background canvas w/ the correct strokeStyle

					//TODO - come up w/ a more elegant solution
					//so this code doesn't need to be repeated

					var currentObj = section.getCurrentObject();
					
					if ( currentObj && currentObj.canvasParams ) {
						adj.pages.homepage.bgCanvas.setParams( currentObj.canvasParams );
					}
					else {
						adj.pages.homepage.bgCanvas.clearParams();
					}

					adj.pages.homepage.bgCanvas.draw();

				},
				choice_after: function( section, $list ) {

					//reset current stylesheet
					section.styleswitcher.replace( section.current );

					//redraw background canvas w/ the correct strokeStyle
					
					//TODO - come up w/ a more elegant solution
					//so this code doesn't need to be repeated

					var currentObj = section.getCurrentObject();
					
					if ( currentObj && currentObj.canvasParams ) {
						adj.pages.homepage.bgCanvas.setParams( currentObj.canvasParams );
					}
					else {
						adj.pages.homepage.bgCanvas.clearParams();
					}

					adj.pages.homepage.bgCanvas.draw();

				}
			},
			
			//options
			null,
			
			//getCurrentFunction
			function() {
				if (
					navigator.userAgent.match( /iPhone/i ) ||
					navigator.userAgent.match( /iPod/i )
				) { 
					return "iphone";
				}
			}
		);

		sections.push( themes );

		/**
		 * Append the shell of the magicmenu to the DOM,
		 * then populate it using the themes config object
		 */
		$( 'body' ).magicmenu( sections, {
			buildHTML: true,
			showMode: ""
		} );

	};

	/**************************************************************************
	 * bind behaviors
	 **************************************************************************/

	$( function() {
		
		$( 'body' ).each( function() {
			
			var $body = $( this );

			/**
			 * site-wide behaviors
			 **/
			
			/**
			 * page-specific behaviors
			 **/
			
			//pages that use typekit for @font-face
			if ( $body.is( ".typekitted" ) ) {
				adj.dom.instantiateTypekit();
			}

			//analtyics-friendly pages
			if ( $body.is( "#homepage, #resume, #tattoo" ) ) {
				adj.analtyics.autoTrack();
			}

			//homepage
			if ( $body.is( "#homepage" ) && $body.not( ".subpage" ) ) {
				adj.pages.homepage();
			}

			//dashboard
			if ( $body.is( "#dashboard" ) ) {
				adj.pages.dashboard();
			}

		} );

	} );
	
} )( jQuery );
