var item_current=0;
var scroll_ind=0;
var ani_ind=0;
var ani_start,ani_delta;
var item_disp;
var line_height=33;
var historylen=0;
var is_jswin=false;
arr_img=['bkg_user','thumb'];
arr_navto=['playlist','html_youtube','xml_applemovie','xml_shoutcast','rss','rss_flickr_daily'];
amatrix=[0,.2,.4,.6,.8,1];
var list_length;

tmp0=new Image();
tmp0.src='/images/preview/pointer-nofocus.cur';
tmp1=new Image();
tmp1.src='/images/preview/pointer-focus.cur';

function itemUse(){
	zitem=item_data[item_current];
	if(zitem.type=='')return false;
	if(zitem.processor>''){
		if(badproc.inArray(zitem.processor)){
			dialog_alert('static','Message',"This item uses a processor which is incompatible with the sim.",{top:-60,left:28});
			return false;
		}
		
		proc_item=item_current;
		procLogPrep();
		//proc_worker.location='/cgi-bin/proc_sim.cgi?url='+unescape(zitem.URL)+'&processor='+unescape(zitem.processor);
		var pargs='url='+escape(zitem.URL)+'&processor='+escape(zitem.processor);
		if(parent.log_verbose==1){
			pargs+='&verbose=1';
		}
		proc_worker.location='/cgi-bin/proc_sim.cgi?'+pargs;
		return false;
	}

	thisURL=zitem.URL
	ext=extGet(thisURL);
	if(thisURL.match(/^rss:/)){
		zitem.type='rss';
		thisURL=thisURL.replace(/^rss/,'http');
	}
	baseargs='URL='+escape(thisURL)+'&background='+zitem.background+'&logo='+zitem.logo+'&name='+zitem.name+'&title='+titl;
	if(zitem.type!='search_youtube' && zitem.type!='search_shoutcast' && zitem.type!='search') parent.addrTypeSet(zitem.type);
	if(arr_navto.inArray(zitem.type)){
		$('list_entries').style.display='none';
		$('description').style.display='none';
		$('please_wait').style.display='block';
		parent.historylen++;
		histRec(thisURL,zitem.type);
		abar(thisURL);
		window.location='/preview/?type='+zitem.type+'&'+baseargs;
		return false;
	}
	switch(zitem.type){
		case 'audio':
			abar(thisURL);
			thisURL=thisURL.replace(/\.pls$/i,'.mp3');
			audioHandle(thisURL);
			return false;
		case 'text':
			abar(thisURL);
			textShow(item_current);
		case 'image':
			abar(thisURL);
			imageShow(thisURL);
			break;
		case 'video':
			abar(thisURL);
			videoHandle(thisURL);
			return false;
		case 'search_youtube':
			search_youtube_prompt();
			return false;
		case 'search':
			search_prompt();
			return false;
		case 'script':
			window.location=thisURL;
			return false;
		case 'download':
			window.location=thisURL;
			return false;
		case 'plugin':
			window.location=thisURL;
			return false;
		default:
			dialog_alert('static','Message',"XBMC will try to open "+thisURL,{top:-60,left:28});
	}
}

/* Processor & logging
********************************/

var proc_item;
function procLogPrep(){
	$('info_text').innerHTML='';
	$P('txt_log').innerHTML='';
	return true;
}

function procLog(str){
	str=str.replace(/</g,'&lt;');
	str=str.replace(/>/g,'&gt;');
	if(arguments.length>1){
		str='<span class="'+arguments[1]+'">'+str+'</span>';
	}else if(agent.ie){
		str='<div>'+str+'</div>';
	}
	$P('txt_log').innerHTML+=str+"\r\n";
	$P('txt_log').scrollTop=$P('txt_log').scrollHeight;
}

function procStatus(what){
	$('info_text').innerHTML=what;
}

function procParamSet(key,val){
	//$('playback_params').innerHTML+='<strong>'+key+'</strong>: '+val+'<br />';
	if(key=='url'){
		item_data[proc_item]['URL']=val;
	}else if(key=='playpath'){
		item_data[proc_item]['playpath']=val;
	}
}

function procFinished(){
	item_data[proc_item].processor='';
	itemUse();
}

/* Interface
********************************/

