var ERROR_NO_VIDEO_BODY_TEXT = 'Please enter a comment.';
var ERROR_LOGIN_REQUIRED = 'Please login and proceed further.';
var ERROR_REPORT_ABUSE_SAME_USER = 'Sorry, you cannot report abuse your own comments';
var ERROR_VIDEO_REMOVE_FROM_FAVS = '<b>Remove From Favorites</b>';
var ERROR_VIDEO_ADD_TO_FAVS = '<b>Add To Favorites</b>';

var ERROR_PLAYLIST_ADD_SUCCESS = 'Video has been added to your playlist successfully.';
var ERROR_PLAYLIST_ADD_FAILURE = 'Sorry, there may be an error while adding video to playlist.';
var ERROR_PLAYLIST_EXISTS = 'Sorry, video already exists in the playlist.';

var ERROR_VIDEO_TERMS = "Your video cannot be uploaded without you agreeing to our Terms of Use and Privacy Policy.";
var ERROR_VIDEO_TITLE = "Looks like you forgot to give a title.";

var extArray = new Array(".flv",".mov",".wmv",".avi",".mpeg",".mpg",".mp4",".ram");
var ERROR_DELETE_VIDEO_COMMENT = "Are you sure you want to delete this video comment?";
var ERROR_DELETE_VIDEO = "Are you sure you want to delete this video?";
var VIDEO_TITLE_ABUSE = 'Report Abuse';

