$(document).ready(function() {
	var empty_field = $('#news-piece-urls .url-section:last');
	if (empty_field.length) {
		var empty_field_html = '<span id="add-url-control">' + empty_field[0].innerHTML + '</span>';
		var urls_section = $('#news-piece-urls');
		empty_field.remove();
		urls_section.append('<span id="add-url-control"><br/><a href="#">add a new URL</a></span>');
		var add_url_control = $('#add-url-control');
		add_url_control.click(function (event) {
			event.preventDefault();
			console.log(empty_field_html);
			add_url_control.before(empty_field_html);
		});
	}
});

// Inspired by http://jehiah.cz/archive/prototype-powered-popup-script
jQuery.fn.popup = function(options) {
	this.options = {
		url: this.get(0).href,
		width: 800,
		height: 600,
		name: "_blank",
		location: "no",
		menubar: "no",
		toolbar: "no",
		status: "yes",
		scrollbars: "yes",
		resizable: "yes",
		left: "",
		top: "",
		normal: false
	}
	this.options = jQuery.extend(this.options, options || {});
   
	if (this.options.normal){
		this.options.menubar = "yes";
		this.options.status = "yes";
		this.options.toolbar = "yes";
		this.options.location = "yes";
	}
   
	this.options.width  = this.options.width  < screen.availWidth  ? this.options.width  : screen.availWidth;
	this.options.height = this.options.height < screen.availHeight ? this.options.height : screen.availHeight;

	var openoptions = 'width=' + this.options.width + ',height=' + this.options.height + ',location=' + this.options.location + ',menubar=' + this.options.menubar + ',toolbar=' + this.options.toolbar + ',scrollbars=' + this.options.scrollbars + ',resizable=' + this.options.resizable + ',status=' + this.options.status;
	
	if (this.options.top != "") {
		openoptions += ",top=" + this.options.top;
	}
	if (this.options.left != "") {
		openoptions += ",left=" + this.options.left;
	}
	window.open(this.options.url, this.options.name,openoptions);
	
	return false;
}