function keymon(e){
	evt=(e)?e:window.event;
	pK=e?e.keyCode:window.event.keyCode;
	if(prompt_active){
		switch(pK){
			case 27:prompt_active=false;dialog_hide();return false;break;
			case 13:
				if(item_data[item_current].type=='search_youtube'){
					search_youtube_do();
				}
				if(item_data[item_current].type=='search'){
					search_do();
				}
				return false;
				break;
			default: return e;
		}
	}
	if(is_jswin && pK!=8 && pK!=27)return false;
	switch(pK){
		case 8:backspace(true);return false;break;
		case 37:backspace(true);return false;break;
		case 40:itemInc(1);parent.pnavClick('down');return false;break;
		case 38:itemInc(-1);parent.pnavClick('up');return false;break;
		case 39:descView();return false;break;
		case 33:itemInc(-10);parent.pnavClick('pgup');return false;break;
		case 34:itemInc(10);parent.pnavClick('pgdn');return false;break;
		case 13:itemUse();return false;break;
		case 27:parent.preview_hide();return false;break;
		case 116:img_refresh();return false;break;
	}
	return e;
}

function backspace(clicksim){
	if(is_jswin){
		vidHide();
		textHide();
		imageHide();
		abar(parent.hist[parent.historylen].URL);
		parent.addrTypeSet(thistype);
	}else{
		if(parent.historylen==0)return false;
		parent.back_clicked=true;
		history.back();
	}
	if(clicksim)parent.pnavClick('back');
}

function itemInc(inc){
	tgt=item_current+inc;
	isPg=Math.abs(inc)>1;
	if(tgt<0){
		if(isPg){
			tgt=0;
		}else{
			tgt=item_data.length-1;
		}
	}
	if(tgt>=item_data.length){
		if(isPg){
			tgt=item_data.length-1;
		}else{
			tgt=0;
		}
	}

/*
	item_disp[item_current].className='';
	try{
		item_disp[tgt].className='focus';
	}catch(err){
		//
	}
	if(item_data[tgt].thumb=='' || item_data[tgt].thumb=='default'){
		$('thumb').src=logo;
	}else{
		$('thumb').src=item_data[tgt].thumb;
	}
	
	// viewable in preview
	var vclass;
	if(item_data[tgt].type=='audio' || item_data[tgt].type=='video'){
		if(item_data[tgt].processor>''){
			if(badproc.inArray(item_data[tgt].processor)){
				vclass='incompat';
			}else{
				vclass='compat';
			}
		}else{
			vclass='compat';
		}
	}else{
		vclass='na';
	}
	$P('preview_viewable').className=vclass;
*/

	/*
	if(item_data[tgt].description>''){
		$('description_text').innerHTML=item_data[tgt].description;
	}else{
		$('description_text').innerHTML='';
	}
	descScrollInit();	
	*/	
	if(tgt>item_current){
		if(tgt-scroll_ind>(list_length-1)){
			item_scroll_init(scroll_ind,tgt-(list_length-1));
			scroll_ind=tgt-(list_length-1);
		}
	}else{
		if(tgt-scroll_ind<0){
			item_scroll_init(scroll_ind,tgt);
			scroll_ind=tgt;
		}
	}
	$('pager').innerHTML=(tgt+1)+'/'+item_data.length;
	itemHilite(tgt);
	item_current=tgt;
	stat(item_data[item_current].URL);
}

function item_scroll_init(start,finish){
	ani_ind=0;
	ani_start=start*line_height;
	ani_delta=finish*line_height-ani_start;
	setTimeout('item_scroll()',1);
}

function item_scroll(){
	ani_ind++;
	if(ani_ind<amatrix.length){
		dest=ani_start+ani_delta*amatrix[ani_ind];
		$('list_entries').style.marginTop='-'+dest+'px';
		setTimeout('item_scroll()',9);
	}
}

function itemMouseover(ev){
	if(prompt_active)return false;
	ev=ev||window.event;
	var srcElem=ev.target||ev.srcElement;
	while(srcElem.parentNode && srcElem.tagName!='LI'){
		srcElem=srcElem.parentNode;
	}
	if(srcElem.id.search(/^i(\d+)/)==0){
		tgt=parseInt(RegExp.$1);
		if(tgt==item_current)return false;
		itemHilite(tgt);
		stat(item_data[item_current].URL);
	}
}

