var currentSection = 'joblo';
var currentTag = 'joblo';
var currentSet = 1;
var maxSets = 5;

var tagLink = new Array();
tagLink['Money Shots'] = 'money shots';
tagLink['The JoBlo Movie Show'] = 'joblo movie show';
tagLink['JoBlo.com Quotes'] = 'joblo.com quote';
tagLink['Comic Con'] = 'comic con';
tagLink['Hottie Clip of the Day'] = 'hottie clip of the day';
tagLink['Hottie of the Month'] = 'hottie of the month';
tagLink['Photo Shoots'] = 'photo shoot';
tagLink['Kill of the Day'] = 'kill of the day';
tagLink['Horror Whores'] = 'horror whores';
tagLink['Oldies but Goodies'] = 'oldies but goodies';
tagLink['Awfully Good Clips'] = 'awfully good';
tagLink['Deleted Scenes'] = 'deleted scene';
tagLink['Video Games'] = 'video game';


$(document).ready(function() {
  //loadPreferredTab();

  // homepage slider buttons - NEXT
  $('#containerCatVids .next a').click(function() { 
    if ((currentSet + 1 <= $('#numPages').val()) && (currentSet == maxSets)) { 
      window.location='/video/video_search.php?tag=' + $('#searchTag').val() + '&from=' + ((maxSets + 1) * 12);
    } else { 
      slideIt('#containerCatVids','next');
      currentSet += 1;
      $('.videoPageCount').html('Page ' + currentSet + ' of ' + $('#numPages').val()); 
    }
  });

  // homepage slider buttons - PREV
  $('#containerCatVids .prev a').click(function() { 
    currentSet = currentSet - 1;
    slideIt('#containerCatVids','prev');
    $('.videoPageCount').html('Page ' + currentSet + ' of ' + $('#numPages').val()); 
  });

  // homepage category buttons
  $('#catNav .main').click(function(){ 
    if ($(this).find('a').html() == 'Show All') { 
      $('#catNav .subnav').show();
    } else { 
      $('#catNav .subnav').hide('fast');
    }
    $(this).next('.subnav').show();
    $(this).next('.subnav').find('li:first a').addClass('active');
    $('#catNav .main a').removeClass('active');
    $(this).find('a').addClass('active');


    $(this).find('.subnav a').removeClass('active');

    if ($(this).find('a').html() == 'Movie Hotties') { currentSection = 'moviehotties'; }
    else if ($(this).find('a').html() == 'Arrow In The Head') { currentSection = 'arrow'; }
    else if ($(this).find('a').html() == 'Digital Dorm') { currentSection = 'dvdclinic'; }
    else if ($(this).find('a').html() == 'JoBlo') { currentSection = 'joblo'; }

    var newSet = loadClips('joblo', currentSection, 0);
    currentTag = currentSection;
    $('#containerCatVids .slider').html(newSet);

    // reset the right-side drop-down
    $('.dropdown dd a').each(function(){ 
      if ($(this).html() == 'Latest') { $(this).click(); }
    });
    

  });


  // click the right category if required
  var urlVars = getUrlVars();
  if (urlVars['section'] != '') { 
    var tmp = 'li.' + urlVars['section'] + " a";
    $(tmp).click();
  }


  // category subnav buttons
  $('#catNav .subnav li a').click(function(){	
    if ($(this).html() == 'All') { 
      currentTag = currentSection;
    } else{  
      currentTag = tagLink[$(this).html()];
    }
    var newSet = loadClips('joblo', currentTag, 0);
    //var newCount = numClips(currentTag);

    $('#containerCatVids .slider').html(newSet);

    currentSet = 1;

    $('#numPages').val($('#newNumPages').val());
    $('.videoPageCount').html('Page '  + currentSet + ' of ' + $('#newNumPages').val()); 
    $('#containerCatVids .prev').hide();

    $('.subnav li a').removeClass('active');
    $(this).addClass('active');

  });

});


/* ------------------------------------------------------- */
function setPreferredTab() { 
  var mainTab = '';
  var subnavTab = '';
  // figure out the currently active tab
  $('#catNav').find('.active').each(function(){ 
    if ($(this).parent().hasClass('main')) { mainTab = $(this).html(); }
    else { subnavTab = $(this).html(); }
  });

  // set the cookie
  var tmp = mainTab + "::" + subnavTab;
  $.cookie('jobloVideoTab', tmp, { expires: 365, path: '/', domain: 'joblo.com' });

  //alert('Active tab saved, you\'ll see this tab from now when you go to this page');
  alert('tab set ' + tmp);
}


/* ------------------------------------------------------- */
function loadPreferredTab() {
  var tabInfo = '';


  if ($.cookie('jobloVideoTab') != '') { 
    tabInfo = $.cookie('jobloVideoTab');
    if ((tabInfo != '') && (tabInfo != null)) { 
      //console.log(tabInfo);
      alert('loading preferred tab ' + tabInfo);
      var parts = tabInfo.split('::');
      var tmp = '';
      var currentSection = '';
      var findTag = '';

      $('.subnav').hide();

      // highlight the 'lock' button
      $('.buttonLock a').addClass('active');

      // set the right main level tab
      $('#catNav').find('.main').each(function(){
	if ($(this).find('a').html() == parts[0]) {	  
	  $(this).addClass('active');
	  $(this).find('a').addClass('active');

	  if ($(this).find('a').html() == 'Movie Hotties') { currentSection = 'moviehotties'; }
	  else if ($(this).find('a').html() == 'Arrow In The Head') { currentSection = 'arrow'; }
	  else if ($(this).find('a').html() == 'Digital Dorm') { currentSection = 'dvdclinic'; }
	  else if ($(this).find('a').html() == 'JoBlo') { currentSection = 'joblo'; }

	  tmp = 'ul.' + currentSection;
	  $(tmp).show();
	} else { 
	  $(this).removeClass('active');
	}
      });

      if (parts[1] == 'All') { 
	findTag = currentSection;
      }
      tmp = 'ul.' + currentSection;
      $(tmp).find('li').each(function(){ 
	if ($(this).find('a').html() == parts[1]) { 
	  $(this).addClass('active');
	  $(this).find('a').addClass('active');
	  findTag = tagLink[$(this).find('a').html()];
	} else { 
	  $(this).find('a').removeClass('active');
	}
      });
      
      // load the relevent clips
      var newSet = loadClips(currentSection, findTag,  0);
      $('#containerCatVids .slider').html(newSet);
    }
  }
}




/* ------------------------------------------------------- */
function getUrlVars() {
  var vars = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}