var AjaxObject = 
{
handleSuccess_pagination:function(o){
	document.getElementById('video_loading').style.display = 'none';
	document.getElementById("video_loading").innerHTML = '';
	document.getElementById("video_view").innerHTML = o.responseText;
	//document.getElementById("pagination2").innerHTML = o.responseText;    	    	
	//document.getElementById("adda_scribble_view").innerHTML = o.responseText;    
  },
 handleFailure_pagination:function(o){
    // Failure handler                                                                                                                                   
    },
 handleSuccess_delete_video:function(o){
    document.getElementById('video_loading').style.display = 'none';
    document.getElementById("video_loading").innerHTML = '';
    document.getElementById("video").innerHTML = o.responseText;    		
  },
 handleFailure_delete_video:function(o){
    // Failure handler                                                                                                                                   
  },
handleStart_videos:function(){
	document.getElementById("video_loading").innerHTML = loading;
    },
handleComplete_videos:function(){
	document.getElementById("video_loading").innerHTML = loading;
    },
handleAbort_videos:function(){
	// Abort handler
    },
 handleStart_video:function(){
    document.getElementById("video_loading").innerHTML = loading;
  },
 handleComplete_video:function(){
    document.getElementById("video_loading").innerHTML = loading;
  },
 handleAbort_video :function(){
    // Abort handler
  },
 handleSuccess_video_abuse:function(o){
    if(o.responseText == 701)
      {
	  //document.getElementById('report_abuse_video').innerHTML = '<a href="#" class="simlinkbtn">Under Review</a>';
	  document.getElementById('report_abuse_video').innerHTML = '<a href="#" class="vertmiddle"><img src="/storage/image/default/icon/ico-under-review.gif" width="25" height="25" border="0" alt="" class="vertmiddle zero"> <b>Under Review</b></a><br class="spacer3">';

	  document.getElementById('video_success_title').innerHTML = '<h2>' + VIDEO_TITLE_ABUSE + '</h2>';
	  success_message = 'This video has been reported as abusive. It is currently under review.'
	  document.getElementById('video_success_message').innerHTML = success_message;
	  document.getElementById('video_dialog1').style.display = 'none';
	  document.getElementById('video_dialog4').style.display = 'block';
	  YAHOO.example.video_container1.video_dialog4.show();
      }
    else
	{
	    hideVideoPopup();
	}
    
  },
 handleSuccess_video_comments_abuse:function(o){
    if(o.responseText == 701)
      {
	var abuse_id = 	document.getElementById('abuse_id').value;
	document.getElementById('abusepopup_'+abuse_id).innerHTML = '<a href="#" class="gray999">Under Review</a>';
	document.getElementById('video_success_title').innerHTML = '<h2>' + VIDEO_TITLE_ABUSE + '</h2>';
	success_message = 'This video comment has been reported as abusive. It is currently under review.'
	document.getElementById('video_success_message').innerHTML = success_message;
	document.getElementById('video_dialog2').style.display = 'none';
	document.getElementById('video_dialog4').style.display = 'block';
	YAHOO.example.video_container1.video_dialog4.show();
      }
    else
      {
	if(o.responseText != '') 
	  {
	    displayErrorMessage('error_video_comments',o.responseText);
	  }
	hideVideoCommentsPopup();
      }
    
  },
 handleSuccess_delete_video_comment:function(o){
    document.getElementById("video_landing_comments").innerHTML = o.responseText;        
  },
 handleFailure_delete_video_comment:function(o){
    // Failure handler                                                                                                                                   
  }
 ,
handleSuccess_video_favs:function(o){
    if(o.responseText == 902)
      {
	document.getElementById("favorites").innerHTML = ERROR_VIDEO_REMOVE_FROM_FAVS;
      }
    else if(o.responseText == 901)
      {
	document.getElementById("favorites").innerHTML = ERROR_VIDEO_ADD_TO_FAVS;
      }
    else
      {
	displayErrorMessage('error_video_comments',o.responseText);
      }

  },
 handleFailure_video_favs:function(o){
    // Failure handler                                                                                                                                   
    },
handleSuccess_video_addtoplaylist:function(o){

    var response = o.responseText;

    if(response == 903)
      {
	displayErrorMessage('error_video_comments',ERROR_LOGIN_REQUIRED);
      }
    else if(response == 905)
      {
	displayErrorMessage('error_video_comments',ERROR_PLAYLIST_ADD_SUCCESS);
      }
    else if(response == 906)
      {
	displayErrorMessage('error_video_comments',ERROR_PLAYLIST_ADD_FAILURE);
      }
    else if(response == 907)
      {
	displayErrorMessage('error_video_comments',ERROR_PLAYLIST_EXISTS);
      }
    else
      {
	displayErrorMessage('error_video_comments',ERROR_PLAYLIST_ADD_FAILURE);
      }
    
    hideAddToPlaylistPopup();

  },
 handleFailure_video_addtoplaylist:function(o){
    // Failure handler                                                                                                             
    displayErrorMessage('error_video_comments',ERROR_PLAYLIST_ADD_FAILURE);
    }
,
handleSuccess_video_favs:function(o){
    if(o.responseText == 902)
      {
	document.getElementById("favorites").innerHTML = ERROR_VIDEO_REMOVE_FROM_FAVS;
      }
    else if(o.responseText == 901)
      {
	document.getElementById("favorites").innerHTML = ERROR_VIDEO_ADD_TO_FAVS;
      }
    else
      {
	displayErrorMessage('error_video_comments',o.responseText);
      }

  },
handleFailure_video_favs:function(o){
	// Failure handler                                                                                                                                   
    },
handleSuccess_video_from_user:function(o){

	var response = o.responseText;
	document.getElementById("same_user_video").innerHTML = '';
	document.getElementById("same_user_video").innerHTML = response;

    },
handleFailure_video_from_user:function(o){
	// Failure handler                                                                                                             
	// displayErrorMessage('error_video_comments',ERROR_PLAYLIST_ADD_FAILURE);
    }
};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_delete_video =
{
    customevents:{ 
	        onStart:AjaxObject.handleStart_video, 
	        onComplete:AjaxObject.handleComplete_video,     
	        onAbort:AjaxObject.handleAbort_video
	    }, 
    success:AjaxObject.handleSuccess_delete_video,
    failure:AjaxObject.handleFailure_delete_video,
    scope: AjaxObject

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_pagination =
{
    customevents:{ 
	        onStart:AjaxObject.handleStart_videos, 
	        onComplete:AjaxObject.handleComplete_videos,	       
	        onAbort:AjaxObject.handleAbort_videos 
	    }, 
    success:AjaxObject.handleSuccess_pagination,
    failure:AjaxObject.handleFailure_pagination,
    scope: AjaxObject
};


var callback_video_abuse =
{
    success:AjaxObject.handleSuccess_video_abuse,
    scope: AjaxObject
};

var callback_video_comments_abuse =
{
    success:AjaxObject.handleSuccess_video_comments_abuse,
    scope: AjaxObject
};


/* Callback object with custom events defined */ 
var callback_delete_video_comment = { 

    customevents:{ 
	        onStart:AjaxObject.handleStart_video, 
	        onComplete:AjaxObject.handleComplete_video,     
	        onAbort:AjaxObject.handleAbort_video
	    }, 
	success:AjaxObject.handleSuccess_delete_video_comment,
	failure:AjaxObject.handleFailure_delete_video_comment,
	scope: AjaxObject
   };      

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_video_favs =
{
 success:AjaxObject.handleSuccess_video_favs,
 failure:AjaxObject.handleFailure_video_favs,
 scope: AjaxObject
};

/* Callback object with custom events defined */ 
var callback_video_addtoplaylist = { 

    customevents:{ 
	        onStart:AjaxObject.handleStart_video, 
	        onComplete:AjaxObject.handleComplete_video,     
	        onAbort:AjaxObject.handleAbort_video
	    }, 
	success:AjaxObject.handleSuccess_video_addtoplaylist,
	failure:AjaxObject.handleFailure_video_addtoplaylist,
	scope: AjaxObject
   };      

/* Callback object with custom events defined */ 
var callback_video_from_user = { 

    customevents:{ 
	        onStart:AjaxObject.handleStart_video, 
	        onComplete:AjaxObject.handleComplete_video,     
	        onAbort:AjaxObject.handleAbort_video
	    }, 
	success:AjaxObject.handleSuccess_video_from_user,
	failure:AjaxObject.handleFailure_video_from_user,
	scope: AjaxObject
   };      

/* Method to Disable Submit Button */
function submitVideoComment(frm)
{
    var add_button = frm.submit_comment;

    add_button.value = "Adding Data....";

    add_button.disabled = true;

    frm.submit();
}

/* Method to Display Character counts */
function restw(frm,max,dispentered)
{
    var inp = frm.value.replace(/^\s+|\s+$/g,'');
    var l = parseInt(inp.length)
        if (l > max)
	    {
                inp = inp.substr(0,max)
		frm.value = inp
	    }
    document.getElementById(dispentered).innerHTML = inp.length;
}

//! Pagination
function pagination(base_url,keyword,show_from)
{  
    var post_data = 'keyword='+keyword+'&pagination=y'+'&show_from='+show_from;

    if(keyword == '')
	{
	    
	    YAHOO.util.Connect.asyncRequest('POST',base_url, callback_pagination, post_data);       
	}
    else
	{
		
	    YAHOO.util.Connect.asyncRequest('POST',base_url, callback_pagination, post_data);       
	
	}
    
      if(env_value == 'Online')
      {
	  pageTracker._trackPageview(base_url);
      }



}

/* Method to toggle TABS display Status */
function toggleDivs(div_id)
{

  var tab_divs = new Array('tab_related_search','tab_related_user','tab_playlist_user');
  var tab_divs_class = new Array('relatedLink','hisherLink','playlist');

  for( var i = 0; i < 3 ; i++) 
    {
      if(tab_divs[i] == div_id)
	{
	  document.getElementById(tab_divs_class[i]).className = 'black videotabactive';
	  document.getElementById(tab_divs[i]).style.display = 'block';
	}
      else
	{
	  document.getElementById(tab_divs_class[i]).className = 'black videotab';
	  document.getElementById(tab_divs[i]).style.display = 'none';
	}
      
    }
}

function saveVideoComments(frm)
{

    document.getElementById('error_video_comments').style.display = 'none';
    document.getElementById('error_video_comments').innerHTML = '';
    
    var body_text = frm.body.value;

    if(body_text == '')
	{
	  displayErrorMessage('error_video_comments_main',ERROR_NO_VIDEO_BODY_TEXT);
	  return false;
	}
    else
	{
	    frm.submit();
	}
}

function deleteVideoComments(comment_id)
{
    if(confirm(ERROR_DELETE_VIDEO_COMMENT))
      {
      
	var video_id = document.getElementById('video_post_id').value;
	var video_comment_id = comment_id;
	var postData ='comment_id='+video_comment_id+'&video_post_id='+video_id;
	var url = system_base_url+'ajax/video/_delete_video_comment/' ;
	YAHOO.util.Connect.asyncRequest('POST',url,callback_delete_video_comment,postData);	    
	
	if(env_value == 'Online')
	    {
		pageTracker._trackPageview(url);
	    }
	
	

      }

}

function saveVideoReportAbuse()
{
    var selected_types = '';
    var comma = '';   
    var select_action = false;
    var abuse_types = document.video_abuse_form.abuse_type;
    var logged_in_user_id = document.getElementById('logged_in_user_id').value;
    
    for(var i=0;i<6;i++)
	{
	    if(abuse_types[i].value != '')
		{
		    if(abuse_types[i].checked)
			{
			    selected_types = selected_types + comma + abuse_types[i].value;			    
			    comma = ',';	
			    select_action = true;
			}
		}
	    
	}
    
    if(logged_in_user_id == '' || isNaN(logged_in_user_id) )
       {
	 document.getElementById('abuse_error_msg').innerHTML = ERROR_LOGIN_REQUIRED;
	 // displayErrorMessage('abuse_error_msg',ERROR_LOGIN_REQUIRED);
       }
    else if(select_action == false)
	{
	  document.getElementById('abuse_error_msg').innerHTML = 'Please select the reason for your complaint.';
	}
    else
	{	 
	  var video_abuse_id = document.getElementById('video_abuse_id').value;
	  var video_user_id = document.getElementById('video_user_id').value;
	  var postData ='abuse_types='+selected_types+'&video_abuse_id='+video_abuse_id+'&video_user_id='+video_user_id;
	  var url = system_base_url+'ajax/video/_abuse_video/' ;
	  YAHOO.util.Connect.asyncRequest('POST',url, callback_video_abuse, postData);	    
	  if(env_value == 'Online')
	      {
		  pageTracker._trackPageview(url);
	      }
	  

	}

}

function saveVideoCommentsReportAbuse()
{

    var selected_types = '';
    var comma = '';   
    var select_action = false;
    var abuse_types = document.video_comments_abuse_form.abuse_type;
    var login_user_id = document.getElementById('login_user_id').value;
    
    for(var i=0;i<6;i++)
	{
	    if(abuse_types[i].value != '')
		{
		    if(abuse_types[i].checked)
			{
			  selected_types = selected_types + comma + abuse_types[i].value;			    
			  comma = ',';	
			  select_action = true;
			}
		}
	    
	}
    
    if(login_user_id == '' || isNaN(login_user_id))
       {
	 document.getElementById('abuse_comments_error_msg').innerHTML = ERROR_LOGIN_REQUIRED;
	 //displayErrorMessage('error_video_comments',ERROR_LOGIN_REQUIRED);	 
       }
    else if(select_action == false)
      {
	document.getElementById('abuse_comments_error_msg').innerHTML = 'Please select the reason for your complaint.';
      }
    else
	{	 
	    var abuse_id = document.getElementById('abuse_id').value;
	    var abuse_user_id = document.getElementById('abuse_user_id').value;
	    var login_user_id = document.getElementById('login_user_id').value;
	    var postData ='abuse_types='+selected_types+'&abuse_id='+abuse_id+'&abuse_user_id='+abuse_user_id+'&login_user_id='+login_user_id;
	    var url = system_base_url+'ajax/video/_abuse_video_comments/'  ;
	    YAHOO.util.Connect.asyncRequest('POST',url, callback_video_comments_abuse, postData);	    

	    if(env_value == 'Online')
		{
		    pageTracker._trackPageview(url);
		}
	  


	}

}

//! Function to add / remove from favorites
function addRemoveFavorites(video_id,login_user_id)
{

    if(login_user_id == '' || isNaN(login_user_id))
       {
	 displayErrorMessage('error_video_comments',ERROR_LOGIN_REQUIRED);
       }
    else
      {	 
	var post_data = 'video_id='+video_id+'&login_user_id='+login_user_id;
	YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/video/_favorite_video',callback_video_favs,post_data);
      }

}

//! add to playlist by user
function addToPlaylist(playlist_id,video_id)
{
  var login_user_id = document.getElementById('logged_in_user_id').value;
  
  if(login_user_id == '' || isNaN(login_user_id))
    {
      displayErrorMessage('error_video_comments',ERROR_LOGIN_REQUIRED);
    }
  else
    {	 
      var post_data = 'playlist_id='+playlist_id+'&video_id='+video_id+'&login_user_id='+login_user_id;
      var url = system_base_url+'ajax/video/_add_to_playlist' ;
      YAHOO.util.Connect.asyncRequest('POST',url,callback_video_addtoplaylist,post_data);
      if(env_value == 'Online')
	  {
	      pageTracker._trackPageview(url);
	  }
      
      
      
    }
  
}

function uploadVideo(form,file,embed) 
{ 
   var allowSubmit = false;
   var radioObj = document.frmVideoUpload.upload_type;
   var radioLength = document.frmVideoUpload.upload_type.length;
  
   for (i=0;i<radioLength;i++)
     {
       if (radioObj[i].checked==true && radioObj[i].value == 'embeded')
	 {
	   file = '';
	 }
     }
  
   if(file !="")
       {
	   while (file.indexOf("\\") != -1)
	       file = file.slice(file.indexOf("\\") + 1);
	       ext = file.slice(file.lastIndexOf(".")).toLowerCase();
       
	       for (var i = 0; i < extArray.length; i++) 
		   {
		       if (extArray[i] == ext) 
			   {
			       allowSubmit = true;
			       break; 
			   }
		   }
       }
   else if(embed != "")
       {
	   str = document.getElementById('embed_code').value;
	   if(str.match(/([\<object])([^\>]{1,})*([\>])/i)==null)
	       {
		   displayErrorMessage('error_video_main',"This is not a embed code");
		   return false;
		   
	       }
	   else
	       {
	       	   allowSubmit = true;
	       }
       }
  
  if (allowSubmit) 
    {
      if(form.title.value == "")
	{
	  displayErrorMessage('error_video_main',ERROR_VIDEO_TITLE);
	  return false;
	}
      if(!(form.policy.checked))
	{
	    alert(ERROR_VIDEO_TERMS);
	    // displayErrorMessage('error_video_main',ERROR_VIDEO_TERMS);
	  return false;
	}
    }
  else
    {
      //alert("Invalid File Format for files . \nPlease upload files with follwing file format: " + (extArray.join(" ")) );
      var errorMsg = "Looks like you forgot to give the details in the required fields below.";
      displayErrorMessage('error_video_main',errorMsg);
      return false;
    }

  var add_button = form.btnadd;
  
  add_button.disabled = true;
  
  form.submit();
  
}

function deleteVideoConfirmation(sendto)
{

  if(confirm(ERROR_DELETE_VIDEO))
    {
      window.location = system_base_url + 'video/_delete_video/'+sendto;
    }
  return false;
}
function doPlaylistAdd()
{
  var video_id=document.getElementById('main_video_id').value;
  window.location = system_base_url + 'video/_show_add_playlist/'+video_id;
}

function videoFromUsers(show_from_video)
{
    var lastpage = document.getElementById('lastpage').value;
    // var show_from_video = document.getElementById('show_from_video').value;
    if(show_from_video != 0)
	{
	    var user_id = document.getElementById('object_user_id').value;
	    var postData ='user_id='+user_id+'&show_from_video='+show_from_video+'&pagination=yes';
	    var url = system_base_url+'ajax/video/_show_user_video/' ;
	    YAHOO.util.Connect.asyncRequest('POST',url,callback_video_from_user,postData);	    
	}

    if(env_value == 'Online')
	  {
	      pageTracker._trackPageview(url);
	  }
      

}
function changeUploadType(div1,div2,frm)
{
  frm.checked = true;
  document.getElementById(div1).style.display='block';
  document.getElementById(div2).style.display='none';

}
