//used by bn.dxlive.com

random_performers = new Array();
var perfRedirect;
var wait4preview;
var cycle;
var last_perf = null;

function tryPreview () {
	if (performers.list.length) {
		clearTimeout(wait4preview);
		cyclePreview(w, h);
	} else {
		wait4preview = setTimeout(tryPreview, 500);
	}
}

function nextVideo(w,h){
	clearTimeout(cycle);
	cyclePreview(w,h);
}

function no_perf(){
    clearTimeout(wait4preview);
    var perfName = document.getElementById('namehere');
    while (perfName.hasChildNodes()) {
        perfName.removeChild(perfName.firstChild)
    }
    perfName.appendChild(document.createTextNode(''));
    return;
}

function openNewWindow(theURL,winName,features) {
        var w = window.open(theURL,winName,features);
        //w.focus();
}

function openProfile(uname) {
  openNewWindow('http://bn.dxlive.com/profile/'+uname, 'profile'+uname, 'resizable=yes,toolbar=no,scrollbars=yes,personalbar=no,menubar=no,width=800,height=950');
}


function cyclePreviewAff(affid){
  cyclePreview(156,117);
}

function cyclePreview (w, h) {
	//if(!user_arr) {
    if (performers.free.length == 0) {
          no_perf();
	  return;
	}
	var count=performers.free.length;
/*	var arr = new Array();
	for(perf in user_arr) {
		if(user_arr[perf].session_type=='110' && user_arr[perf].test_flag!=1){
			count++;
			if(perf!=last_perf){
			   arr.push(perf);
			} 
		}
	}*/

	var perf = '';
	//if(arr.length > 0) {
    if (performers.free.length > 0 ){
		//var index = Math.floor(Math.random() * arr.length);
		var index = Math.floor(Math.random() * performers.free.length);
		//perf = arr[index];
        perf = performers.free[index];
		/*if(arr.length==1){
			perf = arr[0];
		}*/
        if (performers.free.length == 1){
            perf = performers.free[0];
        }
	}

    if (count == 0) {
        no_perf();
        return;
    }

    /*
	if(count == 1 && arr.length == 0) {
		perf = last_perf;
	}
	if(count == 0 && arr.length == 0) {
         	no_perf();
		return;
	}*/
 


	clearTimeout(wait4preview);
	var perfName = document.getElementById('namehere');
	var copyRight = "(C) 2002-2008 DXLIVE.COM. ALL RIGHTS RESERVED.";
	perfRedirect = perf
	while (perfName.hasChildNodes()) {
        perfName.removeChild(perfName.firstChild)
	}
	perfName.innerHTML = '<a href="javascript:;" onClick=openProfile("'+perf+'") style="text-decoration: none;">'+perf+'</a>';
    var previewTD = document.getElementById('vidPlayerHere');
    last_perf = perf;
    var perf_data = performers.online[perf];
    var uid =  perf_data.user_id;  //user_arr[perf].user_id;
	var src = 'http://bn.dxlive.com/flash/chat/freePreview20.swf';
	var str = '';
    if (w == undefined) {
        w = '156';
    }
    if (h == undefined) {
        h = '117';
    }

	str += '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '" id="video_chat" align="">';
  	str += '<param name="allowScriptAccess" value="always" />';
  	str += '<PARAM name="movie" value="'+src+'">';
  	str += '<param name=FlashVars value="channel='+perf+'&performerID='+uid+'&userType=&sessionType=110&langID=jp&userSiteID=1003332&skinName=skin0&ban=0&hd='+perf_data.hd+'" />';
  	str += '<PARAM name="salign" value="LT">';
  	str += '<PARAM name="quality" value="high">';
  	str += '<PARAM name="bgcolor" value="#006666">';
        if(perf_data.hd == 1){
          str += '<PARAM name="hd" value="1">';
        }
    str += '<embed src="'+src+'" salign="LT" FlashVars="channel='+perf+'&performerID='+uid+'&userType=&sessionType=110&langID=jp&webID=A&userSiteID=1000048&skinName=skin0&ban=0&photo=http://imageup.dxlive.com/WebArchive/'+perf+'/flash/LinkedImage.jpg&hd='+perf_data.hd+'" quality="high" bgcolor="#006666" width="' + w + '"  height="' + h + '" name="video_chat" align="" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
  	str += '</OBJECT>';

    fvars = 'channel=' +uid+ '&performerID=' +uid+ '&userType=&sessionType=110&langID=jp&webID=&userSideID=1000048&skinName=skin0'
    str2 = '<script>freePreview(' +w+ ',' +h+ ', "'+fvars+'")</script>';

	global_pf_name = perf;
    previewTD.innerHTML = str;
    cycle = setTimeout('cyclePreview(' + w + ',' + h + ')',30000);

}

function redirect(thiscaller) {
if (thiscaller=='randompreview') {
    var w = window.open('/randpreviewclick.shtml?'+perfRedirect, 'preview', 'resizable=yes,toolbar=yes,scrollbars=yes,personalbar=yes,menubar=yes,location=yes,status=yes,width=860,height=600');
} else {
    var w = window.open('/preview/'+perfRedirect, 'preview', 'resizable=yes,toolbar=yes,scrollbars=yes,personalbar=yes,menubar=yes,location=yes,status=yes,width=860,height=600');
}
  w.focus();
}


