var $webcast = {};


$webcast.startup = function(mode) {

  $webcast.featureThumbnails = $('.feature-item');
  $webcast.morefeature = $('#morefeature ul');
  
  $webcast.thumbnails = $('.grid-pane');
  $webcast.morepages = $('#morepages ul');
  
  $webcast.select = $('#sort-webcast');
  
  $webcast.search = $('#search-webcast');

  
  var category_lookup = Array();
  $webcast.thumbnails.each(function(index, el) {
    
    var category_list = $(el).children('.category').text().split('|');
    
    
    for (var i = 0; i < category_list.length; i++) {
      var category = jQuery.trim(category_list[i]);
      if (!category_lookup[category] && category != '') {
        category_lookup[category] = {};
        $webcast.select.append('<option>' + category + '</option>');
      }
    }

  });
  
  document.getElementById('sort-webcast').selectedIndex = 0;
  
  
  $webcast.select.change(function() {
    var selectedText = this.options[this.selectedIndex].text;
    if (selectedText == 'Select All') {
      $webcast.setup('all');
    } else {
      $webcast.setup('category', selectedText);
    }
  });
  
  $webcast.search.submit(function() {
    var search_value = $(this).children('.search-input').attr('value');
    $webcast.setup('search', search_value);
    return false;
  });
  
  
  $webcast.setup('all');
  
  
  
  $webcast.featureThumbnails.each(function(index, el) {
    

    var li = document.createElement('li');
    li.featureRef = this;
    li.appendChild(document.createTextNode(index + 1));
    li.onclick = function() {
      $webcast.currentFeatureRef.style.display = 'none';
      $webcast.currentFeatureIcon.className = '';
      this.featureRef.style.display = 'block';
      $webcast.currentFeatureRef = this.featureRef;
      $webcast.currentFeatureIcon = this;
      this.className = 'selected';
    }
    $webcast.morefeature.append(li);
    
    if (index == 0) {
      $webcast.currentFeatureRef = el;
      $webcast.currentFeatureIcon = li;
      $webcast.currentFeatureRef.style.display = 'block';
      li.className = 'selected';
    }
    
  });


}


$webcast.setup = function(mode, param) {

  
  $webcast.pages = Array();
  $webcast.morepages.empty();
  
  var li = document.createElement('li');
  li.className = 'grouptitle';
  li.appendChild(document.createTextNode('Pages'));
  $webcast.morepages.append(li);
  
  
  /* build first page */
  var page_no = $webcast.pages.length;
  $webcast.pages[page_no] = {};
  $webcast.pages[page_no].el_arr = Array();

  /* insert page link */
  var page_li = document.createElement('li');
  page_li.pageNumber = page_no;
  page_li.appendChild(document.createTextNode(page_no + 1));
  page_li.onclick = $webcast.pagelinkclick;
  $webcast.pagelink = page_li;
  page_li.className = 'selected';
  
  
  
  
  $webcast.morepages.append(page_li);
  
  var i = 0;
  var checkPage = false;

  $webcast.thumbnails.each(function(index, el) {
    /* create new page link only if current page is full */
    if (i % 6 == 0 && i != 0 && checkPage) {

      page_no = $webcast.pages.length;
      $webcast.pages[page_no] = {};
      $webcast.pages[page_no].el_arr = Array();

      /* build the page links */
      page_li = document.createElement('li');
      page_li.pageNumber = page_no;
      page_li.appendChild(document.createTextNode(page_no + 1));
      
      page_li.onclick = $webcast.pagelinkclick;
      
      $webcast.morepages.append(page_li);
      checkPage = false;
    }
    
    
    if (mode == 'all') {
      $webcast.pages[page_no].el_arr.push(el);
      i++;
      checkPage = true;
    }
    

    if (mode == 'category' && $(el).children('.category').text().indexOf(param) != -1) {
      //console.log(i + ' ::> ' + $(el).children('.category').text() + ' == ' + param);
      $webcast.pages[page_no].el_arr.push(el);
      //console.log($webcast.pages[page_no].el_arr.length);
      i++;
      checkPage = true;
      
    } 
    
    if (mode == 'search' && param && param != '' && $(el).text().toLowerCase().indexOf(param.toLowerCase()) != -1) {
      $webcast.pages[page_no].el_arr.push(el);
      i++;
      checkPage = true;
    }
    
    
  })
  
  if (i > 0) {
    /* show the first page link */
    $webcast.showPage(0);
    
    /* remove last page if there are no thumbnails in it */
    if ($webcast.pages[$webcast.pages.length - 1].el_arr.length == 0) {
      page_li.parentNode.removeChild(page_li);
      
    }
    
  } else {
    
    if (mode == 'search' && !param) {
      $webcast.setup('all');
    } else {
      $webcast.morepages.empty();
      $webcast.hideAllThumbnails();
    }
  }

  


}


$webcast.pagelinkclick = function() {
  $webcast.pagelink.className = '';
  this.className = 'selected';
  $webcast.pagelink = this;
  
  $webcast.showPage(this.pageNumber);
}

$webcast.hideAllThumbnails = function() {
  $webcast.thumbnails.each(function() {
    this.style.display = 'none';
  });  
}

$webcast.showPage = function(index) {

  $webcast.hideAllThumbnails();
  
  
  var arr = $webcast.pages[index].el_arr;
  
  for (var i = 0; i < arr.length; i++) {
    arr[i].style.display = 'block';
  }

}



$(document).ready($webcast.startup);