(function($) {
	var lib = window.lib = function($) {
		return {
			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) {
					settings = $.extend({
         				defaultText: "enter keyword or item"
        			}, settings);

        			return $(selector).each(function(){
						$(this).focus( function() {
							if( $(this).val() == settings.defaultText )
							{
                                $(this).val("");
                                $(this).css("text-align","left");

                            }
						});

						$(this).blur(function() {
							$(this).val( lib.utils.strTrim($(this).val()) );
							if( $(this).val() == "" )
							{
                                $(this).val( settings.defaultText );
                                 $(this).css("text-align","center");
                            }
                            else if( $(this).val() == settings.defaultText )
                            {
                                 $(this).css("text-align","center");
                            }
                            else if( ($(this).val() != settings.defaultText)&& ($(this).val() != "")  )
                            {
                                 $(this).css("text-align","left");
                            }
						});
						$(this).blur();
					});
				},
				setMaxCharacters : function(selector, settings) {
					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) {
					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);

	        		return $(selector).each(function() {
	        			var features = 	"toolbar=" + settings.toolbar + "," +
										"menubar=" + settings.menubar + "," +
										"location=" + settings.location + "," +
										"directories=" + settings.directories + "," +
										"status=" + settings.status + "," +
										"scrollbars=" + settings.scrollbars + "," +
										"resizable=" + settings.resizable;
	        			$(this).click(function(evt) {
	        				evt.preventDefault();
	        				var newWindow = window.open($(this).attr("href"), settings.name, 'width=' + settings.width + ',height=' + settings.height + ',"' + features + '"');
							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);
					addHTML = '<div id="' + selector.split("#")[1] + '"></div>';
					$("body").append(addHTML);
					$(selector).append(defaultHTML);
					lib.layer.vCenter(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) {
					$.get(page,function(data) {
						$(selector + " *").remove();
						$(selector).html("");
						$(selector).append(data);
						lib.layer.vCenter(selector);
						lib.layer.ie6Fix(selector,"u");
						lib.layer.closeButton(closeButton, selector);
					});
				},
				vCenter : function(selector) {
					var wPosition = lib.screen.position();
					var wSize = lib.screen.size();
					var top = ((wSize[1] / 2) - ($(selector).height() / 2)) + wPosition[1];
					top = (top < 0) ? 100 : top;
					$(selector).css("top",top+"px");
					lib.layer.ie6Fix(selector,"u");
				},
				closeButton : function( selector, layerSelector ) {
					$(layerSelector + " " + selector).click(function(evt) {
						evt.preventDefault();
					});
					$(layerSelector + " " + selector).unbind("click.lib.layer.close").bind("click.lib.layer.close", function() { 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="/assets/images/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() {
					if($.browser.msie && (parseFloat($.browser.version) < 7))
					{ return true; }
					return false;
				},
				timestamp : function() {
					return new Date().getTime();
				},
				strTrim : function(s) {
        			return s.replace(/^\s+|\s+$/g, "");
    			},
    			getPosition : function( selector ) {
					var positions = [];
					$(selector).each(function(i) {
						thisObj = $(this).get(0);
						var curleft = curtop = 0;
						if (thisObj.offsetParent) {
							do {
							curleft += thisObj.offsetLeft;
							curtop += thisObj.offsetTop;
							} while (thisObj = thisObj.offsetParent);
						}
						positions[positions.length] = new Array(2);
						positions[i][0] = curleft;
						positions[i][1] = curtop;
					});
					return positions
				},
				trunc : function(selector,num) {
					$(selector).each(function(i) {
						var originalText = $(this).html();
						if(originalText.length >= num) {
							var truncText = originalText.substr(0, num);
							// to remove the end whitespace if there is one
							if(truncText.charAt(truncText.length) == "") {
								truncText = truncText.substr(0, truncText.length - 1);
							}
							truncText = truncText + "...";
						}
						$(this).html(truncText);
					});
				}
			},
			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(".FormEntry") )
						{ 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).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); }
				}
			},
			about : function() {
				alert("Property: Fry, Inc. Version 1.0");
			}
		};
	}($);

	/* button Code */
	lib.obj.button.prototype.init = function(settings) {
		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",
            prevImg: null,
            nextImg: null,
			first: "#widget-slider-first",
			last: "#widget-slider-last",
			item: "div",
			direction: "vertical",
			showAmount: 3,
			scrollAmount : 1,
			circular : false,
			interval : 350,
			easing : "linear",
			preMoveCallback : null,
			postMoveCallback : null
	   }, settings);

		this.viewport = settings.viewport;
		this.content = settings.content;
		this.next = settings.next;
		this.prev = settings.prev;
        this.nextImg = settings.nextImg;
		this.prevImg = settings.prevImg;
		this.first = settings.first;
		this.last = settings.last;
		this.item = settings.item;
		this.circular = settings.circular;
		this.interval = settings.interval;
		this.easing = settings.easing;
		this.direction = settings.direction;
		this.showAmount = settings.showAmount;
		this.sliderInfo = new Object();
		this.sliderInfo.index = 0;
		this.scrollAmount = settings.scrollAmount;
		this.sliderInfo.end = $(this.item).size() - this.showAmount;
		this.preMoveCallback = settings.preMoveCallback;
		this.postMoveCallback = settings.postMoveCallback;

		/* 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.circular && ($(this.item).size() > this.showAmount))
			{
				$(this.next).show();
				$(this.prev).show();
			}
			else if( !this.circular && ($(this.item).size() > this.showAmount))
			{

                $(this.prev).find("img").attr("src","/assets/images/cms/landing_featured_small/left-slider-off.jpg");
                $(this.next).show();
                $(this.prev).show();
            }

			if( this.direction == "vertical" )
			{ this.sliderInfo.itemSize = $(this.item).eq(0).height(); }
			else
			{
				this.sliderInfo.itemSize = $(this.item).eq(0).width();
				$(this.content).css("width", (this.sliderInfo.itemSize * $(this.item).size()) + "px");
			}
			this.removeEvents();

			$(this.next + "," + this.prev + "," + this.first + "," + this.last).click(function(evt){ evt.preventDefault(); });
			this.createEvents();
		}
	};

	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.createEvents = function() {
		var currObj = this;
		$(this.prev).bind("click.itemSlider", function(evt) {
			currObj.backward();
		});
		$(this.next).bind("click.itemSlider", function(evt) {
  			currObj.forward();
		});
		$(this.first).bind("click.itemSlider", function(evt){
			currObj.toFirst();
		});
		$(this.last).bind("click.itemSlider", function(evt){
			currObj.toLast();
		});
	};

	lib.obj.itemSlider.prototype.removeEvents = function() {
		$(this.prev).unbind("click.itemSlider");
		$(this.next).unbind("click.itemSlider");
		$(this.last).unbind("click.itemSlider");
		$(this.first).unbind("click.itemSlider");
   };

	lib.obj.itemSlider.prototype.move = function() {
		this.removeEvents();
		this.overflowBeforeMove();

		/* Make index is in range */
		if(this.circular)
		{
			if( this.sliderInfo.index > this.sliderInfo.end )
			{ this.sliderInfo.index = 0; }
			else if( this.sliderInfo.index < 0 )
			{ this.sliderInfo.index = this.sliderInfo.end }
		}
		else
		{
			if( this.sliderInfo.index > this.sliderInfo.end )
			{ this.sliderInfo.index = this.sliderInfo.end; }
			else if( this.sliderInfo.index < 0 )
			{ this.sliderInfo.index = 0 }
		}
		/* --------------- */

		/* show/hide buttons */
		if( !this.circular )
		{
			 $(this.next).show();
            $(this.prev).show();

            if( this.sliderInfo.index == this.sliderInfo.end )
			{
                //$(this.next).hide();
                $(this.next).find("img").attr("src","/assets/images/cms/landing_featured_small/right-slider-off.jpg");
            }
			else if(this.sliderInfo.end > 0 )
			{
                //$(this.next).show();
                $(this.next).find("img").attr("src",this.nextImg);
            }

			if( this.sliderInfo.index == 0 )
			{
                //$(this.prev).hide();
                $(this.prev).find("img").attr("src","/assets/images/cms/landing_featured_small/left-slider-off.jpg");
            }
			else
			{
                //$(this.prev).show();
                $(this.prev).find("img").attr("src",this.prevImg);
            }
		}
		/* ----------------------- */

		if($.isFunction(this.preMoveCallback))
		{ this.preMoveCallback(); }

		/* Move the item */
		var currObj = this;
		var newPos = this.sliderInfo.index * this.sliderInfo.itemSize;
		if (this.direction == "vertical")
		{ var params = { scrollTop : newPos }; }
		else
		{ var params = { scrollLeft : newPos }; }

		$(this.viewport).animate(
			params,
			{
				duration : this.interval,
				easing : this.easing,
				complete : function() {
					currObj.overflowAfterMove();
					currObj.createEvents();
					if($.isFunction(currObj.postMoveCallback))
					{ currObj.postMoveCallback(); }
				}
			}
		);
		/* ---------------------- */
	};

	lib.obj.itemSlider.prototype.forward = function() {
		this.sliderInfo.index += this.scrollAmount;
		this.move();
	};

	lib.obj.itemSlider.prototype.backward = function() {
		this.sliderInfo.index -= this.scrollAmount;
		this.move();
	};

	lib.obj.itemSlider.prototype.toFirst = function() {
		this.sliderInfo.index = 0;
		this.move();
	};

	lib.obj.itemSlider.prototype.toLast = function() {
		this.sliderInfo.index = this.sliderInfo.end;
		this.move();
	};
	/* ----------------------- */

	/* contentCollection */
	lib.obj.contentCollection.prototype.init = function(settings) {
		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();
	};
	/* ------------------ */

})($);
