/* Tree */
var TREE_TOGGLEBUTTON = 0;
var TREE_POPULARITYBAR = 1;
var TREE_SEGMENTTEXT = 2;
var TREE_SUBSEGCONT = 3;

var currentSeg = 0;
var prevSeg = -1;
var currentSubSeg = 0;
var prevSubSeg = -1;
var currentSubSegID = 1;
var prevSubSegID = -1;
var currentImg = 0;			// stores index of currently displayed img in imageData array
var prevImg = -1;
var embeddedPage = false;

// variables for the Views and Videos tables
var timeOnPage = 0;
var lectViewTimeToDump = 0;
var lectViewTimeTotal = 0;

// variables for the SegmentViews table
var segmentChanged = false;
var segmentViewsID = 0;
var segViewTimeToDump = 0;
var segViewTimeTotal = 0;
var segmentID = "";		 // a string now b/c the segmentID is complicated

// variables for the ViewsTimed table
var viewsTimedID = '';
var viewsTimedStartTime = 0;	// the end time of the current ViewsTimed row
var viewsTimedLength = 0;	   // the length of the current ViewsTimed row
var viewsTimedLengthFinish = 0;   // the length of the ViewsTimed row that is written to the DB when a new row is added to the ViewsTimed table
var viewsTimedSeekEventCode = 0;
var seekedWithNote = false;	 // did the user seek by clicking on a note?
var noTree = false;  //Checks if there is no tree because using only the video

var commentSortBy = 'd';
var commentPrivate = 0;

// Determine if browser is IE
var IS_IE = (window.navigator.userAgent.indexOf("MSIE") >= 0 ? true : false);
// Determine if browser is Camino or FF2.0 (or earlier). NOTE: Camino browser name contains "(like Firefox/2.0...)", so check if is Camino first
var IS_CAMINO = (window.navigator.userAgent.indexOf("Camino") >= 0 ? true : false);
var IS_FF2 = false;
if (!IS_CAMINO && window.navigator.userAgent.indexOf("Firefox") >= 0) {
	IS_FF2 = (parseFloat(window.navigator.userAgent.split("Firefox/").pop().split(".")[0]) <= 2 ? true : false);
}
// Determine if browser is Apple Safari
var IS_SAFARI = (window.navigator.userAgent.indexOf("Safari") >= 0 ? true : false);

var vnplayer;
var curTime = 0;	  //used for the rounding function in whatTime
var videoLength = 0;
var quality = "high";
var swapQuality = 0;		//set to 1 when switching the quality as a flag - to seek
var swapTime = 0;
var prevTime = 0;

