var debug = false;
var skipIntroduction = true;
var centerX = 0;
var pauseSlideshow = false;
var firstToc = true;
var introductionPlayer = new Object();
var opportunityPlayer = new Object();
var opportunityExpose = new Object();
//var logInLink = 'http://ptn.myvoffice.com/';  //set this to poker-tel.com for live
var logInLink = 'https://extranet.securefreedom.com/PTN/Login.aspx'; 

var cdn = 'http://ptn.vo.llnwd.net/o9/u/website/';
var cdnImage = cdn + 'images/';
var cdnSWF = cdn + 'swf/';
var ptnVO = 'https://nettrax.myvoffice.com/ptn/Application/Apply.cfm';

var ptn = new Object();
var disclaimerBump = unescape( 'mp4%3Awebsite%2Fvideo-disclaimer-090809.mp4%3Fh%3D3517be9f909508dc4667f4c2d3af1789' );
var pageOverlay = new Object();
var overlayPlayer = new Object();
var pageExpose = new Object();
var audioPlayer = new Object();

function _flowplayerConfig(){
	this.source = {
		src:  cdnSWF + 'flowplayer.commercial-3.1.1.swf',
		wmode: 'transparent',
		expressInstall: cdnSWF + 'expressinstall.swf'
	};
	this.options = {
		key: '#$31e8476c548691d8b39',
		clip: {
			provider: 'cdn',
			acelerated: true,
			autoBuffering: false,
			bufferLength: 0
		},
		screen: {
			top: 0
		},
		plugins: {
			cdn: {
				url: cdnSWF + 'flowplayer.rtmp-3.1.0.swf',
				netConnectionUrl: 'rtmp://ptn.fcod.llnwd.net/a3947/o9/s'
			}
		},
		onLoad: function(){ this.setVolue( 50 ); }
	}
}

var introductionConfig = new _flowplayerConfig();
introductionConfig.options.canvas = {
	backgroundGradient: 'none',
	backgroundColor: 'transparent'
}
introductionConfig.options.plugins.controls = null;

function setAudioPlayer(){
	var x = new _flowplayerConfig();
	x.options.canvas = {
		backgroundGradient: 'none',
		backgroundColor: 'transparent'
	};
	x.options.plugins.controls = {
		backgroundColor: '#CCCCCC',
		backgroundGradient: 'high',
		buttonColor: '#a1a1a1',
		bufferColor: '#a1a1a1',
		timeBgColor: '#a1a1a1',
		progressColor: '#074279',
		time: false,
		scrubber: true,
		fullScreen: false
	};
	audioPlayer = $f( 'audio', x.source, x.options );
}

function hearAudio( url ){
	var wAudio = window.open( url, 'wAudio', 'status=0,toolbar=0,location=0,menubar=0,resizable=0,height=88,width=345', false );
}

function startIntroductionVideo(){
	try {
		$( 'div#jodi' ).show();
		
		var x = new _flowplayerConfig();
		x.source.buffering = false;
		x.options.plugins.controls = null;
		x.options.clip.url = $( 'input#vi' ).attr( 'value' );
		x.options.clip.linkUrl = 'javascript:startOpportunityVideo()';
		x.options.clip.onBeforePause  = function(){ return false; };
		x.options.clip.onFinish = function(){ introductionPlayer.stop(); $( 'div#jodi' ).remove(); startOpportunityVideo(); };
		x.options.canvas = { backgroundGradient: 'none', backgroundColor: 'transparent' };
		introductionPlayer = $f( 'jodi', x.source, x.options );
	} catch ( introductionVideoErr ){
		alert( 'startIntroductionVideo(): ' + introductionVideoErr );
	}
}

