var curtab = -1;
var navon = false;
var navov = false;
var navextov = false;

$(document).ready(function () {
	if(navigator.userAgent && navigator.userAgent.indexOf('iPad') != -1){
		_isipad = true;
	}
	_promourl = window.location.href;
	if(_promourl.indexOf('/wgbh/pages/frontline/') != -1){
		var workstring = _promourl.substring(_promourl.indexOf('/wgbh/pages/frontline/') + 22, _promourl.length);
		var ws = workstring.split("/");
		_promourl = 'http://www.pbs.org/wgbh/pages/frontline/' + ws[0];
	}
	
	$('li#_pgelatest').click(function() {
		$('div._pgmostouter').hide();
		$('div._pglatestouter').fadeIn('fast');
		$(this).removeClass('_pgactive');
		$('li#_pgemost').addClass('_pgactive');

	});
	
	$('li#_pgemost').click(function() {
		$('div._pglatestouter').hide();	
		$('div._pgmostouter').fadeIn('fast');
		$(this).removeClass('_pgactive');
		$('li#_pgelatest').addClass('_pgactive');
	});

});

function nav_in () {
	navon = true;
	$("#_next").stop();
	$("#_next").css({ 'opacity':1 });
	$("#_next").fadeIn();
	$("#_next").mouseenter(function () {
		navextov = true;
	});
	$("#_t"+curtab).removeClass('non').addClass('non');
	$("#_next").mouseleave(function () {
		navextov = false;
		nav_out_decide();
	});
	$("#extscl").click(function () {
		navov = false;
		navextov = false;
		nav_out();
	});
}

function nav_tab (idx){
	curtab = idx;
	$(".tab").hide();
	$("#tab"+idx).show();
}

function nav_out_decide () {
	ival = setTimeout("nav_out()",800);
}
		
function nav_out () {
	if(!navov && !navextov){
		$("#extscl").unbind('click');
		$("#_next").unbind('mouseenter');
		$("#_next").unbind('mouseleave');
		$("#_next").stop();
		$("#_next").fadeOut();
		$(".non").removeClass('non');
		navon = false;
	}
}


var pp_playercnt = 0;
var loaders = new Array();

function partnerplayer (inarray) {
	this.server = Object.beget(flp_comm);
	this.video = Object.beget(flp_video);
	this.w = (inarray.w != undefined)? inarray.w:512;
	this.h = (inarray.h != undefined)? inarray.h:288;
	this.autoplay = (inarray.autoplay != undefined)? inarray.autoplay:false;
	this.targetdiv = inarray.targetdiv;
	this.playercount = pp_playercnt;
	this.partnerplayer_loader = pp_loader;
	if(navigator.userAgent && (navigator.userAgent.indexOf('iPad') != -1 || navigator.userAgent.indexOf('iPhone') != -1) && inarray.npaid != undefined){
		this.video.parse_video_id(inarray.npaid).get_data_from_server(this.server);
		 loaders[this.playercount] = setInterval(this.partnerplayer_loader,500,this.video,this.targetdiv,this.playercount,this.autoplay);		
	} else {
		var chapterbar = (inarray.chapterbar != undefined)? inarray.chapterbar:false;
		document.getElementById(this.targetdiv).innerHTML = '<iframe id="partnerPlayer' + pp_playercnt + '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="width:' + w + 'px; height:' + h + 'px;" src="http://video.pbs.org/widget/partnerplayer/' + inarray.mediaid + '/?w=' + w + '&h=' + h + '&chapterbar=' + chapterbar + '&autoplay=' + autoplay + '"></iframe>';
	}
	pp_playercnt++;
}

function pp_loader (v,td,pc,ap) {
	if(v.loaded){
		clearInterval(loaders[pc]);
		var output = '<video id="npavid' + pc + '" width="' + this.w + 'px" height="' + this.h + 'px" controls="controls"';
		output += (ap)? ' autoplay="true"':'';
		output += ' poster="' + v.tmb + '" src="http://www-tc.pbs.org' + v.video_file + '"></video>';
		document.getElementById(td).innerHTML = output;
	}
}

function doccloud (target,wid,yesno){
	if(yesno == undefined){
		yesno = true;
	}
		DV.load('http://www.documentcloud.org/documents/' + target + '.js', {
			width: wid,
			height: $(window).height() - 120,
			container: "#viewer-" + target,
			sidebar: yesno
		  });
}

function hasstarted () { }
