(function($) {
	
	// Popup Status
	var popupStatus = 0;
	
	var lib = window.lib = function() {
		return {

			popup : {
				
				/** Load the Popup **/
				load : function() {
					
					// If not Enabled, Enable it
					if (popupStatus == 0) {
						$("#backgroundPopup").css({"opacity":"0.7"});
						$("#backgroundPopup").fadeIn("slow");
						$("#popupContact").fadeIn("slow");
						popupStatus = 1;
					}
					
				},
				
				/** Disable Popup **/
				disable : function() {
					
					// If not disabled, disable it
					if (popupStatus == 1) {
						$("#backgroundPopup").fadeOut("slow");
						$("#popupContact").fadeOut("slow");						
					}
					popupStatus = 0;
					
				},
				
				/** Center the Popup **/
				center : function() {
					
					// Get the Window & Popup Size
					var windowWidth = site.windowWidth();
					var windowHeight = site.windowHeight();
					var popupWidth = $("#popupContact").width();
					var popupHeight = $("#popupContact").height();
									
					// Center the Popup
					$("#popupContact").css({
						"position":"absolute",
						"top":(windowHeight/2)-(popupHeight/2)+(document.documentElement.scrollTop),
						"left":windowWidth/2-popupWidth/2
					});
					
					$("#backgroundPopup").css({
						"height":windowHeight
					}); // For IE 6
				}
				 
			},
			
			format : {
				
				currency : function(amount) {
					
					var i = parseFloat(amount);
					if(isNaN(i)) { i = 0.00; }
					var minus = '';
					if(i < 0) { minus = '-'; }
					i = Math.abs(i);
					i = parseInt((i + .005) * 100);
					i = i / 100;
					s = new String(i);
					if(s.indexOf('.') < 0) { s += '.00'; }
					if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
					s = minus + "$ "+ s;
					return s					
					
				}
				
			},
			
			math : {
				
				add : function(value1, value2) {
					return (value1 * 1) + (value2 * 1);
				}
			},
			
			dialog : {
				
				Ok : function(title, message) {
					
					var $dialog = $('<div></div>')
					.html(message)
					.dialog({
						autoOpen: false,
						title: title,
						modal: true,
						buttons: {
							Ok: function() {
								$( this ).dialog( "close" );
							}
						}
					});
					
					$dialog.dialog('open');
					
				}
				
			},
			
			print : function(title, data) {
				
				var aWindow = window.open('', title, 'height=600,width=600');
				aWindow.document.write('<html><head><title>' + title + '</title>');
				aWindow.document.write('<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />');
				aWindow.document.write('</head>');
				aWindow.document.write('<body>');
				aWindow.document.write('<div class="printTitle">' + title + '</div><br/>');
				aWindow.document.write(data);
				aWindow.document.write('</body></html>');
				aWindow.close();
				aWindow.print();
				return true;
				
			}, 
						
			input : {
			
				defaultValue : function(selector, value) {
					
					return $(selector).each(function(){
						
						$(this).focus( function() {
							if( $(this).val() == value )
							{ 
								$(this).val(""); 
								$(this).css('text-align','left');
							}
						});
			
						$(this).blur(function() {
							$(this).val( lib.utils.sTrim($(this).val()) );
							if( $(this).val() == "" )
							{ 
								$(this).val( value );
								$(this).css('text-align','center');
							}
						});
						$(this).blur()						
						
					});
					
				}
				
			},
			
			selector : {
				
				reset : function(selector) {
					
					if (selector == null) {
						return;
					}
					
					var labelName = $(selector).parent().children(".label").text();
					
					if (labelName == null || labelName == "") {
						labelName = "Value";
					}
					
					var selectText = "Select a " + labelName;
					var value = "";
					
					$(selector).children().remove();
					$(selector).append($("<option></option>").attr("value", value).text(selectText));
					
				},
				
				decrement : function(selector) {

					var q = $(selector).val() * 1;
					var x = q;
					
					if (q > 1) {
						q--;
						$(selector).val(q);
					}
					
					return x;
					
					
				},
				
				increment : function(selector) {

					var q = $(selector).val() * 1;
					var x = q;
					q++;
					$(selector).val(q);
					
					return x;

				}
				
			},
			
			utils : {
				
				sTrim : function(s) {
					while(s.substring(0,1) == ' '){
						s = s.substring(1,s.length);
					}
					while(s.substring(s.length-1,s.length) == ' '){
						s = s.substring(0,s.length-1);
					}
					return s;
				} 
				
			},
			
			image : {
				
				off : function(selector){
					
					$(selector).mouseleave(function(){
		   				var imgUrl = $(this).attr('src');
		   				imgUrl = imgUrl.replace("-On.jpg", "-Off.jpg");
		   				$(this).attr('src',imgUrl);
		   			});
					
				},
				on : function(selector){
					$(selector).mouseover(function(){
		   				var imgUrl = $(this).attr('src');
		   				imgUrl = imgUrl.replace("-Off.jpg", "-On.jpg");
		   				$(this).attr('src',imgUrl);
		   			});					
					
				},
					
				preload : function(image) {
					
					$(image).each(function() {
						$('<img />').attr('src',this).appendTo('body').css('display','none');
					});
					
				}					
				
			}
		
		};
	}($)
	
})($);