function startOpportunityVideo(){
	try { 
		introductionPlayer.stop(); 
		$( 'div#jodi' ).remove(); 
	} catch( introStopErr ){
		if( debug ) alert( 'startOpportunityVideo(): ' + introStopErr );
	}

	try {		
		var x = new _flowplayerConfig();
		x.source.buffering = false;
		x.options.canvas = { backgroundGradient: 'none', backgroundColor: 'transparent'	};
		x.options.screen = {
					width: 640,
					height: 350,
					top: 0,
					left: 0
				}
		x.options.plugins.controls = {
			backgroundColor: '#151515',
			backgroundGradient: 'none',
			buttonColor: '#a1a1a1',
			bufferColor: '#a1a1a1',
			timeBgColor: '#a1a1a1',
			progressColor: '#074279',
			time: false,
			scrubber: false
		}
		x.options.clip.onBeforePause = function(){ return false; };
		x.options.playlist = [/*{ 	
				url: disclaimerBump, 
				duration: 5,
				onBegin: function(){
					opportunityExpose = $( 'div#opportunity' ).expose( { color: '#000000', opacity: 0.95, api: true, closeOnClick: false, closeOnEsc: false } ).load();
				}
			},*/{ 
				url: unescape( $( 'input#vp' ).attr( 'value' ) ),
				onBegin: function(){
					opportunityExpose = $( 'div#opportunity' ).expose( { color: '#000000', opacity: 0.95, api: true, closeOnClick: false, closeOnEsc: false } ).load();
				},
				onFinish: function(){ 
					opportunityPlayer.stop(); 
					$( 'div#opportunity' ).remove(); 
					finishOpeningSequence(); 
					opportunityExpose.close();
			} 
		}];
		x.options.closeSpeed = 'fast';
		$( 'div#opportunity' ).show();
		opportunityPlayer = $f( 'opportunity-video', x.source, x.options );
		//opportunityExpose = $( 'div#opportunity' ).expose( { color: '#000000', opacity: 0.95, api: true, closeOnClick: false, closeOnEsc: false } ).load();
		$( '#skip-video' ).html( '<a href="index.php">Skip Video</a>' );
	} catch( oppStartErr ){
		if( debug ) alert( 'startOpportunityVideo(): ' + oppStartErr );
	}
}

function finishOpeningSequence(){
	try { opportunityPlayer.stop(); } catch( err1 ){};
	try { opportunityPlayer.unload(); } catch( err2 ){};
	try { $( 'div#opportunity' ).remove(); } catch( err3 ){};
	try { opportunityExpose.close(); } catch( err4 ){};
	// place the version information at the top of the site
	placeVersionInformation();
	// initialize the slideshow
	initializeSlideshow();	
	// initialize the chip dock
	//initializeChipDock();
}

function initializeSlideshow(){
	try {
		$( 'div#slideshow' ).flashembed({
			src: cdnSWF + 'slideshow.swf',
			version: [ 10, 0, 22 ],
			expressInstall: cdn + 'expressinstall.swf',
			wmode: 'transparent'			
		});
	} catch( err ){
		if( debug ) alert( 'initializeSlideshow(): ' + err );
	}
}

// create a function to handle when they click on enroll
/*function enrollClick(){
	if( allowEnrollments ){
		if( $( 'input#ci' ).val() != '' ){
			// just to be safe we're going to set a default value for the Enroller ID
			var enrollerID = ( $( '#ci' ).attr( 'value' ) == '' ) ? '2' : $( '#ci' ).attr( 'value' );
			// create the querystring for infoTrax
			var querystring = '?EnrollerID=' + enrollerID + '&LNG=ENG&Theme=Default';
			// send them over to the infoTrax sign up page
			parent.window.location = ptnVO + querystring;
		} else {
			window.location = 'signup.php';
		}
	}
}*/

function enrollClick(){
	if( allowEnrollments ){
		
		if( $( 'input#ci' ).val() != '' ){
		
			// send them over to the register page
			window.location = 'register.php';
		
		} else {
		
			window.location = 'signup.php';
		
		}
	}

}


