function setZIndex() {
  $(".firstSource, .secondSource, .firstImage, .secondImage").click(function(event) {
    if (this.id.length == 9) {
      var subLength = 6;
    } else {
      var subLength = 7;
    }
    var contentId = this.id.substr(subLength);
    var contentId = contentId.split("-");
    //Check if the images are shown, if not slide them open
    $("#frontImage"+contentId[0]).show("slow");
    var newImageId = "#image-"+contentId[0]+"-"+contentId[1];
    if (contentId[1] == 1) {
      var oldImageId = "#image-"+contentId[0]+"-2";
    } else {
      var oldImageId = "#image-"+contentId[0]+"-1";
    }
    $(newImageId).css({'z-index' : '2'});
    $(oldImageId).css({'z-index' : '1'});    
    event.preventDefault();
  });
}

function showContent() {
  $(".iconQuote, .iconExtra, .iconContent").click(function(event) {
    if (this.className == "iconContent") {
      var substrCount = 13;
    } else {
      var substrCount = 5;
    }
    var contentId = this.id.substr(substrCount);
    $("#icon-content-"+contentId).toggle();
    event.preventDefault();
  });
}

function contactSlide() {
  $('#contactSlides').cycle({ timeout: 650, speed: 1500 });
}

function openLinks() {
  $(document).ready(function() {
     $("a[href^=http]").each(function(){
        if(this.href.indexOf(location.hostname) == -1) {
           $(this).attr({
              target: "_blank",
              title: "Opens in a new window"
           });
        }
     })
  });
}

function hideIcon() {
  $("ul#portfolio li a").mouseenter(function(){
    if (this.id != "listSelected") {
      var iconElement = $("#iconSelected");
      if (iconElement.css("opacity") == 1) {
        iconElement.fadeOut("slow");
      }
    }
  });
  $("ul#portfolio").mouseleave(function(){
    var iconElement = $("#iconSelected");  
    if (iconElement.css("display") == "none") {
      iconElement.fadeIn("slow");
    }  
  });
}

$(document).ready(function() {
  setZIndex();
  showContent();
  hideIcon();
  contactSlide();
  openLinks();
});

