var vidBase = 'http://video.neuroscript.net/vid';
//var vidBase = 'http://216.19.207.48/vid';
var scrollSize = 4;
var activeCat = '';
var activePage = 0;
var toPlay = '';
var skipOnBegin = false;
var dlgOpen = false;

var page = window.location.href;
var i = page.lastIndexOf( "/" );
if( i >= 0 ) page = page.substr( i + 1 );

function getPageAndVideo( clip ) {
	var v = clip.url;
	var i = v.lastIndexOf( "/" );
	if( i >= 0 ) v = v.substr( i + 1 );
	var data = page + " : " + v;
	return data;
}

function playVid( vid ) {

	$f( "playerBig", { src: "http://releases.flowplayer.org/swf/flowplayer-3.1.5.swf", wmode: "opaque" }, {			
		clip: { 
			baseUrl: vidBase,
			autoPlay: true,
			autoBuffering: true,
			onBegin: function( clip ) {
				pageTracker._trackEvent("Videos", "Play", getPageAndVideo( clip ));
			},
			onPause: function(clip) {
				pageTracker._trackEvent("Videos", "Pause", getPageAndVideo( clip ), parseInt(this.getTime()));
			},
			onStop: function(clip) {
				pageTracker._trackEvent("Videos", "Stop", getPageAndVideo( clip ), parseInt(this.getTime()));
			},
			onFinish: function(clip) {
				pageTracker._trackEvent("Videos", "Finish", getPageAndVideo( clip ));
			},
			provider: 'pseudo'
		},
		playlist: [
			vid
		],
		plugins:  {
			controls: {
				backgroundColor: '#afb1c8'
			},
      pseudo: {
				url: 'http://releases.flowplayer.org/swf/flowplayer.pseudostreaming-3.1.3.swf'
			}
		}
	});
}

