var searchoverlay = false;
var thumbase = '/wgbh/pages/frontline/art/viewimages/50/';
var searchhack = new Array();
var searchterm = new String();
var publicsearchterm = new String();
var ie7bug = new Array();

$(document).ready(function () {
	if(location.href.indexOf('?cat=') != -1){
		cat_filter(location.href.substring(location.href.indexOf('?cat=') + 5,location.href.length),false);
	} else {
		sidebar_update('all');
	}
	$(".prouter").each(function () {
		var idx = $(this).attr('id').substring(1,$(this).attr('id').length);
		progdata[idx].tit = $(this).find('.title').text();
		progdata[idx].dsc = $(this).find('.dsc').text();
		var tempurl = $(this).find('a').attr("href");
		progdata[idx].url = tempurl.substring(0,tempurl.indexOf('?'));
		progdata[idx].num = $(this).find('.pr').attr('num');
		var lctit = progdata[idx].tit;
		lctit = lctit.toLowerCase();
		searchhack[lctit] = idx;
		ie7bug[idx] = $(this).html();
		$(this).hover(function () {
			var leftright = false;
			var thisoffset = $(this).offset()
			if(thisoffset.left > ($(".mainarea").width() / 2)){
				leftright = true;
			}
			var popx = (leftright)? thisoffset.left - 232:thisoffset.left + 108;
			$("#popover_bg").stop();
			$("#popover").stop();
			$("#popover_bg").hide();
			$("#popover").hide();
			$("#popover").css({ 'top':thisoffset.top + 'px', 'left':popx + 'px','opacity':.9 });
			var finalhtml = '<h2>' + $(this).find('.title').text() + '</h2>';
			finalhtml += '<span class="airdate">Original airdate ' + $(this).find('.dat').text() + '</span>';
			finalhtml += $(this).find('.dsc').text();
			finalhtml += '<span class="length">' + progdata[idx].len + ' minutes';
			if(progdata[idx].med == 0){
				finalhtml += ' / Flash Video';
			}
			finalhtml += '</span>';
			$("#popover").html(finalhtml);
//			$(this).append('<span class="comeon">WATCH NOW &#187;</span>');
			if(leftright){
				$("#popover_bg").css({ 'top':thisoffset.top + 'px', 'left':thisoffset.left + 'px','opacity':.95,'width':0 });
				$("#popover_bg").animate({  'left':thisoffset.left - 232 + 'px', 'width':'232px' }, function () { $("#popover").fadeIn('fast'); });
			 } else {
				$("#popover_bg").css({ 'top':thisoffset.top + 'px', 'left':popx + 'px','opacity':.95,'width':0 });
				$("#popover_bg").animate({ 'width':'232px' }, function () { $("#popover").fadeIn('fast'); });
			 }
		},function () {
			$(this).find('.comeon').remove();
			$("#popover_bg").stop();
			$("#popover").stop();
			$("#popover").hide();
			$("#popover_bg").hide();
		});
	});
	_papobject.embedSWF('../art/swf/viewtop.swf','slider','100%', 220, '9.0.0', null, { 'xmlfile': xmlfile }, { menu: "false", bgcolor: "000000", wmode: "opaque" }, { id: 'slider', name: 'slider' } );
});

