﻿$(document).ready(function(){ 
    $(document).pngFix(); 
}); 
    
var site = function() {
  var keyEvents = new Hash();
    
	return {
	  content :
		{
	    hide : function (callback)
	    {
          var count = 0;
          //Do SlideUps
          var slideup = $(".slideup")
          if (slideup.size() > 0){
            slideup.each(function(){
               $(this).animate({top: "-" + $(this).height() + "px"}, function(){
                                                                                $(this).hide(function(){
                                                                                    count = count + 1;
                                                                                    if (count == 3){callback()};
                                                                                });
                                                                                });
            });                                                     
          }else{
            count = count + 1;
            if (count == 3){callback()};
          };
          
          //Do SlideDowns
          var slidedown = $(".slidedown")
          if (slidedown.size() > 0){
            slidedown.each(function(){
            $(this).animate({top: $(this).parent().height() + "px"}, 1500, function(){
                                                                $(this).hide(function(){
                                                                  count = count + 1;
                                                                  if (count == 3){callback()};
                                                                });
                                                            });
            });
          }else{
            count = count + 1;
            if (count == 3){callback()};
          };
          
          //Do FadeOuts
          var fadeout = $(".fadeout")
          if (fadeout.size() > 0){  
            fadeout.each(function(){                                    
            $(this).fadeOut(1500, function(){
                                                $(this).hide(function(){
                                                  count = count + 1;
                                                  if (count == 3){callback()};
                                                });
                                            });
            });
          }else{
            count = count + 1;
            if (count == 3){callback()};
          };
       }
		},
		keys :
		{
		  registerEvent : function(keycode, callback)
		  {
		    keyEvents.setItem(keycode, callback);
		    return "true";
		  },
		  
		  unregisterEvent : function(keycode)
		  {
		    keyEvents.removeItem(keycode);
		  },
		  
		  callEvent : function(keycode)
		  {
          if (keyEvents.hasItem(keycode))
          {
            keyEvents.getItem(keycode).call();
          }
      },
		  
		  getName : function(keycode)
		  {
        return String.fromCharCode(keycode).toLowerCase();
      },
      
      getCode : function(key)
      {
        if ( key == null )
        {
          keycode = event.keyCode;
        } else {
          keycode = key.keyCode;
        };
        return keycode;
      },
      
		  codes : {
		          esc : "27",
		          left : "37",
              up : "38",
              right : "39",
              down : "40"
		          }
    }
	};
}();

