// 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.
//
if(false) {//,,,ie8 doesn't like this for some reason
$(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;small=1');
}

var infoCards = {
	_urls: {
		g: '/pub/group_card?id=',
		u: '/person/card?id='
	},
	_cards: { },
	_show: function($link, k) {
		if($link.attr('info_card') == k)	// See cancel() notes.
			$link.closest('.infoCardLink').append(this._cards[k]);
		$link.removeAttr('info_card');
		return;
	},

	display: function(link) {
		var $link = $(link);
		var type  = $link.attr('info_card_type');
		var id    = $link.attr('info_card_id');
		if(!type || !id)
			return false;

		//
		// 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 and draws things all over the damn place.
		//
		if($.browser.msie
		&& $.browser.version.indexOf('7') == 0) {
			$('div.infoCardLink').each(function() {
				$(this).css('zIndex', 10);
			});
			$link.closest('div.infoCardLink').css('zIndex', 150);
		}

		var k = type + id;
		$link.attr('info_card', k);
		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;
	},

	//
	// Note that calling cancel() doesn't fully cancel things, it just tells
	// us not to display the card when we get it from the server. We could
	// cancel the $.get() but that would be pointless busy work: the server
	// doesn't have to do that much to generate a single card and we use
	// .hoverIntent() to avoid firing a ton of pointless hover events. We
	// throw the card in the cache and _show() does nothing if the card
	// isn't the current one.
	//
	cancel: function(link) {
		$(link).removeAttr('info_card');
		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(';')
		     : '';
	},
	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;
	},
	sendMessage: function(id, lang) {
		window.Shadowbox.open({
			player:  'iframe',
			content: lang + '/pub/group_message' + this._args(lang, { id: id }),
			title:   'Send a Message',
			width:   600,
			height:  625
		});
		return 0;
	}
};

var popup = {
	dialogFull: function(url, title) {
		window.Shadowbox.open({
			player:  'iframe',
			content: url,
			title:   title
		});
		return 0;
	},
	dialog: function(url, title, width, height) {
		window.Shadowbox.open({
			player: 'iframe',
			content: url,
			title:   title,
			width:   width,
			height:  height
		});
		return 0;
	}
};

function userMuggest(items, q) {
	var html  = '';
	var match = new RegExp(q, 'ig');
	var repl  = '<span class="muggestMatch">' + escapeHTML(q) + '</span>';
	for(var i = 0; i < items.length; ++i) {
		var userid = escapeHTML(items[i].userid);
		var name   = items[i].name ? '<br />(' + escapeHTML(items[i].name) + ')' : '';
		html += '<li class="' + (i % 2 ? 'rowEven' : 'rowOdd') + '" muggest="' + userid + '"><div>';
		html += '<img src="' +  items[i].avatar + '" width="32" height="32" class="panel" />';
		html += '<div class="panel">' + userid.replace(match, repl) + name + '</div>';
		html += '<div class="clearFloats"></div></div>';
	}
	return html;
}

function groupMuggest(items, q) {
	var html  = '';
	var match = new RegExp(q, 'ig');
	var repl  = '<span class="muggestMatch">' + escapeHTML(q) + '</span>';
	for(var i = 0; i < items.length; ++i) {
		var name    = escapeHTML(items[i].name);
		var creator = escapeHTML(items[i].creator);
		var spec    = creator + ': ' + name;
		html += '<li class="' + (i % 2 ? 'rowEven' : 'rowOdd') + '" muggest="' + spec + '"><div>';
		html += '<img src="' +  items[i].avatar + '" width="32" height="32" class="panel" />';
		html += '<div class="panel">' + name.replace(match, repl) + '<br />' + creator + '</div>';
		html += '<div class="clearFloats"></div></div>';
	}
	return html;
}

function setGeocode(countryList, geocodeID, geocodeMap, defGeocode) {
	var list = document.getElementById(geocodeID);

	list.options.length = 0;
	list.options[0] = new Option('-', '', false, false);

	var c    = countryList.options[countryList.selectedIndex].value;
	var geos = geocodeMap[c];
	if(!geos) {
		return;
	}

	for(var i = 0; i < geos.length; ++i) {
		list.options[list.options.length] = new Option(geos[i].name, geos[i].code, false, geos[i].code == defGeocode);
	}
}

// $HeadURL: http://localhost/svn/isos/tmpl/static/util.js $
// $Id: util.js 6867 2011-09-13 04:34:33Z mu $

