function var_dump(element, limit, depth) {
	depth = depth ? depth : 0;
	limit = limit ? limit : 1;

	returnString = '<ol>';

	for (property in element) {
		// Property domConfig isn't accessable
		if (property != 'domConfig') {
			returnString += '<li><strong>' + property + '</strong> <small>('
					+ (typeof element[property]) + ')</small>';

			if (typeof element[property] == 'number'
					|| typeof element[property] == 'boolean')
				returnString += ' : <em>' + element[property] + '</em>';
			if (typeof element[property] == 'string' && element[property])
				returnString += ': <div style="background:#C9C9C9;border:1px solid black; overflow:auto;"><code>' + element[property]
						.replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;') + '</code></div>';

			if ((typeof element[property] == 'object') && (depth < limit))
				returnString += var_dump(element[property], limit, (depth + 1));

			returnString += '</li>';
		}
	}
	returnString += '</ol>';

	if (depth == 0) {
		winpop = window.open("", "",
				"width=800,height=600,scrollbars,resizable");
		winpop.document.write('<pre>' + returnString + '</pre>');
		winpop.document.close();
	}

	return returnString;
}

function addOpenSearch(name, ext, cat, pid, meth) {
	if ((typeof window.external == "object")
			&& ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
		// See bugs 430058/430067/430070 for Camino
		if (((typeof window.external.AddSearchProvider == "unknown") || (window.navigator.vendor == 'Camino'))
				&& meth == "p") {
			alert("This plugin uses POST which is not currently supported by your browser's implementation of OpenSearch.");
		} else {
			window.external
					.AddSearchProvider("http://mycroft.mozdev.org/installos.php/"
							+ pid + "/" + name + ".xml");
		}
	} else {
		alert("You will need a browser which supports OpenSearch to install this plugin.");
	}
}

function report_dl(event) {
	event.preventDefault(); // stop default browser behaviour
	// uri = $(this).attr('href');
	var uri = $(this).attr('request').replace(/\s/g, '+');
	$(".report_link").html('<img alt="Loading..." src="/img/loader.gif" />');
	$(".report_link").load(uri, function(response, status, xhr) {
		// $(".report_link").parent().html(response);
			$(".report_link").replaceWith(response);
		});
	return false;
}

function rate_dl(event) {
	event.preventDefault(); // stop default browser behaviour
	// uri = $(this).attr('href');
	var uri = $(this).attr('request').replace(/\s/g, '+');
	$("#rater").load(uri, function() {
		$('#rater li span').click(rate_dl);
	});
	return false;
}

function ajax_click(event) {
	event.preventDefault(); // stop default browser behaviour
	var uri = $(this).attr('request').replace(/\s/g, '+');
	$(this).html('<img alt="Loading..." src="/img/loader.gif" />');
	$(this).load(uri, function(response, status, xhr) {
		// $(this).removeClass('fakeurl');
			// $(this).html(response);
			$(this).replaceWith(response);
		});
	return false;
}

function fakeurl_ajax_onclick(obj) {
	obj.click(ajax_click);
}

function freehost_more_ajax_onclick(event) {
	event.preventDefault(); // stop default browser behaviour
	var uri = $(this).attr('request').replace(/\s/g, '+');
	var $parent = $(this).parent();

	$parent.html('<img alt="Loading..." src="/img/loader.gif" />');
	$parent.parent().load(uri);
}

function file_part_change(event) {
	var url_index = $("#file_part").val();
	$("#url_frame").attr("src", links_array[url_index]);
}

function confirm_ajax_onclick(obj) {
	obj.click(ajax_click);
	obj.confirm({
		msg : 'Are You Sure?&nbsp;&nbsp;&nbsp;',
		timeout : 10000,
		dialogShow : 'fadeIn',
		dialogSpeed : 'slow',
		buttons : {
			separator : '&nbsp;&nbsp;/&nbsp;&nbsp;'
		}
	});
}

function download_part_onclick(obj) {
	obj.live('click', function(event) {
		$(this).parent('form').submit();
	});

	/*
	 * obj.click(function(event) { $(this).parent('form').submit(); });
	 */
}

function download_part_form_fix(obj) {
	if ($.browser.webkit) {
		obj.each(function(index) {
			// $(this).attr('action', '#' + Math.floor(Math.random()*101));
				$(this).attr('action', '#' + index);
			});
	};
}

function highlight_onload(obj) {
	$.each($("p.s_result span").text().split(" "), function(idx, val) {
		if (val != '') {
			$(".w_title").highlight(val);
		}
	});
}

function show_hide_links() {
	var $obj = $(this).parents(".source_url").find(".url_wrap");

	if ($(this).hasClass('show')) {
		$(this).removeClass('show').addClass('hide').text('hide');

		if ($.trim($obj.html()).length === 0) {
			// load ajax
			$obj
					.html('<ul class="url"><li><img alt="Loading..." src="/img/loader.gif" /></li></ul>');
			$obj.slideDown("slow");

			var dl_w = $(this).parents(".w").attr("id");
			var result = dl_w.match(/\d+/);
			var dl_id = result[0];
			$obj.load('/ajax/dl-info-browse/id/' + dl_id);
		} else {
			$obj.slideDown("slow");
		}
	} else {
		$(this).removeClass('hide').addClass('show').text('show');
		$obj.slideUp("slow");
	}
}

function search_advanced_submit() {
	var $form = $(this);
	// var_dump($(this));
	// alert($form.attr('action'));

	$('#results').load($form.attr('action'), $form.serializeArray(),
			function(response, status, xhr) { // var_dump(response, 1);
				// alert(response);
			$('#results').replaceWith(response);
		});

	return false;
}

function selecttext_setup(obj) {
	obj.live('focus', function(event) {
		$(this).select();
	});
}

function clipcopy_setup(obj) {
	var clip = null;
	ZeroClipboard.setMoviePath('/js/ZeroClipboard.swf');

	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	clip.setHandCursor(true);

	// assign a common mouseover function for all elements using jQuery
	obj.mouseover(function() {
		// set the clip text to our innerHTML
			clip.setText(this.value);

			// reposition the movie over our element
			// or create it if this is the first time
			if (clip.div) {
				clip.receiveEvent('mouseout', null);
				clip.reposition(this);
			} else
				clip.glue(this);

			// gotta force these events due to the Flash movie
			// moving all around. This insures the CSS effects
			// are properly updated.
			clip.receiveEvent('mouseover', null);
		});

	clip.addEventListener('mouseDown', function(client) {
		// set text to copy here
			var_dump(clip);
			var_dump(client);

		});
}