function itemHilite(tgt){
	item_disp[item_current].className='';
	try{
		item_disp[tgt].className='focus';
	}catch(err){
		//
	}
	if(item_data[tgt].thumb=='' || item_data[tgt].thumb=='default'){
		$('thumb').src=logo;
	}else{
		$('thumb').src=item_data[tgt].thumb;
	}

	// viewable in preview
	var vclass;
	if(item_data[tgt].type=='audio' || item_data[tgt].type=='video'){
		if(item_data[tgt].processor>''){
			if(badproc.inArray(item_data[tgt].processor)){
				vclass='incompat';
			}else{
				vclass='compat';
			}
		}else{
			vclass='compat';
		}
	}else{
		vclass='na';
	}
	$P('preview_viewable').className=vclass;
	item_current=tgt;
}

function img_refresh(){
	dt=new Date();
	dtt=dt.getTime();
	for(i=0;i<arr_img.length;i++){
		stmp=$(arr_img[i]).src;
		if(stmp.search(/preview\/blank\.gif$/)<0){
			parts=stmp.split('?');
			$(arr_img[i]).src=parts[0]+'?'+dtt;
		}
	}
	if(logo!="/images/preview/blank.gif"){
		logoCache=new Image();
		logoCache.src=logo+'?'+dtt;
	}
}

function stat(what){
	$P('preview_statusbar').innerHTML=what;
}

function abar(what){
	$P('address_bar').value=what;
//	parent.dbg(item_data[item_current].type);
}

function previewDummies(){
	dialog_alert('static','Message','Sorry - these are just here for show.  :)',{top:-60,left:28});
}

/* History
********************************/


function histRec(what,ptype){
	parent.hist[parent.historylen]={
		URL:what,
		type:ptype,
		ind:item_current,
		scroll:scroll_ind
	};
}

function histCheck(){
	parent.addrTypeSet(thistype);
	if(parent.back_clicked){
		oHist=parent.hist[parent.historylen];
		itemHilite(oHist.ind);
		scroll_ind=oHist.scroll;
		$('list_entries').style.marginTop='-'+(scroll_ind*line_height)+'px';
		parent.back_clicked=false;
		parent.historylen--;
		if(parent.historylen>=0){
			abar(parent.hist[parent.historylen].URL);
		}
	}
}

function histRestore(){
	
}

function previewHome(){
	parent.previewHome();
}

/* Image preview
********************************/

var img_loader;
var arrImg=['gif','jpg','png'];

function imageShow(pic){
	is_jswin=true;
	parent.$('audio_controls').style.visibility='hidden';
	img_loader=new Image();
	img_loader.onload=imgShowDo;
	img_loader.src=pic;
	$('img_preview').style.visibility='hidden';
	$('img_container').style.display='block';
}

function imgShowDo(){
	h=img_loader.height;
	w=img_loader.width;
	if(h/w>.75){ // margin on side
		hOut=480;
		wOut=480*w/h;
		ml=(640-wOut)/2;
		mt=0;
	}else{
		wOut=640;
		hOut=640*h/w;
		ml=0;
		mt=(480-hOut)/2;
	}
	$('img_preview').src=img_loader.src;
	with($('img_preview').style){
		height=hOut+'px';
		width=wOut+'px';
		marginLeft=ml+'px';
		marginTop=mt+'px';
		visibility='visible';
	}
}

function imageHide(){
	is_jswin=false;
	$('img_container').style.display='none';
	$('img_preview').src='/images/preview/blank.gif';
	if(parent.audio_isactive) parent.$('audio_controls').style.visibility='visible';
}

/* Video preview
********************************/

arrJw=['flv','mp4','m4v'];
arrWm=['wmv','asf','asx'];
arrQt=['mov','mpg','mpeg'];
arrDivx=['avi','divx'];

function videoHandle(thisURL){
	ext=extGet(thisURL);
	if(thisURL.search(/^http:\/\/(?:www\.)?youtube\.com(\/v\/([^\.]+)\.swf)?/)==0)
	{
		abar(thisURL,true);
		vidShow(thisURL);
		return false;
	}
	if(arrJw.inArray(ext) || item_data[item_current].playpath>''){
		vidShow(thisURL);
	}else if(arrWm.inArray(ext)){
		wmShow(thisURL);
	}else if(arrQt.inArray(ext)){
		qtShow(thisURL);
	}else if(ext=='swf'){
		swfShow(thisURL);
	}else if(thisURL.search(/^mms:/i)==0){
		wmShow(thisURL);
	}else if(arrDivx.inArray(ext)){
		divxShow(thisURL);
	}else{
		var ajaxIndex=ajaxObjects.length;
		ajaxObjects[ajaxIndex]=new sack();
		with(ajaxObjects[ajaxIndex]){
			method="GET";
			requestFile='/cgi-bin/ctype.cgi';
			setVar('URL',thisURL);
			onCompletion=function(){vidHeader(ajaxIndex,thisURL);};
			runAJAX();
		}
		abar(thisURL,true);
	}
}