function determineEnroll(){
	if( allowEnrollments ){
		
	//exception for this account, new company associates coming in Ñ we want to direct them straight to the application as associates
		
		if( $( 'input#ci' ).val() == '42193' ){
		
			window.location = 'https://extranet.securefreedom.com/PTN/Signup/EnrollNew.asp?RepDID=42193';
		
		} else if( $( 'input#ci' ).val() != '' ){
		
			// send them over to the register page
			window.location = 'register.php';
		
		} else {
		
			window.location = 'signup.php';
		
		}
	}

}

// create a function to launch the overlay object
function showOverlay( uri, type ){
	try {
		var close = $( 'div#overlay > P.close' );
	
		switch( type ){
		// handle the images
		case 'img':
		case 'still':
		case 'image': 
			if( $.browser.msie ){
				showModalDialog( 'bin/popups/ieimage.php?src=' + uri, uri, 'dialogHeight:350px;dialogWidth:600px;center:yes;resizable:no;scroll:no;status:no;' );
				return;
			}
		
			$.get( 'bin/popups/view-image.php', { src: uri }, function( data ){
				try {
					var container = $( 'div#overlay-image' );
					container.html( data );
					pageOverlay.onLoad = function(){
						container.fadeIn( 500 );
						close.fadeIn( 500 );
						pageOverlay.onLoad = null;
					}
					pageOverlay.onBeforeClose = function(){
						close.fadeOut( 500 );
						container.fadeOut( 500 );
						pageOverlay.onBeforeClose = null;
					}
					pageOverlay.load();	
				} catch( overlayImgErr ){
					if( debug ) alert( 'showOverlay(): ' + overlayImgErr );
				}
			}, 'html' );
			break;
			
		// handle a list of images		
		case 'imagelist':
			alert( 'Image slideshow / playlist is currently in development.  Please be patient' );
			break;
		
		// handle video files	
		case 'f4v':
		case 'mp4':
		case 'flv':
		case 'video':
			if( $.browser.msie ){
				var playlist = [ { url: uri } ];
				showModalDialog( 'bin/popups/ievideo.php', playlist, 'dialogHeight:350px;dialogWidth:600px;center:yes;resizable:no;scroll:no;status:no;' );
				return;
			}
		
			var container = $( 'div#overlay-video' );
			try {
				var x = new _flowplayerConfig();
				x.options.canvas = {
					backgroundGradient: 'none',
					backgroundColor: 'transparent'
				};
				x.options.plugins.controls = {
					backgroundColor: '#FFFFFF',
					backgroundGradient: 'none',
					buttonColor: '#a1a1a1',
					bufferColor: '#a1a1a1',
					timeBgColor: '#a1a1a1',
					progressColor: '#074279',
					time: false,
					scrubber: true
				};
				//{ url: disclaimerBump, duration: 3 },
				x.options.playlist = [
					{ url: uri }
				];
				overlayPlayer = $f( 'overlay-video-display', x.source, x.options );
				overlayPlayer.load();
			} catch( err2 ){
				if( debug ) alert( 'initializeOverlay(): ' + err2 );
			}
			
			pageOverlay.onLoad = function(){ 
				container.fadeIn( 500 );
				close.fadeIn( 500 );
				pageOverlay.onLoad = null;
			};
			pageOverlay.onBeforeClose = function(){
				close.fadeOut( 500 );
				container.fadeOut( 500 );
				pageOverlay.onBeforeClose = null;
			};
			pageOverlay.load();
			break;
			
		// handle acrobat or PDF Files	
		case 'acrobat':
		case 'pdf':
			var winPDF = window.open( cdn + 'downloads/' + uri, '_blank', 'toolbar=0,height=600,width=800,resizable=1,status=0,titlebar=0,location=0', true );
			break;
		
		// handle HTML or PTP Files	
		case 'html':
		case 'php':
		default:
			$.get( uri, {}, function( data ){
				try {
					var container = $( 'div#overlay-wrap' );
					container.html( data );
					pageOverlay.onLoad = function(){ 
						container.fadeIn( 500 ); 
						close.fadeIn( 500 ); 
						overlay.onLoad = null 
					};
					pageOverlay.onBeforeClose = function(){ 	
						close.fadeOut( 500 ); 
						container.fadeOut( 500 ); 
						overlay.onBeforeClose = null 
					};
					pageOverlay.load();
				} catch( htmlErr ){ 
					if( debug ) alert( 'showOverlay(): ' + htmlErr ); 
				}
			}, 'html' );
			break;
		}	
	} catch( err ) {
		alert( 'showOverlay: ' + err );
	}
}

