/** 
 * Functions specifically related to processing videos and video data
 */

function hist_EditMyFilm(){
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters:{'event':'editAllFilms'},evalScripts:true});
}

function dpv_markCuePt( ptNum , s){
	if (!s){
		s = lastFilmId;
	}
//	alert('ptNum: '+ptNum);
	if (curUser.isLoggedIn() ){
		new Ajax.Updater('blankDiv',curPath+'handler.php', { parameters: {'event':'markCuePt', 'pt':ptNum, 'sid':s}, evalScripts: true });
	}
}

function dpv_rollB( filmsrc ){
	//alert( filmsrc );
	$('videocontent').innerHTML = dpAC_FL_RunContent_return(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '990',
			'height', '564',
			'src', 'http://doorpost.vo.llnwd.net/o33/webvideos/filmplayerbig',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'FilmPlayer',
			'bgcolor', '#000000',
			'name', 'FilmPlayer',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','always',
			'movie', 'http://doorpost.vo.llnwd.net/o33/webvideos/filmplayerbig',
			'salign', '',
			'flashvars', 'file='+filmsrc
			);	
}

function dpv_redrawFilmVote( film_id ){
	//alert('redraw film vote '+film_id);
	new Ajax.Updater('voteInfoDisplay', curPath+'handler.php', { parameters:{'event':'redrawCuePt', 'filmid':film_id}  } );
}
function dpv_redrawBTS( film_id ){
	new Ajax.Updater('behindthescenes', curPath+'handler.php', { parameters:{'event':'redrawBTS', 'filmid':film_id} } );
}

function dpv_editMyFilm( sid ){
	aj_MarkLoadingBlack('filmDetails', 'Loading...');
	new Ajax.Updater('filmDetails', curPath+'handler.php', { parameters: {'event':'editFilmDetails', 'sid':sid} } );
}

function dpv_remParticipant( film_id, pid ){
	aj_MarkLoadingGrey('film_participants_div', 'Saving...');
	new Ajax.Updater( 'film_participants_div', curPath+'handler.php', { parameters: {'event':'remParticipant', 'pid':pid, 'film_id':film_id}});
}
 
// called to draw the film comments section, pg is obsolete, but the filmid is needed
function aj_LoadFilmCommentsPg( limit, pg, filmId ){
	aj_MarkLoadingBlack('filmCommentsDisplayBox', 'Loading...');
	new Ajax.Updater('filmCommentsDisplayBox', curPath+'handler.php', { parameters: {'event':'filmcommentspg','fc_p':pg,'f':filmId}, evalScripts: true });
} 
// called to draw the film comments section, pg is obsolete, but the filmid is needed
function aj_LoadFilmReviewsPg( limit, pg, filmId ){
	aj_MarkLoadingBlack('filmReviewDisplayBox', 'Loading...');
	new Ajax.Updater('filmReviewDisplayBox', curPath+'handler.php', { parameters: {'event':'filmreviewspg','fc_p':pg,'f':filmId}, evalScripts: true });
}
// called to draw the film comments section, pg is obsolete, but the filmid is needed
function aj_LoadFilmDiscussionsPg( limit, pg, filmId ){
	aj_MarkLoadingBlack('filmDiscussionDisplayBox', 'Loading...');
	new Ajax.Updater('filmDiscussionDisplayBox', curPath+'handler.php', { parameters: {'event':'filmdiscussionspg','fc_p':pg,'f':filmId}, evalScripts: true });
}

 
// hide the //show buttons on the film comments
function aj_FormatFilmDetailsView(){
		formatLoadPg('filmdetails');
		// now actually draw the comments in -- we waited to get the others first.
		aj_LoadFilmCommentsPg('',1,0);		
} 
 
function dpv_Search( directlinks ){
	var type = "default";
	var searchStr =$F('videosearchinput2');
	aj_MarkLoading('searchResults', 'Searching...');
	if (directlinks){
		type = "direct";
	}
	new Ajax.Updater('searchResults', curPath+'handler.php', {parameters: {'event':'searchfilmthumbs','searchtype':type, 'searchFor':searchStr} });
}
 
// Search for films and populate the thumbnails
function dpv_SearchFilms(  ){
	var searchStr = $('videosearchinput').value;
	aj_MarkLoading('filmthumbnails', 'Searching...');
	new Ajax.Updater('filmthumbnails', curPath+'handler.php', {
					 	parameters: {'event':'loadfilmthumbs', 'searchFor':searchStr}
					 });
}

function dpv_showCritReview( cfid , topic){
	aj_MarkLoading('critFilmCommentDiv', 'Loading...');
	new Ajax.Updater('critFilmCommentDiv', curPath+'handler.php', {
					 	parameters: {'event':'drawcritique','cfilmid':cfid, 'topic':topic }
					 });
}

