//------------------------------------------------------[TG]
// Public: JS
//------------------------------------------------------[TG]

// Event Delegation ------------------------------------[TN]
jQuery.delegate = function(Rules) {
	return function(e) {
		var Target = $(e.target);
		for (var Selector in Rules)
			if (Target.is(Selector)) return Rules[Selector].apply(this, $.makeArray(arguments));
	}
}
//------------------------------------------------------[TN]

// Close Errors ----------------------------------------[TG]
$(document).ready(function() { 
	$('.MSG_Error').click(ClickToClose);
	$('.MSG_Success').click(ClickToClose);
});
function ClickToClose(Event) {
	var Parent = $(this).parent();
	$(this).remove();
	if (!Parent.children().length)
		Parent.remove();
}
//------------------------------------------------------[TG]

// Calendar --------------------------------------------[TN]
var CAL = {
	ShowDate : function( date ) {
//		$('.CAL_Badges').css('display','none');
//		$('.CAL_DayView').hide('normal', function() {
			$('.CAL_DayView').load('/calendar/day_view/', {'CalendarDate' : $.datepicker.formatDate('yy-mm-dd', new Date(date))}, function() {
//				$('.CAL_DayView').show('normal', function() { 
//					$('.CAL_Badges').css('display','block');
//				});
			});
//		});
	},
	ShowActivityForm : function() {
		$('.XHReturn').html('');
		$('.CAL_ActivityFormRadios img').attr('src', '/img/v1/ico.radio.gif');

		if ( arguments[0] == null ) {
			$('input[@name=ActivityID]').val( 0 );
			$('input[@name=Activity]').val('ENTER ACTIVITY HERE');
			$('#TypeOnce').next('label').children('img').attr('src', '/img/v1/ico.radio_checked.gif');

			// hide the edit/delete links
			$('.CAL_Actions').empty();
			$('.CAL_Footer .CAL_EditButton').val('EDIT');
			$('.CAL_Footer .CAL_EditButton').attr('src','/img/v1/btn.edit.gif');
		} else {
			$('input[@name=ActivityID]').val( arguments[0] );
			$('input[@name=Activity]').val( $('#Activity' + arguments[0]).html() );
			$('#Type' + $('#Type' + arguments[0]).val()).attr('checked', 'checked');
			$('#Type' + $('#Type' + arguments[0]).val()).next('label').children('img').attr('src', '/img/v1/ico.radio_checked.gif');			
		}

		$('.CAL_ActivityForm').slideDown('fast');
	},
	HideActivityForm : function() {
		$('.CAL_ActivityForm').slideUp('fast');
	},
	ShowEditActions : function( editButton ) {
		if ( $(editButton).val() == 'EDIT' ) {
			if ( jQuery.trim($('.CAL_Activities').html()) == '' )
				return(false);

			$('.CAL_Actions').append('<a href="#" onclick="return(false);" class="LnkActivityEdit" title="Edit Activity""><img src="/img/v1/ico.edit.gif" border="0" /></a><br /><a href="#" onclick="return(false);" class="LnkActivityDelete" title="Delete Activity"><img src="/img/v1/ico.delete.gif" border="0" /></a>');
			
			// bind the edit click event listener
			$('.CAL_Actions .LnkActivityEdit').bind('click', function(){ 
				CAL.ShowActivityForm($(this).parent().attr('id').substr(6));
			});
			
			// bind the delete click event listener
			$('.CAL_Actions .LnkActivityDelete').bind('click', function(){ 
				CAL.DeleteActivity($(this).parent().attr('id').substr(6));
			});

			$(editButton).val('CANCEL');
			$(editButton).attr('src','/img/v1/btn.cancel.gif');
		} else {
			$('.CAL_Actions').empty();
			$(editButton).val('EDIT');
			$(editButton).attr('src','/img/v1/btn.edit.gif');
		}
	},
	AddEditActivity : function() {
		if( $('input[@name=Activity]').val() == 'ENTER ACTIVITY HERE' ) {
			$('.XHReturn').html('Please enter an activity or goal.');
			return(false);
		}
		$.post('/calendar/activity.process/', 
			{
				Action:		'AddEditActivity', 
				ActivityID:	$('input[@name=ActivityID]').val(),
				Activity:	$('input[@name=Activity]').val(),
				Type:		$('input[@name=Type]:checked').val(),
				DateStart:	$('input[@name=DateStart]').val()
			}, 
			function( data ) { 
				if ( data['Msg'] == 'ADDED' ) {
					// add the activity to the display list
					$('.CAL_Activities').append(
						'<div class="CAL_ActivityRow">' + 
							'<div class="CAL_Actions" id="Action' + data['ActivityID'] + '"></div>' + 
							'<div class="CAL_Activity" id="Activity' + data['ActivityID'] + '">' + $('input[@name=Activity]').val() + '</div>' + 
							'<input type="hidden" id="Type' + data['ActivityID'] + '" value="' + data['Type'] + '" />' + 
						'</div>'
					);
				} else if ( data['Msg'] == 'UPDATED' ) {
					$('#Activity' + data['ActivityID'] ).html( $('input[@name=Activity]').val() );
					$('#Type' + data['ActivityID']).val($('input[@name=Type]:checked').val());
				} else {
					$('.XHReturn').html(data['Msg']);
				}

				if ( data['Msg'] == 'ADDED' || data['Msg'] == 'UPDATED' ) {
					// hide and reset the form
					CAL.HideActivityForm();
					$('input[@name=Activity]').val('');
					$('#TypeOnce').attr('checked', 'checked')
				}
			}, 
			'json'
		);
		return(false);		
	},
	DeleteActivity : function(activityID) {
		if ( confirm( 'Are you sure you want to delete this activity?\nNote: if it is part of a series, the whole series will be deleted.\nThis action cannot be undone.' ) ) {
			$.post('/calendar/activity.process/', 
				{Action: 'DeleteActivity', ActivityID: activityID}, 
				function( data ) { 
					if ( data['Msg'] == 'UPDATED' ) {
						$('#Action' + activityID).parents('.CAL_ActivityRow').slideUp( 'normal', function() {
							$('#Action' + activityID).parents('.CAL_ActivityRow').remove();
							if ( jQuery.trim($('.CAL_Activities').html()) == '' ) {
								$('.CAL_Footer .CAL_EditButton').val('EDIT');
								$('.CAL_Footer .CAL_EditButton').attr('src','/img/v1/btn.edit.gif');
							}								
						});
					} else {
						$('.XHMessage').html(data['Msg']);
					}
				}, 
				'json'
			);
		}
		return(false);
	},
	LoadPromoFooter : function( activityIDs, randomMoreInfo ) {
		$('.CAL_Suggestions').load( '/calendar/suggestions/', {'ActivityIDs' : activityIDs, 'MoreInfo' : randomMoreInfo} );
		$('.CAL_PromoMiddle').html( randomMoreInfo );
		return(false);
	},
	PickSuggestion : function( suggestionID ) {
		CAL.ShowActivityForm();
		$('input[@name=Activity]').val( $('#Suggestion' + suggestionID + ' a' ).html() );
		$('#Type' + $('#SuggestionType' + suggestionID).val()).attr('checked', 'checked');		
		$('.CAL_ActivityFormRadios img').attr('src', '/img/v1/ico.radio.gif');
		$('#Type' + $('#SuggestionType' + suggestionID).val()).next('label').children('img').attr('src', '/img/v1/ico.radio_checked.gif');			
		return(false);
	},
	CheckRadio : function( parentNode, theRadio ) {
		$('.' + parentNode + ' img').attr('src', '/img/v1/ico.radio.gif');
		$(theRadio).children('img').attr('src', '/img/v1/ico.radio_checked.gif');
		$(theRadio).prev('input:radio').attr('checked', 'checked');
	},
	CheckCheckBox : function( e ) {
		var UserChecked = $(e.target).parent().prev('input:checked').length == 0;
		$(e.target).parent().prev('input:checkbox').attr('checked', ( ( UserChecked ) ? 'checked' : ''));
		$(e.target).parent().find('img').attr('src', '/img/v1/ico.radio' + ( ( UserChecked ) ? '_checked' : '' ) + '.gif');
		var Target = e.target;
		if ( UserChecked ) {
			$.post('/ajax/challenge.process/', { Action: 'MarkDone', ChallengeID: $(e.target).parent().attr('rel') },
				function( data ) {
					if ( data.Msg == 'Updated' ) {
						$(Target).parents('.Challenge').unbind('click');
						$(Target).parent().children().each( function() {
							$(this).addClass( 'CheckBoxTriggered' );
						})
					} else {
						alert( data.Msg );
					}
				},'json'
			);
		}
	},
	ChallengeComplete : function( theLabel ) {
		if ( $(theLabel).children('img').attr('src') == '/img/v1/ico.radio_checked.gif' ) {
			alert('checked');
		}
	},
	SetCurrentMonth : function() {
		$('.datepicker_current').empty();
		$('.datepicker_current').append($('.datepicker_newMonth option:selected').html() +' ' + $('.datepicker_newYear option:selected').html());

		$('.datepicker_prev').empty();
		$('.datepicker_prev').append('<a onclick="jQuery.datepicker._adjustDate(0, -1, \'M\'); CAL.SetCurrentMonth();"><img border="0" alt="previous" src="/img/v1/btn.arrow_left.gif"/></a>');

		$('.datepicker_next').empty();
		$('.datepicker_next').append('<a onclick="jQuery.datepicker._adjustDate(0, +1, \'M\'); CAL.SetCurrentMonth();"><img border="0" alt="previous" src="/img/v1/btn.arrow_right.gif"/></a>');

		$('.datepicker_titleRow').children().children('a').attr('onclick', '').css({'cursor' : 'default', 'textDecoration' : 'none'});
	}
};
//------------------------------------------------------[TN]

