jQuery(document).ready(function($){
	
	
	
	
$.fn.psHornKanter = function(options) {
	
	// build main options before element iteration
	var opts = $.extend({}, $.fn.psHornKanter.defaults, options);
	
	// iterate and reformat each matched element
	return this.each(function() {
		
		var $this = $(this);
		
		// build element specific options (METADATA PLUGIN)(optional)
		var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
		
		/**
		 * The content tabel settings
		 */
		var c;
		if($this.find(o.contentClass).find("table:first").length > 0){
			var table = $this.find(o.contentClass).find("table:first");
			c = {
				height: parseInt(table.css("height")),
				width: parseInt(table.css("width"))
			}
		}else{
			c = {
				height: 100,
				width: 300
			}
		}
		
		$('.ps_horn_kanter_body').css({display: "block"});
		
		/**
		 * Position
		 */
		$.fn.psHornKanter.position($this, o);
		
		$(window).resize(function(){
			$.fn.psHornKanter.position($this, o);
		})
		
		/**
		 * Hover on the img
		 */
		if(o.imgHover === true){
			$this.find(o.imgClass).hover(function(){
				var rel = $(this).attr("rel"),
					src = $(this).attr("src");
				$(this).attr("src", rel).attr("rel", src);
			},function(){
				var rel = $(this).attr("rel"),
					src = $(this).attr("src");
				$(this).attr("src", rel).attr("rel", src);
			});
		}
		
		
		/**
		 * Transition
		 */
		/** starts the fx */
		var init = $.fn.psHornKanter.transitions[o.fx];
		// if the effect exist
		if ($.isFunction(init)) {
			init($this, o, c);
		} else {
			//$.fn.psHornKanter.transitions.slideDown($this, o);
			debug("no " + o.fx);
		}
		
		

	});
};


//
// plugin defaults
//
$.fn.psHornKanter.defaults = {
	top: false,
	right: false,
	bottom: false,
	left: false,
	horiCenter: false,
	vertCenter: false,
	position: 'fixed',
	imgHover: false,
	width: 100,
	height: 100,
	content: false,
	contentClass: '.ps_horn_kanter_content',
	imgClass: '.ps_horn_kanter_img',
	imgDivClass: '.ps_horn_kanter_img_div',
	speedIn: 600,
	speedOut: 600,
	fx: "slideRight",
	
	
	img: false,
	link: false,
	overUnder: "under"
};

/**
 * Set the position on init and resize
 * 
 * @param {Object} $this
 * @param {Object} o
 */
$.fn.psHornKanter.position = function($this, o){
		/**
		 * VARIABLES
		 */
		var docHeight = $(window).height(),
			docWidth = $(window).width(),
			cssObject = Object(),
			positions = ["top","left","right","bottom"];
		
		/**
		 * Set the correct position
		 */
		// set the positions if not center
		for(var i = 0; i != 4; i++){
			if(o[positions[i]] !== false){
				if(o[positions[i]] === ""){o[positions[i]] = "0";}// if empty
				cssObject[positions[i]] = o[positions[i]] + "px";
			}
		}
		
		// set the position if center
		if(o.horiCenter !== false){
			cssObject["left"] = ( (docWidth/2) - (o.width/2) ) + "px";
		}
		if(o.vertCenter !== false){
			cssObject["top"] = ( (docHeight/2) - (o.height/2) ) + "px";
		}
		
		// the position
		cssObject["position"] = (o.position === "absolute") ? "absolute" : "fixed";
		
		// apply the settings on $(this)
		$this.css(cssObject);
}


//
// private function for debugging if not exist
//
function debug($obj) {
	if (window.console && window.console.log)
	window.console.log($obj);
};









/**
 * ALL TRANSITIONS
 * 
 * - slideDown
 * 
 */
$.fn.psHornKanter.transitions = {
	none: function() {
		debug("none");
	}
};

$.fn.psHornKanter.transitions.slideDown = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		img = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	img.css({top: 0, left: 0});
	
	/**
	 * The position vert
	 */
	if(o.horiCenter){
		//content.css({left: "-" + ((c.width/2) - (o.width/2)) + "px"});
		content.css({left: "0px"});
	}else if(o.right !== false){
		content.css({right: "0px"});
		img.css({right: "0px", left: "auto"});
	}else{
		content.css({left: "0px"});
	}
	
	content.css({
		position: "absolute",
		top: "-" + c.height + "px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	});
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		
		$this.css({width: c.width + "px", height: c.height + "px"});
		content.stop().animate({top: "0px"}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({top: "-" + c.height + "px"}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}





$.fn.psHornKanter.transitions.slideLeft = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		imgDiv = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	imgDiv.css({top: 0, right: 0, left: "auto"});
	
	/**
	 * The position hori
	 */
	if(o.bottom !== false){
		content.css({bottom: "0px", top: "auto"});
		imgDiv.css({bottom: "0px", top: "auto"});
	}
	if(o.left !== false){
		imgDiv.css({left: "0", right: "auto"});
	}
	
	content.css({
		position: "absolute",
		right: "-" + c.width + "px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	});
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		$this.css({width: c.width + "px", height: c.height + "px"});
		
		content.stop().animate({right: "0px"}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({right: "-" + c.width + "px"}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}




$.fn.psHornKanter.transitions.slideUp = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		img = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	img.css({bottom: 0, left: 0, top: "auto"});
	
	/**
	 * The position vert
	 */
	if(o.horiCenter){
		//content.css({left: "-" + ((c.width/2) - (o.width/2)) + "px"});
		content.css({left: "0px"});
	}else if(o.right !== false){
		content.css({right: "0px"});
		img.css({right: "0px", left: "auto"});
	}else{
		content.css({left: "0px"});
	}
	
	content.css({
		position: "absolute",
		bottom: "-" + c.height + "px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	});
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		
		$this.css({width: c.width + "px", height: c.height + "px"});
		content.stop().animate({bottom: "0px"}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({bottom: "-" + c.height + "px"}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}




$.fn.psHornKanter.transitions.slideRight = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		imgDiv = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	imgDiv.css({top: 0, left: 0});
	
	/**
	 * The position hori
	 */
	if(o.bottom !== false){
		content.css({bottom: "0px", top: "auto"});
		imgDiv.css({bottom: "0px", top: "auto"});
	}
	if(o.left === false){
		imgDiv.css({right: "0", left: "auto"});
	}
	
	content.css({
		position: "absolute",
		left: "-" + c.width + "px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	});
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		$this.css({width: c.width + "px", height: c.height + "px"});
		
		content.stop().animate({left: "0px"}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({left: "-" + c.width + "px"}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}


/**
 * 
 * Other effects
 * 
 */


$.fn.psHornKanter.transitions.slideDownRight = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		imgDiv = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	imgDiv.css({top: 0, left: 0});
	
	/**
	 * The position hori
	 */
	if(o.bottom !== false){
		content.css({bottom: "0px", top: "auto"});
		imgDiv.css({bottom: "0px", top: "auto"});
	}
	if(o.right !== false){
		imgDiv.css({right: "0", left: "auto"});
	}
	
	content.css({
		position: "absolute",
		left: "-" + c.width + "px",
		top: "-" + c.height + "px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	});
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		$this.css({width: c.width + "px", height: c.height + "px"});
		
		content.stop().animate({left: "0px", top: "0px"}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({left: "-" + c.width + "px", top: "-" + c.height + "px"}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}





$.fn.psHornKanter.transitions.fade = function($this, o, c){
	
	var content = $this.find(o.contentClass),
		imgDiv = $this.find(o.imgDivClass);
	
	$this.css({overflow: "hidden"});
	
	imgDiv.css({top: 0, left: 0});
	
	/**
	 * The position hori
	 */
	if(o.bottom !== false){
		content.css({bottom: "0px", top: "auto"});
		imgDiv.css({bottom: "0px", top: "auto"});
	}
	if(o.right !== false){
		imgDiv.css({right: "0", left: "auto"});
	}
	
	content.css({
		position: "absolute",
		left: "0px",
		top: "0px",
		display: "block",
		width: c.width + "px",
		height: c.height + "px"
	}).animate({opacity: 0}, 10);
	
	$(o.imgClass +", "+ o.contentClass, $this).hover(function(e){
		$this.css({width: c.width + "px", height: c.height + "px"});
		
		content.stop().animate({opacity: 1}, o.speedIn);
		
	}, function(e){
		
		content.stop().animate({opacity: 0}, o.speedOut, function(){
			$this.css({height: o.height + "px",width: o.width + "px"});

		});
		
	});
}






	
	
	
	
});
