if(jQuery.Lightbox)
{
	jQuery.Lightbox.construct({
		'text': {
			'image': locale.lightbox.image,
			'of': locale.lightbox.of,
			'close': locale.lightbox.close,
			'closeInfo': locale.lightbox.closeInfo,
			'download': locale.lightbox.download,
		},
		'show_linkback': false,
		'show_helper_text': false,
		'show_info': true
	});
}

jQuery.noConflict();
jQuery(document).ready(function() {
	// Facebook Widget
	jQuery('.facebook').hover(
		function() {
			jQuery('.facebook').stop(true, false).animate({left:'0'}, 'medium');
		},
		function() {
			jQuery('.facebook').stop(true, false).animate({left:'-205'}, 'medium');
		}
	);

	// Accordion Navigation
	var opts = {
		active: 'div.side-label:first-child',
		header: 'div.side-label',
		alwaysOpen: false,
		autoHeight: false
	}
	if(jQuery.browser.msie)
		opts.animated = false;

	if(jQuery('div#Navigation').length > 0)
		jQuery('div#Navigation').accordion(opts);

	// Side-label scrolling
	jQuery('div#User div.side-label').click(function() {
		jQuery('#' + jQuery(this).attr('id') + '_box').toggle('slow');
		return false;
	});

	// Article scrolling
	jQuery('a#Article').click(function() {
		jQuery('#' + jQuery(this).attr('rel')).toggle('slow');
		return false;
	});

	// Form input value fading
	jQuery('form[name=loginform] input[type=text], form[name=loginform] input[type=password], #shout_name').focusin(function() {
		if(jQuery(this).val() == locale.input)
			jQuery(this).val('');
	});

	jQuery('form[name=loginform] input[type=text], form[name=loginform] input[type=password], #shout_name').focusout(function() {
		if(jQuery(this).val() == '')
			jQuery(this).val(locale.input);
	});

	// Panel scrolling
	jQuery('img#prebox').click(function() {
		var target = jQuery(this).attr('rel');
		jQuery('#' + target).toggle('slow');

		var tmp = jQuery(this).attr('src'), status = 'block';
		if(tmp.indexOf('_on') == -1) {
			status = 'none';
			jQuery(this).attr('src', tmp.replace('_off', '_on'));
		} else
			jQuery(this).attr('src', tmp.replace('_on', '_off'));

		jQuery.cookie(target, status);
		return false;
	});

	// Status save
	jQuery('#statusSubmit').click(function() {
		jQuery('#statusInfo').html(locale.status.saving);
		jQuery('#statusInfo').toggle('slow');

		jQuery.ajax({
			type: 'POST',
			url: path + 'infusions/user_info_panel/user_info_result.php',
			data: 'statusInput=' + jQuery('#statusInput').val(),
			success: function(data, textStatus, XMLHttpRequest) {
				jQuery('#statusInfo').html(locale.status.saved);
				setTimeout("jQuery('#statusInfo').toggle('slow')", 2000);
			}
		});
	});

	// Shoutbox
		// New message
	jQuery('#shout_post').click(function() {
		var message = jQuery('#shout_message').val(), proccess = true;
		if(message != '')
			message = '&shout_message=' + message;
		else
			proccess = false;
		
		var id = jQuery('#shout_id').val(), name = '';
		if(id == '') {
			if(jQuery('#shout_name').length > 0) {
				name = jQuery('#shout_name').val();
				if(name != '')
					name = '&shout_name=' + name;
				else
					proccess = false;
			}
		} else
			id = '&shout_id=' + id;

		if(proccess) {
			jQuery.ajax({
				type: 'POST',
				url: refresh.shoutbox,
				data: 'action=submit' + message + id + name,
				success: function(data, textStatus, XMLHttpRequest) {
					jQuery('#shout_id').val('');
					jQuery('#shout_message').val('');
					Ajax.Refresh(refresh.shoutbox, 'shoutbox-ajax', 0);
				}
			});
		} else {
			var tmp = false;
			if(jQuery('#shout_name').length > 0 && name == '')
				tmp = true;

			if(message == '') {
				if(tmp)
					alert(locale.shoutbox.error.both);
				else
					alert(locale.shoutbox.error.message);
			} else if(tmp)
				alert(locale.shoutbox.error.nick);
		}
	});

		// Clear values
	jQuery('#shout_clear').click(function() {
		jQuery('#shout_id').val('');
		jQuery('#shout_message').val('');
	});

		// Others...
	Ajax.Update();

	// Poll
	var option;
		// Submit
	jQuery('#vote_button').click(function() {
		if(option) {
			jQuery('#vote_info').toggle('slow');
			jQuery.ajax({
				type: 'POST',
				dataType: 'html',
				url: path + 'infusions/member_poll_panel/member_poll_result.php',
				data: 'vote_poll=' + jQuery('#vote_poll').val() + '&vote_answer=' + jQuery(option).val(),
				success: function(data, textStatus, XMLHttpRequest) {
					jQuery('#vote_box').fadeOut('slow', function() {
						jQuery(this).html(data);
						jQuery(this).fadeIn('slow');
					});
				}
			});
		}
	});

		// Choose
	jQuery('input.vote_option').click(function() {
		if(!option) {
			if(jQuery('#vote_submit').length > 0)
				jQuery('#vote_submit').toggle('slow');
		} else
			jQuery(option).attr('checked', false);

		option = this;
	});

	// AdTaily
		// Round borders
	/*if(jQuery.browser.msie)
	{
		var settings = {
			tl: { radius: 5 },
			tr: { radius: 5 },
			bl: { radius: 5 },
			br: { radius: 5 },
			antiAlias: true
		}
		
		setTimeout("curvyCorners(settings, '#AdTaily_Widget_Container div')", 1500);
	}
	else*/
		setTimeout("jQuery('#AdTaily_Widget_Container div').addClass('round-border')", 1500);

		// Change logo color
	setTimeout("jQuery('.AdTaily_Widget_Logo').css('color', '#fff')", 1500);
		// Hide title
	setTimeout("jQuery('.AdTaily_Widget_Title').hide()", 1500);

	// Fire events
	setTimeout("Ajax.Refresh('" + refresh.shoutbox + "', 'shoutbox-ajax', " + refresh.timeout.shoutbox + ")", refresh.timeout.shoutbox);
	setTimeout("Ajax.Refresh('" + refresh.onlineList + "', 'onlineList-ajax', " + refresh.timeout.onlineList + ")", refresh.timeout.onlineList);
	setTimeout("Ajax.Refresh('" + refresh.friendList + "', 'friendList-ajax', " + refresh.timeout.friendList + ")", refresh.timeout.friendList);
});

