

$(document).ready(function() {
  // remove any target attr
  $('a').removeAttr('target');

  // open all external links and pdf's in new window
  $('a').click(function(){
    // don't override any onclick behaviour
    if ($(this).attr('onclick')) {
      return true;
    }
    // remove any target attribute
    if ($(this).attr('target') == '_blank') {
      $(this).removeAttr('target');
    }

    var href = $(this).attr('href');
    var host = window.location.hostname;
    // remove www. from links so hard-coded urls work on either
    if (host.indexOf('www.') > -1) {
      host = host.substr(4);
    }

    var external = ((href.indexOf('http://') == 0) && (href.indexOf(host) == -1));
    var pdf = href.indexOf('.pdf');

    if (external || pdf > -1) {
      window.open(this.href);
      return false;
    }
    return true;
  });

  //homepage feature box links click
  $("#main-img-links li.why-wood a").click(function(){
      $("#main-img-links ul").removeClass("what how");
      $("#main-img-links ul").addClass("why");
      $("#main-img p").html('<p>.</p>');
      $("#sub-img").html('<p>.</p>');
      $("#feature-box #text-area").html('<h1>.</h1><p>.</p><p class="float-right"><a href="http://www.nzwood.co.nz/why-wood/" title="View more information" class="view">View more information</a></p>');
      return false;
  });
  $("#main-img-links li.what-wood a").click(function(){
      $("#main-img-links ul").removeClass("why how");
      $("#main-img-links ul").addClass("what");
      $("#main-img p").html('<p>.</p>');
      $("#sub-img").html('<p>.</p>');
      $("#feature-box #text-area").html('<h1>.</h1><p>.</p><p class="float-right"><a href="http://www.nzwood.co.nz/what-wood/" title="View more information" class="view">View more information</a></p>');
      return false;
  });
  $("#main-img-links li.how-wood a").click(function(){
      $("#main-img-links ul").removeClass("why what");
      $("#main-img-links ul").addClass("how");
      $("#main-img p").html('<p>.</p>');
      $("#sub-img").html('<p>.</p>');
      $("#feature-box #text-area").html('<h1>.</h1><p>.</p><p class="float-right"><a href="http://www.nzwood.co.nz/how-wood/" title="View more information" class="view">View more information</a></p>');
      return false;
  });


  //homepage species / vendors box
  $("#species-tab a").click(function(){
      $("#vendors-tab").removeClass("current");
      $("#species-tab").addClass("current");
      $("#vendors-box").css("display", "none");
      $("#species-box").css("display", "block");
      return false;
  });

  $("#vendors-tab a").click(function(){
      $("#species-tab").removeClass("current");
      $("#vendors-tab").addClass("current");
      $("#vendors-box").css("display", "block");
      $("#species-box").css("display", "none");
      return false;
  });

  //homepage toolkits
  $(".recommended-link").click(function(){
      $("#recommended li").removeClass("current").show("slow");
      $(this).parent().addClass("current").show("slow");
      return false;
  });

  //tabbed content
  $("#tab-1").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-1").css("display", "block");
      $("#tab-2").removeClass("current");
      $("#tab-1").addClass("current");
      return false;
  });



  $("#tab-1").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-1").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-1").addClass("current");
      return false;
  });

  $("#tab-2").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-2").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-2").addClass("current");
      return false;
  });

  $("#tab-3").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-3").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-3").addClass("current");
      return false;
  });

  $("#tab-4").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-4").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-4").addClass("current");
      return false;
  });


  $("#tab-5").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-5").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-5").addClass("current");
      return false;
  });


  $("#tab-6").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-6").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-6").addClass("current");
      return false;
  });

  $("#tab-7").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-7").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-7").addClass("current");
      return false;
  });

  $("#tab-8").click(function(){
      $(".characteristics-box").css("display", "none");
      $("#tabbed-content-8").css("display", "block");
      for(j=1; j<9; j++) {
        $("#tab-" + j).removeClass("current");
      }
      $("#tab-8").addClass("current");
      return false;
  });

  //site map
  $("a.toggle").click(function(){
    if($(this).parent().attr("class") == "closed") {
      $(this).parent().removeClass("closed");
      $(this).parent().addClass("open");
      $(this).removeClass("plus");
      $(this).addClass("minus");
    } else {
      $(this).parent().removeClass("open");
      $(this).parent().addClass("closed");
      $(this).removeClass("minus");
      $(this).addClass("plus");
    }
      return false;
  });


  $("#search-tab").click(function(){
      $("#search-tab").parent().addClass("current");
      $("#browse-tab").parent().removeClass("current");
      $("#search-box").css("display", "block");
      $("#browse-box").css("display", "none");
      return false;
  });

  $("#browse-tab").click(function(){
      $("#browse-tab").parent().addClass("current");
      $("#search-tab").parent().removeClass("current");
      $("#browse-box").css("display", "block");
      $("#search-box").css("display", "none");
      return false;
  });

  /**
   * Keyword search
   */
  if ($('input#vendor').length > 0) {
    // set default prompt text
    var defaultValue = 'Keyword';
    if ($('#vendor').attr('value') == '' || $('#vendor').attr('value') == undefined) {
      $('#vendor').attr('value', defaultValue).css('color', '#999');
    }

    // highlight text
    $('#vendor')
    .focus(function() {
      this.style.color = "#333";
      if (this.value == defaultValue) {
        this.value = "";
      } else {
        this.select();
      }
    })
    .blur(function() {
      if (this.value == "") this.value = defaultValue;
      if (this.value == defaultValue) this.style.color = "#999";
    });
    
    // replace defaultValue on submission
    $('.search-filter input.submit-button').click(function() {
      if ($('#vendor').attr('value') == defaultValue) {
        $('#vendor').attr('value', '');
      }
      $('form').submit();
      return false;
    });
  }

  /**
   * Dynamic dropdown filters
   */
  if ($('select[name="filter0"]').length > 0) {
    $('select[name="filter0"], select[name="filter1"]').each(function() {
      var n = $(this).attr('name');
      if ($_GET[n] != 0) {
        updateFilter(n, $_GET[n]);
      }
    });

    $('select[name="filter0"], select[name="filter1"]').bind('change', function() {
      var c = $(this).val();
      updateFilter($(this).attr('name'), c);
    });
  }

  function updateFilter(filter, parent) {
    var target = 'filter2';
    
    // remove existing categories and disable filters
    if (filter == 'filter0') {
      $('select[name="filter1"] option').not(':first-child').remove();
      $('select[name="filter1"]').attr('disabled', true);
      target = 'filter1';
    }
    $('select[name="filter2"] option').not(':first-child').remove();
    $('select[name="filter2"]').attr('disabled', true);

    // populate target filter
    for (var c in categories[parent]) {
      var data = categories[parent][c];
      var selected = ($_GET[target] == data.cat_id) ? 'selected="selected" ': '';
      var option = '<option ' + selected + 'value="' + data.cat_id + '">' + data.cat_name + '</option>';
      $('select[name="' + target + '"]').append(option);
    }

    // enable filters
    if ($('select[name="filter1"] option').length > 1) {
      $('select[name="filter1"]').removeAttr('disabled');
    }
    if ($('select[name="filter2"] option').length > 1) {
      $('select[name="filter2"]').removeAttr('disabled');
    }

  }



  // Lightbox for image gallery
  if(jQuery().lightBox) {
    $('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
  }

  //show flash story
  $(".story").click(function(){
     //var attr = "width=515,height=295,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no";
     //storyWindow = window.open("story.html","storyWindow",attr);
     //storyWindow.moveTo(0,0);

     var w = document.body.clientWidth;
     var h = document.body.clientHeight;
     var l = Math.round(w/2 - 250);

     $("#flashcontent").hide();
     $("#shade").css("height",h).css("width",w).show();
     $("#storywindow").css("left",l).show();
     return false;
  });

  $("#closestory, #shade").click(function(){
     $("#flashcontent").show();
     $("#shade").hide();
     $("#storywindow").hide();
  });


}); // close $(document).ready




//case studies
function showCaseStudy(id, total) {
  for(x=1; x<=total; x++) {
    $('#case-study-'+x).css("display", 'none');
  }
  $('#case-study-'+id).css("display",'block');
}

function debug(data) {
  if (window.console) {
    console.log(data);
  }
}



