/*	******************************
		PLUGIN - widont  
		Author: Jack Lukic - KNI 
		Notes: Prevents widowed paragraphs
	******************************	*/
jQuery.fn.extend({
	// if resize set to true, trigger widont on browser resize
	widont: function(resize) {
		if(typeof resize == 'undefined') {
			var resize = false;	
		}
		return $(this).each(function() {
			var $this = $(this);
			var html = $this.html();
			$this.html(html.replace(/\s([^\s<]+)\s*$/,'&nbsp;$1'));
			if(resize) {
				$(window).resize(function() {
					$this.html(html.replace(/\s([^\s<]+)\s*$/,'&nbsp;$1'));					
				});
			}
		});
	}
});	

/*	******************************
		PLUGIN - Utility Functions 
		Author: Jack Lukic - KNI (all plugins)
		Notes: Used to extend jQuery functionality and shorten code
	******************************	*/

jQuery.fn.extend({
	// test if el is animated
	animated: function() {
		if(this.filter(':animated').size() > 0) {
			return true;
		}
		else {
			return false;	
		}
	},
	// test if el is visible
	visible: function() {
		if(this.filter(':visible').size() > 0) {
			return true;
		}
		else {
			return false;	
		}
	},
	// test if el exists
	exists: function() {
		if(this.size() > 0) {
			return true;
		}
		else {
			return false;	
		}
	}
});


/*	******************************
		PLUGIN - Fancy Dropdowns 
		Author: Jack Lukic - KNI
		Last revision: February 2009
	******************************	*/
jQuery.fn.extend({
	initDropdown: function(params) {
		// grab selector from declaration
		$(this).each(function() {
			var settings = {
				opacity: 0.35,
				shadowOffset: -12,
				choicesID: '.dropdown-choices',
				filterClass: '.disabled, .readonly',
				align: 'default'
			}
			// allow custom settings to be passed in
			jQuery.extend(settings, params);
			
			// cache dropdown choices
			var $dropdown = $(this);
			var $choices = $(this).find(settings.choicesID);
			
			// Add click functionality to dropdown
			$dropdown.click(function() {
				// if dropdown is disabled or readonly do not allow click behavior
				if(!($(this).filter(settings.filterClass).visible())) {
					// if dropdown is currently opened, close the dropdown
					if($choices.visible()) {
						// hide choices
						$choices.hide();
						// Reset styles on dropdowns that aren't this one
						$('.dropdown').not(this).attr('style','');
						// Remove the 'clickaway' div from DOM
						$('.clickaway').remove();
					}
					// otherwise open dropdown 
					else {
						// Make other dropdowns go below this one
						$('.dropdown').not(this).css('z-index','1');
						
						// assess dropdown height for positioning
						var dropdownHeight = parseInt($(this).height()) + settings.shadowOffset;
						var pos = dropdownHeight;
						
						// if vertically aligned calculate differently
						if(settings.align == 'valign') {
							dropdownHeight = dropdownHeight / 2; 
							var pos = (-(parseInt($choices.height()) / 2) + dropdownHeight) + 'px';
						}
						// if top align calculate differently
						if(settings.align == 'topalign') {
							var pos = (-dropdownHeight) + 'px';
						}
						
						// Add a clickaway div that covers page, to allow people to click away from dropdown
						$('<div/>').addClass('clickaway').css({height: $(document).height() + 'px', opacity: settings.opacity}).appendTo(document.body)
						.click(function() {
							$dropdown.removeClass('hover');
							$('.dropdown').not($dropdown).removeAttr('style');
							$(this).remove();	
							$(settings.choicesID).filter(':visible').hide();
							return false;
						});
						$choices.css('top',pos).show();
					}
				}
				return false;
			});
			
			$dropdown.hover(function(){
				$(this).addClass('hover');
			}, function(){
				if(!$choices.visible()) {
					$(this).removeClass('hover');
				}
			});	
			// Hover effect for dropdown choices
			$choices.find('li').not(settings.filterClass).hover(function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});	
			
			$choices.find('li a').click(function() {
				// highlight this choice in blue for future visits to dropdown choices
				$(this).parent().addClass('selected');
				$(this).parent().siblings().removeClass('selected');
				// Reset styles on dropdowns that aren't this one
				$('.dropdown').not(this).not('.promotional').attr('style','');
				// Remove the 'clickaway' div from DOM
				$('.clickaway').remove();
				// Don't hover this result anymore
				$(this).parent().removeClass('hover');
				// Hide the dropdown choice list
				$(this).parent().parent().hide();
				// copy to dropdown only text specified in .text of option
				if($(this).find('span.text').exists()) {
					$dropdown.find('.dropdown-text').html($(this).find('span.text').html()+'&hellip;');		
				}
				else {
					// otherwise use all the text in the dropdown
					$dropdown.find('.dropdown-text').html($(this).html());
				}
				// populate hidden field with value
				var choice = $(this).attr('href').substr(1);
				$dropdown.find('input:hidden').val(choice);
				return false;
			});	
		});
	}
});

