/* $Id: class_blog.js 5 2009-01-11 06:01:16Z john $ */
	function truncate(text, length, ellipsis) {  
	
		if (typeof length == 'undefined') var length = 100;
		if (typeof ellipsis == 'undefined') var ellipsis = '...';
		if (text.length < length) return text;
	    return text.substr(0,length) + ellipsis;

	}
	
function runScripts(e) {
	if (e.nodeType != 1) return; //if it's not an element node, return
 
	if (e.tagName.toLowerCase() == 'script') {
		eval(e.text); //run the script
	}
	else {
		var n = e.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
}



var startPlay = true;

var refreshFriend = true;




SocialEngineAPI.Youtorium = new Class({
	
  
  
  Base: {},
  
  
  options: {
    'ajaxURL' : 'youtorium_ajax.php',
	'ajaxMethod' : 'post',
    'ajaxSecure' : false
  },



  // GET VIDEO
  getVideoCareer: function(user, video_id, owner, refreshFriend, startPlay)
  {	  
	  
	  //alert("Get Video Careers() video_id + " + video_id);
	  
	if (alreadyPlayed()) 
	{		
		//alert ('CANT WATCH');
		showSignupForm();
		return false;	
	}

	
	//alert('NOT NOT');
		  
	if (startPlay == undefined) startPlay = true;

	if (refreshFriend == undefined) refreshFriend = true;

	var topLeftContent = $('top_left_content_area');

	
    // AJAX
    var bind = this;
    var request = new Request.JSON({
      'url' : this.options.ajaxURL,
      'method' : this.options.ajaxMethod,
      'secure' : this.options.ajaxSecure,
      'data' : {
        'task'  	: 'get_video',
        'user'  	: owner,
        'video_id'  : video_id
      },
      'onComplete' : function(responseObject, responseText)
      {
        bind.setVideoCareer(responseObject, refreshFriend, startPlay);
      }
    });
    
    request.send();

   // alert('bind ' + bind)
	if (pageTracker !== undefined) pageTracker._trackPageview("/youlife/career_videos.php?video_id=" + video_id );
	

	myIframe = $('a691e015');
	
	if (myIframe != undefined) 
	{
		myIframe.src = myIframe.src;		
	}
	
  },
  
    // GET VIDEO
  getCategoryCareer: function(catId, p)
  {
	var topLeftContent = $('subcats');

	
    // AJAX
    var bind = this;
    var request = new Request({
      'url' : 'career_videos.php',
      'method' : this.options.ajaxMethod,
      'data' : {
        'task'  	: 'get_categories_ajax',
        'category_id'  	: catId,
        'p'  : p
      },
      'onComplete' : function(responseText)
      {
        topLeftContent.empty();
        topLeftContent.innerHTML = responseText;
		//alert(responseText);
		//setAndExecute('top_left_content_area', responseText)
      }
    });
    
    request.send();
	
  },
  
  setVideoCareer: function(responseObject, refreshFriend, startPlay)
  {
	  
	//alert('setVideoCareer() + '); 
	  
	if (refreshFriend == undefined) refreshFriend = true;

    if( $type(responseObject)!="object" )
    {
      alert('Career: There was an error processing the request.');
      return false;
    }
	
	if (startPlay == undefined) startPlay = true;

    // AJAX
    var bind = this;
	var total_comments 			= parseInt(responseObject.friend_total_comments) || 0;
    var video_info				= responseObject.friend_video_info;
	var allowed_to_comment		= responseObject.friend_allowed_to_comment;
	var allowed_to_rate			= responseObject.friend_allowed_to_rate;
	var videos					= responseObject.friend_videos;
	var total_videos			= responseObject.friend_total_videos;
	var owner_display_name		= responseObject.owner_display_name;
	var owner_user_name			= responseObject.owner_user_name;
	var rating_html				= responseObject.rating_html; 
	
	
	
	var setting_comment_html	= responseObject.setting_comment_html;
	var setting_comment_code	= responseObject.setting_comment_code;
	
	//alert(responseObject.friend_video_info.user_fname.length);
	
	if (responseObject.friend_video_info.user_fname.length == 0 && responseObject.friend_video_info.user_lname.length == 0) {
		var user_full_name			= owner_user_name;
	}
	else {
		var user_full_name			= responseObject.friend_video_info.user_fname + " " + responseObject.friend_video_info.user_lname;
	}

    //alert('video_info.video_id ' + video_info.video_id)
	//var videoFilePath = video_info.video_dir+video_info.video_id+'.flv';
	var videoFilePath = video_info.video_id;

	
	//alert('YOUtorium: Load Player:  path ' + videoFilePath);
	
	loadPlayer( videoFilePath , startPlay);
	
	
  },
  
  ////////////////// ADDING VER 2
  // GET VIDEO YOUtorium
  ///////////
  
  getVideoYOUtorium: function(user, video_id, owner, refreshFriend, startPlay)
  {
	 //alert("Get Video YOUtorium() video_id + " + video_id);
	  
	if (alreadyPlayed()) {
		
		//alert ('CANT WATCH');
		showSignupForm();
		return false;
	
	}

	  
	if (startPlay == undefined) startPlay = true;

	if (refreshFriend == undefined) refreshFriend = true;

	var topLeftContent = $('top_left_content_area');

	
	
    // AJAX
    var bind = this;
    var request = new Request.JSON({
      'url' : this.options.ajaxURL,
      'method' : this.options.ajaxMethod,
      'secure' : this.options.ajaxSecure,
      'data' : {
        'task'  	: 'get_video',
        'user'  	: owner,
        'video_id'  : video_id
      },
      'onComplete' : function(responseObject, responseText)
      {
        bind.setVideoYOUtorium(responseObject, refreshFriend, startPlay);
      }
    });
    
    request.send();

   // alert('bind ' + bind)
	if (pageTracker !== undefined) pageTracker._trackPageview("/youlife/youtorium.php?video_id=" + video_id );
	

	myIframe = $('a691e015');
	if (myIframe != undefined) 
	{		
		myIframe.src = myIframe.src;
		
	}
	
  },
  
  
  setVideoYOUtorium: function(responseObject, refreshFriend, startPlay)
  {
	  
	//alert('setVideoCareer()'); 
	  
	if (refreshFriend == undefined) refreshFriend = true;

    if( $type(responseObject)!="object" )
    {
      alert('YOUTORIUM:  There was an error processing the request.');
      return false;
    }
	
	if (startPlay == undefined) startPlay = true;
	


    // AJAX
    var bind = this;
	var total_comments 			= parseInt(responseObject.friend_total_comments) || 0;
    var video_info				= responseObject.friend_video_info;
	var allowed_to_comment		= responseObject.friend_allowed_to_comment;
	var allowed_to_rate			= responseObject.friend_allowed_to_rate;
	var videos					= responseObject.friend_videos;
	var total_videos			= responseObject.friend_total_videos;
	var owner_display_name		= responseObject.owner_display_name;
	var owner_user_name			= responseObject.owner_user_name;
	var rating_html				= responseObject.rating_html; 
	
	
	
	var setting_comment_html	= responseObject.setting_comment_html;
	var setting_comment_code	= responseObject.setting_comment_code;
	
	//alert(responseObject.friend_video_info.user_fname.length);
	
	/// GET USERNAME FROM REPONSE OBJECT
	if (responseObject.friend_video_info.user_fname.length == 0 && responseObject.friend_video_info.user_lname.length == 0) {
		var user_full_name		= owner_user_name;
	}
	else {
		var user_full_name		= responseObject.friend_video_info.user_fname + " " + responseObject.friend_video_info.user_lname;
	}

	//var videoFilePath = video_info.video_dir+video_info.video_id+'.flv';
	var videoFilePath = video_info.video_id;
	
	//alert('YOUtorium: Load Player:  path ' + videoFilePath);
	
	//// SEND VIDEO TO LAYER LOAD FUNCTION
	loadPlayer( videoFilePath , startPlay);
	
	/// GET VARS FROM VIDEO INFO OBJECT
	var title = truncate(video_info.video_title,50);
	var desc  = truncate(video_info.video_desc,130);

	//// INPUT HEADER INFO FOR VIDEO
	insertHeader( title, desc);
	
	/// INSERT USER RATING FOR VIDEO
	insertRating( rating_html);
	
	///// UPDATE BOTTOM RIGHT FRIENDS VIDEO INFO	
	if (refreshFriend == true) bind.showFriendResults(video_info.video_id, null, null, null, user_full_name);
	
	///// INSERT COMMENTS FOR VIDEO
	bind.showCommentResult(video_info.video_id, total_comments, allowed_to_comment, setting_comment_html , setting_comment_code , owner_user_name);
	
	removeVidCapture();
	
	
  },
  
  ////////////// EOF ADDING VER 2
  

  
  // GET VIDEO
  getVideo: function(user, video_id, owner, refreshFriend, startPlay)
  {

	  
	 //alert('getVideo: video_id  = ' + video_id )
	///// HACK TO CONVERT YOUTORIUM TO FUNCTION LIKE CAREERS
	this.getVideoYOUtorium(user, video_id, owner, refreshFriend, startPlay);
	
  },
  
  setVideo: function(responseObject, refreshFriend, startPlay)
  {
	  
  },
	
	
  // SHOW FRIEND RESULTS
  showFriendResults: function(video_id, s,v,p, user_full_name)
  {
	  
	//alert("Show Friend Results() user: " + user_full_name + "   Video_id: " + video_id);
	var bottomRightContent = $('bottom_right_content_area');
	if (s == undefined) s = 'video_datecreated DESC';
	if (v == undefined) v = 1;
	if (p == undefined) p = 1;
	
    // Ajax
    var bind = this;
    var request = new Request({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'show_friend_videos',
		's' : s,
		'v' : v,
		'p' : p,
		'video_id' : video_id,
		'user_full_name' : user_full_name
      },
      'onComplete':function(responseText)
      {
        bottomRightContent.empty();
        bottomRightContent.innerHTML = responseText;
		//setAndExecute('top_left_content_area', responseText)
      }
    });
    
    request.send();
	pageTracker._trackPageview("/youlife/youtorium.php?video_id=" + video_id + "&s" + s + "&v=" + v + "&p=" + p  );

  },
	
	
  showCommentResult: function(video_id, total_comments, can_comment, comment_html , comment_code ,owner_user_name)
  {
	  	  //alert($$('.comments_section_postcomment')[0].id);
	  
	  	  //alert(video_id + " - " + total_comments + " - " +  can_comment + " - " + comment_html  + " - " +  comment_code );
		  
		  // lets first modify the divs section so it will work with comments class --- yes I know it's a hack
		  $$('.comments_section_postcomment')[0].id="video_" + video_id + "_postcomment";
		  $$('.comments_section_comments')[0].id="video_" + video_id + "_comments";
		  
	  
          SocialEngine.VideoComments.options.canComment = can_comment;
          SocialEngine.VideoComments.options.commentHTML = comment_html;
          SocialEngine.VideoComments.options.commentCode = comment_code;

		  SocialEngine.VideoComments.options.user_username = owner_user_name
    
          SocialEngine.VideoComments.options.type =  'video';
          SocialEngine.VideoComments.options.typeIdentifier =  'video_id';
          SocialEngine.VideoComments.options.typeID =  video_id;
              
          SocialEngine.VideoComments.options.typeTab =  'videos';
          SocialEngine.VideoComments.options.typeCol =  'video';
		  SocialEngine.VideoComments.options.init =  false;
		  
		  SocialEngine.VideoComments.options.paginate =  true;
		  SocialEngine.VideoComments.options.cpp =  5;
          SocialEngine.VideoComments.options.textareaWidth = 450;
          SocialEngine.VideoComments.options.initialTotal =  total_comments;
	  
        SocialEngine.VideoComments.showPostComment();
        SocialEngine.VideoComments.getComments(1);

	  
  },
	
	
	  // SHOW BROWSE
  showSearchResults: function(search_text,s,v,p)
  {

	var topRightContent = $('top_right_content_area');
	if (s == undefined) s = '';
	if (v == undefined) v = '';
	if (p == undefined) p = '';
	
    // Ajax
    var bind = this;
    var request = new Request({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'show_search',
		's' : s,
		'v' : v,
		'p' : p,
		'search_text' : search_text
      },
      'onComplete':function(responseText)
      {
        topRightContent.empty();
        topRightContent.innerHTML = responseText;
		//alert(topRightContent);
		//setAndExecute('top_left_content_area', responseText)
      }
    });
    
    request.send();
	pageTracker._trackPageview("/youlife/youtorium.php?task=show_search&s" + s + "&v=" + v + "&p=" + p + "&search_text=" + search_text );

  },
	
  // SHOW BROWSE
  showBrowseResults: function(s,v,p)
  {
	var topRightContent = $('top_right_content_area');
	if (s == undefined) s = '';
	if (v == undefined) v = '';
	if (p == undefined) p = '';
	
	savedParamS = s;
	savedParamV = v;
	savedParamP = p;
	
	
    // Ajax
    var bind = this;
    var request = new Request({
      'method' : 'post',
      'url' : this.options.ajaxURL,
      'data' : {
        'task' : 'show_browse',
		's' : s,
		'v' : v,
		'p' : p
      },
      'onComplete':function(responseText)
      {
    	  
        topRightContent.empty();
        topRightContent.innerHTML = responseText;
		//setAndExecute('top_left_content_area', responseText)
      }
    });
    
    request.send();
	pageTracker._trackPageview("/youlife/youtorium.php?task=show_browse&s" + s + "&v=" + v + "&p=" + p );

  },
  
  displayRegister: function()
  {

	var notebook = $('top_left_content_area');
	
    // Ajax
    var bind = this;
    var request = new Request({
      'method' : 'post',
      'url' : 'cvt_ajax.php',
      'data' : {
        'task'  	: 'display_register_youtorium'
      },
      'onComplete':function(responseText)
      {
        notebook.empty();
        notebook.innerHTML = responseText;
      }
    });
	
    request.send();
	
  },
  
  displayRegisterCareer: function()
  {

	var notebook = $('playerWrapper');
	
    // Ajax
    var bind = this;
    var request = new Request({
      'method' : 'post',
      'url' : 'cvt_ajax.php',
      'data' : {
        'task'  	: 'display_register_career'
      },
      'onComplete':function(responseText)
      {
        notebook.empty();
        notebook.innerHTML = responseText;
      }
    });
	
    request.send();
	
  },
  
  ////// MISC UTILS
  
  get_vid_id_from_url: function(url)
  {  
  return url.substring(url.lastIndexOf('=')+1);    
  }

  

});
