var idx = 0;
var clicked = null;
var caseInterval = 0;

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
    
$(window).load(function() {
  showOverlayFromQueryString();
});

function showOverlayFromQueryString() {
  var url = window.location;
  if(url.toString().indexOf("showOverlayId") > -1) {
    var nodeId = getParameterByName("showOverlayId");
    $("#caseThumb" + nodeId).click();
  }
}
//alert(window.location.indexOf("showOverlayId"));
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function loadLightBoxContentByIndex(index)
{
    $("#lightbox h2").text(nodes[index].lightboxTitle);
    $("#lightbox h3").text(nodes[index].lightboxSubTitle);
    // Assume we have at least one image
    $(".imageRotator").html("");
    $(".imageRotator").append('<li class="active"><img class="lightboxImage" src="' + nodes[index].lightboxImage + '" alt="' + nodes[index].lightboxTitle + '" /></li>');

    if (nodes[index].lightboxImage2 != "")
    {
        $(".imageRotator").append('<li><img style="display:none;" class="lightboxImage" src="' + nodes[index].lightboxImage2 + '" alt="' + nodes[index].lightboxTitle + '" /></li>');
    }
    if (nodes[index].lightboxImage3 != "")
    {
        $(".imageRotator").append('<li><img style="display:none;" class="lightboxImage" src="' + nodes[index].lightboxImage3 + '" alt="' + nodes[index].lightboxTitle + '" /></li>');
    }
    if (nodes[index].lightboxImage4 != "")
    {
        $(".imageRotator").append('<li><img style="display:none;" class="lightboxImage" src="' + nodes[index].lightboxImage4 + '" alt="' + nodes[index].lightboxTitle + '" /></li>');
    }
    if (nodes[index].videourl != "") {
        $(".imageRotator").append('<li><iframe style="display:none; margin-top:49px;" class="youtube-player lightboxImage" type="text/html" height="168" src="' + nodes[index].videourl + '?controls=0&modestbranding=1&rel=0&autoplay=1&showinfo=0" frameborder="0" allowfullscreen="true"></iframe></li>');
    }
    $("#lightbox p.lbP1").text(nodes[index].lightboxParagraph1);
    $("#lightbox p.lbP2").text(nodes[index].lightboxParagraph2);
    $("#lightbox p.lbP3").text(nodes[index].lightboxParagraph3);

    if ($(".imageRotator .lightboxImage").length > 1)
    {
        clearInterval(caseInterval);
        caseInterval = setInterval(cycleGallery, 3000);
    }
}

function cycleGallery() {
   
    
    $(".imageRotator li.active .lightboxImage").fadeOut(1000);
    $(".imageRotator li.active").removeClass("active").next().addClass("active");
    if ($(".imageRotator li.active").length == 0)
    {
        $(".imageRotator li").eq(0).addClass("active");
    }
    if ($(".imageRotator li.active").html().indexOf("allowfullscreen") > -1) {
        $(".imageRotator li.active .lightboxImage").fadeIn(500);
        clearInterval(cycleGallery);
        clearInterval(caseInterval);
    } else {
        $(".imageRotator li.active .lightboxImage").fadeIn(1000);
    }
}

function loadLightBox()
{
    if (clicked != null)
    {
        idx = parseInt(clicked.replace("caseThumb", ""), 10) - 1;
        loadLightBoxContentByIndex(idx);
    }
}

function initServices()
{
    // Lightbox close button
    $("#lightbox a.close-btn").click(function() { $.fancybox.close(); return false; });

    function lightboxPrev() { 
            $('.red').hide("pulsate", { times:1 }, 200);
        idx = idx - 1; 
        if (idx < 0) 
        {
            idx = nodes.length - 1;
        }
        loadLightBoxContentByIndex(idx);  
        return false; 
    }

    // Lightbox left nav button button
    $("#lightbox a.btn-left").click(lightboxPrev);

    function lightboxNext() { 
   //   alert('flash next');
     // $('#lightbox').css('background', '#ffffff')
        $('.red').hide("pulsate", { times:1 }, 200);
    //  $.delay(1000)('#lightbox').css('background', 'url("/images/lightbox.png") transparent no-repeat 0 0 !important;')
        idx = idx + 1; 
        if (idx > nodes.length - 1) {
            idx = 0;  
        }
        loadLightBoxContentByIndex(idx);
        return false; 
    }

    // Lightbox right nav button button
    $("#lightbox a.btn-right").click(lightboxNext);

    // Thumbnail click - show lightbox
    $("#caseStudyGallery li a").click(function() { clicked = $(this).attr("id"); }).fancybox({titleShow: false, showCloseButton: false, overlayColor: "#fff", onStart: function() { loadLightBox($(this)); }});

    // Show strapline for thumbnail on hover
    $("#caseStudyGallery li a").hover(
        function () {
            var index = $(this).attr("id").replace("caseThumb", "");
            $("#caseStudyGallery a#caseRollover").text($(this).attr("title"));
            $("#caseStudyGallery a#caseRollover").attr("href", $(this).attr("href"));
            $("#caseStudyGallery a#caseRollover").fadeIn("slow");
            $(".thegreybox").hide();
            $("#thegreybox" + index).fadeIn();
            return false;
        },
        function () {
            var index = $(this).attr("id").replace("caseThumb", "");
            $("#caseStudyGallery a#caseRollover").css("display", "none");
            return false;
        }
    );  

    /*
    // Preload image rollovers
    jQuery.preLoadImages("/images/lightbox-close-rollover-btn.png", "/images/lightboxnavleft-rollover.png", "/images/lightboxnavright-rollover.png");

    $("a.close-btn").hover(
        function() { 
            $(this).find("img").attr("src", "/images/lightbox-close-rollover-btn.png"); 
        }, 
        function() { 
            $(this).find("img").attr("src", "/images/lightbox-close-btn.png"); 
        }
    );

    $("#lightbox a.btn-left").hover(
        function() { 
            $(this).find("img").attr("src", "/images/lightboxnavleft-rollover.png"); 
        }, 
        function() { 
            $(this).find("img").attr("src", "/images/lightbox-nav-left.png"); 
        }
    );

    $("#lightbox a.btn-right").hover(
        function() { 
            $(this).find("img").attr("src", "/images/lightboxnavright-rollover.png"); 
        }, 
        function() { 
            $(this).find("img").attr("src", "/images/lightbox-nav-right.png"); 
        }
    );
    */
}
