/*	******************************
		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 - Dim Screen
		Author: Jack Lukic - KNI 
		Last revision: June 2009
	******************************	*/
jQuery.extend( {
    dimScreen: function(speed, opacity, callback) {
		if(typeof speed == 'function') {
            callback = speed;
            speed = null;
        }
        if(typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }
        if(speed < 1 && speed > 0) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }
        if(opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }
		
		speed = (speed >= 0) ? speed : 200;
        opacity = (opacity > 0) ? opacity : 0.22;
		
		if($('#dimmer').size() < 1) {
			jQuery('<div/>').attr('id','dimmer-wrapper').prependTo(document.body).html(jQuery('<div/>').attr('id','dimmer'));
			var curOpacity = $('#dimmer').css('opacity');
			
			if(curOpacity != opacity) {
				if(speed == 0) {
					$('#dimmer').css({ opacity: opacity, visibility: 'visible' });
					$('#dimmer-wrapper').css({ visibility: 'visible' });
				}
				else {
					$('#dimmer, #dimmer-wrapper').css({	visibility: 'visible'});
					$('#dimmer').css({ opacity: '0.00'});
					
					$('#dimmer').fadeTo(speed, opacity, callback);
				}			
			}
		}
	},
    unDimScreen: function(speed) {
		speed = (speed >= 0 && typeof(speed) != 'undefined') ? speed : 200;
		if($('#dimmer').size() > 0) {
			if(speed == 0) {
				return $('#dimmer, #dimmer-wrapper').css({
					visibility: 'hidden'
				});
				$('#dimmer, #dimmer-wrapper').remove();
			}
			else {
				$('#dimmer').fadeTo(speed, '0.00', function(){					
					$('#dimmer, #dimmer-wrapper').remove();
				});
			}
		}
	}
});

/*	******************************
		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.2,
				shadowOffset: 0,
				choicesID: '.dropdown-choices',
				filterClass: '.disabled, .readonly',
				align: 'default',
				animate: {
					show: {
						duration: 500,
						easing: 'easeInOutQuint'						
					},
					hide: {
						duration: 200,
						easing: 'easeInOutQuint'							
					}
				}
			}
			// 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() {
				$(this).removeClass('down');
				// 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()) {
						// remove active class
						$dropdown.removeClass('active').addClass('hover');
						// hide choices
						$choices.slideUp(settings.animate.hide.duration, settings.animate.hide.easing);
						// 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');
						
						// Add active class
						$dropdown.removeClass('hover').addClass('active');
						
						// 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('active');
							$('.dropdown').not($dropdown).removeAttr('style');
							$(this).remove();	
							$(settings.choicesID).filter(':visible').slideUp(settings.animate.hide.duration, settings.animate.hide.easing);
							return false;
						});
						// show
						$choices.css('top',pos);
						$choices.slideDown(settings.animate.show.duration, settings.animate.show.easing);							
					}
				}
				return false;
			});
			
			$dropdown
				.bind('mousedown', function() {
					$(this).addClass('down');
			    })
			    .bind('mouseup',function() {
					$(this).removeClass('down');	
				})
				.hover(function(){
					if(!$(this).hasClass('active')) {
						$(this).addClass('hover');
					}
				}, function(){
					$(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')
				.bind('mousedown', function() {
					$(this).parent().addClass('down');						
				})
				.bind('mouseup', function() {
					var $choice = $(this);
					$choice.parent().removeClass('down');
					$dropdown.removeClass('active');
					// Reset styles on dropdowns that aren't this one
					$('.dropdown').not(this).not('.promotional').attr('style','');
					// Remove the 'clickaway' div from DOM
					$('.clickaway').remove();
					// Result no longer active
					$choice.parent().removeClass('active');
					// Hide the dropdown choice list
					$choice.parent().parent().slideUp(settings.animate.hide.duration, function() {
						// highlight this choice as selected for future visits to dropdown choices
						$choice.parent().addClass('selected');
						$choice.parent().siblings().removeClass('selected');
					});
					// 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 - Default Text 
		Author: Jack Lukic - KNI
		Last revision: March 2009
	******************************	*/