function vidHeader(ind,URL){
	str=ajaxObjects[ind].response;
	var lines=str.split("\n");
	//alert(lines[0]);
	switch(lines[0]){
		case '0':
			dialog_alert('static','Error',"<p align='center'>XBMC will try to open "+URL+"</p><p align='center'><small>I couldn't get the header</small></p>",{top:-60,left:28});
			break;
		case 'video/x-ms-asf':
			abar(URL,true);
			wmShow(URL);
			break;
		case 'video/x-msvideo':
			abar(URL,true);
			divxShow(URL);
			break;
		case 'application/wmv':
			abar(URL,true);
			wmShow(URL);
			break;
		case 'video/x-flv':
			if(lines[1].match(/^attachment/)){
				dialog_alert('static','Message',"<p align='center'>This URL opens an FLV which XBMC will probably play.</p><p align='center'>Unfortunately, it's content-disposition is &ldquo;attachment&rdquo;,<br />so it can&rsquo;t be viewed in the sim.</p>",{top:-60,left:28});
				return false;
			}
			abar(URL,true);
			if(!URL.match(/\.flv$/i)){
				URL+='#.flv';
			}
			vidShow(URL);
			break;
		case 'flv-application/octet-stream':
			abar(URL,true);
			vidShow(URL+'#.flv');
			break;
		case 'application/octet-stream':
			if(URL.match(/\.flv\?/)){
				abar(URL,true);
				vidShow(URL+'#.flv');
				break;
			}else if(URL.match(/\.wmv\?/)){
				abar(URL,true);
				wmShow(URL);
				break;
			}
		case 'video/mp4':
			abar(URL,true);
			if(!URL.match(/\.mp4$/i)){
				startchar=(URL.match(/\?/)?'&':'?');
				URL+=startchar+'nxdummy=video.mp4';
			}
			vidShow(URL);
			break;
		case 'text/plain': // usually flv
			if(URL.match(/\.wmv\?/)){
				abar(URL,true);
				wmShow(URL);
			}else{
				abar(URL,true);
				vidShow(URL+'#.flv');
			}
			break;
		default:
			dialog_alert('static','Message',"<p align='center'>XBMC will open "+URL+"</p><p align='center'><small>(I'm not sure what to do with video of type "+str+")</small></p>",{top:-60,left:28});
	}
}

