// JavaScript Document
// We're going to use an instanced object to make sure we only have one vlog recorder open at a time.
var vLogRec = null;
var lastVlogId = 0;

var vLogTracker = Class.create({
	initialize: function( ) {
		this.userid = curUser.id;
		this.div = "";
		this.type = "";
		this.params = "";
		this.active = false;
		this.width = '700';
		this.height = '400';
		this.lastFile = '';
		this.isNew = false;
	},
	
	setFile: function( file ){
		// Called when the vlog swf loads
		this.lastFile = file;
	},
	isActive: function(){
		return this.active;
	},
	hide: function(  ){
		this.active = false;
		if (this.div != "" )
			$(this.div).innerHTML = "";
		this.div = "";
		this.type = "";
		this.params = "";

	},

	publish: function( sName, sSubj ){
		this.lastFile = sName;

		switch( this.type ){
			case 'VlogResponse':
			case 'BlogResponse':
				divN = 'blogDisplayArea';
				
				// loginDetails won't exist... we're in lala land.
				aj_MarkLoadingBlack( "vlogRecSubmitDiv_"+lastVlogId , 'Saving vlog...');
				
				if (this.isNew == true ){
					dpv_editVlog( -1, sName, true );
				} else {
				
					new Ajax.Updater(  divN  , curPath+"handler.php", {
						parameters: {'event':'postvlogreply', 'sub':'vlog', 'params':this.params, 'fName':sName, 'sSubj':sSubj},
						evalScripts: true} );
				}

				this.hide();
				break;
			case 'InboxResponse':
				aj_MarkLoadingBlack("vlogRecSubmitDiv_"+lastVlogId, 'Saving video message...');
				new Ajax.Updater("loginDetails", curPath+"handler.php", {
						parameters: { 'event':'postinboxreply', 'sub':'vlog', 'fName':sName, 'params':this.params } ,
						evalScripts: true} );
				this.hide();
				break;
			default:
				alert('You just attempted to publish a vLog of type: '+this.type+' with the filename: '+sName);
				alert("Unrecognized vlog type -- please contact technical support.");
				break;
		}
	},

	// Call this when changing divs containing vLog inputs
	checkCloseOk: function( ){
		if (!this.isActive())
			return true;
		if (confirm( 'You are about to close your active vLogger. Do you wish to continue?')){
			this.hide();
			return true;
		}
		return false;
	},
	
	display: function( divName, type, addParams ){
		if (this.active == true){
			this.hide();
			this.display( divName, type, addParams );
		}
		
		if (!curUser.isLoggedIn()){
			alert("You need to be logged in to use this utility.");
			return false;
		}
		
		lastVlogId++;
		
		this.active = true;
		this.div = divName;
		this.type = type;
		this.params = addParams;
		
		divName = $(divName);
		divName.show();
		
//		&extPath='+curPath+'flash/&vlogtype='+this.type
		flashvars = 'uid='+this.userid+'&type='+type+'&extPath='+curPath+'flash/';
		if (addParams != ""){
			flashvars = flashvars+"&"+addParams;
		}
		
		var str = dpAC_FL_RunContent_return( 
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
				'width', this.width,
				'height', this.height,
				'src', curPath+'flash/tutorial_record_3',
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'false',
				'scale', 'showall',
				'wmode', 'window',
				'devicefont', 'false',
				'id', ''+lastVlogId+'_tutorial_record_3',
				'bgcolor', '#c0c0c0',
				'name', curPath+'flash/tutorial_record_3',
				'menu', 'true',
				'allowFullScreen', 'false',
				'allowScriptAccess','sameDomain',
				'movie', curPath+'flash/tutorial_record_3',
				'salign', '',
				'flashvars', flashvars);
		
		str = "<div style=\"width: "+this.width+"px; margin-top: 10px; padding-top: 5px; padding-left: 5px;\">"+str+"</div>";
		str = str+"<div id='vlogRecSubmitDiv_"+lastVlogId+"'></div>";
		divName.innerHTML = str;
		}
		
	});

function dpv_removeVlog( id ){
	if (!confirm('You are about to remove this entry. Are you sure you wish to continue? All responses will be removed as well.')){
		return;
	}
	aj_MarkLoadingBlack('blogDisplayArea', 'Saving vlog...');
	new Ajax.Updater('blogDisplayArea', curPath+"handler.php", {
		parameters: {'event':'removeVlog', 'sub':'vlog', 'id':id},
		evalScripts: true} );
}

function dpv_editVlog( id, sFile, isNew ){
	
	var divN = 'blogDisplayArea';
	
	if (isNew){
		divN = 'newBlogFormDiv';
	}
	
	if (!sFile)
		sFile = "";
			
	
//	aj_MarkLoading( divN, 'Loading...');
	new Ajax.Updater( divN, curPath+"handler.php", {
					 parameters:{'event':'formvlogedit', 'sFile':sFile, 'vid':id }, evalScripts: true});
}

function hist_newBlogComment(){
	vlog_textNewBlogComment();
}