function cat_filter (targetcategory){
	if(searchoverlay){
		removeoverlay();
	}
	var allmode = (targetcategory == 'all')? true:false;
	var classicmode = (targetcategory == 'cls')? true:false;
	
	sidebar_update(targetcategory);
	
	$("#programs").stop();
	$("#programs").hide();
	
	$("#programs").html();
	var output = '';
	var adds = new Array();

	for(var x = 0; x < progdata.length; x++){
		if(allmode){
			output += '<div class="prouter" id="p' + x + '">';
			output += ie7bug[x];
			output += '</div>';
			adds.push(x);
		} else {
			if(classicmode){
				if(progdata[x].cls == 1){
					output += '<div class="prouter" id="p' + x + '">';
					output += ie7bug[x];
					output += '</div>';
					adds.push(x);
				}
			} else {
				if(progdata[x].cat.indexOf(targetcategory) != -1){
					output += '<div class="prouter" id="p' + x + '">';
					output += ie7bug[x];
					output += '</div>';
					adds.push(x);
				}
			}
		}
	}
	$("#programs").html(output);	
	
	for(var y = 0; y < adds.length; y++){
		var z = adds[y];
			$("#p" + z).hover(function () {
				var leftright = false;
				var thisoffset = $(this).offset()
				if(thisoffset.left > ($(".mainarea").width() / 2)){
					leftright = true;
				}
				var popx = (leftright)? thisoffset.left - 232:thisoffset.left + 108;
				$("#popover_bg").stop();
				$("#popover").stop();
				$("#popover_bg").hide();
				$("#popover").hide();
				$("#popover").css({ 'top':thisoffset.top + 'px', 'left':popx + 'px','opacity':.9 });
				var finalhtml = '<h2>' + $(this).find('.title').text() + '</h2>';
				finalhtml += '<span class="airdate">Original airdate ' + $(this).find('.dat').text() + '</span>';
				finalhtml += $(this).find('.dsc').text();
				finalhtml += '<span class="length">' + progdata[z].len + ' minutes';
				if(progdata[z].med == 0){
					finalhtml += ' / Flash Video';
				}
				finalhtml += '</span>';
				$("#popover").html(finalhtml);
//				$(this).append('<span class="comeon">WATCH NOW &#187;</span>');
				if(leftright){
					$("#popover_bg").css({ 'top':thisoffset.top + 'px', 'left':thisoffset.left + 'px','opacity':.95,'width':0 });
					$("#popover_bg").animate({  'left':thisoffset.left - 232 + 'px', 'width':'232px' }, function () { $("#popover").fadeIn('fast'); });
				 } else {
					$("#popover_bg").css({ 'top':thisoffset.top + 'px', 'left':popx + 'px','opacity':.95,'width':0 });
					$("#popover_bg").animate({ 'width':'232px' }, function () { $("#popover").fadeIn('fast'); });
				 }
			},function () {
				$(this).find('.comeon').remove();
				$("#popover_bg").stop();
				$("#popover").stop();
				$("#popover").hide();
				$("#popover_bg").hide();
			});
	}
	
	$("#programs").fadeIn();

}

function sidebar_update (target) {

	$("#vsid").find('li').each(function () {

		if(target == $(this).attr('id')){
			$(this).addClass("highlighted");
			$(this).unbind('click');
		} else {
			$(this).removeClass("highlighted");
			$(this).bind('click',function () { cat_filter($(this).attr('id')) });
		}
	
	});
	
}

function searchprocess () {
	if(!searchoverlay){
		createoverlay();
	}
	document.getElementById('searchresults_inner').innerHTML = '';
	var realvar = document.getElementById("videosearch").value;
	if(realvar.indexOf('The ') != -1 || realvar.indexOf('the ') != -1){
		realvar = realvar.substring(4,realvar.length);
	}
	
	publicsearchterm = realvar;
	searchterm = realvar.toLowerCase();
	$("#searchresults_inner").html("<img src=\"../art/vprogress.gif\" width=\"24\" height=\"24\" style=\"margin: 100px 0 0 320px\" />");
	_pap_inplacesearch("searchrender",encodeURIComponent(realvar),0,false);
	$("#searchresults_count").hide();

}

