// Copyright (c) 2009 Isos Media Ltd.

function cmpNum(a, b) {
	return a - b;
}

// tweaked to force empty strings to the end.
function cmpStr(a, b) {
	a = a.toLowerCase();
	b = b.toLowerCase();
	if(a.length == 0 && b.length == 0)
		return 0;
	else if(a.length == 0)
		return 1;
	else if(b.length == 0)
		return -1;
	else if(a < b)
		return -1;
	else if(a > b)
		return 1;
	return 0;
}

// based on prototype's
function escapeHTML(s) {
	var div = document.createElement('div');
	div.appendChild(document.createTextNode(s));
	return div.innerHTML;
}

function returnFalse() {
	return false;
}

//
// Can't use the disabled attribute here 'cuz that keeps the browser
// from sending the button back to the server and we like to know which
// button was pressed now and then.
//
// Sometimes we need to suppress the magic (such as a form that sends
// back a zip file) so you can add the noSubmitMagic class to the submit
// button to keep the magic away.
//
$(document).ready(function() {
	// Take care of back-button issues first.
	$('form').find(':submit').unbind('click', returnFalse);
	// Then wire up the auto-disabling stuff.
	$('form').submit(function() {
		$(this).find(':submit :not(.noSubmitMagic)').click(returnFalse);
		return true;
	});
});

function likeAd(a, id) {
	$(a).parent().load('/pub/rate?ad=' + id + ';rating=1;ajax=1');
}

var infoCards = {
	urls: {
		g: '/pub/group_card?id=',
		u: '/person/card?id='
	},
	cards: { },
	show: function($link, k) {
		$link.closest('.avatarLink').append(this.cards[k]);
		return;
	},
	display: function(link, type, id) {
		var $link = $(link);

		if($.browser.msie
		&& $.browser.version.indexOf('7') == 0) {
			//
			// IE7, you are a reeking pile of stupid; the stupid isn't nearly as
			// dense as IE6's stupid but it is dense enough to cause a lot of
			// teeth gnashing. I hate you.
			//
			// This nonsense is needed or IE7 gets confused by box://user and
			// draws things all over the damn place.
			//
			$('div.avatarLink').each(function() {
				$(this).css('zIndex', 10);
			});
			$link.closest('div.avatarLink').css('zIndex', 150);
		}
		else if($.browser.opera) {
			//
			// Opera, your positioning is full of confusion. I don't hate you
			// Opera but you're not popular enough to warrant a proper fix.
			//
			window.open($link.attr('href'));
			return false;
		}

		var k = type + id;
		if(!infoCards.cards[k]) {
			var cursor = $link.css('cursor');
			$link.css('cursor', 'progress');
			$.get(infoCards.urls[type] + id, function(data) {
				$link.css('cursor', cursor);
				infoCards.cards[k] = data;
				infoCards.show($link, k);
			});
		}
		else {
			infoCards.show($link, k);
		}
		return false;
	}
};

var groupWin = {
	_args: function(lang, p) {
		var args = [ ];
		for(var k in p) {
			if(p[k] && k == 'where_next') {
				args.push(encodeURIComponent(k) + '=' + encodeURIComponent(lang + p[k]));
			}
			else if(p[k]) {
				args.push(encodeURIComponent(k) + '=' + encodeURIComponent(p[k]));
			}
		}
		return args.length
		     ? '?' + args.join(';')
		     : '';
	},
	bio: function(gid, uid, userid, lang) {
		window.Shadowbox.open({
			player:  'iframe',
			content: lang + '/person/group_bio' + this._args(lang, { g: gid, u: uid }),
			title:   'Member Bio: ' + escapeHTML(userid),
			width:   500,
			height:  500
		});
		return 0;
	},
	request: function(id, lang, url) {
		window.Shadowbox.open({
			player:  'iframe',
			content: lang + '/pub/group_request' + this._args(lang, { id: id, where_next: url }),
			title:   'Request Group Membership',
			width:   500,
			height:  500
		});
		return 0;
	}
};

// $HeadURL: http://192.168.1.66/svn/isos/tmpl/static/util.js $
// $Id: util.js 5334 2010-03-02 21:37:01Z mu $