//This is a built in function with the video player it gets called whenever a state changes (incl time)
function whatTime(obj) {
	if (Math.abs(curTime - obj.position) < .5) return;

	curTime = obj.position;
		
	//Update the correct SegmentIDs after a case 2 edit.
	if ($get("inpUpdateFlag").value == "1") {
		$get("inpWaitSaving").value = 0;
		if($get('rateframe')) $get('rateframe').src = "videorate.aspx?SegmentID=" + segmentData[currentSeg].segID;
		$get("inpUpdateFlag").value = 0;
	}
	// Update time variables
	if (Math.abs(curTime - prevTime) > 2) { // Jump
		lectViewTimeTotal += 2;
		segViewTimeTotal += 2;
		viewsTimedLength += 2;
		segViewTimeToDump += 2;
		lectViewTimeToDump += 2;
	} else {   // normal playback
		lectViewTimeTotal += Math.max(curTime - prevTime, 0);					
		segViewTimeTotal += Math.max(curTime - prevTime, 0);
		viewsTimedLength += Math.max(curTime - prevTime, 0);
		segViewTimeToDump += Math.max(curTime - prevTime, 0);
		lectViewTimeToDump += Math.max(curTime - prevTime, 0);
	}
	// determine which Image should be displayed
	var imageCon = $get('pnlSlidesContainer');
	if(imageCon && typeof imageData != "undefined" && $get('pnlSlides').style.display == "block"){
		if(prevImg == -1) imageCon.images[0].play();
		prevImg = currentImg;
		var i = imageData.length - 1;
		while (i > 0 && ( typeof imageData[i].linksec != "undefined" ? imageData[i].linksec > (curTime + .5) : true ) ) { i--; }
		currentImg = i;
		if(prevImg != currentImg){
			imageCon.images[prevImg].noplay();
			imageCon.images[currentImg].play();
			imageCon.images[currentImg].scrollIntoView();
		}
	}
	
	// determine which Segment is currently playing
	var i = 0;
	il = segmentData.length;
	while (i < il && segmentData[i].startTime < (curTime + .5)) { i++; }
	currentSeg = i - 1;		   // this is the array index of the current Segment in the segmentData array
	//NOTE: This will be -1 if there are NO segments.

	// determine which SubSegment is currently playing, count backwards...
	//THERE seem to be rounding errors going on here that cause it to highlight improperly??
	//CRASHES HERE IF NO SUBSEGS!!  So check if there are some.
	if (subSegmentData.length > 0 && currentSeg != -1) {
		i = subSegmentData.length - 1;
		while (i > 0 && subSegmentData[i].startTime > Math.floor(curTime)) i--;
		if (subSegmentData[i].startTime < segmentData[currentSeg].startTime) i++;   //If the subseg is behind the segment (i.e. the first subseg of the seg has a gap, then just highlight the first subseg of this seg)
		currentSubSeg = i;		   // this is the array index of the current subsegment in the subSegmentData array
		if (i == subSegmentData.length) { currentSubSeg = i - 1; }   //error for if the last seg has no subsegs
		// The ID of the current subsegment that is playing (starts from 1 with every new Segment)
		//currentSubSegID = parseFloat(subSegmentData[currentSubSeg].subSegID.split("-").pop());
		//PROBLEM!!  Not using this structure anymore, can't do string parsing!
		currentSubSegID = subSegmentData[currentSubSeg].subSegNum;
	} else {
		currentSubSegID = -1;
		currentSubSeg = -1;
	}
	// change style to highlighting only if segment playing has changed
	var treeCon = $get("pnlVideoNotesTree");
	if (treeCon != null) {
		if (currentSeg != prevSeg && currentSeg != -1) {
			if ($get('segmentTextDisplay') != null) {
				$get('segmentTextDisplay').innerHTML = "<span style='font-size:13px'><b>How difficult is this topic to understand?</b><br />Topic: " + segmentData[currentSeg].topic + "</span>";
				if($get('rateframe')) $get('rateframe').src = "videorate.aspx?SegmentID=" + segmentData[currentSeg].segID;
			}
			// change row highlighting to playing
			try {
				treeCon.segments[currentSeg].play();
			} catch(ex){}
			if (prevSeg > -1) {	//If it's the first segment EVER being played on the page...
				// change row highlighting back to not playing
				treeCon.segments[prevSeg].noplay();
				// reset variable tracking time that each segment is played when segment changes
				segViewTimeTotal = 0;
				segViewTimeToDump = 0;
				segmentChanged = true;
				if ($get('inpPreviewMode').value == "0") {
					setTimeout('if(currentSeg == ' + currentSeg + '){if($get("inpWaitSaving").value != 1){addSegmentView();}}', 2000);	 //Wait 2 seconds before posting back a segment change, not a legit view 
				}
			} else {   //If it's the FIRST time a segment is played - i.e. the page was just loaded
				var firstSegTime;
				if (segmentData.length > 1) firstSegTime = parseFloat(segmentData[1].startTime / 3);
				else firstSegTime = 30;

				//If it's the first segment, then wait 10" and see if it's still playing, if YES, then postback
				segmentChanged = true;
				if ($get('inpPreviewMode').value == "0") setTimeout("if(currentSeg == 0){if($get('inpWaitSaving').value != 1){addSegmentView();}}", firstSegTime * 1000);
			}
		}
		if (currentSubSeg != -1) {	 //If there are subsegments, will be -1 if none.
			// change highlighting of subsegment
			if (currentSubSeg != prevSubSeg) {
				treeCon.segments[currentSeg].subsegments[currentSubSegID - 1].play();
				
				//To make sure the previous subsegment is defined.
				if (prevSubSeg > -1) {
					prevSubSegID = parseFloat(subSegmentData[prevSubSeg].subSegNum);
					prevSeg = parseFloat(subSegmentData[prevSubSeg].segNum) - 1;

					treeCon.segments[prevSeg].subsegments[prevSubSegID - 1].noplay();
				}
			}
		}
		if (currentSeg != -1) {
			prevSeg = currentSeg;
			prevSubSeg = currentSubSeg;

			// get the current segment ID. this is important for the DB.
			segmentID = segmentData[currentSeg].segID;
		}
	}
	prevTime = curTime;

	if (swapQuality == 1) {
		swapQuality = 0;
		setTimeout('vnplayer.sendEvent("SEEK",swapTime);', 2000);
	}
}
//this is a hack because of an error in the player - when I jump, needs play to be true or it'll play in pause mode?
function playerSeeked(obj) {
	vnplayer.sendEvent("PLAY", "true");
	//viewsTimedStartTime = curTime;
	viewsTimedStartTime = obj.position;
	viewsTimedLengthFinish = viewsTimedLength;
	viewsTimedSeekEventCode = 2;
	addTimedView();
	whatTime(obj);
	try {
		updateTimeStamp();
	} catch(ex){}
}
function playerPlayed(obj) {
	if (obj.state) { // if obj.state == true, player is going to start playing
		viewsTimedSeekEventCode = 4;
		viewsTimedStartTime = obj.position;
		viewsTimedLengthFinish = viewsTimedLength;
		addTimedView();	 // update ViewsTimed table because "play" was clicked
	}
}
function playerReady(player) {
	if ($get(player.id).addModelListener) {
		vnplayer = $get(player.id);
		vnplayer.addModelListener('TIME','whatTime');
		vnplayer.addControllerListener('SEEK','playerSeeked');
		vnplayer.addControllerListener('PLAY','playerPlayed');
	} else setTimeout('playerReady("player")', 100);
}