//------------------------------------------------------[TG]

//----------------------------------------7/21/2008------[JG]

var ALClass  = function(event){
	this.charCount = function(Src,Dest,MaxLength){
		var charlen = $('#'+Src).val().length;
		var remaining =  MaxLength - charlen;
		if(charlen >= MaxLength) { 
			$('#'+Src).val( $('#'+Src).val().substr(0, MaxLength ) );
			return false;
		}
		$('#'+Dest).html('Chars Remaining: ' + remaining);
	}
}

var AL = new ALClass();


	if (!Array.prototype.indexOf) {
		Array.prototype.indexOf = function(val, fromIndex) {
			if (typeof(fromIndex) != 'number') fromIndex = 0;
			for (var index = fromIndex,len = this.length; index < len; index++)
				if (this[index] == val) return index;
			return -1;
		}
	}

var alhlp = function(){
		this.requestList = new Array();

		this.getTags =  function(TagType){
			if($('#'+TagType +'_ExpandLink').html() == "[+] Add"){
				$('#'+TagType +'_ExpandLink').html("[-] Close");
				$('#'+TagType+'_Tag_List').show().load(PATH['Admin'] + '/activity.tags.get/',{'TagType':TagType});
			} else {
				$('#'+TagType +'_ExpandLink').html("[+] Add");
				$('#'+TagType+'_Tag_List').hide();
			}
		}

		this.addTag =  function(TagType,TagId,myText){
			var bool = true;
			$('.'+TagType+'Tags').each(function(){
				if ($(this).val() ==  TagId){ 
					alert('This is already added');
					bool = false;
					return false;
				}
			});
			if (!bool)
				return false;

			$('<li><a href="#">x</a>&nbsp '+myText+' <input type="hidden" name="'+TagType+'Tags[]" class="'+TagType+'Tags" value="'+TagId+'"/></li>').appendTo('#Activity_'+TagType+'_List').children('a').click( function(){
					$(this).parent().remove();return false;
				}
			);
		}
		this.refreshTag =  function(TagType){
			// Refresh tag type if listing open

			if($('#'+TagType +'_ExpandLink').html() == "[-] Close"){
				$('#'+TagType+'_Tag_List').show().load(PATH['Admin'] + '/activity.tags.get/',{'TagType':TagType});
			} 
		}
		this.activityFormCheck = function(){
			
			if ($('.WhoTags').val() == undefined){
				alert('You have to have at least one of each Tag');
				return false;
			}
			if ($('.WhereTags').val() == undefined){
				alert('You have to have at least one of each Tag');
				return false;
			}
			if ($('.WhatTags').val() == undefined){
				alert('You have to have at least one of each Tag');
				return false;
			}
		}
};
