var ERROR_PLAYLIST_TITLE_EMPTY_OR_NO_HTML = 'Looks like you forgot to give a Title!';
var ERROR_PLAYLIST_DESCRIPTION_EMPTY_OR_NO_HTML = 'Looks like you forgot to give a Description!';
var ERROR_LOGIN_REQUIRED = 'Please login and proceed further.';
var loading = "Loading...<img src='/storage/image/default/loading.gif'/>";
var ERROR_PLAYLIST_DELETE_FAILURE = 'Sorry, There may be error while deleting video.';
var ERROR_DELETE_PLAYLIST = "Are you sure you want to delete this playlist?.";
var AjaxObject = 
{
handleSuccess_pagination:function(o){
	document.getElementById('playlist_loading').style.display = 'none';
	document.getElementById("playlist_loading").innerHTML = '';
	document.getElementById("playlist_view").innerHTML = o.responseText;    		
  },
 handleFailure_pagination:function(o){
    // Failure handler                                                                                                                                   
  },
handleSuccess_delete_playlist:function(o){

    document.getElementById('playlist_loading').style.display = 'none';

    if(o.responseText == 909)
      {
	document.getElementById("playlist_refresh").reload;
	//window.location.href = document.getElementById("playlist_refresh").value;
      }
    else if(o.responseText == 903)
      {
	displayError('error_playlist_delete',ERROR_LOGIN_REQUIRED+'<br>');
      }
    else if(o.responseText == 904)
      {
	// do redirect 
	window.location.href = system_base_url+'video/_show_playlist_delete';       
      }
    else if(o.responseText == 908)
      {
	displayError('error_playlist_delete',ERROR_PLAYLIST_DELETE_FAILURE+'<br>');
      }
     else
      {
	document.getElementById("playlist_view").innerHTML = '';
	document.getElementById("playlist_view").innerHTML = o.responseText;    		
      }
    var total_playlist_count = document.getElementById("total_playlist_count").value;
    
    if(total_playlist_count == 0)
    {
	setTimeout("editPlaylistToshowmessage()",3000);
    }
    
    },
handleFailure_delete_playlist:function(o){
    // Failure handler                                                                                                                                   
  },
 handleStart_playlist:function(){
    document.getElementById("playlist_loading").innerHTML = loading;
  },
 handleComplete_playlist:function(){
    document.getElementById("playlist_loading").innerHTML = loading;
  },
 handleAbort_playlist:function(){
    // Abort handler
  }
}

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

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_delete_playlist =
{
    customevents:{ 
	        onStart:AjaxObject.handleStart_playlist, 
	        onComplete:AjaxObject.handleComplete_playlist,     
	        onAbort:AjaxObject.handleAbort_playlist
	    }, 
    success:AjaxObject.handleSuccess_delete_playlist,
    failure:AjaxObject.handleFailure_delete_playlist,
    scope: AjaxObject

};

/* 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;
}

function submitPlaylist(frm)
{
  document.getElementById('error_playlist_title').innerHTML = '';
  document.getElementById('error_playlist_description').innerHTML = '';
  if(trim(frm.title.value) == '')
    {
     document.getElementById('title_error').setAttribute('class','err');
     document.getElementById('error_playlist_title').innerHTML = ERROR_PLAYLIST_TITLE_EMPTY_OR_NO_HTML+'<br>';
     return false;
    }
  else if(trim(frm.description.value) == '')
    {
      document.getElementById('title_error').setAttribute('class','clr');
      document.getElementById('description_error').setAttribute('class','err');
      document.getElementById('error_playlist_description').innerHTML = ERROR_PLAYLIST_DESCRIPTION_EMPTY_OR_NO_HTML+'<br>';
      return false;
    }
  frm.submit();
}

function deletePlaylist(playlist_id)
{
  
  if(confirm(ERROR_DELETE_PLAYLIST))
    {

      var user_id = document.getElementById('logged_in_user').value;
      var show_from = document.getElementById('playlist_show_from').value;
      var postData ='playlist_id='+playlist_id+'&playlist_user_id='+user_id+'&playlist_delete=yes&show_from='+show_from;
      var url = system_base_url+'ajax/video/_delete_playlist/' ;
      YAHOO.util.Connect.asyncRequest('POST',url,callback_delete_playlist,postData);	    
      if(env_value == 'Online')
	{
	    pageTracker._trackPageview(url);
	}
    

    }
}

function editPlaylistToshowmessage()
{
    window.location = system_base_url + "ajax/video/_show_playlist";
}

//! Pagination
function pagination(base_url,keyword,show_from)
{           
  var post_data = 'keyword='+keyword+'&paginatioOAn=y'+'&show_from='+show_from;    
  
  YAHOO.util.Connect.asyncRequest('POST',base_url, callback_pagination, post_data);       
        if(env_value == 'Online')
	{
	    pageTracker._trackPageview(base_url);
	}

  
}


// method to delete playlist through jquery
function deletePlaylistJquery(playlist_id)
{
   if(confirm(ERROR_DELETE_PLAYLIST))
    {
      var user_id = document.getElementById('logged_in_user').value;
      var show_from = document.getElementById('playlist_show_from').value;
      var postData ='playlist_id='+playlist_id+'&playlist_user_id='+user_id+'&playlist_delete=yes&show_from='+show_from;
      var url = system_base_url+'ajax/video/_delete_playlist/' ;
      jQuery.post(url,postData,callback_delete_playlistjquery);	    
      if(env_value == 'Online')
	{
	  pageTracker._trackPageview(url);
	}

    }
}
// method that returns data after ajax through jquery
function callback_delete_playlistjquery(data)
{
  document.getElementById('playlist_loading').style.display = 'none';
  
    if(data == 909)
      {
	document.getElementById("playlist_refresh").reload;
	//window.location.href = document.getElementById("playlist_refresh").value;
      }
    else if(data == 903)
      {
	displayError('error_playlist_delete',ERROR_LOGIN_REQUIRED+'<br>');
      }
    else if(data == 904)
      {
	// do redirect 
	window.location.href = system_base_url+'video/_show_playlist_delete';       
      }
    else if(data == 908)
      {
	displayError('error_playlist_delete',ERROR_PLAYLIST_DELETE_FAILURE+'<br>');
      }
     else
      {
	location.reload(true);
	//document.getElementById("playlist_view").innerHTML = '';
	//document.getElementById("playlist_view").innerHTML = data;    		
      }
    var total_playlist_count = document.getElementById("total_playlist_count").value;
    
    if(total_playlist_count == 0)
    {
	setTimeout("editPlaylistToshowmessage()",3000);
    }
  
}
// method to display error to the div
function displayError(divId,errorMessage)
{
  document.getElementById(divId).style.display = "block";
  if(divId == 'error_playlist_delete')
    {
      document.getElementById(divId).innerHTML = errorMessage;
    }
  return false;
}

// method to delete playlist view page directly through jquery
function deletePlaylistJqueryMain(playlist_id)
{
  if(confirm(ERROR_DELETE_PLAYLIST))
    {
      var user_id = document.getElementById('logged_in_user').value;
      var postData ='playlist_id='+playlist_id+'&playlist_user_id='+user_id+'&do_redirect=yes';
      var url = system_base_url+'ajax/video/_delete_playlist/' ;
      jQuery.post(url,postData,callback_delete_playlistJquery);	    
      if(env_value == 'Online')
      {
	  pageTracker._trackPageview(url);
      }

    }
}
