//------------------------------------------------------[BD]
// Public: JS
//------------------------------------------------------[BD]

// Event Delegation ------------------------------------[TN]
jQuery.delegate = function(Rules) {
	return function(e) {
		var Target = $(e.target);
		for (var Selector in Rules)
			if (Target.is(Selector)) return Rules[Selector].apply(this, $.makeArray(arguments));
	}
}
//------------------------------------------------------[TN]

var PublicFunctions = function() {
	var This = {
		AccordianOpen : function(e) {
			if ( $(e.target).siblings('.Content').css('display') != 'none' )
				return;

			if ( This.isAnimating == false ) {
				This.isAnimating = true;
				$('.ScrollContent').css({'overflow' : 'hidden'});

				$('h2.Selected').removeClass('Selected');

				$('.jScrollPaneContainer').css('display', 'none');

				$(".Content:visible").slideUp('normal', function() {
					$('.jScrollPaneContainer').fadeIn('fast');					
				});

				$(e.target).addClass('Selected');

				$(e.target).siblings('.Content').slideDown('normal', function() {
					This.isAnimating = false;	
					$('.ScrollContent').css({'overflow' : 'visible'});
				});
			}
		},
		isAnimating : false
	}
	return This;
}

var VPI = new PublicFunctions();


// ModalBox jQuery Plugin ------------------------------[MN]
( function( $ ) {
	var VPI_ModalBox = function() {
		function This( Config ) {
			This._Settings				= $.extend( This._Settings, Config );
			This._Settings.Content_Type	= This._Settings.Content_Type.toLowerCase()
			This.Show();

			$( window ).resize( This.AdjustDimensions );
		}

		This._Settings = {
			BG_Class: 'VPI_ModalBox_Background',
			Container_Class: 'VPI_ModalBox_Container',
			Box_Class: 'VPI_ModalBox',
			Img_BasePath: '/images',
			Width: false,
			Height: false,
			MinPadding: 20
		};

		This.AdjustDimensions = function() {
			// Resize Transparent BG -------------------------------[MN]
			$( '.'+ This._Settings.BG_Class ).css( {
				width: $( document ).width() +'px',
				height:	$( document ).height() +'px'
			} );
			//------------------------------------------------------[MN]

			// Resize Container ------------------------------------[MN]
			var Width_Max		= $( window ).width() - ( 2 * This._Settings.MinPadding );
			var Height_Max		= $( window ).height() - ( 2 * This._Settings.MinPadding );
			var Width_Content	= $( '.'+ This._Settings.Box_Class ).width();
			var Height_Content	= $( '.'+ This._Settings.Box_Class ).height();

			if( This._Settings.Content_Type == 'iframe' ) {
				var Body_IFrame = $( $( '.'+ This._Settings.Box_Class ).get( 0 ).contentWindow.document.body );

				if( Body_IFrame.attr( 'scrollWidth' ) )
					Width_Content = Body_IFrame.attr( 'scrollWidth' );
				if( Body_IFrame.attr( 'scrollHeight' ) )
					Height_Content = Body_IFrame.attr( 'scrollHeight' );
			}

			var Width	= This._Settings.Width ? ( This._Settings.Width <= Width_Max ? This._Settings.Width : Width_Max ) : Math.min( Width_Max, Width_Content );
			var Height	= This._Settings.Height ? ( This._Settings.Height <= Height_Max ? This._Settings.Height : Height_Max ) : Math.min( Height_Max, Height_Content );

			$( '.'+ This._Settings.Container_Class ).css( {
				width: Width +'px',
				height: Height +'px',
				position: 'absolute',
				left: parseInt( Number( $( window ).width() / 2 ) - ( Width / 2 ) + $( window ).scrollLeft() ) +'px',
				top: parseInt( Number( $( window ).height() / 2 ) - ( Height / 2 ) + $( window ).scrollTop() ) +'px'
			} );
			//------------------------------------------------------[MN]

			// Resize Box ------------------------------------------[MN]
			$( '.'+ This._Settings.Box_Class ).css( {
				width: Width +'px',
				height: Height +'px'
			} );
			//------------------------------------------------------[MN]

		};

		This.Close = function() {
			if (This._Settings.Success == true) {
				window.location="http://vpi.net";
			} else {
				$( '.'+ This._Settings.Container_Class ).fadeOut( 500, function() {
					$( this ).remove();
					$( '.'+ This._Settings.BG_Class ).remove();
					$( 'html' ).css( 'overflow', This._Settings.HTML_Overflow );
					// Show Selects (Hidden for IE6)
					//$('.DOMMDY').css('display', 'block');
					//$('.REGSelect').css('display', 'block');
				});	
			}
		};

		This.Content_Loaded = function() {
			This.AdjustDimensions();
			$( '.VPI_ModalBox_Loading' ).fadeOut( 250, function() {
				$( this ).remove();

				$( '.'+ This._Settings.Container_Class ).css( { display: 'none', visibility: 'visible' } ).fadeIn( 500 );
			} );
		};

		This.Show = function() {
			// Remove Scrollbars -----------------------------------[MN]
			This._Settings.HTML_Overflow = $( 'html' ).css( 'overflow' );
			$( 'html' ).css( 'overflow', 'hidden' );
			//------------------------------------------------------[MN]

			// Attach Transparent BG -------------------------------[MN]
			var HTML = '<div class="'+ This._Settings.BG_Class +'"></div>';
				HTML += '<img class="VPI_ModalBox_Loading" style="left: '+ parseInt( Number( $( window ).width() / 2 ) - 48 + $( window ).scrollLeft() ) +'px; top: '+ parseInt( Number( $( window ).height() / 2 ) - 48 + $( window ).scrollTop() ) +'px;" src="'+ This._Settings.Img_BasePath +'/vpi_modalbox.loading.gif" />';
			$( document.body ).append( HTML );
			//------------------------------------------------------[MN]

			// Attach Box ------------------------------------------[MN]
			var HTML = '<div class="'+ This._Settings.Container_Class +'">';
				HTML += '<div class="CloseWrapper"><img class="VPI_ModalBox_Close" src="'+ This._Settings.Img_BasePath +'/vpi_modalbox.close.gif" border="0" alt="X" /></div>';


			switch( This._Settings.Content_Type ) {
				case 'html':	HTML += '<div class="'+ This._Settings.Box_Class +'">'+ This._Settings.Content +'</div>';								break;
				case 'iframe':	HTML += '<iframe frameborder="0" class="'+ This._Settings.Box_Class +'" src="'+ This._Settings.Content +'"></iframe>';	break;
			}
				HTML += '</div>';

			$( document.body ).append( HTML );
			//------------------------------------------------------[MN]

			// Bind Events -----------------------------------------[MN]
			$( '.VPI_ModalBox_Close' ).click( This.Close );
			if( This._Settings.Content_Type == 'iframe' ) {
				This.AdjustDimensions();
				$( '.'+ This._Settings.Box_Class ).load( This.Content_Loaded );
			} else {
				This.Content_Loaded()
			}
			//------------------------------------------------------[MN]
		};

		return This;
	}
	$.VPI_ModalBox = new VPI_ModalBox();
} ) ( jQuery );
//------------------------------------------------------[MN]

//------------------------------------------------------[BD]