$(document).ready(function(){

	$("input.autocomplete").attr("autocomplete","off");
	
	// START button scripts
		$(".btn").hover( 
			function() {
				$(this).addClass("contrast");
			},
			function() {
				$(this).removeClass("contrast");
			}
		);
		
		/* 
		   simulate click on btn styled button graphics (obviously the anchors and buttons will work just fine)
		   Oh, and yes, all this nonsense is required because for some unknown reason jQuery can't replicate
		   an anchor click, it can only trigger an onclick event. Dur!
		*/
		
		$("div.btn").mousedown( function(e) {
			if ($("a", this).length > 0) {// there's an anchor
				if ($('a[href*=.pdf],a[href*="://"]:not(a[href^="http://www.domain-safe.net/"]):not(a[href^="http://manager.domain-safe.net/"])', this).length > 0) {
					//window.open( $("a:first", this).attr("href") );         
				} else {
					location.href = $("a:first", this).attr("href");
				}
			} else if ($("button", this).length > 0) {// there's a button
				
			}
			return false;
		});
	// END button scripts
	
	// START open in new window script
		$('a[href*=.pdf],a[href*="://"]:not(a[href^="http://www.domain-safe.net/"]):not(a[href^="http://manager.domain-safe.net/"])').each (function() {
			$(this).attr("title", "This link will open in a new window");
			$(this).bind("click", function() {
				window.open( $(this).attr('href') );
				return false;
			});
		});
	// END open in new window script
	
	$(".selectRow").live("click", 
		function() {
			if ($(this).closest("tr").hasClass("selected")) {
				$(this).closest("tr").removeClass("selected");
			} else {
				$(this).closest("tr").addClass("selected");
			}
		}
	);
	
	$("#report_problem").live("click", 
		function() {
			href = $(this).attr("href");
			contents = $("html").html();
			$.post("ajax/write_error_file.php", {html_contents: contents},
				function(data) {
					location.href = href;
				}
			);
			return false;
		}
	);
	
	requiredFields('init');
	
	$(window).resize(function(){
		requiredFields('update');
	});
	
	// remove legends
	$("legend").css("display","none");
});

function requiredFields(status) {
	$(".required-star").remove();
	$("form").each( function() {
		maxLeft = 0;
		$(".required", $(this)).after("<img src='/img/star.png' class='required-star' alt='required field' title='required field'>");
		// next functionality moves the stars so they're inline with each other to the right of the box
		$(".required-star").each(function() {
			thisLeft = $(this).position().left;
			if (thisLeft > maxLeft) maxLeft = thisLeft;
		});
		$(".required-star").css("left", maxLeft + 'px');
		if (status == 'init') {
			if ($(".required", $(this)).length > 0) $(this).after("<p><img src='/img/star.png' alt='required field' title='required field'> <em>This marks a field which is mandatory and must be filled in.</em></p>");
		}
	});
}
function selectChecks(this_elem, mode, class_name) { // simulate "click" event on all matching classes for THAT element's form only
	if (mode == "all") {
		check_var = ":not(:checked)";
	} else if (mode == "none") {
		check_var = ":checked";
	} else {
		check_var = "";
	}
	if ($(this_elem).closest("fieldset").length > 0) {
		$(this_elem).closest("fieldset").find("." + class_name + check_var).click();
	} else {
		$(this_elem).closest("form").find("." + class_name + check_var).click();
	}
}
function js_dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);
}

function bookmark_me() {
	if (document.all) {
		window.external.AddFavorite(location.href,document.title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(document.title,location.href,'');
	} else {
		alert("Please press Ctrl + D to bookmark this site");
	}
}