function vidShow(vid){
	parent.audioKill();
	var fo = new SWFObject("/swf/player.swf", "sp", "640", "480", "8", "#000000");
	if(vid.search(/^http:\/\/(?:www\.)?youtube\.com\//)==0)
	{
		fo.addVariable('type','youtube');
		if(vid.search(/^http:\/\/(?:www\.)?youtube\.com(\/v\/([^\.]+)\.swf)?/)==0){
			vid='http://youtube.com/watch?v='+RegExp.$2;
		}
	}
	if(item_data[item_current].playpath>''){
		fo.addVariable('file',item_data[item_current].playpath);
		fo.addVariable('streamer',vid);
		fo.addVariable('type','video');
		procLog("Sim: playing RTMP video with JW Media Player\n URL: "+vid+"\n playpath: "+item_data[item_current].playpath,'log_sim');
	}else{
		fo.addVariable('file',vid);
		procLog("Sim: playing standard video URL with JW Media Player\n "+vid,'log_sim');
	}
	fo.addVariable("backcolor", "0x404040");
	fo.addVariable("skin","/swf/nacht.swf");
	fo.addVariable("fullscreen", "true");
	fo.addVariable("controlbar", "over");
	fo.addVariable("autostart", true);
	fo.addParam("allowfullscreen", "true");
	fo.write("vid_player");
	$('vid_player').style.visibility='visible';
	is_jswin=true;
}

function qtShow(vid){
	parent.audioKill();
	procLog("Sim: playing standard video URL with Quicktime\n "+vid,'log_sim');
	htm='<OBJECT classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="640" height="480" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'+
	'<param name="src" value="'+vid+'">'+
	'<param name="autoplay" value="true">'+
	'<param name="scale" value="aspect">'+
	'<param name="controller" value="false">'+
	'<param name="loop" value="false">'+
	'<EMBED src="'+vid+'" width="640" height="480" autoplay="true"'+
	'controller="false" loop="false" scale="aspect" bgcolor="#000000" pluginspage="http://www.apple.com/quicktime/download/">'+
	'</EMBED></OBJECT>';
	$('vid_player').innerHTML=htm;
	$('vid_player').style.visibility='visible';
	is_jswin=true;
}

function wmShow(vid){
	parent.audioKill();
	procLog("Sim: playing standard video URL with Windows Media Player\n "+vid,'log_sim');
	htm='<object id="MSIE" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" type="video/x-ms-wmv" width="640" height="480">'+
	'<param name="URL" value="'+vid+'">'+
	'<param name="AutoStart" value="true">'+
	'<param name="ShowTracker" value="false">'+
	'<param name="ShowControls" value="false">'+
	'<param name="ShowGotoBar" value="false">'+
	'<param name="ShowDisplay" value="false">'+
	'<param name="ShowStatusBar" value="false">'+
	'<param name="AutoSize" value="false">'+
	'<param name="StretchToFit" value="true">'+
	'<param name="uiMode" value="none">'+
	'</object>'+
	'<object type="application/x-ms-wmp" id="FireFox" width="640" height="480">'+
	'<param name="URL" value="'+vid+'">'+
	'<param name="AutoStart" value="true">'+
	'<param name="ShowTracker" value="false">'+
	'<param name="ShowControls" value="false">'+
	'<param name="ShowGotoBar" value="false">'+
	'<param name="ShowDisplay" value="false">'+
	'<param name="ShowStatusBar" value="false">'+
	'<param name="AutoSize" value="false">'+
	'<param name="StretchToFit" value="true">'+
	'<param name="uiMode" value="none">'+
	'<a></a> <!--MSIE workaround-->'+
	'</object>';
	$('vid_player').innerHTML=htm;
	$('vid_player').style.visibility='visible';
	is_jswin=true;
}

var divxPlugin;
var divxReady=false;
var strStatus=['init done','open done','video end','shut done','embedded start','embedded end','windowed start','windowed end','fullscreen start','fullscreen end','playing','paused','ff','rw','stopped','buffering start','buffering stop','download start','download failed','download done'];

function divxShow(vid){
	procLog("Sim: playing standard video URL with DivX\n "+vid,'log_sim');
	str='<div id="divxContainer">'+
	'<div id="divxPlayer">'+
	'<object id="ie_divxPlugin" classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" width="1800" height="1470" mode="zero" statusCallback="divxStatusChanged" autoPlay="true" bannerEnabled="false" bufferingMode="null" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">'+
	'<param name="src" value="'+vid+'" />'+
	'<embed id="np_divxPlugin" '+
		'type="video/divx" '+
		'src="'+vid+'" '+
		'width="640" '+
		'height="360" '+
		'mode="zero" '+
		'bufferingMode="null" '+
		'autoPlay="true" '+
		'bannerEnabled="false" '+
		'pluginspage="http://go.divx.com/plugin/download/" '+
		'statusCallback="divxStatusChanged" '+
		'>'+
	'</embed>'+
'</object>'+
'</div>'+
'</div>';
	$('vid_player').innerHTML=str;
	$('vid_player').style.visibility='visible';
	if(navigator.userAgent.indexOf('MSIE') != -1 || navigator.userAgent.indexOf('Safari') != -1){
		divxPlugin = document.getElementById('ie_divxPlugin');
	}else{
		divxPlugin = document.getElementById('np_divxPlugin');
	}
}

function divxStatusChanged(event){
	//$('dbg').innerHTML=strStatus[event];
	if(event==1){ // OPEN_DONE
		divxReady=true;
		divxResize();
	}
}

function divxResize(){
	h=divxPlugin.GetVideoHeight();
	w=divxPlugin.GetVideoWidth();
	aspect=w/h;
	tgtWidth=640;
	tgtHeight=Math.round(tgtWidth/aspect);
	divxPlugin.Resize(tgtWidth,tgtHeight);
	$('divxPlayer').style.marginTop=Math.round((480-tgtHeight)/2)+'px';
	$('divxPlayer').style.visibility='visible';
}

function swfShow(URL){
	parent.audioKill();
	var fo = new SWFObject(URL, "sp", "640", "480", "8", "#000000");
	fo.addParam("allowfullscreen", "true");
	fo.write("vid_player");
	$('vid_player').style.visibility='visible';
	is_jswin=true;
}

function vidHide(){
	$('vid_player').innerHTML='';
	$('vid_player').style.visibility='hidden';
	is_jswin=false;
}

/* Audio
********************************/

arrAudio=['mp3','m4a'];

function audioHandle(thisURL){
	ext=extGet(thisURL);
	if(arrAudio.inArray(ext)){
		abar(thisURL,true);
		parent.audioPlay(thisURL);
	}else{
		myalert("<p align='center'>XBMC will open "+thisURL+"</p><p align='center'><small>(dunno how to play this file type)</small></p>");
	}
}

/* Text preview
********************************/

function textShow(which){
	$('text_preview').style.visibility='visible';
	objTxt=item_data[which];
	if(objTxt.background>''){
		$('bkg_txt').src=objTxt.background;
	}else{
		$('bkg_txt').src='/images/preview/blank.gif';
	}
	is_jswin=true;
	var ajaxIndex=ajaxObjects.length;
	ajaxObjects[ajaxIndex]=new sack();
	with(ajaxObjects[ajaxIndex]){
		method="GET";
		requestFile='/preview/?type=text&URL='+escape(objTxt.URL);
		onCompletion=function(){textShowResponse(ajaxIndex);};
		runAJAX();
	}
}

function textShowResponse(index){
	str=ajaxObjects[index].response;
	$('text_preview_inner').innerHTML=str;
}

function textHide(){
	$('text_preview').style.visibility='hidden';
	$('text_preview_inner').innerHTML='';
	$('bkg_txt').src='/images/preview/blank.gif';
	is_jswin=false;
}

function descView(){
	txt=item_data[item_current].description;
	if(txt=='')	return false;
	$('bkg_txt').src='/images/preview/blank.gif';
	$('text_preview').style.visibility='visible';
	$('text_preview_inner').innerHTML=txt;
	is_jswin=true;
}

/* Search
********************************/

var prompt_active=false;

function search_youtube_prompt(){
	htm='<div id="youtube_search">'+
	'<p><label>Search for</label><input type="text" class="intxt" id="ytquery" /></p>'+
	'<p><label>Sort by</label><select size="1" class="intxt" id="ytsort" />'+
	'<option value="">Relevance</option>'+
	'<option value="video_date_uploaded">Date Added</option>'+
	'<option value="video_view_count">View Count</option>'+
	'<option value="video_avg_rating">Rating</option>'+
	'</select></p></div>'+
	'<p id="dialog_ok">'+
	'<button class="v_button" onclick="search_youtube_do()">OK</button>'+
	'&nbsp;&nbsp;<button class="v_button" onclick="prompt_active=false;dialog_hide()">Cancel</button>'+
	'</p>';
	prompt_active=true;
	dialog_show('static','Search Youtube',htm,{top:-60,left:28});
	$('ytquery').focus();
}

function search_youtube_do(){
	q=$('ytquery').value;
	srchurl='http://www.youtube.com/results?search_query='+q;
	srt=$('ytsort').value;
	if(srt>'') srchurl+='&search_sort='+srt;
	prompt_active=false;
	dialog_hide();
	resloc='type=html_youtube&URL='+escape(srchurl)+'&name=search+results:+'+escape(q);
	baseargs='URL='+escape(thisURL)+'&background='+zitem.background+'&logo='+zitem.logo+'&name='+zitem.name+'&title='+titl;
	parent.historylen++;
	parent.hist[parent.historylen]={
		URL:srchurl,
		type:'html_youtube',
		ind:item_current,
		scroll:scroll_ind
	};
	abar(srchurl);
	parent.addrTypeSet('html_youtube');
	window.location='/preview/?'+resloc;
}

function search_prompt(){
	htm='<div id="search">'+
	'<p><label>Search for</label><input type="text" class="intxt" id="query" /></p></div>'+
	'<p id="dialog_ok">'+
	'<button class="v_button" onclick="search_do()">OK</button>'+
	'&nbsp;&nbsp;<button class="v_button" onclick="prompt_active=false;dialog_hide()">Cancel</button>'+
	'</p>';
	prompt_active=true;
	dialog_show('static','Search',htm,{top:-60,left:28});
	$('query').focus();
}

function search_do(){
	q=$('query').value;
	srchurl=item_data[item_current].URL+escape(q);
	prompt_active=false;
	dialog_hide();
	resloc='type=playlist&URL='+escape(srchurl)+'&name=search+results:+'+escape(q);
	baseargs='URL='+escape(thisURL)+'&background='+zitem.background+'&logo='+zitem.logo+'&name='+zitem.name+'&title='+titl;
	parent.historylen++;
	parent.hist[parent.historylen]={
		URL:srchurl,
		type:'playlist',
		ind:item_current,
		scroll:scroll_ind
	};
	abar(srchurl);
	parent.addrTypeSet('playlist');
	window.location='/preview/?'+resloc;
}

/* Description
********************************/

var desc_pages=1;
var desc_page_height=60;
var desc_scrollcurr=1;
var desc_offset=(document.all?0:1);
desc_offset=0;

function descScrollInit(){
	desc_pages=1;
	desc_scrollcurr=1;
	h=$('description_inner').offsetHeight;
	while(h-1>desc_page_height*desc_pages){
		desc_pages++;
		$('description_pager').innerHTML+='<div class="v_spacer"></div>'+
			'<div class="buttons" id="desc_buttons_'+desc_pages+'"><ul>'+
			'<li><a href="javascript:descScroll(1)"><span>p</span></a></li>'+
			'<li><a href="javascript:descScroll(-1)"><span>n</span></a></li>'+
			'</ul><span class="count" id="desc_count_'+desc_pages+'">1/1</span></div>';
		h=$('description_inner').offsetHeight;
	}
	for(i=1;i<=desc_pages;i++){
		$('desc_count_'+i).innerHTML=i+'/'+desc_pages;
	}
	if(desc_pages>1){
		$('desc_buttons_1').style.visibility='visible';
	}
}

function descScroll(inc){
	dtgt=desc_scrollcurr+inc;
	if(dtgt<1)dtgt=desc_pages;
	if(dtgt>desc_pages)dtgt=1;
	$('desc_buttons_'+desc_scrollcurr).style.visibility='hidden';
	desc_scrollcurr=dtgt;
	$('desc_buttons_'+desc_scrollcurr).style.visibility='visible';
	$('description_inner').style.marginTop='-'+(desc_offset+desc_page_height*(dtgt-1))+'px';
}

/* Utility
********************************/

function otherFormats(l_id){
	var htm='<p align="center">Click or copy the links below for other formats</p>'+
	'<p align="center">'+
		'<a href="http://navix.turner3d.net/asx/list/'+l_id+'/list.asx" target="_blank">ASX</a> &bull; '+
		'<a href="http://navix.turner3d.net/wii/list/'+l_id+'/list.smil" target="_blank">SMIL</a>'+
	'</p>';
	dialog_alert('static','Other formats',htm,{top:-60,left:28});
}

function myalert(what){
	dialog_alert('static','Message',what,{top:-60,left:28});
}

function pageInit(){
	parent.edit_button_set();
	descScrollInit();
	list_length=(has_description?8:10);
	oUl=$('list_entries');
	item_disp=oUl.getElementsByTagName('LI');
	histCheck();
	for(k=0;k<item_disp.length;k++){
		l=k.toString();
		addEvent(item_disp[k],'mousemove',itemMouseover,false);
		addEvent(item_disp[k],'click',itemUse,false);
	}
	if(item_data[item_current]){
		stat(item_data[item_current].URL);
		itemHilite(item_current);
	}
	if(errmsg>''){
		htm='<p style="text-align:center;margin:2.5em 0 3.5em;">'+errmsg+'</p>';
		dialog_show('static','Error',htm,{top:-60,left:28});
	}
	cMenu.setup({'preventDefault':true,'preventForms':false});
	cMenu.attach('container', 'CM1');
	parent.pFocus();
}

function sourceShow(){
	url=escape($P('address_bar').value);
	window.open('/sourceview/'+url,'','height=600,width=800,scrollbars=1,resize=1');
}

function plxShow(){
	url=window.location.href;
	url=url.replace(/preview\/\?/,'plxview/');
	window.open(url,'','height=600,width=800,scrollbars=1,resize=1');
}

function sourceOpen(){
	window.open($P('address_bar').value);
}

function extGet(filename){
	srch=filename.search(/\.(\w+)$/);
	ext=srch>0?RegExp.$1:'';
	return ext.toLowerCase();
}


