(function($) {
	$(document).ready(function() {
		/* Form Default Values */
		$('input[type="text"]').each(function() { $(this).attr("defValue", $(this).val()); });
		
		$('input[type="text"]').focus(function() {
			if($(this).val() == $(this).attr("defValue")) $(this).val("");
			
		}).blur(function() {
			if($(this).val() == "") $(this).val($(this).attr("defValue"));
			
		});
		
		
		/* Offsite Links Open In New Window (target: _blank) */
		//$('a:not([href^="http://' + window.location.host + '"])').attr("target", "_blank");
		
		
		/* Image Hover */
		image_hover = function() {
			var src = $(this).attr("src");
			var src2 = $(this).attr("src2");
			if(src2) $(this).attr("src", src2).attr("src2", src);
			
		};
		
		$('input[type="image"]').hover(image_hover, image_hover);
		
		
		/* AJAX Aweber */
		$('[rel="ajax_aweber"]').submit(function() {
			aweber = {};
			$("input", $(this)).each(function() {
				aweber[$(this).attr("name")] = $(this).val();
				
			});


			$.post($(this).attr("action"), aweber);
			
			
			$("#optin_form", $(this)).fadeOut("slow");
			$("#optin_thankyou", $(this)).fadeIn("slow");
			return false;
			
		});
		
		/* AJAX Window */
		$('a[rel="ajax"]').click(function() {
			_window = $("<div></div>");
			_window
				.click(function() {
					$("#ajax_popup").remove();
					$(this).remove();
					
				})
				.css({
					position: "fixed",
					zIndex: 100,
					top: 0,
					left: 0,
					width: "100%",
					height: "100%",
					background: "black",
					opacity: .75
					
				});
				
			$("body").append(_window);


			_popup = $("<div></div>");
			_popup.attr("id", "ajax_popup");
			
			
			$(".content").append(_popup);
			
			
			$("#ajax_popup").load(
				$(this).attr("href") + " #body",
				{},
				function() {
					$("#body", $("#ajax_popup"))
						.attr("id", "ajax_inside")
						.attr("class", "");
					
					
					_height = $("#ajax_popup").height() + 20;
					if(_height > $(window).height() - 100) _height = $(window).height() - 100;
					
					_width = 600;
					if(_width > $(window).width() - 100) _width = $(window).width() - 100;
					
					_top = ($(window).height() - _height) / 2;
					_left = ($(window).width() - _width) / 2;
							
							
					$("#ajax_popup").attr("id", "ajax_popup")
						.css({
							position: "fixed",
							zIndex: 150,
							top: _top,
							left: _left,
							width: _width - 20,
							height: _height - 20,
							background: "white",
							padding: "10px",
							"-moz-border-radius": "1em",
							"-webkit-border-radius": "1em",
							"border-radius": "1em"
						});
					
					$("#ajax_inside").css({
						overflow: "auto",
						width: _width - 30,
						height: _height - 20,
						paddingRight: "10px"
					});
					
				}
			);
			
			
			return false;
			
		});
		
		/* Exit Popup */
		//exit_call = true;
		
		/*
window.onbeforeunload = function(){
			if (exit_call) {
				exit_call = false;
				location.href = 'http://solarpowerformula.com/thanks-for-not-leaving';
				
				return 'Are you sure you want to leave?';
			}
			
		}
*/
		
		
	});

})(jQuery);