function toggleVideoURL(){
	var urlbutton = $get('lblVideoURLButton');
	var urlpanel = $get('lblVideoURLWrapper');
	if (urlpanel.style.display == 'none') {
		urlbutton.innerHTML = "[hide]";
		urlpanel.style.display = 'inline';
	} else {
		urlbutton.innerHTML = "show video URL";
		urlpanel.style.display = 'none';
	}
}
function copyToClipboard() {
	var cb = $get('pnlClipboard');
	cb.innerHTML='<embed src="/flash/clipboard.swf" FlashVars="clipboard=' + encodeURIComponent($get('lblVideoURL').innerHTML) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}

/* Functions to Format Time */
// convert a time in seconds to a Minutes:Seconds format
function getProperTime(sectime) {
	sectime = Math.round(sectime);
	secs = (sectime % 60);
	mins = Math.floor(sectime / 60);
	secstr = secs.toString();
	minstr = mins.toString();
	if (secs < 10) secstr = "0" + secstr;
	if (mins < 10) minstr = "0" + minstr;
	timestr = minstr + ":" + secstr;
	return timestr;
}
// coverts a time in MM:SS format to seconds
function getSecondTime(time) {
	var min = parseFloat(time.split(":")[0]);
	var sec = parseFloat(time.split(":")[1]);
	return (min * 60 + sec);
}
// set the number of columns of a textrea based on the target width
function setColNumber(txtarea, tgtwidth) {
	while (txtarea.offsetWidth < tgtwidth) txtarea.cols += 1;
	txtarea.cols -= 1;
}

