/**
 * jQuery.ai-tools - Active Ingredients Library
 * Copyright (c) 2011
 * Date: September 2011
 * @author Roi Kingon
 * @version 1.0
 */

/* 
 * jQuery inView
 * usage: $("element").inView(options,speed);
 * options: target, offset, effect
 * effects: slide, fade, slidefade
 */
(function($){
	$.fn.inView = function(options,speed) {
		$this = $(this);
		var scrollTop = $(window).scrollTop();
		var windowHeight = $(window).height();
		
		speed = (!speed && typeof(options)=="number") ? options : speed;
		speed = (speed) ? speed : 500;
		
		var settings = jQuery.extend({
			target: $this,
			offset: 10,
			effect: "slide",
			effectspeed: speed
		}, options);
		
		function display(target){
			switch(settings.effect){
				case "slide":
					target.slideDown(settings.effectspeed);
					break;
				case "fade":
					target.fadeIn(settings.effectspeed);
					break;
				case "slidefade":
					target.css({opacity:"0"});
					target.animate({opacity:'1',height:'toggle'},settings.effectspeed);
					break;
				default:
					target.show();
					break;
			}
		}
		
		settings.target.css({visibility:"hidden",display:"block"})
		
		var tHeight = settings.target.outerHeight();
		var tTop = settings.target.offset().top;
		
		settings.target.css({display:"none",visibility:"visible"});
		
		if(scrollTop > (tTop-settings.offset)){
			$("body,html").animate({
				scrollTop: ((tTop)-settings.offset)
			},speed,function() {
				
			});
		}

		if (tTop+tHeight+settings.offset > scrollTop+windowHeight) {
			$("body, html").animate({
				scrollTop: (((tTop+tHeight)-windowHeight)+settings.offset)
			}, speed);
		}
		
		display(settings.target);
		
	}
})(jQuery);

/* 
 * jQuery linkable
 * usage: $("element").linkable();
 * How to use: Place <a href="http://example.com" class="linkable-href"></a> within container element
 * Info: Gives standard block elements ability to have a goto action
 */
(function($){
	$.fn.linkable = function() {
		var $this = $(this);
		
		$('a.linkable-href').hide();
		
		$this.each(function() {
			$this.css("cursor", "pointer");
			$(this).click(function() {
				var href = $(this).find('a.linkable-href').attr('href');
				location.href=href;
			});
		});
	}
})(jQuery);

/* 
 * jQuery innerOverlay
 * usage: $("element").innerOverlay(speed);
 * How to use: Place <div class="overlay"></div> within container element
 * Info: Displays overlay on hover
 */
(function($){
	$.fn.innerOverlay = function(speed) {
		var $this = $(this);
		speed = (speed) ? speed : "";
		
		$this.each(function() {
			$(this).hover(function() {
				$(this).find(".overlay").stop(true,true).fadeIn(speed);
			}, function() {
				$(this).find(".overlay").stop(true,true).fadeOut(speed);
			});
		});
	}
})(jQuery);

/* 
 * jQuery clearInput
 * usage: $(elements).clearInput();
 * requires: input has id name
 * Info: Clears default text on focus, then on blur replaces text
 */
(function($){
	$.fn.clearInput = function() {
		var $this = $(this);
		
		$this.each(function() {
			if($(this).attr("id")){
				var defaultVal = document.getElementById($(this).attr("id")).defaultValue;
				if(defaultVal){
					$(this).focus(function() {
						if($(this).val() == defaultVal) {
							$(this).val("");
						}
					});
					$(this).blur(function() {
						if($(this).val() == "") {
							$(this).val(defaultVal);
						}
					});
				}
			}
		});
	}
})(jQuery);





