function searchrender (res) {
	
	var finalrescount = 0;
	var finalres = new Array();
	var finalresa = new Array();
	var finalresb = new Array();
	var titles = new Array();
	
	for(var x = 0; x < res.length; x++){
		if(res[x].brandid == 2 || res[x].brandid == 3){
			var thisres = new Object();
			var goonthen = true;
			thisres.title = res[x].title;
			thisres.brandid = res[x].brandid;
			thisres.desc = res[x].description;
			thisres.thumbnail = res[x].thumbnail;
			if(res[x].packagemode){
				var titlesinparens = res[x].title;
				if(titlesinparens.indexOf(' (') != -1){
					titlesinparens = titlesinparens.substring(0,titlesinparens.indexOf(' ('));
				}
				var lctitlesinparens = titlesinparens.toLowerCase();
				
				if(searchhack[lctitlesinparens]){
					thisres.title = titlesinparens;
					thisres.url = progdata[searchhack[lctitlesinparens]].url + '?utm_campaign=viewpage&utm_medium=viewsearch&utm_source=viewsearch';
					thisres.thumbnail = thumbase + progdata[searchhack[lctitlesinparens]].num + '.jpg';
					thisres.spawn = false;
				} else {
					thisres.url = res[x].idcode;
					thisres.spawn = true;
				}
				if(titles[thisres.title]){
					goonthen = false;
				} else {
					titles[thisres.title] = 1;
				}
			} else {
				if(res[x].packagetitle != undefined && res[x].packagetitle != res[x].title){
					thisres.title = res[x].packagetitle + ': ' + res[x].title;
				}
				thisres.url = res[x].idcode;
				thisres.spawn = true;
			}
			if(goonthen){
				if(res[x].brandid == 2){
					finalresa.push(thisres);
				} else {
					finalresb.push(thisres);
				}
			}
		}
	}

	for(var y = 0; y < progdata.length; y++){
		if(!titles[progdata[y].tit]){
			var ptit = progdata[y].tit.toLowerCase();
			var pdsc = progdata[y].tit.toLowerCase();
			if(ptit.indexOf(searchterm) != -1 || pdsc.indexOf(searchterm) != -1){
				var thisres = new Object();
				thisres.title = progdata[y].tit;
				thisres.url = progdata[y].url + '?utm_campaign=viewpage&utm_medium=viewsearch&utm_source=viewsearch';
				thisres.brandid = 2;
				thisres.spawn = false;
				thisres.desc = progdata[y].dsc;
				thisres.thumbnail = thumbase + progdata[y].num + '.jpg';
				finalresa.push(thisres);
			}
		}
	}
	
	finalres = finalresa.concat(finalresb);
	var finaltext = '<p style="text-align: center; color: #ffffff; padding: 35px 5px 35px 5px; font-size: 17px">Sorry, no video was found containing <span style="color: #aaaaaa;">"' + publicsearchterm + '"</span></p>';
	
	if(finalres.length > 0){
		finaltext = '';
		
		for(var z = 0; z < finalres.length; z++){
			finaltext += '<div class="srhres" onclick="';
			if(finalres[z].spawn){
				finaltext += '_pap_spawn(\'' + finalres[z].url + '\',1);" ';
				finalres[z].desc += ' <span class="srhwrn">(opens in a new window)</span>';
			} else {
				finaltext += 'location.href=\'' + finalres[z].url + '\'" ';
			}
			finaltext += '><img src="' + finalres[z].thumbnail + '" width="50" height="50" alt="" />';
			finaltext += finalres[z].title + '<span class="srhdsc">' + finalres[z].desc + '</span></div>';
		}
		
		if(finalres.length > 5){
			$("#searchresults_outer").animate({ height: ((finalres.length * 62) + 60) + 'px' });
			$("#searchresults_inner").animate({ height: (finalres.length * 62) + 'px' });
		}
		
		var resout = (finalres.length == 1)? finalres.length + ' result': finalres.length + ' results';
		
		$("#searchresults_count").html(resout);
		$("#searchresults_count").show();
		
	}
	
	
	document.getElementById('searchresults_inner').innerHTML = finaltext;
	
}

function createoverlay() {

	var nd = document.createElement('div');
	nd.setAttribute('id','overlay');
	document.body.appendChild(nd);
	$("#overlay").hide();
	$("#overlay").css({ top: '155px', height:($("#t1").height() + $("#t2").height()) + 'px', width:($("#container").width() - 210) + 'px', left:'0px' });
	$("#overlay").fadeIn();

	var sd = document.createElement('div');
	sd.setAttribute('id','searchresults_outer');
	document.body.appendChild(sd);
	$("#searchresults_outer").hide();
	$("#searchresults_outer").css({ top: '190px', left: ((($("#container").width() - 210) / 2) - 350) + 'px' });
	$("#searchresults_outer").fadeIn();

	var so = document.createElement('div');
	so.setAttribute('id','searchresults_inner');
	document.body.appendChild(so);
	$("#searchresults_inner").css({ top: '190px', left: ((($("#container").width() - 210) / 2) - 350) + 'px' });

	var sc = document.createElement('span');
	sc.setAttribute('id','searchresults_count');
	document.body.appendChild(sc);
	$("#searchresults_count").css({ top: '195px', left: ((($("#container").width() - 210) / 2) - 350) + 'px' });

	var sl = document.createElement('span');
	sl.setAttribute('id','searchresults_close');
	document.body.appendChild(sl);
	$("#searchresults_close").hide();
	$("#searchresults_close").css({ top: '190px', left: ((($("#container").width() - 210) / 2) - 350) + 'px' });
	$("#searchresults_close").html('<span style="color: #aaaaaa">X</span> CLOSE');
	$("#searchresults_close").click(function () {
		removeoverlay();
	});
	$("#searchresults_close").fadeIn();
	
	searchoverlay = true;
	sidebar_update(0);
}

function removeoverlay () {
	$("#searchresults_inner").fadeOut("fast",function () {
		document.body.removeChild(document.getElementById('searchresults_inner'));
	});
	$("#searchresults_outer").fadeOut("fast",function () {
		document.body.removeChild(document.getElementById('searchresults_outer'));
	});
	$("#overlay").fadeOut("fast",function () {
		document.body.removeChild(document.getElementById('overlay'));
		searchoverlay = false;
	});
	document.body.removeChild(document.getElementById('searchresults_count'));
	document.body.removeChild(document.getElementById('searchresults_close'));
}