/* Tree Text Search */
var numSearches = 0;
var searchString = "";
	
var prevSearch = "";
var prevSearchLoc = -1;

function searchButton() {
	$get("SearchError").innerHTML = ""
	$get("SearchError").style.display = "inline";
	var s = $get("txtSearch").value;
	var finds = s.escapeHTML();

	var hOTag = '<span class=searchNote>';	 //Note: need a space here, this can mess up the replacing area...
	if (!IS_IE) { hOTag = '<span class="searchNote">'; }		//stupid IE has no quotes, Firefox has quotes...

	var hCTag = "</span>";

	var treeCon = $get("pnlVideoNotesTree");
	var numSegs = treeCon.segments.length;
	var out = "";

	//Need to clear existing search result.  Clear previous location (If there actually was a search)
	if (prevSearchLoc != -1) {
		//First clear any existing search results - just rebuild from data, replace to remove is messed up.
		out = treeCon.segments[prevSearchLoc].text;
		out = out.replace(new RegExp(hOTag, "gi"), "");
		out = out.replace(new RegExp(hCTag, "gi"), "");
		treeCon.segments[prevSearchLoc].settext(out);

		var numSubs = treeCon.segments[prevSearchLoc].subsegments.length;

		for (var i = 0; i < numSubs; i++) {
			out = treeCon.segments[prevSearchLoc].subsegments[i].text;
			//Get rid of the existing span tags - this won't be in the actual notes, they're htmlEncoded.
			out = out.replace(new RegExp(hOTag, "gi"), "");
			out = out.replace(new RegExp(hCTag, "gi"), "");
			treeCon.segments[prevSearchLoc].subsegments[i].settext(out);
		}
	}

	//IF it's a symbol like "$" or "." then it needs a slash to interpret literally
	if (s.replace(/^\s+|\s+$/g, '') == "" || s == "$" || s == ".") {
		$get("SearchError").innerHTML = "Enter something to search!";
		return false;
	}

	//If they're doing a new search, reset it.
	if (s != prevSearch) {
		prevSearchLoc = -1;

		//Update the hidden inputs so we can keep track in the views whether they searched or not
		numSearches += 1;
		searchString = s;
	}

	var ii = new Array(numSegs);
	var temp = prevSearchLoc + 1;
	for (var i = 0; i < numSegs; i++) {
		if (temp == numSegs) temp = 0;
		ii[i] = temp;
		temp++;
	}

	//Now clean, go through the ii[] segments and look for a result!
	var doneSearch = false;
	for (i = 0; i < numSegs; i++) {
		numSubs = treeCon.segments[ii[i]].subsegments.length;
		
		//First look if the result is in the segment
		out = treeCon.segments[ii[i]].text;
		if (out.search(new RegExp(finds, "i")) != -1) {
			doneSearch = true
			out = out.replace(new RegExp(finds, "gi"), hOTag + finds + hCTag);
			treeCon.segments[ii[i]].settext(out);
		}

		//This loops through the subsegments and tries to find it and highlight it
		for (var a = 0; a < numSubs; a++) {
			out = treeCon.segments[ii[i]].subsegments[a].text;
			//Highlight the subsegment
			if (out.search(new RegExp(finds, "i")) != -1) {
				doneSearch = true;
				out = out.replace(new RegExp(finds, "gi"), hOTag + finds + hCTag);
				treeCon.segments[ii[i]].subsegments[a].settext(out);
			}
		}

		//exit the loop if we found it.
		if (doneSearch) {
			prevSearchLoc = ii[i];
			treeCon.collapseAllSegments();
			treeCon.segments[ii[i]].expand();
			i = 1000000;
		}
	}

	//If didn't find it anywhere, then oh well...
	if (!doneSearch) $get("SearchError").innerHTML = "not found";
	else {
		$get("SearchError").style.display = "none";
		showNextResultButton();
	}

	//Set the previous search, so we can tell if it's new.
	prevSearch = s;

}