var thumbs = function() {
  var currentImage = 1;
  var imageShown = false;
  var animating = false;
  var currentSlide = 0;
  var slideposition = 0;
  var thumbheight = 128;
  var maxthumbs = 0;
  
	return {
		init : function() {
      maxthumbs = $(".thumbs ul li").size();

      if (maxthumbs > 3)
      { 
        $("a#thumbDown").click(function(){return thumbs.slideImageList(currentSlide + 1);});
        $("a#thumbDown").removeClass("disabled");
        $("a#thumbDown img").attr("src", $("a#thumbDown img").attr("src").replace("_disabled.png", ".png"));
      }
      
      $(".thumbs ul li img").click(function(){
        thumbs.showImage($(".thumbs ul li").index($(this).parent()));
      });
      
		},
		showImage: function(image) {
		  currentImage = image + 1;
		  $(".thumbs ul li").removeClass("current");
		  var img = $(".thumbs ul li:eq(" + image + ")").addClass("current").find("img");

		  $("#page_content_image").show();
		  $("#imgBig").attr("src", img.attr("src").replace("_th", ""));
		  $("#imgBig").attr("alt", img.attr("alt"));
		  $("#imgBig").attr("title", img.attr("title"));
		  		  
		  $("#imgBig").animate({width: "629px", height: "472px"}, 1000, function()
		  {
		    $("#page_content_content").hide();
    
        $("a.previous").click(function(){thumbs.changeImage("prev"); return false;});
        $("a.next").click(function(){thumbs.changeImage("next"); return false;});
        
        $("a.previous, a.next").hover(function(){
          $(this).find("img").addClass("hover");
        }, function(){
          $(this).find("img").removeClass("hover");
        });
       
	      if (currentImage > 1){
	        $("a.previous").show().css("display", "block");
	        site.keys.registerEvent("37", function(){thumbs.changeImage('prev');});
	      }else{
	        $("a.previous").hide();
	      }
  	    
        if (currentImage < maxthumbs){
          $("a.next").show().css("display", "block");
          site.keys.registerEvent("39", function(){thumbs.changeImage('next');});
        }else{
          $("a.next").hide()
        }
   
		    $("a.close").unbind();
		    $("a.close").click(function(){return thumbs.hideImage()});
		    imageShown = true;
		  });
		},
		hideImage: function() {
		  $("img#imgBig").animate({width: "100px", height: "100px"}, 1000, function(){
		    $("a.previous").hide();
		    $("a.next").hide();
		    $("#page_content_image").hide();
		    $("#page_content_content").fadeIn();
		    $(".thumbs ul li").removeClass("current");
        $("a.close").unbind();
		    imageShown = false;
		  });
		  return false;
		},
    changeImage: function(direction){
      if (!animating){
		  $(".thumbs ul li").removeClass("current");

        switch (direction)
        {
         case "prev":
          $("a.next").show();
          site.keys.registerEvent("39", function(){thumbs.changeImage('next')});
          currentImage = currentImage - 1;
          if (currentImage < (maxthumbs - 1))
          {
            thumbs.slideImageList(currentSlide - 1);
          }
          $("#imgBig").attr("src", $(".thumbs ul li:eq(" + (currentImage - 1) + ")").addClass("current").find('img').attr('src').replace("_th", ""));
          if (currentImage == 1)
          {
            $("a.previous").hide();
            site.keys.unregisterEvent("37");
          }
          break;
          
         case "next":
          $("a.previous").show();
          site.keys.registerEvent("37", function(){thumbs.changeImage('prev')});
          currentImage = currentImage + 1;
          if (currentImage > 2)
          {
            thumbs.slideImageList(currentSlide + 1);
          }
          $("#imgBig").attr("src", $(".thumbs ul li:eq(" + (currentImage - 1) + ")").addClass("current").find('img').attr('src').replace("_th", ""));
          if (currentImage == maxthumbs)
          {
            $("a.next").hide();
            site.keys.unregisterEvent("39");
          }
          break;
        }

      }
      return false;
    },
    slideImageList: function(image){
      if(!animating){
        var position = $("#casestudy_thumbs div ul").css("top");
        if (image < currentSlide){
            if (slideposition == 0){return false;}
            slideposition = -(thumbheight * image);
            if (slideposition > 0){slideposition = 0;}
            if (slideposition == 0)
            {
              $("a#thumbUp").unbind();
              $("a#thumbUp").click(function(){return false;});
              $("a#thumbUp img").attr("src", $("a#thumbUp img").attr("src").replace(".png", "_disabled.png"));
              $("a#thumbUp").addClass("disabled");
            }
            if ($("a#thumbDown").hasClass("disabled"))
            {
              $("a#thumbDown").click(function(){return thumbs.slideImageList(currentSlide + 1);});
              $("a#thumbDown").removeClass("disabled");
              $("a#thumbDown img").attr("src", $("a#thumbDown img").attr("src").replace("_disabled.png", ".png"));
            }
                          
        }else if (image > currentSlide){
          
           var maxpos = -((maxthumbs - 3) * thumbheight);
           if (slideposition == maxpos){return false;}
           slideposition = -(thumbheight * image);
           if (slideposition < maxpos){slideposition = maxpos;}
           if (slideposition == maxpos)
           {
              $("a#thumbDown").unbind();
              $("a#thumbDown").click(function(){return false;});
              $("a#thumbDown img").attr("src", $("a#thumbDown img").attr("src").replace(".png", "_disabled.png"));
              $("a#thumbDown").addClass("disabled");  
           }
           if ($("a#thumbUp").hasClass("disabled"))
           {
              $("a#thumbUp").click(function(){return thumbs.slideImageList(currentSlide - 1);});
              $("a#thumbUp").removeClass("disabled");
              $("a#thumbUp img").attr("src", $("a#thumbUp img").attr("src").replace("_disabled.png", ".png"));
           }
        }
        currentSlide = image;
        $(".thumbs ul").animate({top: slideposition.toString() + 'px'}, 500);
      }
      return false;
    }
	};
}();

$(document).keydown(function (eh){
  var key = site.keys.getCode(eh);
  site.keys.callEvent(key);
});

///////////////////////////////////////////////////
//              Hash table object                //
///////////////////////////////////////////////////
function Hash()
{
  this.length = 0;
  this.items = new Array();
  for (var i = 0; i < arguments.length; i += 2) {
    if (typeof(arguments[i + 1]) != 'undefined') {
      this.items[arguments[i]] = arguments[i + 1];
      this.length++;
    }
  }
   
  this.removeItem = function(in_key)
  {
    var tmp_previous;
    if (typeof(this.items[in_key]) != 'undefined') {
      this.length--;
      var tmp_previous = this.items[in_key];
      delete this.items[in_key];
    }
	   
    return tmp_previous;
  }

  this.getItem = function(in_key) {
    return this.items[in_key];
  }

  this.setItem = function(in_key, in_value)
  {
    var tmp_previous;
    if (typeof(in_value) != 'undefined') {
      if (typeof(this.items[in_key]) == 'undefined') {
	      this.length++;
      }
      else {
	      tmp_previous = this.items[in_key];
      }

      this.items[in_key] = in_value;
    }
	   
    return tmp_previous;
  }

  this.hasItem = function(in_key)
  {
    return typeof(this.items[in_key]) != 'undefined';
  }

  this.clear = function()
  {
    for (var i in this.items) {
      delete this.items[i];
    }

    this.length = 0;
  }
}
