(function ($) {
	$.fn.closebutton = function() {
		$(this).each(function () {
			$(this).prepend(
				'<a title="Close" href="javascript:;" ' +  
				'onclick="$(this).parent().fadeTo(400,0,function(){$(this).slideUp(\'fast\');});" ' + 
				'class="close-button">X</a>'
			);
		});
		return this;
	}
})(jQuery);

$(document).ready(function () {
//====================================================================================================

	var ajaxLoader = '<div class="ajaxLoader" style="width:100%;padding:1em 0;text-align:center;float:none;">' +
			'<img src="' + BASE_URL + 'public/icons/loading/loading.gif" alt="Loading" />' +
			'</div>';

//====================================================================================================

	$('.PAGES .toolbar,.NEWS .toolbar').closebutton();
	$('.message').closebutton().hide().fadeIn();

//====================================================================================================

	/* Nav dropdowns for IE */
	$(".nav ul").css({display: "none"}); // Opera Fix
	$(".nav li").hover(function () {
		$(this).find('ul:first').css({visibility: "visible", display: "none"}).show(140);
	},
	function () {
		$(this).find('ul:first').css({visibility: "hidden"});
	});

//====================================================================================================

	 /* Calendar Widget */
	$('.calendar a.prev,.calendar a.next').live('click', function () {
		$('.calendar').html(ajaxLoader).load($(this).attr('href'));
		return false;
	});
	$('.calendar td a').live('click', function () {
		var $href = $(this).attr('href');
		$('.calendar div').removeClass('selected');
		$(this).parent().addClass('selected');
		$('#content').prepend(ajaxLoader + '<div class="hidden content-block"></div>');
		$('#content div.content-block:first').load($href, function () {
			$('.ajaxLoader').slideUp();
			$(this).slideDown().closebutton();
		});
		return false;
	});

//====================================================================================================

	/* Marquee */
	$('.marquee').marquee('marquee').mouseover(function () {
		$(this).trigger('stop');
	}).mouseout(function () {
		$(this).trigger('start');
	}).mousemove(function (event) {
		if ($(this).data('drag') === true) {
			this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
		}
	}).mousedown(function (event) {
		$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
		$(this).data('drag', false);
	});

//====================================================================================================

//TODO: Move to logged in/moderator only!
	// CBUA Roster Management
	$('.MEMBERS-ROSTER tr .details[id] a[href=#edit_user]').click(function () {
		$id = $(this).parents('.details').attr('id').replace('row_', '');
		$field = $(this).attr('rel');
		$icon = $(this).find('img');
		$value = $icon.attr('rel');
		$old_icon = $icon.attr('src').split('/').reverse();
		$old_icon = $old_icon[0];
		
		if ($old_icon == 'delete.png') {
			$new_icon = 'success.png';
			$icon_title = 'yes';
			$value = 'yes';
		} else if ($old_icon == 'success.png') {
			$new_icon = 'disabled.png';
			$icon_title = 'n/a';
			$value = 'n/a';
		} else {
			$new_icon = 'delete.png';
			$icon_title = 'no';
			$value = 'no';
		}

		$.post(BASE_URL + "admin/users/update_user", {id: $id, field: $field, value: $value},
		function(data){
			if (data.status == true) {
				$icon.attr('src', $icon.attr('src').replace($old_icon, $new_icon));
				$icon.attr('title', $icon_title);
			} else {
				alert(data.message);
			}
		}, "json");
		return false;
	});

//====================================================================================================
});