function dpv_submitCritReview(){
	var comment = $F('criticalComment');
	var comment2 = $F('criticalComment2');
	var comment3 = $F('criticalComment3');
	var critfilmid = $F('cfilmid');
	
	if (comment == "" && comment2 == "" && comment3 == ""){
		alert("You cannot submit an empty critique.");
		return;
	}
	
	if (!confirm("Are you sure you wish to submit your critique as-is?  Your one-time review will not be available to edit after submission.")){
		return;
	}
	
	aj_MarkLoading('critFilmCommentDiv', 'Saving your critique...');
	new Ajax.Updater('critFilmCommentDiv', curPath+'handler.php', {
					 	parameters: {'event':'savecritique', 'cfid':critfilmid,'ccomment':comment,'c2comment':comment2,'c3comment':comment3}, onComplete: function(){
							Element.hide('critique_terms');
						}
					 });
	
}

// called by?
function dp_LookupAndPlay(  ){
	onCritique = false;
	
	if (!sFilmId || sFilmId == "")
		return;
	// Look up the film src and restriction for the film
 	// call startplayingfilm
	
	skipFilmReload = true;
	
	new Ajax.Updater('filmProcessDiv', curPath+'handler.php',{
					 parameters:{'event':'lookupFilmData','SID':sFilmId},
					 evalScripts: true
					 });
		
	// hide the topic questions etc.
	loadFormatPg('nocontent');
}

// Called when a user logs in or registers
function dp_StartPlayingFilm(  ){
	onCritique = false;
	
	if (lastFilmSrc == ""){
		// can't play a film we don't have tracked.
		return;
	}

	filmSrc = lastFilmSrc;
	
	// check if the film requires age restriction and if the user is old enough
	if (!checkAgeOkMsg(lastIsRestricted)){
		return;
	}
	dpv_Play( filmSrc );	
	aj_FormatFilmDetailsView();		
}

function dp_PlayFilmLink(  ){	   

	onCritique = false;
	
	if (modUserManage == true && displayingUserInfo == true){
		dispA.togTo('maintopic');
		dpu_DoneManage();
	}
		
	aj_MarkLoading('filmDetails', 'Loading film details...');
	
	new Ajax.Updater('filmDetails', curPath+'handler.php',
					 { 
					 	parameters: {'event':'filmdetails', 'fid':lastFilmId },
						onComplete: aj_FormatFilmDetailsView
					 });

	// where is restrict set?
	if (!checkAgeOkMsg( restrict )){
		return;
	}
	
	dpv_Play( lastFilmSrc );
}

/*
 * Called anytime you click on a thumbnail, and through the direct link series
 */
function dp_PlayFilm( filmSrc, filmId, restrict, topic ){
	onCritique = false;
	skipFormatThumbs = true;
	
	
	// history information 
	var cObj = new Object();
	cObj.filmSrc = filmSrc;
	cObj.filmId = filmId;
	cObj.restrict = restrict;
	cObj.topic = topic;
	cObj.link_clicked = 'dp_play_film';
	hist_CheckAdd('dp_PlayFilm', 'film_'+filmId , cObj );
	

	if (displayingUserInfo == true)
		dpu_DoneManage();

	lastFilmId = filmId;
	lastFilmSrc = filmSrc;
	lastIsRestricted = restrict;

	if (topic != sCurTopic){
		aj_ChangeTopicTwo( topic );
	} else if (loadedDirectURL == true){
		loadedDirectURL = false;
		aj_RedrawTopicTwo();
	} 
	
	formatLoadPg('filmdetails');
//	fDetails.innerHTML = "Loading film details... <img src=\"images/working.gif\" alt=\"loading\">";
	// aj_FormatFilmDetailsView will call the formatting to turn on the appropriate divs
	aj_MarkLoading('filmDetails', 'Loading film details...');
	new Ajax.Updater('filmDetails', curPath+'handler.php',
					 { 
					 	parameters: {'event':'filmdetails', 'fid':filmId },
						onComplete: aj_FormatFilmDetailsView
					 });

	// none of these stored values are used...?
	if (!checkAgeOkMsg( restrict )){
		storedfilmSrc = filmSrc;
		storedfilmId = filmId,
		storedrestrict = true;			
		return;
	}
	
//	if (dispC1.current != 'filmDetails'){
		dispC1.togTo('filmDetails');
		dispC.togTo('fullBodyDiv');
//	}
	dpv_Play( filmSrc );
}

function dp_PlayFilmCrit( filmSrc, filmId, restrict ){
	onCritique = false;
	
	lastFilmId = filmId;
	lastFilmSrc = filmSrc;
	lastIsRestricted = restrict;
	
	// none of these stored values are used...?
	if (!checkAgeOkMsg( restrict )){
		storedfilmSrc = filmSrc;
		storedfilmId = filmId,
		storedrestrict = true;			
		return;
	}

	dpv_Play( filmSrc );
}