//If enter is hit then we want to submit the search button...
function searchCheckEnter(event) {
	isEnter = (event.keyCode == 13);
	if (isEnter) searchButton();
}

/* Page Layout */
function resizeBottom() {
	var newHeight;
	var iniframe;

	//If in an iframe, parent.document returns Permission denied from the browser and javascript errors.
	try {
		if (parent.$get('bottomframe') != null) {
			iniframe = false;
		} else { iniframe = true; }

	} catch (err) {
		iniframe = true;
		embeddedPage = true;
	}

	//find the height of the page in the frame
	if (!iniframe) {
		newHeight = 850;

		parent.$get('bottomframe').height = newHeight + 150;
		document.location.href = '#top';
	}
}
//select video quality
function selectVideoQuality(qual) {
	var obj;
	if (quality != qual) {
		if (qual == "high") {
			swapQuality = 1;
			swapTime = curTime;
			obj = { file: fileName };
			vnplayer.sendEvent("LOAD", obj);
			quality = "high";
			$get('btnVideoQualityHigh').style.fontWeight = "bold";
			$get('btnVideoQualityLow').style.fontWeight = "normal";
		} else if (qual == "low") {
			swapQuality = 1;
			swapTime = curTime;
			obj = { file: fileNameLQ };
			vnplayer.sendEvent("LOAD", obj);
			quality = "low";
			$get('btnVideoQualityHigh').style.fontWeight = "normal";
			$get('btnVideoQualityLow').style.fontWeight = "bold";
		}
	}
}
function selectTab(tab) {
	var tabareas = {
					btnVideoNotesTab: 'pnlVideoNotes',
					btnSlidesTab: 'pnlSlides',
					btnCommentsTab: 'pnlComments',
					btnMetadataTab: 'pnlMetadata',
					btnEditNotesTab: 'pnlEditNotes',
					btnScreenshotsTab: 'pnlScreenshots',
					btnThumbnailsTab: 'pnlThumbnails'
				   };

	var tbs = getElementsByClassName('tabbutton selected');
	var pnv = getElementsByClassName('panel nested visible');
	for (var i = 0, len = tbs.length; i < len; i++) tbs[i].className = 'tabbutton notselected';
	for (var i = 0, len = pnv.length; i < len; i++) pnv[i].className = 'panel nested hidden';

	if (tab.className == 'tabbutton notselected') tab.className = "tabbutton selected";
	$get(tabareas[tab.id]).className = "panel nested visible";

	if (tab.id == 'btnEditNotesTab' && $get('txtEditNotesInputText').cols == 1) $get('txtEditNotesInputText').cols = NUM_COLUMN_NEWNOTE;
	else if (tab.id == 'btnCommentsTab') getComments();
	else if (tab.id == 'btnSlidesTab') getSlides();
	else if (tab.id == "btnScreenshotsTab") getScreenshots();
	else if (tab.id == 'btnThumbnailsTab') getThumbnails();
}
function postTimes() {
	//setTimeout("timeOnPage = 2; logViewTimes(); addTimedView();", 2000);
	//setInterval("timeOnPage += 10; logViewTimes();", 100000000);
}