function initializeOverlay(){
	// we have to set up the overaly div before it's called
	// so we'll do it here
	try {
		pageOverlay = $( "#overlay" ).overlay({
			oneInstance: false,
			api: true,
			color: '#ccc',
			close: 'p.close',
			expose: {
				color: '#000000',
				opacity: 0.8
			},
			onBeforeLoad: function(){
				try{ $( 'div#video-panel' ).css( 'visibility', 'hidden' ); } catch( err ) {};
			},
			onClose: function(){
				try{ $( 'div#video-panel' ).css( 'visibility', 'visible' ); } catch( err ) {};
			}
		});
	} catch( err ){
		if( debug ) alert( 'initializeOverlay(): ' + err );
	}
	
	
}

function isEmpty( value ){
	return ( value == '' || value == null ) ? true : false;
}	

function initializeChipDock(){
	try {
		// load the chip dock
		$( 'div#layout-chipdock' ).flashembed({
			src: cdnSWF + 'chip_dock.swf',
			version: [ 10, 0, 22 ],
			expressInstall: cdnSWF + 'expressinstall.swf',
			wmode: 'transparent'
		});
	
	} catch( chipLoadErr ){
		if( debug ) alert( 'div#layout-chipdock.ready: ' + chipLoadErr );
	}
}

// we need to find out where the center of the screen is to position
// both the introduction spokesperson and the navigation bar since
// both of those divs are in absolute position mode

// here we'll create a function that will position those items when the window
// loads and when it resizes
$( window ).bind( 'resize', positionAbsolutes );
function positionAbsolutes(){	
	try {
		var w = $( window ).width();
		var h = $( window ).height();
		$( 'div#jodi' ).css( 'left', ( ( w / 2 ) + 225 ) + 'px' );
		$( 'div#opportunity' ).css( 'left', ( ( w / 2 ) - 384 ) + 'px' );
		$( 'div#div-version' ).css( 'left', ( ( w / 2 ) ) + 'px' );
		$( 'div#search-line' ).css( 'left', ( ( w / 2 ) -325 ) + 'px' );
		
		var left = ( isEmpty( $( 'input#cn' ).attr( 'value' ) ) ) ? ( ( w / 2 ) - 156 ) + 'px' : ( ( w / 2 ) - 100 ) + 'px';
		$( 'div.nav-container' ).css( 'left', left ); //165 with FAQs active
	} catch( resizeErr ) {
		if( debug ) alert( 'positionAbsolutes: ' + resizeErr );
	}
}

function placeVersionInformation(){
	var w = $( window ).width() / 2;
	var div = $( 'div#div-version' );
		
	if( $( 'input#useSplash' ).attr( 'value' ) == 1 ){
		div.css( 'left', ( w - 200 ) + 'px' );
		div.css( 'z-index', '1010' );
		div.css( 'top', '150px' );
		div.css( 'font-size', '40px' );
		div.fadeIn( 800, function(){
			div.animate({ top: '160px' }, 400, 'linear', function(){
				div.animate({
					fontSize: '8pt',
					borderWidth: '0',
					top: '750px',
					left: ( w + 340 ) + 'px'
				}, 300, 'linear', function(){ div.css( 'backgroundColor', 'transparent' ); div.fadeOut( 200 ); } );
			});
		});
	} 
}

