( function($) {

	var jQ = typeof jQuery == 'function';
	var box	;
	var boxes = {};
	var showDefault		=		function ( boxNav )						
			{
				if( boxNav )boxNav.ikToggleNav( 'close' );
				this.css( 'visibility', 'visible' );
			}																		/*	default handler show new content																			*/

	function initMethods( props )						
	{
		 return jQuery.extend( this, 
		{
			_init 					:		function ( p )						
			{
	
				var props 	= p, pp = p;
		
				if( p.ease ) p.ease = p.ease;												/*	easing default in/extro animation																			*/
				if( p.handle != null ) p.handle =p.handle;								/*	optional handlers for start/load/unload events														*/
		
				if( !p.box )
				{
					this.append( $(	'<div id=\"' + p.name + '\" ' 
										+	( ( p.classname != null )
										?	'class=\"' + name + '\" '
										:	'style=\"position:absolute; left:0px; top:0px; z-index:100; overflow:hidden' ) 
										+	'\">'
										+	'</div>' ) );
		
					p.box = this.find( '#' + p.name );
					_setDimensions.call( p.box, p.origin, p );
			
					pp.name = "boxBg";
					p.boxBg = $( p.box ).ikBox( pp ).find( '#boxBg' );
				
					p.box.append( 	'<div id=\"boxContent\" ' 
										+	'style=\"position:absolute; left:0px; top:0px; width:100%; height:100%; z-index:10000; visibility: hidden' 
										+	'\">'
										+	'</div>' );
							
					p.target = $( p.box ).find( '#boxContent' );
		
				}																		/*	build a container box																							*/
				else
				{
					if( p.box.find( '.bg' ).html() != null )
					{
						p.boxBg = p.box.find( '.bg' );
					}
					else
					{
						pp.name = "boxBg";
						p.boxBg = $( p.box ).ikBox( pp ).find( '#boxBg' );
					}
					p.target = p.box.find( '.content' );
				}																			/*	find the existing container box																				*/
				if( p.cover != null ) p.cover = this.ikCover( pp );						/*	catch overlay																										*/
		
				if( p.nav != null )
				{
					pp = p.nav;
					p.boxNav = p.box.ikNav( pp );
/*			props.boxNav.ikButton( 'close' ).click( handleClose );
													 function( e )
													 {
														$( '.goodie.active' ).removeClass( 'active' )
														_blow.call( this, props)
														props.boxNav.ikToggleNav( 'close' )
														if( props.cover )props.cover.toggle()
													 } );*/
				}																		/*	optional add navigation																						*/
				$( p.box ).addClass( 'editContainer' )

				return p;
			},																			/*	handle box setup																								*/
			
			_toggleContent	:		function( url, obj, p, toggled )						
			{
				p.url = url;
				if( !p.box.hasClass( 'boxactive' ) )
				{
					if( obj != null )
					{
						this._setDimensions.call( p.box, ( ( obj != null ) ? p.origin = [ parseFloat( obj.offset().left ), parseFloat( obj.offset().top ), parseFloat( obj.width() ), parseFloat( obj.height() ) ] : null ), p );
					}
					else if( p.origin )
					{
					this._setDimensions.call( p.box, p.origin, p );
					}
				
					if( !p.preload )
					{
						this._blow.call( this, p, p.box.hasClass( 'boxactive' ), toggled );
					}
					else
					{
						this._loadPage( p, this._blow, toggled );
					}
				}
				else
				{
					this._blow.call( this, p, p.box.hasClass( 'boxactive' ), toggled );
				}
			},																			/*	handle content switches																						*/
			
			_showDefault		:		function ( boxNav )						
			{
				if( boxNav )boxNav.ikToggleNav( 'close' );
				this.css( 'visibility', 'visible' );
			},																			/*	default handler show new content																			*/

			_loadPage			:		function _loadPage( p, handle, toggled )						
			{
				$.get( p.url, function( data )
				{ 
				//alert(data)
					$( p.target ).html( data );
					
					$( p.target ).pngFix();

					
					showDefault.call( p.target, p.boxNav );
					if( handle ) handle.call( p.target, p, null, toggled );
				});
			},																			/*	load new content																									*/

			handleClose		:	 	function( e )	
			{
				if( this.name )
				{
					props = this;
				}
					if( props.contentchild && props.contentchild.box.hasClass( 'boxactive' ) )_blow( props.contentchild )
					_blow( props );
				if( props.boxNav )props.boxNav.ikToggleNav( 'close' );
			},																			/*	close the content container																					*/

			_setDimensions	:		function( obj, p )						
			{
				$( this ).css(
				{
					left			:		( ( obj ) ? obj[ 0 ] :  ( _pageDim( 0 ) - $( this ).width() )  * 0.5 ),
					top			:		( ( obj ) ? obj[ 1 ] :  ( _pageDim( 1 ) - $( this ).height() )  * 0.5 ),
					width			:		( ( obj ) ? obj[ 2 ] : $( p.boxBg ).width() ),
					height		:		( ( obj ) ? obj[ 3 ] : $( p.boxBg ).height() )
				} );
			},																			/*	set init dimensions: default to scale factor window [optional to object or rect array ]	*/
	
			_blow					:		function ( p, toggle, wait )						
			{
				if( wait )
				{
					p.box.removeClass( 'boxactive' );
					wait = null;
				}																		/*	are we toggled?																									*/
				else
				{
					if( p.handle != null && p.handle[ 1 ] != null && !toggle ) { p.handle[ 1 ].call( p.obj, p.box.hasClass( 'boxactive' ) ); wait = ( !p.box.hasClass( 'boxactive' ) ) ? 1000 : 0; };
				}																			/*	toggle init handler [ p.handle[ 0 ] ]																		*/
				if( p.cover ) p.cover.toggle();												/*	toggle overlaying cover																						*/
				$( p.box ).css	( { visibility: 'visible' } );
				$( p.obj ).css	( { visibility: 'visible' } );
				p.target.css	( { visibility: 'visible' } )
				
				
				p.box.delay( ( !wait ) ? 50 : wait  ).animate		(
				{
					left			:		( ( !p.box.hasClass( 'boxactive' ) ) ? ( ( p.destination != null ) ? p.destination[ 0 ] : ( _pageDim( 0 )   	- parseInt(p.boxBg.width()) 	)  * 0.5 ) : p.origin[ 0 ] ),
					top			:		( ( !p.box.hasClass( 'boxactive' ) ) ? ( ( p.destination != null ) ? p.destination[ 1 ] : ( _pageDim( 1 )   	- parseInt(p.boxBg.height() )	)  * 0.5 ) : p.origin[ 1 ] ),
					width			:		( ( !p.box.hasClass( 'boxactive' ) ) ? ( ( p.destination != null ) ? p.destination[ 2 ] : p.boxBg.width() )  	: p.origin[ 2 ] ),
					height		:		( ( !p.box.hasClass( 'boxactive' ) ) ? ( ( p.destination != null ) ? p.destination[ 3 ] : ( ( jQuery.support.leadingWhitespace ) ? parseInt(p.boxBg.height()) : parseInt( p.boxBg.height() ) + 1 ) ) : p.origin[ 3 ] )
				},																		/*	animate in: destination / animate out: origin															*/
				{
					duration		: 		700,
					easing		:		( ( !p.box.hasClass( 'boxactive' ) ) ? p.ease[ 0 ] : p.ease[ 1 ] ),
					complete	:		function()
					{
						if( !p.box.hasClass( 'boxactive' ) )
						{
							p.box.addClass( 'boxactive' );
		//		alert( 'blow 1 '+p.name + '\nactive: ' + p.box.hasClass( 'boxactive' )+', '+$( p.boxBg  ).parent().parent().css('overflow'))
							if( !p.preload  )_loadPage( p );
							if( p.handle != null && p.handle.length > 3 ) { p.handle[ 3 ].call( p.target ) };
						}																/*	toggle post load handle [ p.handle[ 1 ]	]																*/
						else if( p.box.hasClass( 'boxactive' ) )
						{
								if( p.handle != null && p.handle.length > 2 && p.handle[ 2 ] ) { p.handle[ 2 ].call( p.target ) };
							p.target.html( '' );p.target.css	( { visibility: 'hidden' } )
								p.box.removeClass( 'boxactive' );
			//alert( 'blow 2 '+p.name + '\nactive: ' + p.box.hasClass( 'boxactive' ))
							if( !p.defaultshow ) 
							{ 
								$( p.obj  ).css	( { visibility: 'hidden' } );
								$( p.box ).css	( { visibility: 'hidden' } ); 
							}
							if( !toggle )
							{
								if( p.handle != null &&  p.handle[ 0 ] != null ) p.handle[ 0 ].call( p.obj, null, p.box.hasClass( 'boxactive' ) );
							}
							else
							{
								_toggleContent( p.url, null, p, true )
							}																/*	empty content, post extro handler [ p.handle[ 2 ] ]													*/
						}
					}
				});
			},																			/*	handle in/extro animation																					*/

			_pageDim			:		function( dim )						
			{
				return ( ( dim == 0 ) 
						  ? ( ( !jQuery.support.leadingWhitespace ) ? document.body.clientWidth : window.innerWidth ) 
						  : ( ( !jQuery.support.leadingWhitespace ) ? document.body.clientHeight : window.innerHeight ) );
			}																			/*	get window dimensions																						*/

		});																	/*	box methods																					*/
	}

	$( window ).resize( function ()											
	{   
		//if( $( '.boxactive' ).length )
		//_setDimensions();   
	});

	jQuery.fn.ikContent = function( options, toggle ) 
	{
		if( !toggle )
		{
			var props = jQuery.extend( this,
			{
  				name		: 		"content-box",
				nav			:		null,
				url				:		null,
   				direction		:		"y",													// 		x, xy
				scale			:		[ 1,  1 ],
				ease			:		[ "easeOutBack", "easeInBack" ],
   				shape		: 		"straight",											// 		rounded
   				color			: 		"white",												// 		black, hex
				cover			:		null,
   				global		:		false
			}, 															/*	default options																				*/
			options);
				
			var n = props.name;													/*	store box name																				*/
			var c = ( props.contentchild ) ? props.contentchild : null;
			var methods = initMethods( props );								/*	publish box methods																		*/
		
			props = _init.call( this, props );										/*	initiate box																						*/
			
			props.contentchild = c;
			props.name = props.box.attr('id');															/*	reset box name																				*/
			if( props.boxNav )props.boxNav.ikButton( 'close' ).click( handleClose );
						
			boxes[ n ] = this											/*	store me in boxarray 																		*/
			props.obj = this;															/*	add me to my properties																	*/	
				
			return boxes[ n ];
		}																	/*	initiate me																						*/
		else
		{
			if( toggle.length > 1 )
			{
				return _toggleContent( toggle[ 0 ], toggle[ 1 ], toggle[ 2 ] );
			}													/*	toggle me																						*/
			else
			{
				if( this.name )
				{
					handleClose.call( this )
				}
				else
				{
					handleClose.call()
				}
			}													/*	close me																						*/
		}																		/*	public methods																				*/
	};

})(jQuery);
