﻿/*

  (c) 2008 Interphace Ltd.

*/

$(function() {
  var inProgress = false, abort = null;
  $.blockUI.defaults.elementMessageCSS = {
    backgroundColor: 'transparent'
  };
  $.blockUI.defaults.overlayCSS = {
    backgroundColor: '#fff', 
    opacity: '0.5'
  };
  $.blockUI.defaults.css = {
    width:          '100%',  
    left:           '0',  
    border:         'none', 
    backgroundColor:'transparent'
  };  
  $(document).keydown(function(e) {
    if (e.which == 27 && abort != null) {
      abort();
      // stopping the escape key because firefox stops 
      // animated gifs by default. TODO: decide whether 
      // we should we override this behaviour for any 
      // escape keypress, or just those during an 
      // image download?
      return false;
    }
  });
  $("#tabs li").click(function() {
    if (inProgress) { return false; }
    inProgress = true;
    var $this = $(this),
        $prevTab = $("#tabs li.active");
    var imageLoad = function(url) {
      var $imageView = $("#largeView img"),
          $target = $imageView.parent();
      var img = new Image();
      img.onload = function() {
        if ($.browser.msie) {
          // not standards-compliant, hence browser check
          $imageView[0].replaceNode(img);
        } else {
          $imageView.attr("src", img.src);
        }
        complete(true);
      };
      var complete = abort = img.onabort = img.onerror = function(completed) {
        abort = img.onerror = img.onabort = img.onload = null;
        cancelInterval();
        if (!completed) {
          $this.removeClass("active");
          $prevTab.addClass("active");
        }
        $target.unblock();
        inProgress = false;
      };
      var cancelInterval = function() {
        if (interval != 0) {
          clearInterval(interval);
          interval = 0;
        }
      };
      var interval = setInterval(function() {
        $target.block({message: '<img src="/assets/images/wait/wait-bar.gif" style="height: 19px" />'});
        cancelInterval();
      }, 100);
      img.src = url;
    };
    if (!$this.hasClass("active")) {
      imageLoad($this.attr("hover"));
      if ($prevTab) {
        $prevTab.removeClass("active");
      }
      $this.addClass("active");
    }
    return false;
  });
});

/*

  /main/cards.aspx accordion style widget

*/

$(function() {
  $("#card-list h2").click(function() {
  
    var $img = $("img", this);

    var url = $img.attr("src"),
        templateUrl = "/assets/images/panels/";

    if (url.indexOf("collapse") != -1) {
      $img.parent().next().slideUp();
      url = "expand.gif";
    } else {
      $img.parent().next().slideDown();       
      url = "collapse.gif";
    }
       
    $img.attr("src", templateUrl + url);
     
  }).each(function() {
    $("img", this).attr("src", "/assets/images/panels/expand.gif").parent().next().hide();
  });
     
  $("#card-list h2:first img").attr("src", "/assets/images/panels/collapse.gif").parent().next().show();
});