var have_subnav = false;
var have_tertiary = false;

function highlight_location() {
  var channel = '';
  try {
    channel = window.location.href.split('/')[3];
    if (channel == 'extras') {
      channel = window.location.href.split('/')[4];
    }
    var att = '/images/banners/'+channel+'.jpg';
    $('#banner').attr('src',att);
  } catch (err) {}
  $('#primary_nav a').each(function(i){
    var a = $(this);
    var href = a.attr('data-link');
    if(href!='/' && window.location.href.indexOf(href)>-1){
      a.addClass('current_page');
    }
  });
  $('#secondary_nav a').each(function(i){
    var a = $(this);
    var secondary_link = a.attr('data-link');
    if(window.location.href.indexOf(secondary_link)>-1){
      a.addClass('current_page');
      // test for tertiary nav
      var selector = '#all_nav > ul > li > ul > li > a[href="' + secondary_link + '"] ~ ul > li > a';
      var tertiary = $(selector);
      tertiary.each(function(t){
        var tclone = $(this).clone();
        $("#tertiary_nav").append(tclone);
        have_tertiary = true;
      });
    }
  });
  if(have_tertiary){
    $('#tertiary_nav a').each(function(i){
      var a = $(this)
      if(window.location.href.indexOf(a.attr('href'))>-1){
        a.addClass('current_page');
      }
    });
  }
}

function get_subnav(link) {
  var secondary = $('#secondary_nav');
  var subnavs = link.find('~ ul > li > a');
  if(subnavs.size()>1){
    subnavs.each(function(i){
      var tsecondary = $(this);
      var styled = tsecondary.clone();
      styled.attr('data-link',tsecondary.attr('href'));
      styled.addClass('secondary_nav_link');
      // check for a tertiary child; link to it if it exists
      var tlink = tsecondary.find('~ ul > li > a').first().attr('href');
      if(tlink != null){
        styled.attr('href',tlink);
      }
      secondary.append(styled);
    });
  }
  if(have_subnav){
    highlight_location();
  }
  have_subnav = true;
}

function create_navigation() {
  var primary = $('#primary_nav')
  $('#all_nav > ul > li > a').each(function(i){
    var styled = $(this).clone();
    var link = styled.attr('href');
    if(link!='/extras' && link!='/homepage'){
      styled.attr('data-link',link);
      if(link=='/homepage'){
        styled.attr('href','/');
      } else {
        // link to first child, not the actual collection
        var href = $(this).find('~ ul > li > a').first().attr('href');
        styled.attr('href',href);

        // check for a tertiary child; link to it if it exists
        var tlink = $(this).find('~ ul > li > ul > li > a').first().attr('href');
        if(tlink != null){
          styled.attr('href',tlink);
        }
      }
      if(window.location.href.indexOf(link)>-1 && link.indexOf('photo-gallery')==-1){
        get_subnav($(this));
      }
      primary.append(styled);
    }
  });
  if(!have_subnav){
    var secondary = $('#secondary_nav');
    secondary.append('&#160;');
  }
  var restated = $('#restated_nav');
  $('#primary_nav a').each(function(i){
    restated.append($(this).clone())
  });
  primary.append('<a href="http://www.myvetdirect.com/hollywoodhillah/">Pharmacy</a>');
  restated.append('<a href="/extras/site-map">Site Map</a>');
  restated.append('<a href="/extras/terms-of-use">Terms of Use</a>');
  highlight_location();
  if(have_tertiary){
    var tertiary = $("#tertiary_nav");
    $("#band_2").prepend(tertiary);
    tertiary.addClass("visible_tertiary");
    $("#col_single").addClass("right_of_tertiary");
  }
}

function draw_decorations() {
  $('#copy').append("<div class='bottom_whitespace'></div>");
  var bottom_decoration = $('#bottom_decoration')
  var bottom_decoration_img = bottom_decoration.attr('data-image');
  if(bottom_decoration_img && bottom_decoration_img != ''){
    bottom_decoration.append("<img src='" + bottom_decoration_img + "' class='bottom_decoration'/>");
  }
}

$(document).ready(function() {
  create_navigation();
  draw_decorations();
});

