/**
* Code Expander for Wordpress CodeColorer plug-in
* Author: Matt Hobbs (http://nooshu.com/)
*/
if($(".codecolorer-container").length){
	var $code = $(".codecolorer-container").each(function(){
		var $this = $(this);
		
		//Animation decision object
		var decisionObject = {
			w: false,
			h: false
		};
		
		//Original width / height of the displayed code
		var originalWidth = $this.width();
		var originalHeight = $this.height();
		
		//Width / height of hidden portion of code
		var mainWidth = $this.find(".codecolorer").width();
		var mainHeight = $this.find(".codecolorer").height();
		var lineWidth = $this.find(".line-numbers").width();
		
		//Only attach events if needed (ie has scroll bars)
		if((mainWidth + lineWidth) > originalWidth || mainHeight > originalHeight){
			//Uncomment if you use $.event.special.hover
			//$.event.special.hover.delay = 140;
			$this.bind("hover", function(e){
				//Current position in relation to the page
				var offsetTop = $this.offset().top;
				var offsetLeft = $this.offset().left;
				
				//Clone the code and attach it to the page
				$this.clone().css({
					position: "absolute",
					top: offsetTop,
					left: offsetLeft,
				}).attr("id", "cloned").appendTo("body");
				
				//Hide the code underneath
				$this.css("visibility", "hidden");
				
				//Detect what to animate
				var animateObject;
				if(mainWidth + lineWidth > originalWidth){decisionObject.w = true;}
				if(mainHeight > originalHeight){decisionObject.h = true;}
				
				if(decisionObject.w && decisionObject.h){
					animateObject = {
						height: mainHeight + 20,
						width: mainWidth + lineWidth + 40
					}
				} else if(decisionObject.w){
					animateObject = {width: mainWidth + lineWidth + 40}
				} else if(decisionObject.h){
					animateObject = {height: mainHeight + 20}
				}
				
				//Animate & add leave event
				$("#cloned").animate(animateObject, 500).bind("mouseleave", function(){
					var $cloned = $(this);
					//Animate back and remove
					$cloned.animate({
						width: originalWidth,
						height: originalHeight
					}, 400, function(){
						$cloned.remove();
						//Show the code underneath
						$this.css("visibility", "visible");
					});
				});
			}, function(){/*Nothing*/});
		}
	});
}