jQuery.fn.extend({
	preserveDefaultText: function(params) {
		var settings = {
			defaultValue: $(this).val(),	
			replaceValue: '',
			alwaysReplace: false,
			disabledClassList: '.readonly, .disabled'
		};
		jQuery.extend(settings,params);
		// no replaced start value for IE
		if($.browser.msie) {
			settings.defaultValue = '';
			settings.replaceValue = '';
		}	
		$(this).focus(function(){
			if(!$(this).filter(settings.disabledClassList).size() > 0) { 
				if(settings.alwaysReplace) {
					$(this).attr('last',$(this).val());
					$(this).val(settings.replaceValue);
				}
				else {
					if($(this).val() == settings.defaultValue) {
						$(this).val(settings.replaceValue);
					}
				}
			}
		});
		$(this).blur(function(){	 
			if(!$(this).filter(settings.disabledClassList).size() > 0) { 
				if($(this).val() == settings.replaceValue) {
					if(settings.alwaysReplace) {
						$(this).val($(this).attr('last'));
						$(this).removeAttr('last');
					}
					else {
						$(this).val(settings.defaultValue);
					}
				}
			}
		});
		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);
		$(this).each(function() {
			if(settings.useLive) {	
				$(this).live('mousedown',function(){
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) { 
						$this.addClass(className);
					}
				});
				$(this).live('mouseup', function() {
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$this.removeClass(className);							  
					}
				});	
			}
			else {
				$(this).bind('mousedown', function(){
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$(this).addClass(className);
						// bind mouseleave event to fix down sticky
						$(this).bind('mouseleave.fix', function() {
							$(this).removeClass(className);										
						});
					}				   
				});
				$(this).bind('mouseup', function(){
					if(settings.filter == '' || $(this).filter(settings.filter).size() == 0) {
						$(this).removeClass(className);	
						// remove down sticky handler
						$(this).unbind('mouseleave.fix');						  
					}
				});
			}
		});
		return this;
	},
	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);
		$(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;
	},
	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;
	}
});


// Tooltip Class
// Written by: Jack Lukic
// Jan 19, 2008
jQuery.fn.extend({
	tooltip: function(params) {
		// default settings
		var settings = {
			className: '',
			xOffset: 10,
			yOffset: -20,
			animate: false,
			duration: 0,
			mouseCenter: true,
			prependTo: 'body',
			zIndex: 9999,
			text: ''
		}
		// allow for params to be passed in
		jQuery.extend(settings, params);
		// iterate through all selected
		$(this).each(function() {
			// attach mouse move event listener with live events jQuery 1.3
			$(this).mousemove(function(cursor){
				var $link = $(this);
				// create hover html if not available
				if(!$link.data('$hover')) {
					var html =	''	+
					'<div id="tooltip">'	+
						'<div id="tooltip-copy"></div>'	+
					'</div>';
					// create hover div
					var $hover = $(html);
					// prepend to body so position is always relative to body
					$hover
						.find('#tooltip-copy')
						.html(settings.text)
					;
					$hover
						.addClass(settings.className)
						.prependTo(settings.prependTo);
					// add text content
					
					// attach hover div to this
					$(this).data('$hover',$hover);
				}
				// retreive hover div 
				var $hover = $link.data('$hover');
				if(typeof($hover) != 'undefined') {
					// offset for height of hover
					var hoverHeight = $hover.height();
					
					// center div on mouse if setting enabled
					if(settings.mouseCenter == true) {
						var xOffset = -($hover.width() / 2) + settings.xOffset;
					}
					else {
						var xOffset = settings.xOffset	
					}
					// find cursor location and add offset
					var cursorX = (cursor.pageX) + xOffset ;
					var cursorY = (cursor.pageY - hoverHeight) + settings.yOffset;
					// assign style properties
					$hover.css({
						display: 'block',
						left: cursorX,
						top:  cursorY
					});
				}
			});
			$(this).mouseout(function() {
				// cache hover and link div 
				var $link = $(this);
				var $hover = $link.data('$hover');
				// make sure it sits under any new hover
				if(typeof($hover) != 'undefined') {
					$hover
						.removeClass(settings.hoverClass)
						.css({zIndex: settings.zIndex});
					// allow for hover to animate 
					if(settings.animate) {
						$hover.fadeOut(settings.duration,function(){
							// remove hover data on callback
							$hover.remove();
							$link.removeData('$hover');
						});	
					} 
					else {
						// remove hover data on callback
						$hover.remove();
						$link.removeData('$hover');						
					}
				}
			});
		});
		// return jQuery object for chaining
		return this;
	}
});