function setupVidPlayer( cat, vid, ap ) {
	var pl = Array();
	getPlayList( cat );

	function getPlayList( theCat ) {
		pl = [];
		var idx = 0;
		if( theCat == null || theCat == '' ) return;
		var tc = "vidCat" + theCat;
		$("div.navClick").each( function() {
			par = $(this).parent();
			parScroll = par ? par.parent() : null;
			parNav = parScroll ? parScroll.parent() : null;
			if( parNav ) {
				if( parNav.attr('id') == tc ) {
					pl[idx] = vidBase + '/' + $(this).attr('title');
					idx++;
				}
			}
		});
	}

	$f( "player", { src: "http://releases.flowplayer.org/swf/flowplayer-3.1.5.swf", wmode: "opaque", width:576, height:457 }, {
		 /*
		screen: {
			width:576,
			height:433,
		},
		*/
		clip: { 
			baseUrl: vidBase,
			autoPlay: ap,
			autoBuffering: true,
			onBegin: function( clip ) {
				if( !skipOnBegin ) {
					var i = clip.completeUrl.lastIndexOf( "/" );
					if( i >= 0 ) {
						var vid = clip.completeUrl.substr( i + 1 );
						var res = findVid( vid );
						var obj = res.theObj;
						if( obj ) setActiveVid( obj, activeCat, vid, res.theIdx );
					}
					toPlay = '';
					pageTracker._trackEvent("Videos", "Play", getPageAndVideo( clip ));
				}
				else skipOnBegin = false;
			},
			onPause: function(clip) {
				pageTracker._trackEvent("Videos", "Pause", getPageAndVideo( clip ), parseInt(this.getTime()));
			},
			onStop: function(clip) {
				pageTracker._trackEvent("Videos", "Stop", getPageAndVideo( clip ), parseInt(this.getTime()));
			},
			onFinish: function(clip) {
				pageTracker._trackEvent("Videos", "Finish", getPageAndVideo( clip ));
			},
			provider: 'pseudo'
		},
		playlist: [],
		plugins:  {
			controls: {
				playlist: true,
				backgroundColor: '#afb1c8'
			},
      pseudo: {
				url: 'http://releases.flowplayer.org/swf/flowplayer.pseudostreaming-3.1.3.swf'
			}
		},
		onLoad: function() {
			$f().setPlaylist( pl );
			return true;
		},
		onPlaylistReplace: function( npl ) {
			if( toPlay != '' ) {
				var l = pl.length;
				var comp = vidBase + '/' + toPlay;
				for( var i = 0; i < l; i++ ) {
					if( comp == pl[i] ) {
						$f("player").play( i );
						break;
					}
				}
			}
		}
	});
	
	$(".vertical").scrollable({vertical:true, size:scrollSize}).mousewheel();

	$(".theSlide").bind( "click", function() {
		activePage = 0;
		$(".theSlide").removeClass( "active" );
		$(this).addClass( "active" );
		$(".vidCat").css( "display", "none" );
		switch( $(this).attr( 'id' ) ) {
			case 'navDemos': $("#vidCatDemos").css( "display", "block" ); activeCat = 'Demos'; break;
			case 'navSamples': $("#vidCatSamples").css( "display", "block" ); activeCat = 'Samples'; break;
			case 'navHelp': $("#vidCatHelp").css( "display", "block" ); activeCat = 'Help'; break;
			case 'navProducts': $("#vidCatProducts").css( "display", "block" ); activeCat = 'Products'; break;
		}
		getPlayList( activeCat );
		$f("player").setPlaylist( pl );
	});

	$(".items > div").bind( "mouseover", function() { if( !$(this).hasClass( 'activeVid' ) ) $(this).addClass( "over" ); });
	$(".items > div").bind( "mouseout", function() { $(this).removeClass( "over" ); });

	if( cat != '' ) {
		activeCat = cat;
		var cn = '#nav' + cat;
		$(cn).addClass( "active");
		var vn = '#vidCat' + cat;
		$(vn).css( "display", "block" );
	}
	else {
		activeCat = 'Samples';
		$("#vidCatSamples").css( "display", "block" );
		$("#navSamples").addClass( "active" );
	}

	if( vid != '' ) {
		skipOnBegin = true;
		if( ap ) toPlay = vid;
		var res = findVid( vid );
		var obj = res.theObj;
		if( obj ) setActiveVid( obj, cat, vid, res.theIdx );
	}

	$("div.navClick").bind( "click", function() {
		setActiveVid( $(this), activeCat, $(this).attr("title") );
		$f().unload();
		toPlay = $(this).attr("title");
		$f().play( toPlay );
// Not working in ie7-...do we really need it anyway?
//		getPlayList( activeCat );
//		$f("player").setPlaylist( pl );
	 });
	
	function setActiveVid( obj, theCat, theVid, idx ) {
		if( obj != null ) {
			if( theCat != '' ) activeCat = theCat;
			$("div.navClick").removeClass( 'activeVid' );
			$(obj).addClass( 'activeVid' );
			var l = 'http://www.neuroscript.net/videos.php?cat=' + theCat + '&vid=' + theVid;
			var vl = '<a href="' + l + '">' + l + '</a>';
			$("#vidLink").html( vl );
			scrollToVid( obj, idx );
		}
	}

	function scrollToVid( obj, idx ) {
		if( idx == null || idx == "undefined" ) return;
		var divPar = $(obj).parent();
		var divScroll = divPar ? divPar.parent() : null;
		if( divScroll ) {
			var api = $(divScroll).scrollable();
			if( api != null ) {
				var pg = ( idx / scrollSize ) | 0;
				if( pg != activePage ) {
					activePage = pg;
					api.setPage( pg );
					api.seekTo( idx );
				}
			}
		}
	}

	$("span.navClick").bind( "click", function() {
		var tv = $(this).attr('id');
		var res = findVid( tv );
		var obj = res.theObj;
		if( obj != null ) {
			$(".theSlide").removeClass( "active" );
			$(".vidCat").css( "display", "none" );
			var cat = $(this).attr( 'title' );
			var cn = '#nav' + cat;
			$(cn).addClass( "active");
			var vn = '#vidCat' + cat;
			$(vn).css( "display", "block" );
			setActiveVid( obj, cat, tv, res.theIdx );
			$f().unload();
			toPlay = tv;
			$f().play( toPlay );
//			getPlayList( activeCat );
//			$f("player").setPlaylist( pl );
			scroll( 0, 0 );
		}
	 });

	function findVid( v ) {
		var idx = 0, found = false;
		var obj = null, par = null, parScroll = null, parNav = null, lastPar = null;
		$("div.navClick").each( function() {
			par = $(this).parent();
			parScroll = par ? par.parent() : null;
			parNav = parScroll ? parScroll.parent() : null;
			if( !found && ( parNav.attr('id') != lastPar ) ) {
				idx = 0;
				lastPar = parNav.attr('id');
			}
			if( $(this).attr( 'title' ) == v ) { found = true; obj = this; }
			if( !found ) idx++;
		});
		return { theObj:obj, theIdx:idx };
	}

	if( ap ) {
		var res = findVid( vid );
		var obj = res.theObj;
		if( obj != null ) {
			$(obj).addClass( 'activeVid' );
			var divPar = $(obj).parent();
			var divScroll = divPar ? divPar.parent() : null;
			if( divScroll ) {
				var api = $(divScroll).scrollable();
				if( api != null ) {
					var pg = ( res.theIdx / scrollSize ) | 0;
					api.setPage( pg );
					api.seekTo( res.theIdx );
				}
			}
		}
	}
}