/*	******************************
		PLUGIN - Smooth Hover, Down
		Author: Jack Lukic - KNI
		Last revision: July 2010
	******************************	*/

jQuery.fn.extend({
	smoothHover: function(duration, params) {
		var settings = {
			instantOn: false,
			ieFix: false
		};
		var settings = $.extend(settings, params);
		if(settings.ieFix && $.browser.msie) {
			duration = 0;	
		}
		$(this).each(function() {
			var $this = $(this);
			var $hover = $this.find('.hover');
			$this
				.bind('mouseenter', function() {
					if(!$this.hasClass('disabled')) {
						if(settings.instantOn) {
							$hover.show();
						}
						else {
							$hover
								.protectAnimation()
								.fadeIn(duration)
							;
						}
					  }
				})
				.bind('mouseleave', function() {
					if(!$this.hasClass('disabled')) {
						$hover
							.fadeOut(duration)
						;
					}
				})
			;
		});
		return this;
	},
	smoothDown: function(duration, params) {
		var settings = {
			click: function() {},
			instantOn: false,
			ieFix: false
		};
		var settings = $.extend(settings, params);
		if(settings.ieFix && $.browser.msie) {
			duration = 0;	
		}
		var userAgent = navigator.userAgent;
		var iPad = userAgent.match(/iPad/i) != null;
		var iPhone = userAgent.match(/iPhone/i) != null;
		
		$(this).each(function() {
			var $this = $(this),
				$hover = $this.find('.hover'),
				$down = $this.find('.down'),
				mousedownEvent = 'mousedown',
				mouseupEvent = 'mouseup'
			;
			if(iPad || iPhone) {
				var mousedownEvent = 'touchstart';
				var mouseupEvent = 'touchend';
			}
			$this
				.bind(mousedownEvent, function() {
					if(settings.instantOn) {
						$down.show();
					}
					else {
						$hover.hide();
						$down.fadeIn(duration);
						if(!(iPad || iPhone)) {
							$this.bind('mouseleave.escape', function() {
								$down.fadeOut(duration);
							});
						}
					}
				})
				.bind(mouseupEvent, function() {
					$down.fadeOut(duration);
					settings.click();
				})
			;
		});
		return this;
	},
	forceHover: function() {
		$(this).each(function() {
			var $this = $(this);
			var offset = $this.offset();
			var width = $this.width();
			var height = $this.height();
			// look for first mousemove event
			$(document).bind('mousemove.poll', function(e) {
				var offsetX = e.pageX - offset.left;
				var offsetY = e.pageY - offset.top;
				if( (offsetX >= 0) && (offsetX <= width) && (offsetY <= height) && (offsetY >= 0) ) {
					$this.trigger('mouseenter');
				}
				$(document).unbind('mousemove.poll');
			});
		});
		return this;
	},
	protectAnimation: function() {
		$(this).each(function() {
			// stop current animation
			if($(this).filter(':animated').size() > 0) {
				$(this).stop();	
			}
			// prevent stuck style tag
			$(this).attr('style','');
		});
		return this;
	}
});

/*	******************************
		PLUGIN - Follow Link 
		Author: Jack Lukic - KNI
		Last revision: July 2010
	******************************	*/
jQuery.fn.extend({
	followLink: function(filter) {
		return $(this).each(function() {
			$(this).bind('click', function() {
				if(typeof filter == 'undefined') {
					var href = $(this).find('a').eq(0).attr('href');
				}
				else {
					var href = $(this).find(filter).eq(0).attr('href');
				}
				if(href) {
					// find base 
					var base = $('base').attr('href');
					window.location.href = base + href;	
				}
				return false;
			});
		});
	}
});


/*	******************************
		PLUGIN - Resolution Class 
		Author: Jack Lukic - KNI
		Last revision: July 2009
	******************************	*/
jQuery.fn.extend({
	resolutionClass: function(width, largeClass) {
		
		// used for reference in resize event
		var $this = $(this);
		$(this).each(function() {
			if($(window).width() > width) {
				$this.addClass(largeClass);
			}
			else {
				$this.removeClass(largeClass);	
			}
			// Add resize event
			$(window).bind('resize.fitToDocument', function() {
				if($(window).width() > width) {
					$this.addClass(largeClass);	
				}
				else {
					$this.removeClass(largeClass);	
				}
			});
				
		});
		return this;
	}
});


/*	******************************
		PLUGIN - 
		Hover / Focus Class - Add class for css hover and focus 
		Force Hover - check mouse position to trigger hover
		
		Author: Jack Lukic - KNI
		Last revision: November 2009
	******************************	*/
	
