/* ################################################################# */
/*                                                                   */
/*  TITLE:        NBC_VIDEO.JS                                       */
/*  VERSION:      2.00                                               */
/*  LAST UPDATED: 9/10/2009                                          */
/*  UPDATED BY:  Nekoro                                              */
/*                                                                   */
/*  NAMESPACE: NBC                                                   */
/*                                                                   */
/*  PUBLIC FUNCTIONS:                                                */
/*                                                                   */
/*  PREREQUISTES:                                                    */
/*                                                                   */
/* ################################################################# */

NBC.Video = function() {
  this.ini = function() {    
     
    // initiatest poll function  
      $('a.video').click(function(event) {
         var sortType = "";
         sortType = $("#filter").html();
         var section = $("#searchSection").html();
         var maxItems = $("#maxItems").html();
         maxItems = trim(maxItems);
         var showPage = $("#showPage").html();
         var UrlArr = window.location.href.split('/'); // gets domain         
         var url = 'http://' + UrlArr[2] + '/i/dispatcher/?command=GetVideo&action=getcontent&section='+section+'&sort='+sortType.toLowerCase()+'&items='+maxItems+'&showPage='+showPage;
         getData(url);  
         $("#currFilter span").removeClass('dd-interesting');
         $("#currFilter span").removeClass('dd-recent');
         $("#currFilter span").addClass('dd-' + sortType);      
      })   
    }  
    
    $("#subnav_filter").change(function() {
     $(this).find("option:selected").each(function () {
        var sortType ="";
        sortType = $(this).text();
        var section = $("#searchSection").html();
        var maxItems = $("#maxItems").html();
        maxItems = trim(maxItems);
        var showPage = $("#showPage").html();
        var UrlArr = window.location.href.split('/'); // gets domain          
      var url = 'http://' + UrlArr[2] + '/i/dispatcher/?command=GetVideo&action=getcontent&section='+section+'&sort='+sortType.toLowerCase()+'&items='+maxItems+'&showPage='+showPage;
      getData(url);
     })
    })    
  
    // initiatest poll function  
    $('a.showvideo').click(function(event) {
         var pagesBefore = 3;
         var pagesAfter = 3;
         var page = $("#vidPage").html();
         var maxPage = $("#maxVidPage").html();
         var type = $("#type").html();
         var category = $("#category").html();
         var sectionId = $("#section_id").html();
         var UrlArr = window.location.href.split('/'); // gets domain         
         var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=video&action=getshowcontent&page='+page+'&type='+type+'&category='+category+"&sectionId="+sectionId;
         getShowData(url, type, page);   
     })   
     
         // initiatest poll function  
    $('a.shownonvideo').click(function(event) {
         var page = $("#nonVidPage").html();
         var type = $("#type").html();
         var maxPage =$("#maxNonVidPage").html();
         var sectionId = $("#section_id").html();
         var UrlArr = window.location.href.split('/'); // gets domain         
         var url = 'http://' + UrlArr[2] + '/i/dispatcher/?h=video&action=getshowcontent&page='+page+'&type='+type+'&sectionId='+sectionId;
         getShowData(url, type, page);  
         
         page = parseInt(page);
         if( page == 1 ){           
           document.getElementById('page_nonvideo_prev').style.display = "none";
         } else {
           document.getElementById('page_nonvideo_prev').style.display = "block";
         }
         
         //next page link
         if( page == maxPage ){
           document.getElementById('page_nonvideo_next').style.display = "none";
         } else {
           document.getElementById('page_nonvideo_next').style.display = "block";
         }
     })  
  
  function setFilter2(filval) {
    filter = filval;
    $("filter").html(filter);
      var section = $("#searchSection").html();
      var maxItems = $("#maxItems").html();
      var showPage = $("#showPage").html();
      maxItems = trim(maxItems);
      var UrlArr = window.location.href.split('/'); // gets domain          
      var url = 'http://' + UrlArr[2] + '/i/dispatcher/?command=GetVideo&action=getcontent&section='+section+'&sort='+filter+'&items='+maxItems+'&showPage='+showPage;
      getData(url);
      runSubjax();
      $("#currFilter span").removeClass('dd-interesting');
      $("#currFilter span").removeClass('dd-recent');
      $("#currFilter span").addClass('dd-' + filter);
  }
    
    function getData(url){
      $.ajax({
           type: "GET",
           url: url,
           dataType: "html",
           success: function(data) {
            $("#video_additional").html(data);
           }
         });      
         runSubjax();
    }
    
    function getShowData(url, type, currentPage){
      var result = "";
      $.ajax({
           type: "GET",
           url: url,
           dataType: "html",
           success: function(data) {
             result = data;
             
             if (type == 'video'){
	             var dataSplit = result.split("|||")
		           var estimatedPages = dataSplit[0];
		           U.log("estPages="+dataSplit[0]);
		           $("#video_additional").html(dataSplit[1]);
		         
		           currentPage = parseInt(currentPage); 
		         
		           //previous page link
		           if( currentPage == 1 ){
		             document.getElementById('page_prev').style.display = "none";
		           } else {
		             document.getElementById('page_prev').style.display = "block";
		           }
		         
		           //next page link
		           estimatedPages = parseInt(trim(estimatedPages));
		           U.log("EstimatedPages="+estimatedPages);
		           if( currentPage == estimatedPages ){
		             document.getElementById('page_next').style.display = "none";
		           } else {
		             document.getElementById('page_next').style.display = "block";
		           }
	           } else {
	             var dataSplit = result.split("|||")
		           var estimatedPages = dataSplit[0];
		           U.log("estPages="+dataSplit[0]);
		           $("#spotlightContent").html(dataSplit[1]);
		         
		           currentPage = parseInt(currentPage); 
	           }
	        }
       });  
    }
    
    function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
  }
} // end of NBC Video 
