(function(global){

	function NewVSOldController(container){

		this.container = $(container);
		$(".source",this.container).hide();
		this.container.addClass("showhide-new");
		this.state="new";
    this.container.append("<a href='#' class='showNewLink'></a><a href='#' class='showOldLink'></a>");
    
    $("a",this.container).click($.proxy(this.blockAClick,this));
    
    $(".showNewLink",this.container).bind("mouseenter",$.proxy(this.previewNew,this));
    $(".showNewLink",this.container).bind("click",$.proxy(this.showNewSticky,this));
    $(".showNewLink",this.container).bind("mouseleave",$.proxy(this.setToState,this));
    
    $(".showOldLink",this.container).bind("mouseenter",$.proxy(this.previewOld,this));
    $(".showOldLink",this.container).bind("click",$.proxy(this.showOldSticky,this));
    $(".showOldLink",this.container).bind("mouseleave",$.proxy(this.setToState,this));
    
    $(".visible img",this.container).bind("click",$.proxy(this.showOldSticky,this));
    $(".hidden img",this.container).bind("click",$.proxy(this.showNewSticky,this));      
	}

  NewVSOldController.prototype.blockAClick = function(event){
    event.preventDefault();
		event.currentTarget.blur();
  }
  
  NewVSOldController.prototype.setToState = function(){
    switch(this.state){
      case "old":
        this.container.removeClass("showhide-new");
    		this.container.addClass("showhide-old");
        $(".visible",this.container).hide();
        $(".hidden",this.container).show();
        break;
      default:
      case "new":
        this.container.removeClass("showhide-old");
    		this.container.addClass("showhide-new");
        $(".visible",this.container).show();
        $(".hidden",this.container).hide();
        break;
    }
  }

	NewVSOldController.prototype.showOldSticky = function(event){
		event.preventDefault();
		event.currentTarget.blur();
		if(this.state==="old"){
		  return;
		}
		this.state="old";
		this.setToState();
	};

	NewVSOldController.prototype.showNewSticky = function(event){
		event.preventDefault();
		event.currentTarget.blur();
		if(this.state==="new"){
		  return;
		}
		this.state="new";
		this.setToState();
	};

	NewVSOldController.prototype.previewOld = function(event){
		event.preventDefault();
		event.currentTarget.blur();
    this.container.removeClass("showhide-new");
		this.container.addClass("showhide-old");
    $(".visible",this.container).hide();
    $(".hidden",this.container).show();
	};

	NewVSOldController.prototype.previewNew = function(event){
		event.preventDefault();
		event.currentTarget.blur();
    this.container.removeClass("showhide-old");
		this.container.addClass("showhide-new");
    $(".visible",this.container).show();
    $(".hidden",this.container).hide();
	};

	function onDOMReady(){
	  var blocks = $(".showhide");
	  var index = blocks.length;
	  while(index--){
		  new NewVSOldController(blocks[index]);
		}
	}

	$(document).ready(onDOMReady);

})(this);
