(function($) {

	var lib = window.lib = {
		image : {
			preload : function(images) {
				var tImageArr = [];
				var tImages = images.split(",");
				$(tImages).each(function(i) {
		   			tImageArr[i] = new Image();
		   			tImageArr[i].src = tImages[i];
 				});
 			},
 			isComplete : function( selector ) {
 				var complete = true;
 				$(selector).each(function() {
 					if( !$(this).get(0).complete )
 					{ complete = false; }
 				});
 				return complete;
 			}
		},
		input : {
			defaultText : function(selector, settings) {
				var settings = $.extend({
        				defaultText: "enter keyword or item"
       			}, settings);
       			
       			$(selector).each(function(){
					$(this).focus( function() {
						if( $(this).val() == settings.defaultText )
						{ $(this).val(""); }
					});
		
					$(this).blur(function() {
						$(this).val( lib.utils.strTrim($(this).val()) );
						if( $(this).val() == "" )
						{ $(this).val( settings.defaultText ); }
					});
					$(this).blur();
				});
			},
			setMaxCharacters : function(selector, settings) {
				var settings = $.extend({
      				limit: 500,
      				results : "#_donotplace"
     			}, settings);
     			
     			$(selector).bind("keyup.max_characters", function() {
     				if( $(this).val().length > settings.limit )
     				{ $(this).val( $(this).val().substring(0, settings.limit) ); }
     			
     				var remainingCount = ((settings.limit - $(this).val().length) == 0) ? "0" : settings.limit - $(this).val().length;
     				$(settings.results).html(remainingCount);
     			});
     			$(selector).keyup();
     		},
			autoAdvance : function(selector) {
				$(selector).each(function(i) {
					$(this).keyup(function() {
						if( $(this).val().length >= $(this).attr("maxlength") )
						{ 
							$(this).blur();
							$(selector).eq(i+1).focus();
						 }
					});
				});
			}
		},
		link : {
			popupWindow : function(selector, settings) {
				$(selector).click(function(evt) {
					evt.preventDefault();
					lib.link.openPopup( $(this).attr("href"), settings );
				});
			},
			openPopup : function( url, settings ) {
				var settings = jQuery.extend({
          		width: 500,
          		height: 500,
          		toolbar: 0,
		  			menubar: 0,
		  			location: 0, 
		  			directories: 0,
		 			status: 0, 
		  			scrollbars: 0,
		 			resizable: 0,
		  			name: "Popup_Window"
        		}, settings);
				
				var features =	"toolbar=" + settings.toolbar + "," +
									"menubar=" + settings.menubar + "," +
									"location=" + settings.location + "," +
									"directories=" + settings.directories + "," +
									"status=" + settings.status + "," +
									"scrollbars=" + settings.scrollbars + "," +
									"resizable=" + settings.resizable;	
				
				var newWindow = window.open( url, settings.name, 'width=' + settings.width + ',height=' + settings.height + ',"' + features + '"');
				if( newWindow != null) { newWindow.focus(); }
			}
		},
		screen : {
			position : function() {
				if( typeof( window.pageYOffset ) == 'number' ) {
					//Netscape compliant
					return [ window.pageXOffset, window.pageYOffset ];
				} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				    //DOM compliant
				    return [ document.body.scrollLeft, document.body.scrollTop ];
				} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				    //IE6 standards compliant mode
				    return [ document.documentElement.scrollLeft, document.documentElement.scrollTop ];
				} else {
					return [ -1, -1 ];
				}
			},
			size : function() {
				var vpW = 0, vpH = 0;
				if (typeof window.innerWidth != 'undefined')
				{ return [ window.innerWidth, window.innerHeight ]; }
				else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
				{ return [ document.documentElement.clientWidth, document.documentElement.clientHeight ]; }
				else
				{ return [ document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight ]; }
			}
		},
		layer : {
			add : function(selector, defaultHTML, closeButton) {
				lib.layer.remove(selector);
				var addHTML = '<div id="' + selector.split("#")[1] + '"></div>';
				$("body").append(addHTML);
				$(selector).append(defaultHTML);
				lib.layer.center(selector);
				lib.layer.ie6Fix(selector, "a");
				lib.layer.closeButton(closeButton, selector);
			},
			remove : function(selector) {
				if( $(selector).size() > 0 )
				{
					$(selector).remove();
					lib.layer.ie6Fix(selector,"r");
				}
			},
			loadHTML : function(selector,page,closeButton, loadHTMLcallback) {
				$.get(page,function(data) {
					lib.layer.add(selector, data, closeButton);
					if( loadHTMLcallback != null )
					{ loadHTMLcallback(); }
				});
			},
			center : function(selector, settings) {
				var settings = $.extend({
					xaxis : true,
					yaxis : true		
				}, settings);
			
				var wPosition = lib.screen.position();
				var wSize = lib.screen.size();
			
				var left = 0;
				if( settings.xaxis )
				{ 
					left = (( wSize[0] - $(selector).width() ) / 2);
					left = ( left < 0 ) ? 20 : left;
					left += wPosition[0];
				}
			
				var top = 0;
				if( settings.yaxis )
				{ 
					top = (( wSize[1] - $(selector).height() ) / 2);
					top = ( top < 0 ) ? 20 : top;
					top += wPosition[1]; 
				}
				
				$(selector).css("top",top+"px").css("left", left+"px");
				lib.layer.ie6Fix(selector,"u");
			},
			closeButton : function( selector, layerSelector ) {
				$(layerSelector + " " + selector).unbind("click.lib.layer.close").bind("click.lib.layer.close", function(evt) { evt.preventDefault(); lib.layer.remove(layerSelector); });
			},
			ie6Fix : function(selector,action) {
				if(lib.utils.isIE6())
				{  
					var fixId = selector.split("#")[1] + "-iframe";
					var exists = $("#" + fixId).size() > 0 ? true : false;
					
					if(action == "r" && exists)
					{ 	$("#" + fixId).remove(); }
					
					if(action == "a" && !exists)
					{
						fixHTML = '<div id="' + fixId + '"><iframe width="100%" height="100%" frameborder="0" src="/css/blank.gif" style="filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"><!-- --></iframe></div>';
						$("body").append(fixHTML);
						exists = $("#" + fixId).size() > 0 ? true : false;
					}
					
					if( (action == "a" || "u") && exists)
					{
						$("#" + fixId).css("position", $(selector).css("position"));
						$("#" + fixId).css("height", $(selector).height());
						$("#" + fixId).css("width", $(selector).width());
						$("#" + fixId).css("margin-left", $(selector).css("margin-left"));
						$("#" + fixId).css("margin-right", $(selector).css("margin-right"));
						$("#" + fixId).css("margin-top", $(selector).css("margin-top"));
						$("#" + fixId).css("margin-bottom", $(selector).css("margin-bottom"));
						$("#" + fixId).css("top", $(selector).css("top"));
						$("#" + fixId).css("left", $(selector).css("left"));
						$("#" + fixId).css("bottom", $(selector).css("bottom"));
						$("#" + fixId).css("right", $(selector).css("right"));
						$("#" + fixId).css("z-index", $(selector).css("z-index")-1);
					}
				}
			}
		},
		utils : {
			isIE6 : function() {
				var IE6 = /msie|MSIE 6/.test(navigator.userAgent);
				return IE6;
			},
			timestamp : function() {
				return new Date().getTime();
			},
			strTrim : function(s) {
       			return s.replace(/^\s+|\s+$/g, "");
   			},
   			getPosition : function( selector ) {
				var positions = [];
				$(selector).each(function(i) {
					var offset = $(this).offset();
					positions[positions.length] = [ offset.left, offset.top ];
				});
				return positions
			},
			strTruncate : function(selector, settings) {
				var settings = $.extend({
					length : 20,
					postfix : "..."
				}, settings);
				
				settings.length -= settings.postfix.length;
				
				$(selector).each(function() {
					var text = $(this).text();
					if( text.length > settings.length )
					{
						var tIndex = settings.length-1;
						while( ( text.charAt(tIndex) != " ") && (tIndex > 0) )
						{ tIndex--; } 
												
						tIndex = ( tIndex == 0 ) ? settings.length : tIndex+1;
						$(this).text( text.substr(0,tIndex) + settings.postfix);	
					}
				});
			}
		},
		func : {
			formSetup : function() { 
				/* Basicly Stylesheet Entries */
				/*
					div.formArea {}
					div.formArea div.formEntry { border: 1px solid #fff; padding: 5px; margin-bottom: 15px; }
					div.formArea div.formEntry label { display: block; margin-bottom: 5px; font-weight: bold; }
					div.formArea div.formEntry label.optional { font-weight: normal; }
					div.formArea div.formError { border: 1px solid red; background-color: #fff; }
					div.formArea div.formError .errorText { display: block; margin-bottom: 5px; }
					.formFieldHighlight { background-color: yellow; }
				*/
				
				var formSelector = ".formArea";
				var globalErrorClass = "errorText";
				var formFieldHighlightClass = "formFieldHighlight";
				var formEntryErrorClass = "formError";
				var formEntryClass = "formEntry";
				var inputs = "input, select, textarea";
				
				$(formSelector + " ." + globalErrorClass).each(function() {
					var formEntry = $(this).parent();
					while( !$(formEntry).is("." + formEntryClass) )
					{ formEntry = $(formEntry).parent(); }
					$(formEntry).addClass( formEntryErrorClass );
				});
				$(inputs, $(formSelector)).bind("focus.fields", function(evt) {
					if( (!$(evt.target).is("input[type=image]")) && (!$(evt.target).is("input[type=radio]")) && (!$(evt.target).is("input[type=checkbox]")) )
					{ $(evt.target).addClass( formFieldHighlightClass ); }
				});
				$(inputs, $(formSelector)).bind("blur.fields", function(evt) {
					$(evt.target).removeClass( formFieldHighlightClass );
				});
				$("select", $(formSelector)).mousedown(function(evt) {
					$(evt.target).addClass( formFieldHighlightClass );
				});
			}
		},
		obj : {
			contentCollection : function(settings) {
				if(arguments.length > 0)
				{ this.init(settings); }
			},
			itemSlider : function(settings) {
				if(arguments.length > 0)
				{ this.init(settings); }	
			},
			button : function(settings) {
				if(arguments.length > 0)
				{ this.init(settings); }
			},
			pageOverlay : function(settings) {
				if(arguments.length > 0)
				{ this.init(settings); }
			}
		},
		about : function() {
			
		}
	};
	
	/* button Code */
	lib.obj.button.prototype.init = function(settings) {
		var settings = jQuery.extend({
        	off: "but-off.gif",
         on: "but-on.gif",
         hover: "but-hover.gif",
         hasClick: false,
        	hasHover: true,
         activeId: "but-active",
        	cssButton: false,
         cssOff: "glo-but-css-off",
         cssOn: "glo-but-css-on",
         cssHover: "glo-but-css-hover",
         buttonSelector: ".but-class",
         buttonCollectionSelector : ".but-class"
      }	, settings);
	
	  this.buttonSelector = settings.buttonSelector;
	  this.buttonCollectionSelector = settings.buttonCollectionSelector;
	  this.activeId = settings.activeId;
	  this.hasClick = settings.hasClick;
	  this.hasHover = settings.hasHover;
	
	  if( settings.cssButton )
	  { 
	  	this.type = "CSSBUTTON";
	  	this.cssOff = settings.cssOff;
	  	this.cssOn = settings.cssOn;
	  	this.cssHover = settings.cssHover;
	  	var pObj = this;
	  	
	  	$(this.buttonSelector).each(function() {
	  		// make sure the right styles are on the button by default.
	  		if( $(this).attr("id") != pObj.activeId )
			{ $(this).addClass(pObj.cssOff);} 
			else
			{ 
				$(this).removeClass(pObj.cssOff);
				$(this).addClass(pObj.cssOn); 
			}
			//setup the hover
			if( pObj.hasHover )
			{
				$(this).unbind("mouseover.button").bind("mouseover.button", function() {
					if( $(this).attr("id") != pObj.activeId )
					{ 
						$(this).removeClass(pObj.cssOff); 
						$(this).addClass(pObj.cssHover); 
					}
				});
				$(this).unbind("mouseout.button").bind("mouseout.button", function() { 
					if( $(this).attr("id") != pObj.activeId )
					{ 
						$(this).removeClass(pObj.cssHover);
						$(this).addClass(pObj.cssOff);
					}
				});
			}
			//setup the click
			if( pObj.hasClick )
			{
				$(this).unbind("click.button").bind("click.button", function() { 
					if( $(this).attr("id") != pObj.activeId )
				 	{	
						$(pObj.buttonCollectionSelector).attr("id","");
						$(pObj.buttonCollectionSelector).removeClass(pObj.cssHover);
						$(pObj.buttonCollectionSelector).removeClass(pObj.cssOn);
						$(pObj.buttonCollectionSelector).trigger("mouseout.button");
						$(this).removeClass(pObj.cssOff);
						$(this).addClass(pObj.cssOn);
						$(this).attr("id",pObj.activeId);
					}
				});
			}
		});
	  }
	  else
	  { 
	  	this.type = "IMAGEBUTTON"; 
	  	this.off = settings.off;
	  	this.on = settings.on;
	  	this.hover = settings.hover;
	  	var pObj = this;
	  	$(this.buttonSelector).each(function() {
	  		// always have the mouseout
	  		$(this).unbind("mouseout.button").bind("mouseout.button", function() { 
				if( $(this).attr("id") != pObj.activeId )
				{ $(this).attr("src",pObj.off); }
			});
	  		//setup the hover
			if( pObj.hasHover )
			{
				$(this).unbind("mouseover.button").bind("mouseover.button", function() {
					if( $(this).attr("id") != pObj.activeId )
					{ $(this).attr("src",pObj.hover); }
				});
			}
			//setup the click
			if( pObj.hasClick )
			{
				$(this).unbind("click.button").bind("click.button", function() { 
					if( $(this).attr("id") != pObj.activeId )
					{
						$(pObj.buttonCollectionSelector).attr("id","");
						$(pObj.buttonCollectionSelector).trigger("mouseout.button");
						$(this).attr("src",pObj.on);
						$(this).attr("id",pObj.activeId);
					}
				});
			}
		});
	  
	  	lib.image.preload(pObj.off);
	  	if( pObj.hasClick )
	  	{lib.image.preload(pObj.on); }
	  	if (pObj.hasHover )
	  	{ lib.image.preload(pObj.hover); }
	  }
	};
	/* ------------- */
	
	/* itemSlider Object Code */
	lib.obj.itemSlider.prototype.init = function(settings) {
		var settings = $.extend({
			viewport: "#widget-slider-viewport",
	      content: "#widget-slider-content",
	      next: "#widget-slider-next",
	      prev: "#widget-slider-prev",
			first: "#widget-slider-first",
			last: "#widget-slider-last",
	      item: "div",
	      direction: "vertical",
	   	showAmount: 3
	   }, settings);
	    
		this.viewport = settings.viewport;
		this.content = settings.content;
		this.next = settings.next;
		this.prev = settings.prev;
		this.first = settings.first;
		this.last = settings.last;
		this.item = settings.item;
		this.direction = settings.direction;
		this.showAmount = settings.showAmount;
		this.sliderInfo = new Object();
		this.sliderInfo.index = 0;

		/* Check Overflows (initial) */
		$(this.item + " *").add( $(this.item) ).each(function() {
			if( $(this).css("overflow") == "auto" )
			{ $(this).addClass("is__overflow"); }	
		});
		/* ----------------- */		
		 
		if ( $(this.viewport).size() > 0 )
		{
			this.overflowBeforeMove();
			this.overflowAfterMove();
			
			$(this.next).hide();
			$(this.prev).hide();
			
			if( $(this.item).size() > this.showAmount )
			{ $(this.next).show(); }
			
			if( this.direction == "vertical" )
			{
				this.sliderInfo.start = 0;
				this.sliderInfo.end = ($(this.item).size() - this.showAmount) * $(this.item).eq(0).height() * -1;
				this.sliderInfo.itemSize = $(this.item).eq(0).height();
				this.sliderInfo.top = 0;
			}
			else
			{ 
				//setup the vars.
				this.sliderInfo.start = 0;
				this.sliderInfo.end = ($(this.item).size() - this.showAmount) * $(this.item).eq(0).width() * -1;
				this.sliderInfo.itemSize = $(this.item).eq(0).width();
				this.sliderInfo.left = 0;
				$(this.content).css("width", (this.sliderInfo.itemSize * $(this.item).size()) + "px");
			}
			this.nextSetup();
			this.prevSetup();
			
			var currObj = this;
			$(this.first).click(function(evt){
				evt.preventDefault();
				currObj.toFirst();
			});
			$(this.last).click(function(evt){
				evt.preventDefault();
				currObj.toLast();
			});
		}
	};
	
	lib.obj.itemSlider.prototype.overflowBeforeMove = function() {
		$(this.content + " .is__overflow").css("overflow", "hidden");
	};
	
	lib.obj.itemSlider.prototype.overflowAfterMove = function() {
		$(this.item).slice(this.sliderInfo.index, this.sliderInfo.index + this.showAmount).each(function() {
			if( $(this).hasClass("is__overflow") )
			{ $(this).css("overflow", "auto"); }
			$(".is__overflow", this).css("overflow", "auto");
		});
	};
	
	lib.obj.itemSlider.prototype.nextSetup = function() {
		$(this.next).unbind("click");
		var currObj = this;
    	setTimeout(function() { 
    		$(currObj.next).click(function(evt) {
    			evt.preventDefault();
    			currObj.forward();
	         currObj.nextSetup();
			});
     	}, 351);
	};
	
	lib.obj.itemSlider.prototype.forward = function() {
		this.overflowBeforeMove();
		this.sliderInfo.index++;
		if(this.direction == "vertical")
		{
			this.sliderInfo.top = parseInt($(this.content).css("top").split("px")[0]) - this.sliderInfo.itemSize;
			$(this.content).animate( { top:this.sliderInfo.top+"px"}, 350 );
			if( this.sliderInfo.top == this.sliderInfo.end )
			{ $(this.next).hide(); }
			if( this.sliderInfo.top != 0 )
			{ $(this.prev).show();  }
		}
		else
		{
			this.sliderInfo.left = parseInt($(this.content).css("left").split("px")[0]) - this.sliderInfo.itemSize;
			$(this.content).animate( { left:this.sliderInfo.left+"px"}, 350 );
			if( this.sliderInfo.left == this.sliderInfo.end )
			{ $(this.next).hide(); }
			if( this.sliderInfo.left != 0 )
			{ $(this.prev).show();  }
		}
		
		var currObj = this;
		setTimeout( function() {
			currObj.overflowAfterMove();
		}, 500);
	};
	
	lib.obj.itemSlider.prototype.prevSetup = function() {
		$(this.prev).unbind("click");
		var currObj = this;
    	setTimeout(function() { 
			$(currObj.prev).click(function(evt) {
				evt.preventDefault();
				currObj.backward();
				currObj.prevSetup();
			});
		}, 351);
	};
	
	lib.obj.itemSlider.prototype.backward = function() {
		this.overflowBeforeMove();
		this.sliderInfo.index--;
		if( this.direction == "vertical")
		{
			this.sliderInfo.top = parseInt($(this.content).css("top").split("px")[0]) + this.sliderInfo.itemSize;
			$(this.content).animate( { top:this.sliderInfo.top+"px"}, 350 );
			if( this.sliderInfo.top != this.sliderInfo.end )
			{ $(this.next).show(); }
			if( this.sliderInfo.top == 0 )
			{ $(this.prev).hide();  }
		}
		else
		{
			this.sliderInfo.left = parseInt($(this.content).css("left").split("px")[0]) + this.sliderInfo.itemSize;
			$(this.content).animate( { left:this.sliderInfo.left+"px"}, 350 );
			if( this.sliderInfo.left != this.sliderInfo.end )
			{ $(this.next).show(); }
			if( this.sliderInfo.left == 0 )
			{ $(this.prev).hide();  }
		}
		
		var currObj = this;
		setTimeout( function() {
			currObj.overflowAfterMove();
		}, 500);
	};
	
	lib.obj.itemSlider.prototype.toFirst = function() {
		this.overflowBeforeMove();
		var newLocation = 0;
		this.sliderInfo.index = 0;
		
		if( $(this.item).size() > this.showAmount )
		{ $(this.next).show(); }
		$(this.prev).hide();
		 
		if( this.direction == "vertical")
		{
			this.sliderInfo.top = newLocation;
			$(this.content).animate( { top:this.sliderInfo.top+"px"}, 350 );
		}
		else
		{
			this.sliderInfo.left = newLocation;
			$(this.content).animate( { left:this.sliderInfo.left+"px"}, 350 );
		}
		
		var currObj = this;
		setTimeout( function() {
			currObj.overflowAfterMove();
		}, 500);
	};
	
	lib.obj.itemSlider.prototype.toLast = function() {
		this.overflowBeforeMove();
		var newLocation = ($(this.item).size() - this.showAmount) * this.sliderInfo.itemSize * -1;
		this.sliderInfo.index = ($(this.item).size() - this.showAmount);
		
		if( $(this.item).size() > this.showAmount )
		{ $(this.prev).show(); }
		$(this.next).hide();
		 
		if( this.direction == "vertical")
		{
			this.sliderInfo.top = newLocation;
			$(this.content).animate( { top:this.sliderInfo.top+"px"}, 350 );
		}
		else
		{
			this.sliderInfo.left = newLocation;
			$(this.content).animate( { left:this.sliderInfo.left+"px"}, 350 );
		}
		
		var currObj = this;
		setTimeout( function() {
			currObj.overflowAfterMove();
		}, 500);
	};
	/* ----------------------- */
	
	/* contentCollection */
	lib.obj.contentCollection.prototype.init = function(settings) {
		var settings = jQuery.extend({
        	selectorContent : ".lib_cC_Content",
        	selectorActivator : ".lib_cC_Activator",
        	defaultIndex : 0
        }, settings);
  		
  		this.selectorContent = settings.selectorContent;
  		this.defaultIndex = settings.defaultIndex;
  		this.eventName = "click.contentCollection_" + lib.utils.timestamp();
  		this.selectorActivators = settings.selectorActivator.split(",");
  		
  		//Initialize the collection correctly.
  		this.activateContent(this.defaultIndex);
  		
  		//setup the events
  		var this_contentCollection = this;
  		for( var x = 0; x < this.selectorActivators.length; x++)
  		{
  			$(this.selectorActivators[x]).each(function(i) {
  				$(this).unbind(this_contentCollection.eventName).bind(this_contentCollection.eventName, function(evt) {
  					evt.preventDefault();
  					this_contentCollection.activateContent(i);
  				});
  			});
  		}
  	};
	lib.obj.contentCollection.prototype.activateContent = function(index) {
		$(this.selectorContent).hide();
  		$(this.selectorContent).eq(index).show();
	};
	/* ------------------ */
	
	/* pageOverlay */
      lib.obj.pageOverlay.prototype.init = function(settings){
			var settings = jQuery.extend({
				selector : "#widget-pageOverlay"
			}, settings);
			
			this.selector = settings.selector;
			var myOverlay = this;
			this.interval = setInterval(function(){
				if($(document).height() > $(window).height()){
					myOverlay.resize();
				}
			},250);
			
			this.addOverlay();
			
			$(window).resize(function(){
				myOverlay.resize();
			});
			
		};	
		
		lib.obj.pageOverlay.prototype.resize = function(){
			$(this.selector).height($(window).height());
			$(this.selector).width($(window).width());  
			
			if($(document).width() > $(window).width()){
				lib.layer.ie6Fix(this.selector,'u');
				$(this.selector).width($(document).width());
			}
			
			if($("body").height() > $(window).height()){
				$(this.selector).height($("body").height()); 
			}
			else if($(document).height() > $(window).height()){
				$(this.selector).height($(document).height()); 
			}
			else {
				$(this.selector).height($(window).height());
			}
		};
		
		lib.obj.pageOverlay.prototype.addOverlay = function(){
			lib.layer.add(this.selector);
			$(this.selector).css("position", "absolute").css("top", "0").css("left", "0").css("width", "100%").css("height", "100%");
			this.resize();
		};
		
		lib.obj.pageOverlay.prototype.removeOverlay = function(){
			lib.layer.remove(this.selector);
			clearInterval(this.interval);
		};
		/* ------------------ */
	
})($);