jQuery.fn.extend({
	hoverClass: function(className, params) {
		if(typeof(className) == 'undefined') {
			var className = 'hover';
		}
		if(typeof(className) == 'object') {
			params = className;
			className = 'hover';
		}
		var settings = {
			useLive: false,
			filter: ''
		}
		$.extend(settings, params);
		
		var userAgent = navigator.userAgent;
		var iPad = userAgent.match(/iPad/i) != null;
		var iPhone = userAgent.match(/iPhone/i) != null;
		
		if(!iPad && !iPhone) {
			$(this).each(function() {
				if(settings.useLive) {	
					$(this).live('mouseover',function(){
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) { 
							$this.addClass(className);
						}
					});
					$(this).live('mouseout', function() {
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$this.removeClass(className);							  
						}
					});	
				}
				else {
					$(this).hover(function(){
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).addClass(className);							  
						}				   
					},
					function() {
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).removeClass(className);							  
						}
					});
				}
			});
		}
		return this;
	},
	touchClass: function(className, params) {
		if(typeof(className) == 'undefined') {
			var className = 'hover';
		}
		if(typeof(className) == 'object') {
			params = className;
			className = 'hover';
		}
		var settings = {
			filter: ''
		}
		$.extend(settings, params);
		
		var userAgent = navigator.userAgent;
		var iPad = userAgent.match(/iPad/i) != null;
		var iPhone = userAgent.match(/iPhone/i) != null;
		
		if(iPad || !iPhone) {
			$(this).each(function() {
				$(this)
					.bind('touchstart', function(){
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).addClass(className);							  
						}				   
					})
					.bind('touchend', function() {
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).removeClass(className);							  
						}
					})
				;
			});
		}
		return this;
	},
	downClass: function(className, params) {
		if(typeof(className) == 'undefined') {
			var className = 'down';
		}
		if(typeof(className) == 'object') {
			params = className;
			className = 'down';
		}
		var settings = {
			useLive: false,
			filter: ''
		}
		$.extend(settings, params);
		
		var userAgent = navigator.userAgent,
			iPad = userAgent.match(/iPad/i) != null,
			iPhone = userAgent.match(/iPhone/i) != null,
			downEvent = 'mousedown',
			upEvent = 'mouseup'
		;			
		if(iPad || iPhone) {
			downEvent = 'touchstart';
			upEvent = 'touchend';	
		}
		
		$(this).each(function() {
			if(settings.useLive) {	
				$(this).live(downEvent,function(){
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) { 
						$this.addClass(className);
					}
				});
				$(this).live(upEvent, function() {
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$this.removeClass(className);							  
					}
				});	
			}
			else {
				$(this)
					.bind(downEvent, function(){
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).addClass(className);							  
						}				   
					})
					.bind(upEvent, function() {
						if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
							$(this).removeClass(className);							  
						}
					})
				;
			}
		});
		return this;
	},
	focusClass: function(className, params) {
		if(typeof(className) == 'undefined') {
			var className = 'active';
		}
		if(typeof(className) == 'object') {
			params = className;
			className = 'active';
		}
		var settings = {
			filter: ''
		}
		$.extend(settings, params);
		
		$(this).each(function() {
			$(this)
				.focus(function(){
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$(this).addClass(className);							  
					}				   
				})
				.blur(function() {
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$(this).removeClass(className);							  
					}
				})
			;
		});
		return this;
	},
	forceHover: function() {
		$(this).each(function() {
			var $this = $(this);
			var offset = $this.offset();
			var width = $this.width();
			var height = $this.height();
			// look for first mousemove event
			$(document).bind('mousemove.poll', function(e) {
				var offsetX = e.pageX - offset.left;
				var offsetY = e.pageY - offset.top;
				if( (offsetX >= 0) && (offsetX <= width) && (offsetY <= height) && (offsetY >= 0) ) {
					$this.trigger('mouseover');
				}
				$(document).unbind('mousemove.poll');
			});
		});
		return this;
	}
});

/*
 * jQuery.Preload
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 */
;(function($){var n=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},n.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),g={loaded:0,failed:0,next:0,done:0,total:f.length};if(!g.total)return m();var h='<img/>',j=d.threshold;while(--j>0)h+='<img/>';h=$(h).load(k).error(k).bind('abort',k).each(l);function k(e){g.found=e.type=='load';g.image=this.src;var a=g.original=c[this.index];g[g.found?'loaded':'failed']++;g.done++;if(d.placeholder&&a.src)a.src=g.found?g.image:d.notFound||a.src;if(d.onComplete)d.onComplete(g);if(g.done<g.total)l(0,this);else{if(h.unbind)h.unbind('load').unbind('error').unbind('abort');h=null;m()}};function l(i,a,b){if($.browser.msie&&g.next&&g.next%n.gap==0&&!b){setTimeout(function(){l(i,a,1)},0);return!1}if(g.next==g.total)return!1;a.index=g.next;a.src=f[g.next++];if(d.onRequest){g.image=a.src;g.original=c[g.next-1];d.onRequest(g)}};function m(){if(d.onFinish)d.onFinish(g)}};n.gap=14;n.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){n(this,a);return this}})(jQuery);