function hist_newVlogComment(){
	if (vLogRec == null){
		vLogRec = new vLogTracker();
	}
	if (vLogRec.isActive()){
		if (!confirm( 'You are already running the vLogger, do you wish to stop the current instance and create a new one?')){
			return;
		}
	}
	vLogRec.isNew = true;
	vLogRec.display( 'newBlogFormDiv', 'VlogResponse', 'parent_id=-1' );
}

function hist_ReplyBlogComment( id ){
	// don't really need to do anything here... it's going to append normally...
	vlog_ReplyBlogComment( id );
}

// mId is the message ID to be replied to
function vlog_ReplyBlogComment( id ){
	if (vLogRec == null){
		vLogRec = new vLogTracker();
	}
	if (vLogRec.isActive()){
		if (!confirm( 'You are already running the vLogger, do you wish to stop the current instance and create a new one?')){
			return;
		}
	}
	vLogRec.isNew = false;
	vLogRec.display( 'replyBlogMsg_'+id, 'VlogResponse', 'parent_id='+id );
//	vlog_drawInputPanel( 'replyInboxMsg', 'uid='+curUser.id+'&type=InboxResponse&forid='+mId);	
}

function hist_textReplyBlogComment( mId ){
	if (!curUser.isLoggedIn()){
		alert("You  must be logged in to post a comment.");
		return;
	}
	var cObj = new Object();
	cObj.mid = mId; 
	cObj.link_clicked = 'text_reply_vlog';
	hist_CheckAdd('hist_textReplyVlogMsg', 'reply', cObj );
	vlog_textReplyBlogComment( mId );
}


function vlog_textReplyBlogComment( mId ){
	var divN = 'replyBlogMsg_'+mId;
	aj_MarkLoadingBlack(divN, 'Loading...');
	new Ajax.Updater(divN, curPath+'handler.php', {parameters: {'event':'formvlogreply', 'parent_id':mId}, evalScript: true});
}

function vlog_textNewBlogComment(  ){
	var divN = 'loginDetails';
	aj_MarkLoadingBlack(divN, 'Loading...');
	new Ajax.Updater(divN, curPath+'handler.php', {parameters: {'event':'formvlogreply', 'parent_id':'-1'}, evalScript: true});
}

function vlog_postReplyVlogComment( id ){
//	var mid = $F('parent_id_'+id);
	var bodyMsg = $F('replyBodyText_'+id);
	if (bodyMsg == ""){
		alert("You cannot post an empty comment.");
		return;
	}
	aj_MarkLoadingBlack('blogDisplayArea', 'Saving message...');
	new Ajax.Updater('blogDisplayArea', curPath+"handler.php", {
		 parameters: { 'event':'postvlogreply', 'sub':'text', 'bodyMsg':bodyMsg, 'parent_id':id } , evalScripts: true});
}

function vlog_postEditVlogComment( ){
	var bodyMsg = $F('vlogBodyText');
	var vid = $F('vlogFormId');
	var sSubj = $F('vlogSubject');
	var sFile = $F('vlogFileName');
	var divN = "blogDisplayArea";
	if (vid == "-1"){
		divN = "newBlogFormDiv";
	}
	
	aj_MarkLoadingBlack( divN, 'Saving ...');
	new Ajax.Updater(divN, curPath+'handler.php', {
					 parameters:{'event':'postvlogedit', 'sMsg':bodyMsg, 'sSubj':sSubj, 'vid':vid, 'sFile':sFile },
					 evalScripts: true});
	
}

function vlog_postNewVlogComment(  ){
//	var mid = $F('parent_id_'+id);
	var bodyMsg = $F('vlogBodyText');
	if (bodyMsg == ""){
		alert("You cannot post an empty comment.");
		return;
	}
	var sSubj = $F('vlogSubject');
	
	aj_MarkLoadingBlack('newBlogFormDiv', 'Saving message...');
	new Ajax.Updater('newBlogFormDiv', curPath+"handler.php", {
		 parameters: { 'event':'postvlogreply', 'sub':'text', 'sSubj':sSubj, 'bodyMsg':bodyMsg, 'parent_id':-1} , evalScripts: true});
}


function hist_ReplyInboxMsg( mId ){
	var cObj = new Object();
	cObj.mid = mId; 
	cObj.link_clicked = 'vlog_reply_inbox';
	hist_CheckAdd('hist_ReplyInboxMsg', 'reply', cObj );
	vlog_ReplyInboxMsg( mId );
}

// mId is the message ID to be replied to
function vlog_ReplyInboxMsg( mId ){
	if (vLogRec == null){
		vLogRec = new vLogTracker();
	}
	if (vLogRec.isActive()){
		if (!confirm( 'You are already running the vLogger, do you wish to stop the current instance and create a new one?')){
			return;
		}
	}
	vLogRec.display( 'replyInboxMsg', 'InboxResponse', 'forid='+mId );
//	vlog_drawInputPanel( 'replyInboxMsg', 'uid='+curUser.id+'&type=InboxResponse&forid='+mId);	
}

function hist_emailVlog( id ){
	alert('you selected to email vlog '+id+' to a friend');
}

function hist_UserVlog(){
	hist_CheckAdd('hist_UserVlog','vLog', 'vlog_UserVlog');
	vlog_UserVlog();
}

function vlog_UserVlog(){
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', { parameters: {'event':'editVlog', 'id':'-1'} } );
}