/*	******************************
		PLUGIN - Utility Functions 
		Author: Jack Lukic - KNI (all plugins)
		Notes: Used to extend jQuery functionality and shorten code
	******************************	*/


// Alias for selecting flash for externalInterface
function $$(id) {
	// allow for jQuery style id
	if(id.charAt(0) == '#') {
		id = id.substr(1,id.length);	
	}
	if ($.browser.msie) {
		return window[id];
	}
	else {
		return document[id];
	}	
}


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;	
		}
	}
});

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;
	},
	hoverClass: function(className, live) {
		if(typeof(className) == 'undefined') {
			var className = 'hover';
		}
		if(typeof(live) == 'undefined') {		
			$(this).hover(function(){
				$(this).addClass(className);				   
			},
			function() {
				$(this).removeClass(className);
			});
		}
		else {
			$(this).live('mouseover',function(){
				$(this).addClass(className);
			});
			$(this).live('mouseout', function() {
				$(this).removeClass(className);							  
			});
		}
		return this;
	},
	smoothFade: function(opacity, time) {
		// just for hbo, chrome doesnt like canvas, do old fashion way
			if(typeof(time) == 'undefined') {
				var time = 300; 	
			}
			$(this).each(function() {
				if($.browser.chrome) {
					// google chrome hates changing opacity, lets just make it easy for chrome..
					$(this).attr('opacity', '1');
				}
				else {
					$(this).hover(function() {	
						// stop animation and reset inline styles to prevent getting stuck
						if($(this).animated()) {
							$(this).stop();	
						}
						$(this).attr('style','').css({opacity: 1});
					},function() {
						$(this).fadeTo(time, opacity, function() {
							$(this).attr('style','');
						});				
					});
				}
			});
		return this;
	},
	smoothHover: function(selector, time) {
		if(typeof(time) == 'undefined') {
			var time = 300; 	
		}
		$(this).each(function() {
			$(this).hover(function() {
				if(typeof(selector) == 'undefined') {
					var $hover = $(this);	
				}
				else {
					var $hover = $(this).find(selector);	
				}
				// stop animation and reset inline styles to prevent getting stuck
				if($hover.animated()) {
					$hover.stop();	
				}
				$hover.attr('style','').show();
			},function() {
				if(typeof(selector) == 'undefined') {
					var $hover = $(this);	
				}
				else {
					var $hover = $(this).find(selector);	
				}
				// MSIE cannot fade transparent PNGs so just hide
				if($.browser.msie) {
					$hover.hide();	
				}
				else {
					$hover.fadeOut(time);
				}
			});
		});
		return this;
	},
	initDropdown: function(params) {
		// grab selector from declaration
		$(this).each(function() {
			var settings = {
				opacity: 0.35,
				shadowOffset: 0,
				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).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;
			});	
		});
	},
	initCheckbox: function() {
		$(this).each(function() {
			$(this).toggle(
				function() {
					$(this).addClass('active');
					$(this).find('input').attr('checked','checked');
				},
				function() {
					$(this).removeClass('active');
					$(this).find('input').removeAttr('checked');
				}
			);
		});
	},
	initMarquee: function(params) {
		var settings = {
			wrapperClass: '.wrapper',
			controlClass: '.controls',
			prev: '.prev',
			next: '.next',
			animationDuration: 300,
			beforeCallback: function(){},
			afterCallback: function(){},
			timer: 0
		}
		jQuery.extend(settings, params);
		
		$(this).each(function() {
			var $cycler = $(this).find(settings.wrapperClass);			
			var pagerDOM = $cycler.next(settings.controlClass);
			
			$cycler.cycle({ 
				fx:     'fade', 
				pauseOnPagerHover: 1,
				pause: 1,
				speed:  settings.animationDuration, 
				timeout: settings.timer, 
				pager:  pagerDOM, 
				prev: settings.prev,
				next: settings.next,
				before: settings.beforeCallback,
				after: settings.afterCallback,
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#">'+idx+'</a></li>'; 
				} 
			});
			
		});		
	},
	submitButton: function(selector) {
		$(this).each(function() {
			$(this).keydown(function(event) {
				// if user clicks enter, click button
				if(event.keyCode == 13) {
					$(selector).click();	
				}
			});
		});
	}
});

// Tooltip Class
// Written by: Jack Lukic
// Jan 19, 2008
jQuery.fn.extend({
	tooltip: function(params) {
		// default settings
		var settings = {
			className: '',
			xOffset: 0,
			yOffset: -10,
			animate: false,
			duration: 0,
			mouseCenter: true,
			prependTo: 'body',
			zIndex: 9999
		}
		// 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
						.addClass(settings.className)
						.prependTo(settings.prependTo);
					// 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;
	}
});

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);
				}			
			}
		}
		if(typeof(callback) == 'function') {
			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();
				});
			}
		}
	}
});


// Reverse order of jQuery object using reverse
$.fn.reverse = [].reverse; 

/*	******************************
		PLUGIN - Third Party
		Author: Various
	******************************	*/

// small open source replace all prototype, from jQuery BB
String.prototype.replaceAll = function(strTarget, strSubString){
	var strText = this;
	var intIndexOfMatch = strText.indexOf(strTarget);
	while (intIndexOfMatch != -1) {
		strText = strText.replace(strTarget, strSubString)
		intIndexOfMatch = strText.indexOf(strTarget);
	}
	return(strText);
}