function initializePageVideo(){
	if( $( '#page-video' ).attr( 'href' ) != null  ){
		try {
			var x = new _flowplayerConfig();
			x.options.canvas = {
				backgroundGradient: 'none',
				backgroundColor: 'transparent'
			};
			x.options.screen = {
					width: 432,
					height: 256,
					top: 0,
					left: 0
			}
			x.options.plugins.controls = {
				backgroundColor: '#CCCCCC',
				backgroundGradient: 'high',
				buttonColor: '#a1a1a1',
				bufferColor: '#a1a1a1',
				timeBgColor: '#a1a1a1',
				progressColor: '#074279',
				time: false,
				scrubber: true
			};
			x.options.playlist = [/*{ 	
					url: disclaimerBump, 
					duration: 5,
					onBeforePause: function(){ return false; },
					onBegin: function(){ $( '#video-panel' ).addClass( 'playing' ); pageExpose = $( '#page-video' ).expose( { api: true, opacity: .9, color: '#000000', closeOnClick: false, closeOnEsc: false } ).load(); }
				},*/{ 
					url: $( '#page-video' ).attr( 'value' ),
					onBeforePause: function(){ return true; },
					onFinish: function(){ pageExpose.close(); },
					onPause: function(){ pageExpose.close(); },
					onResume: function(){ pageExpose.load(); }
			}];
			pagePlayer = $f( 'page-video', x.source, x.options );
		} catch( errVideo ){
			if( debug ) alert( 'initializePageVideo(): ' + errVideo );
		}
	}
}

function initializePWPInformation(){
	
	logInLink = ( allowLogins ) ? logInLink : 'javascript:';
	try {
		x = '';
		y = '<div id="affiliate">';
		if( $( 'input#cn' ).attr( 'value' ) != '' ){
			x = '<strong>';
			x += $( 'input#cn' ).attr( 'value' );
			x += '</strong><br />';
			x += '<a href="mailto:' + $( 'input#ce' ).attr( 'value' ) + '">';
			x += $( 'input#ce' ).attr( 'value' );
			x += '</a><br />';
			x += $( 'input#cp' ).attr( 'value' );
			
			// hide any corp only information
			$( '.corp-only' ).hide();
			$( 'A#nav_5' ).css( 'border-right', 'none' );

			$( '#layout-header' ).addClass( 'affiliate' );	
			$( 'div#login-buttons' ).fadeIn( 1000 );
		} 
		z = '&nbsp;</div>';
		$( '#layout-header' ).append( y + x + z );
		
		if( $( 'input#page' ).attr( 'value' ) == 'corp'){
			
			alert($( 'input#page' ).attr( 'value' ));
			$( 'div#affiliate' ).fadeOut( 1000 );
		
		}
		
	} catch( err ){
		if( debug ) alert( 'initializePWPInformation(): ' + err );
	}
}

$( document ).ready( function(){
	$( 'div#layout-highlight' ).show();
	// position the absolute positioned elements
	positionAbsolutes();
	// show the navigation bar
	$( 'div.nav-container' ).show();
	// initialize the overlay object to use later
	initializeOverlay();
	// initialize any page videos
	initializePageVideo();
	// if this is a redirect from a PWP then display
	// the information passed to us from infoTrax
	initializePWPInformation();	
	// create a catch for the chip dock clicks
	ptn.chipClick = function( index ){
		switch( index ){
		case 0: window.location = 'learn.php'; break;
		case 1: window.location = 'play.php'; break;
		case 2: window.location = 'earn.php'; break;	
		case 3: window.location = 'market.php'; break;
		case 4: window.location = 'calling.php'; break;
		case 5: enrollClick(); break;
		}
	}
	
	
	
	// start the introduction sequence if this is their first time
	if( $( 'input#useSplash' ).attr( 'value' ) == '1' ){
	
		startOpportunityVideo();
		//startIntroductionVideo();
		//showOverlay( $('input#vp').attr('value') , 'video');	
	
	} else {
	
		//startOpportunityVideo();
		placeVersionInformation();
		initializeSlideshow();
	}
	$( 'BODY.corporate' ).fadeIn( 200 );
	$( 'A#nav_0' ).css( 'border-left', 'none' );
	$( 'A#nav_5' ).css( 'border-right', 'none' );
	
	if( typeof _pageInit == 'function' ){
		_pageInit();
	}
});
