function textCounter(obj, counterField, maxLength)
{
	if ( obj.value.length > maxLength )
	{
		obj.value = obj.value.substring(0, maxLength);
	}

	count = maxLength - obj.value.length;
	counterField.value = count;
}

function select_switch(obj, status)
{
	for (i = 0; i < obj.length; i++)
	{
		obj.elements[i].checked = status;
	}
}

function editStatusAjaxCallback(response)
{
	if (!response['success'])
	{
		alert('Error while editing status...');
	}
}

function editStatusOnSubmit()
{
	var status = document.frmEditStatus.status.value;
	$.post(_scripturl + 'account/editprofile.php?act=ajax-editstatus', {status: status}, editStatusAjaxCallback, 'json');

	$('#status').text(status).fadeIn();
	$('#editStatusButton').fadeIn();
	$('#editStatusLayer').hide();
}

function editStatus()
{
	$('#status, #editStatusButton').hide();
	$('#editStatusLayer').fadeIn();
	var status = $('#status').text() || '';

	frm = document.frmEditStatus;
	frm.status.value = status;
	frm.status.focus();
}

var ARDNI = {};

ARDNI.pngFix = function(selectors) {
	if (jQuery.browser.msie && navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && (navigator.appVersion.indexOf("MSIE 5.5") != -1 || navigator.appVersion.indexOf("MSIE 6.0") != -1)) {
		$(selectors).each(function() {
			$this = $(this);

			$this.attr('width', $this.width());
			$this.attr('height', $this.height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = ($this.attr('id')) ? 'id="' + $this.attr('id') + '" ' : '';
			var imgClass = ($this.attr('class')) ? 'class="' + $this.attr('class') + '" ' : '';
			var imgTitle = ($this.attr('title')) ? 'title="' + $this.attr('title') + '" ' : '';
			var imgAlt = ($this.attr('alt')) ? 'alt="' + $this.attr('alt') + '" ' : '';
			var imgAlign = ($this.attr('align')) ? 'float:' + $this.attr('align') + ';' : '';
			var imgHand = ($this.parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + $this.width() + 'px;' + 'height:' + $this.height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $this.attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + $this.width() + 'px;' + 'height:' + $this.height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			$this.hide();
			$this.after(strNewHTML);
		});
	}
}

ARDNI.searchbar = {
	defaultSearchText: 'Search profiles or web...',
	hover: false,
	init: function() {
		$('#searchbar-query')
			.val(ARDNI.searchbar.defaultSearchText)
			.css('color', '#aaa')
			.focus(function() {
				$('#searchbar-options').fadeIn(200);
				if (this.value == ARDNI.searchbar.defaultSearchText) {
					this.value = '';
					this.style.color = '#222';
				}
			})
			.blur(function() {
				if (!ARDNI.searchbar.hover) {
					$('#searchbar-options').fadeOut(500);
					if (this.value == '') this.value = ARDNI.searchbar.defaultSearchText;
					if (this.value == ARDNI.searchbar.defaultSearchText) this.style.color = '#999';
				}
			});
		$('#searchbar')
			.hover(function() { ARDNI.searchbar.hover = true; }, function() { ARDNI.searchbar.hover = false; })
			.find('#searchbar-form')
				.submit(function(e) {
					var query = $('#searchbar-query', this).val(), type = $('#searchbar-searchtype', this).val();
					if (query == '' || query == ARDNI.searchbar.defaultSearchText) {
						e.preventDefault();
						$('#searchbar-query').focus();
					}
					switch(type) {
						case 'web':
							e.preventDefault();
							ARDNI.searchbar.submitWebSearch(query);
						break;

						case 'group':
							$(this).attr('action', _scripturl + 'groups/index.php');
							$('#searchbar-query', this).attr('name', 'k');
						break;

						default:
							$(this).attr('action', _scripturl + 'account/members.php');
							$('#searchbar-query', this).attr('name', 'username');
						break;
					}
				});
		$('#searchbar-options').click(function() { $('#searchbar-query').focus(); });
	},
	switchType: function(anchor, type) {
		$('#searchbar-searchtype').val(type);
		$('#searchbar-options a').removeClass('current');
		$(anchor).addClass('current');
	},
	submitWebSearch: function(q) {
		$('<form action="http://www.ardni.com/googlesearch.php" id="cse-search-box" class="searchbar_form">' +
		'<input type="hidden" name="cx" value="partner-pub-6636654976702313:y76im5s77tv" />' +
		'<input type="hidden" name="cof" value="FORID:10" />' +
		'<input type="hidden" name="ie" value="ISO-8859-1" />' +
		'<input type="text" name="q" value="" />' +
		'<input type="hidden" name="sa" value="Search" />' +
		'</form>')
			.append($('<input type="text" name="q" />').val(q))
			.submit();
	}
}

ARDNI.FrontpageHighlights = {
	delay: 3000,
	timeout: null,
	tabs: {},
	stories: {},
	init: function()
	{
		ARDNI.FrontpageHighlights.stories = $('#highlight-stories a');
		ARDNI.FrontpageHighlights.tabs = $('#highlight-tabs img');
		ARDNI.FrontpageHighlights.tabs.each(function(i) {
			$(this).mouseenter(function() {
				ARDNI.FrontpageHighlights.go(i);
			});
		});
		ARDNI.FrontpageHighlights.go(0);
	},
	"go": function(to)
	{
		if (to >= ARDNI.FrontpageHighlights.tabs.length)
		{
			to = 0;
		}
		ARDNI.FrontpageHighlights.tabs.each(function(i) {
			this.src = to == i ? this.src.replace('.png', '-hover.png') : this.src.replace('-hover.png', '.png');
		});
		ARDNI.FrontpageHighlights.stories.fadeOut().eq(to).fadeIn();

		if (ARDNI.FrontpageHighlights.timeout)
		{
			clearTimeout(ARDNI.FrontpageHighlights.timeout);
			ARDNI.FrontpageHighlights.timeout = null;
		}
		ARDNI.FrontpageHighlights.timeout = setTimeout('ARDNI.FrontpageHighlights.go(' + (to + 1) + ')', ARDNI.FrontpageHighlights.delay);
	}
	
}

$(function() {
	ARDNI.searchbar.init();

	$('#logo-userpages').hover(function() {
		var img = $('#logo', this).get(0);
		img.src = img.src.replace('.png', '-hover.png');
	}, function() {
		var img = $('#logo', this).get(0);
		img.src = img.src.replace('-hover.png', '.png');
	});

	$('#myardni-userpages').hover(function() {
		var img = $('#myardni-logo', this).get(0);
		img.src = img.src.replace('.png', '-hover.png');
	}, function() {
		var img = $('#myardni-logo', this).get(0);
		img.src = img.src.replace('-hover.png', '.png');
	});

	$('#myardni').hover(function() {
		var img = $('#myardni-button > img', this).get(0);
		img.src = img.src.replace('.png', '-hover.png');
	}, function() {
		var img = $('#myardni-button > img', this).get(0);
		img.src = img.src.replace('-hover.png', '.png');
	});

	$('.dropdown').hover(function() { $(this).addClass('hover').find('ul').fadeIn(300); }, function() { $(this).removeClass('hover').find('ul').fadeOut(500); });

	//ARDNI.pngFix('#myardni-logo');
});