$(setupButtonPressEffect);
$(fixLinkButtons);
$(tipAnythingFocusBehaviour);

function tipAnythingFocusBehaviour() {
	var tipTarget = $('#tipTarget');
	if (tipTarget.size() == 0) return;
	var origValue = tipTarget.val();
	
	tipTarget.focus(function() {
		if (tipTarget.val() == origValue) tipTarget.val('');
	});
	
	tipTarget.blur(function() {
		if (!tipTarget.val()) tipTarget.val(origValue);
	});
}

function fixLinkButtons() {
	$("button.linkButton,input.linkButton").each(function() {
		var me = $(this);
		var replacement = $('<a href="#" class="linkButton"></a>');
		replacement.html(me.html());
		replacement.click(function(event) {
			event.preventDefault();
			$(this).parents("form:first").submit();
		});
		me.replaceWith(replacement);
	});
}

function balloon(selector, content, preferTop, timeout) {
	var x = $(selector);
	x.focus();
	if (!x.data('ballooned')) {
		x.blur(function(event) {
			$(event.target).btOff();
		});
		
		x.keydown(function(event) {
			$(event.target).btOff();
		});
		
		x.click(function(event) {
			$(event.target).btOff();
		});
		x.data('ballooned', true);
	}
	
	x.bt(content, {
		trigger: 'none',
		positions: preferTop ? ['top', 'bottom'] : ['bottom', 'top'],
		strokeWidth: 1,
		cornerRadius: 10
	});
	
	setTimeout(function() {
		x.btOn();
	}, 10);
	
	if (timeout) {
		setTimeout(function() {
			x.btOff();
		}, timeout + 10);
	}
}

function setupButtonPressEffect() {
	$(".button").mousedown(depressButton);
	$(".button").mouseup(undepressButton);
}

function depressButton(event) {
	$(event.target).addClass("buttonDepressed");
}

function undepressButton(event) {
	$(event.target).removeClass("buttonDepressed");
}

// Finds all entries that line-wrap, and shortens them back to one line by replacing the overflow with ellipsis,
// then adds a popup balloon-on-hover with the full text.

// Lines are shortened by replacing it with a combination of text box which is overflow: hidden, and a small space on the
// far right for a lone ellipsis.
function eliminateWordWrap(selection) {
	setTimeout(function() {
		selection.each(function() {
			var cell = $(this);
			var height = cell.height();
			var lineHeight = 0;
			try {
				lineHeight = parseInt(cell.css("lineHeight"));
			} catch (e) {}
			if (!lineHeight) lineHeight = 20;
			if (height / lineHeight <= 3.5) return;
		
			var cellHtml = cell.html();
			var cellText = cell.text();
			cell.html("");
			var ellipsisBox = $("<div>&hellip;</div>").css({
				float: "right",
				paddingRight: "4px",
				color: "#777"
			});
			var width = cell.width();
			cell.append(ellipsisBox);
			var textBox = $("<div />").css({
				overflow: "hidden",
				whiteSpace: "nowrap",
				width: (width - ellipsisBox.width() - 6) + "px",
				height: ellipsisBox.height() + "px"
			}).html(cellHtml);
			cell.append(ellipsisBox).append(textBox);
			cell.bt(cellText, {
				width: 350,
				animate: true
			});
		});
	}, 100);
}

//Copies some content to the clipboard via flash.
function copyToCC(text) {
	var enc = encodeURIComponent(text).split("%5Cn").join("%0A");
	var fc = document.getElementById("flashCopier");
	fc.innerHTML = "";
	fc.innerHTML= '<object width="0" height="0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="movie" value="/p/base/_clipboard.swf" /><param name="flashvars" value="clipboard='+enc+'" /><embed src="/p/base/_clipboard.swf" flashvars="clipboard='+enc+'" width="0" height="0" type="application/x-shockwave-flash"></embed></object>';
}

function copyContent(idn) {
	var obj = document.getElementById(idn);
	var txt = obj.value;
	obj.select();
	
	copyToCC(txt);
	
	var copied = document.createElement("div");
	copied.style.border = "1px solid #222";
	copied.style.backgroundColor = "#FFFFDE";
	copied.style.position='absolute';
	copied.style.width='200px';
	copied.style.height='20px';
	copied.innerHTML=i18n("snippet.copied");
	var left=0;
	var top=0;
	var objo = obj;
	if (objo.offsetParent) do {
		left += objo.offsetLeft;
		top += objo.offsetTop;
	} while (objo=objo.offsetParent);
	copied.style.left=(left+obj.offsetWidth-200)+'px';
	copied.style.top=top+'px';
	var bdy = document.getElementById("fBody");
	bdy.appendChild(copied);
	setTimeout(function() {
		bdy.removeChild(copied);
	}, 2500);
	return false;
}

//color must be specified as an object with red, green, and blue components. numbers are interpreted as 0-255, strings as 0-FF.
//animates backgroundColour; assumes it starts out as transparent.

jQuery.fn.extend({
	flash: function(params) {
		var me = $(this);
		var from = params.from;
		var to = params.to;
		var speed = params.speed;
		var callback = params.callback;
		
		if (!from) from = {red: 255, green: 255, blue: 255};
		if (!to) to = {red: 255, green: 190, blue: 190};
		if (speed in $.fx.speeds) speed=$.fx.speeds[speed];
		if (typeof speed != 'number') speed=$.fx.speeds["_default"];
		
		$.each([from, to], function() {
			var o = this;
			$.each(['red', 'green', 'blue'], function() {
				if (typeof o[this] != 'number') o[this] = parseInt(o[this], 0x10);
			});
		});
		
		me.queue(function () {
			if (speed < 1) {
				if (callback) callback(this);
				me.dequeue();
				return;
			}
			
			var now = new Date().getTime();
			var interval = setInterval(function() {
				var p = -1 + 2 * (new Date().getTime() - now) / speed;
				if (p >= 1) {
					clearInterval(interval);
					if (callback) callback(this);
					me.dequeue();
				} else {
					p = (p < 0 ? -p : p);
					var red = Math.round(to.red + (from.red - to.red) * p);
					var green = Math.round(to.green + (from.green - to.green) * p);
					var blue = Math.round(to.blue + (from.blue - to.blue) * p);
					me.css("backgroundColor", "rgb(" + red + "," + green + "," + blue + ")");
				}
			}, 10);
		});
	}
});