// hide the film details section
function aj_CloseFilmDetails( ){
	$('filmDetails').innerHTML = "";
	if (sCurTopic == 'hope' || sCurTopic == 'doorpostinfo'){
		dispC2.togTo('contentWrapper');
	} else {
		dispC2.togTo('bannerDiv');
	}
}

// called by all the film play routines , make sure the user may view the film, if not,
// display the age required message in the place of the video
function checkAgeOkMsg( restrict ){
	if (restrict == true && ageok == false){
		onCritique = false;		
		$('videocontent').innerHTML = "<img src=\"http://c0625612.cdn.cloudfiles.rackspacecloud.com/webimages/filmagealert.gif\" alt=\"This film requries age verification.\">";
		return false;
	}		
	return true;
}


// Display the film thumbnails
function aj_LoadFilmThumbs( keepformat ){
	if (!keepformat){
		if (sCurTopic != 'hope')
			new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs'}, onComplete: aj_WriteAReview });
		else
			new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs', 'fortopic':'hope'}, onComplete: aj_TopicConversation });		
	} else {
		if (sCurTopic != 'hope')
			new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs'}});
		else
			new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs', 'fortopic':'hope'} });		
	}
}

// Display a specific page of the film thumbnails
function aj_LoadFilmThumbsPg( limit, pg ){
	aj_MarkLoading('filmthumbnails', 'Loading...');
	new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs', 'limit':limit, 'p':pg} });
}

// Run a sort operation on the thumbnails display
function sortThumbs( sortType ){
	// Replace the sort icon with the highlighted sort icon
	var sPath = "http://c0625612.cdn.cloudfiles.rackspacecloud.com/webimages/";
	// whatever the last one was, turn it off.
	MM_swapImage( lastThumbSortImg+"Img", '', sPath+lastThumbSortImg+".jpg", 1);
	
	// update the sort menu with the highlighted item from the preloaded base
	switch (sortType){
		case '1':  MM_swapImage( 'highestrankImg', '', sPath+'highestrank2.jpg',1); lastThumbSortImg = "highestrank"; break; // highestrankImg
		case '2':  MM_swapImage( 'lowestrankImg', '', sPath+'lowestrank2.jpg',1); lastThumbSortImg = "lowestrank"; break; // lowestrankImg
		case '3':  MM_swapImage( 'newestImg', '', sPath+'newest2.jpg',1); lastThumbSortImg = "newest"; break; // newestImg
		case '4':  MM_swapImage( 'oldestImg', '', sPath+'oldest2.jpg',1); lastThumbSortImg = "oldest"; break; // oldestImg
		case '5':  MM_swapImage( 'mostviewedImg', '', sPath+'mostviewed2.jpg',1); lastThumbSortImg = "mostviewed"; break; // mostviewedImg
		case '6':  MM_swapImage( 'leastviewedImg', '', sPath+'leastviewed2.jpg',1); lastThumbSortImg = "leastviewed"; break; // leastviewedImg
		default:  break;
	}
	aj_MarkLoading('filmthumbnails', 'Loading...');
	new Ajax.Updater('filmthumbnails', curPath+'handler.php', { parameters: {'event':'loadfilmthumbs','sorttype':sortType, 'p':'1'} });
}


function dpv_Play( filmSrc ){
	dpAC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0','width','505','height','330','src','http://doorpost.vo.llnwd.net/o33/webvideos/videoholder','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','flashvars','file=http://doorpost.vo.llnwd.net/o33/uploads/'+filmSrc+'&autostart=true','type','application/x-shockwave-flash' );
	// If we're calling this from the direct link page, dispB will not have been defined yet.
	// so skip togging to it, as that will happen at the bottom of the index page.
	if (loadedDispB == true){
		dispB.togTo('videocontent');
	}
}

function dpAC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '';
  if (isIE && isWin && !isOpera)
  {
    str += '<object ';
    for (var i in objAttrs)
    {
      str += i + '="' + objAttrs[i] + '" ';
    }
    str += '>';
    for (var i in params)
    {
      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    }
    str += '</object>';
  }
  else
  {
    str += '<embed ';
    for (var i in embedAttrs)
    {
      str += i + '="' + embedAttrs[i] + '" ';
    }
    str += '> </embed>';
  }

	return str;
	
//  document.write(str);
}

function dpv_loadswf( src, w, h, vars, color ){
	if (!color || color == ''){
		color = "#c0c0c0;";
	}
	
	var str = dpAC_FL_RunContent_return( 
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
				'width', w,
				'height', h,
				'src', src,
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'showall',
				'wmode', 'window',
				'devicefont', 'false',
				'id', src,
				'bgcolor', color,
				'name', src,
				'menu', 'true',
				'allowFullScreen', 'false',
				'allowScriptAccess','sameDomain',
				'movie', src,
				'salign', '',
				'flashvars', vars);
	return str;
}

function dpAC_FL_RunContent_return(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
	var str = dpAC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
	return str;
}

function dpAC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
	var str = dpAC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
	$('videocontent').innerHTML = str;
}