function showNextResultButton() {
	var btn = $get('btnVideoNotesSearch');
	btn.src = "images/video/search/next.png";
	btn.onmousedown = function() { btn.src = "images/video/search/nextclick.png"; }
	btn.onmouseout = function() { btn.src = "images/video/search/next.png"; }
	btn.onmouseover = function() { btn.src = "images/video/search/nextover.png"; }
	btn.onclick = function() {
		btn.src = "images/video/search/next.png";
		searchButton();
	}
}
function showSearchButton() {
	var btn = $get('btnVideoNotesSearch');
	btn.src = "images/video/search/search.png";
	btn.onmousedown = function() { btn.src = "images/video/search/searchclick.png"; }
	btn.onmouseout = function() { btn.src = "images/video/search/search.png"; }
	btn.onmouseover = function() { btn.src = "images/video/search/searchover.png"; }
	btn.onclick = function() {
		btn.src = "images/video/search/search.png";
		searchButton();
	}
}
function updateCommentVideoTime() {
	$get('lblCommentVideoTime').innerHTML = "@ " + getProperTime(curTime);
	$get('inpCommentVideoTime').value = curTime;
}
function showNewCommentPanel() {
	if ($get('txtCommentText').cols == 1) {
		setColNumber($get('txtCommentText'), 290);
	}
	$get('pnlNewComment').style.top = "300px";
	$get('txtCommentText').focus();
}
function showNewCommentPanel() {
	$get("pnlNewComment").style.display = "block";
}
function hideNewCommentPanel() {
	$get("pnlNewComment").style.display = "none";
	$get("chkCommentTimeRefer").checked = false;
	$get("chkCommentPersonal").checked = false;
	$get("lblCommentVideoTimeWrapper").style.display = "none";
	$get("txtCommentText").value = "";
}
function editNotesModeChanged(obj) {
	if (obj.id == "lblEditNotesPauseMode") {
		obj.style.fontWeight = "bold";
		$get("lblEditNotesContinuousMode").style.fontWeight = "normal";
		IS_PAUSE_MODE = true;
		showAddVideoNoteButton();
	} else {
		obj.style.fontWeight = "bold";
		$get("lblEditNotesPauseMode").style.fontWeight = "normal";
		IS_PAUSE_MODE = false;
		showAddVideoNoteTextArea();
	}
}
function showAddVideoNoteButton() {
	$get('btnAddVideoNote').style.display = 'block';
	$get('pnlEditNotesControlButtons').style.display = 'none';
	$get('txtEditNotesInputText').style.display = 'none';
}
function showAddVideoNoteTextArea() {
	$get('btnAddVideoNote').style.display = 'none';
	$get('pnlEditNotesControlButtons').style.display = 'block';
	$get('txtEditNotesInputText').style.display = 'block';
}
function getSlides() { if($get("pnlSlidesContainer").childNodes.length < parseFloat($get("inpNumSlides").value)) makeImagesPanel(); }
function getScreenshots() {
	if($get("pnlScreenshotsContainer").childNodes.length < parseFloat($get("inpNumScreens").value)) {
		vnajax.textget("video.aspx?img=s&vid=" + $get("inpVideoID").value, "", null,
			function(respobj){
				if (respobj) $get("pnlScreenshotsContainer").innerHTML = respobj;
			},
			null, false
		);
	}
}
function saveVideoMetadata() {
	if ($get('vid_md_vt').value.trim(' ') == '') {
		$get("vid_md_msgarea").innerHTML = '<span style="color: red">A title must be supplied</span>';
		return;
	}
	else if ($get('vid_md_vt').value.length > 255) {
		$get("vid_md_msgarea").innerHTML = '<span style="color: red">The title must be shorter than 256 characters.</span>';
		return;
	}
	else if ($get('vid_md_vd').value.trim(' ').length > 0 && !isValidTSQLDate($get('vid_md_vd').value)) {
		$get("vid_md_msgarea").innerHTML = '<span style="color: red">Invalid date format. Try: mm/dd/yyyy hh:mm:ss am|pm</span>';
		return;
	}
	var query = "update=video&VideoID=" + upstreamEnc($get('inpVideoID').value) + "&Title=" + upstreamEnc($get('vid_md_vt').value) + "&VideoDate=" + upstreamEnc($get('vid_md_vd').value) + "&Prof=" + upstreamEnc($get('vid_md_pr').value) + "&Description=" + upstreamEnc($get('vid_md_dn').value);
	vnajax.textset("accountpanel.aspx?aid=" + $get('vid_md_aid').value, query, null,
		function(response) {
			if (response == '1') $get('vid_md_msgarea').innerHTML = '<span>Changes successfully saved.</span>';
			else $get('vid_md_msgarea').innerHTML = '<span style="color: red">An error occurred.</span>';
		},
		null, false
	);
}
function getThumbnails() {
	if($get("pnlThumbnailsContainer").childNodes.length < parseFloat($get("inpNumThumbs").value)){
		vnajax.textget("video.aspx?img=t&vid=" + $get("inpVideoID").value, "", null,
			function(respobj) {
				if (respobj) $get("pnlThumbnailsContainer").innerHTML = respobj;
			},
			null, false
		);
	}
}
function logViewTimes() {
	var pm = ($get('inpPreviewMode').value == "0" && viewsTimedID != "") ? false : true;
	vnajax.textset("video.aspx?log=vt", "pm=" + pm + "&uid=" + upstreamEnc($get('inpUserID').value) + "&vid=" + $get('inpVideoID').value + "&view=" + upstreamEnc($get('inpViewsID').value) +
					"&top=" + Math.round(timeOnPage) + "&lvtt=" + Math.round(lectViewTimeTotal) + "&lvttd=" + Math.round(lectViewTimeToDump) + "&sid=" + upstreamEnc(segmentID) +
					"&svttd=" + Math.round(segViewTimeToDump) + "&vtid=" + upstreamEnc(viewsTimedID) + "&vtl=" + Math.round(viewsTimedLength), null,
		function() {
			segViewTimeToDump = 0;
			lectViewTimeToDump = 0;
			segmentChanged = false;
		}, null, false
	);
}
function addTimedView() {
	if (seekedWithNote) {
		viewsTimedSeekEventCode = 3;
		seekedWithNote = false;
	}
	vnajax.textset("video.aspx?log=atv", "uid=" + upstreamEnc($get('inpUserID').value) + "&view=" + upstreamEnc($get('inpViewsID').value) + "&vtec=" + viewsTimedSeekEventCode +
				   "&vtid=" + upstreamEnc(viewsTimedID) + "&vtst=" + Math.round(viewsTimedStartTime) + "&vtlf=" + Math.round(viewsTimedLengthFinish), null,
		function(result) {
			viewsTimedID = result;
			viewsTimedLength = 0;
		},
		null, false
	);
}
function addSegmentView() {
	if (!segmentChanged) return;
	vnajax.textset("video.aspx?log=asv", "uid=" + upstreamEnc($get('inpUserID').value) + "&sid=" + segmentID, null,
		function() { segmentChanged = false; },
		null, false
	);
}
function getComments(p, s, pri) {
	if (s) commentSortBy = s;
	if (pri) commentPrivate = pri;
	vnajax.textget("video.aspx?com=get&vid=" + $get("inpVideoID").value + "&p=" + ((p) ? p : 0) + "&s=" + ((s) ? s : commentSortBy) + "&pri=" + ((pri) ? pri : commentPrivate), "", null,
		function(respobj) {
			if (respobj) {
				removeElement('pnlPostedComments');
				removeElement('pnlCommentsPager');
				$get("pnlComments").innerHTML += respobj;
			}
		},
		null, false
	);
}
function postComment() {
	vnajax.textset("video.aspx?com=post", "vid=" + $get("inpVideoID").value + "&c=" + upstreamEnc($get("txtCommentText").value) + "&tr=" + (($get('chkCommentTimeRefer').checked) ? "1" : "0") +
				   "&t=" + $get('inpCommentVideoTime').value + "&n=" + upstreamEnc($get('txtCommentUserName').value) + "&pri=" + (($get('chkCommentPersonal').checked) ? "1" : "0"), null,
		function(response) {
			if (response == "1") {
				getComments();
				hideNewCommentPanel();
			}
		}, null, false
	);
}
function deleteComment(cid) {
	vnajax.textset("video.aspx?com=del", "cid=" + cid, null,
		function(response) {
			if (response == "1") getComments();
		}, null, false
	);
}
