var ytvbp = {};

ytvbp.MAX_RESULTS_LIST = 10;
ytvbp.MAX_FEATURED_RESULTS_LIST = 12;

ytvbp.PREVIOUS_PAGE_BUTTON = 'previousPageButton';


ytvbp.NEXT_PAGE_BUTTON = 'nextPageButton';


ytvbp.VIDEO_LIST_CONTAINER_DIV = 'searchvids';
ytvbp.FEATURED_VIDEO_LIST_CONTAINER_DIV = 'featuredvids';

ytvbp.VIDEO_PLAYER_DIV = 'searchvideoPlayer';
ytvbp.FEATURED_VIDEO_PLAYER_DIV = 'featuredvideoPlayer';

ytvbp.MAIN_SEARCH_CONTAINER_DIV = 'mainSearchBox';


ytvbp.TOP_SEARCH_CONTAINER_DIV = 'searchBox';


ytvbp.nextPage = 2;


ytvbp.previousPage = 0;


ytvbp.previousSearchTerm = '';


ytvbp.previousQueryType = 'all';


ytvbp.listVideos = function(queryType, searchTerm, page) {
  ytvbp.previousSearchTerm = searchTerm; 
  ytvbp.previousQueryType = queryType; 
  var maxResults = ytvbp.MAX_RESULTS_LIST;
  var startIndex =  (((page - 1) * ytvbp.MAX_RESULTS_LIST) + 1);
  ytvbp.presentFeed(queryType, maxResults, startIndex, searchTerm);
  ytvbp.updateNavigation(page);
};

ytvbp.listFeaturedVideos = function(queryType, searchTerm, page) {
  ytvbp.previousSearchTerm = searchTerm; 
  ytvbp.previousQueryType = queryType; 
  var maxResults = ytvbp.MAX_FEATURED_RESULTS_LIST;
  var startIndex =  (((page - 1) * ytvbp.MAX_FEATURED_RESULTS_LIST) + 1);
  ytvbp.presentFeaturedFeed(queryType, maxResults, startIndex, searchTerm);
  ytvbp.updateNavigation(page);
};


ytvbp.sendRequest = function(filePath, params, resultDivName) {
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
        
  xmlhr.open('POST', filePath, true);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

  xmlhr.onreadystatechange = function() {
    var resultDiv = document.getElementById(resultDivName);
    if (xmlhr.readyState == 1) {
      resultDiv.innerHTML = '<b>Loading...</b>'; 
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
        var data = xmlhr.responseText;
        if(data.indexOf("videoList") != -1) {
          var start = data.indexOf("presentVideo");
          start = start + 12 + 2;
          var end = data.indexOf(")");
          end = end - 1;
          var videoid = data.substring(start,end);
          //alert(videoid);
          ytvbp.loadvideo(videoid);
        }
      }
    } else if (xmlhr.readyState == 4) {
      //alert('Invalid response received - Status: ' + xmlhr.status);
    }
  }
  xmlhr.send(params);
}

ytvbp.sendRequest2 = function(filePath, params, resultDivName) {
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
        
  xmlhr.open('POST', filePath, true);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

  xmlhr.onreadystatechange = function() {
    var resultDiv = document.getElementById(resultDivName);
    if (xmlhr.readyState == 1) {
      resultDiv.innerHTML = '<b>Loading...</b>'; 
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
        var data = xmlhr.responseText;
        if(data.indexOf("videoList") != -1) {
          var start = data.indexOf("presentFeaturedVideo");
          start = start + 20 + 2;
          //alert(start);
          var end = data.indexOf(")");
          end = end - 1;
          //alert(end); 
          var videoid = data.substring(start,end);
          //alert(videoid);
          ytvbp.loadFeaturedvideo(videoid);
        }
      }
    } else if (xmlhr.readyState == 4) {
      //alert('Invalid response received - Status: ' + xmlhr.status);
    }
  }
  xmlhr.send(params);
}


ytvbp.presentVideo = function(videoId) {
  var params = 'queryType=show_video&videoId=' + videoId;
  var filePath = 'index.php';
  ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_PLAYER_DIV);
}

ytvbp.presentFeaturedVideo = function(videoId) {
  var params = 'queryType=show_video&featured=1&videoId=' + videoId;
  var filePath = 'index.php';
  ytvbp.sendRequest(filePath, params, ytvbp.FEATURED_VIDEO_PLAYER_DIV);
}

ytvbp.presentFeed = function(queryType, maxResults, startIndex, searchTerm){
  var params = 'queryType=' + queryType + 
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex + 
               '&searchTerm=' + searchTerm;
  var filePath = 'index.php';
  ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_LIST_CONTAINER_DIV);
}

ytvbp.presentFeaturedFeed = function(queryType, maxResults, startIndex, searchTerm){
  var params = 'queryType=' + queryType + 
               '&maxResults=' + maxResults +
               '&startIndex=' + startIndex + 
               '&searchTerm=' + searchTerm +
               '&featured=1';
  var filePath = 'index.php';
  ytvbp.sendRequest2(filePath, params, ytvbp.FEATURED_VIDEO_LIST_CONTAINER_DIV);
}

ytvbp.updateNavigation = function(page) {
  ytvbp.nextPage = page + 1;
  ytvbp.previousPage = page - 1;
  document.getElementById(ytvbp.NEXT_PAGE_BUTTON).style.display = 'inline';
  document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).style.display = 'inline';
  if (ytvbp.previousPage < 1) {
    document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = true;
  } else {
    document.getElementById(ytvbp.PREVIOUS_PAGE_BUTTON).disabled = false;
  }
  document.getElementById(ytvbp.NEXT_PAGE_BUTTON).disabled = false;
};


ytvbp.hideMainSearch = function() {
  document.getElementById(ytvbp.MAIN_SEARCH_CONTAINER_DIV).style.display = 
      'none';
  document.getElementById(ytvbp.TOP_SEARCH_CONTAINER_DIV).style.display = 
      'inline';
};


ytvbp.queryTypeChanged = function(queryType, searchTermInputElement) {
  if (queryType != 'all') {
    searchTermInputElement.value = '';
  }
};

ytvbp.loadvideo = function (videoId) {
  var params = 'queryType=show_video&load=1&videoId=' + videoId;
  var filePath = 'index.php';
  ytvbp.sendRequest(filePath, params, ytvbp.VIDEO_PLAYER_DIV);
};

ytvbp.loadFeaturedvideo = function (videoId) {
  var params = 'queryType=show_video&featured=1&load=1&videoId=' + videoId;
  var filePath = 'index.php';
  ytvbp.sendRequest(filePath, params, ytvbp.FEATURED_VIDEO_PLAYER_DIV);
};