var Ajax = {};
Ajax.refreshing = {};

Ajax.Refresh = function(link, target, timeout) {
	if(Ajax.refreshing[target] == true)
		return;

	Ajax.refreshing[target] = true;
	if(jQuery('#' + target + '-status').length > 0)
		jQuery('#' + target + '-status').toggle('slow');

	jQuery.ajax({
		dataType: 'html',
		url: link,
		success: function(data, textStatus, XMLHttpRequest) {
			if(data != '')
			{
				jQuery('#' + target).fadeOut('slow', function() {
					jQuery(this).html(data);
					jQuery(this).fadeIn('slow');
				});
			}

			Ajax.Update();
			if(jQuery('#' + target + '-status').length > 0)
				jQuery('#' + target + '-status').toggle('slow');

			Ajax.refreshing[target] = false;
			if(timeout > 0)
				setTimeout("Ajax.Refresh('" + link + "', '" + target + "', " + timeout + ")", timeout);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			jQuery('#' + target + '-status').toggle('slow');
			setTimeout("jQuery('#" + target + "-status').html('<span style=\"color:red;font-weight:bold;\">" + locale.refresh.error + ":</span> " + textStatus + ".')", 1000);
			setTimeout("jQuery('#" + target + "-status').toggle('slow')", 1000);
		}
	});
}

Ajax.Update = function() {
	// Shoutbox
		// Edit message
	jQuery('a.shout_edit').click(function() {
		var tmp = jQuery(this).attr('rel');
		jQuery('#shout_message').val(jQuery('#shoutbox_' + tmp).html());
		jQuery('#shout_id').val(tmp);
	});

		// Delete message
	jQuery('a.shout_delete').click(function() {
		if(!confirm(locale.shoutbox.confirm.delete))
			return;

		jQuery.ajax({
			type: "POST",
			url: refresh.shoutbox,
			data: 'action=delete&shout_id=' + jQuery(this).attr('rel'),
			success: function(data, textStatus, XMLHttpRequest) {
				Ajax.Refresh(refresh.shoutbox, 'shoutbox-ajax', 0);
			}
		});
	});

		// Ban user
	jQuery('a.shout_ban').click(function() {
		if(!confirm(locale.shoutbox.confirm.ban))
			return;

		jQuery.ajax({
			type: "POST",
			url: refresh.shoutbox,
			data: 'action=ban&user_id=' + jQuery(this).attr('rel'),
			success: function(data, textStatus, XMLHttpRequest) {
				Ajax.Refresh(refresh.shoutbox, 'shoutbox-ajax', 0);
			}
		});
	});

		// Unban user
	jQuery('a.shout_unban').click(function() {
		if(!confirm(locale.shoutbox.confirm.unban))
			return;

		jQuery.ajax({
			type: "POST",
			url: refresh.shoutbox,
			data: 'action=unban&user_id=' + jQuery(this).attr('rel'),
			success: function(data, textStatus, XMLHttpRequest) {
				Ajax.Refresh(refresh.shoutbox, 'shoutbox-ajax', 0);
			}
		});
	});
}
