$(document).ready(function(){
	$('form[name=GODOSSIER]').djbQuickDossierSearch('txtAutoName');
	$('.autocomplete-form').djbQuickDossierSearch('calc');
	$('#tabs > ul').djbDossierTabs();
});

/****************************
 * DJB jQuery Plugin
 ****************************/
(function($){
	/****************************
	 * DJB Quick Dossier Search (Auto complete)
	 ****************************/
	$.fn.djbQuickDossierSearch = function(name_id){
		return this.each(function(index){
			var the_form = $(this);
			
			//set up the autocompletion, alter the name, and create a hidden form element to hold pins
			$('input.member-autocomplete', the_form).each(function(){
				var pin_id = $(this).attr('id');
				var new_name_id = name_id;
								
				$(this).autocomplete({
					url: "http://www.darkjedibrotherhood.com/" + url_part + "/ajax/ajaxMemberAutocomplete.asp",
					minChars: 2,
					width: 260,
					selectFirst: false
				});
				
				if(name_id == 'calc')
				{
					var split_id = pin_id.split('-');
					if(split_id[1])
					{
						new_name_id = 'txtAutocompleteMember_ID'+split_id[1];
					}
					else
					{
						new_name_id = 'txtAutocompleteMember_ID';
					}
				}
				
				$(this).attr('name', new_name_id).attr('id', new_name_id);
				
				$(this).after('<input type="hidden" id="'+pin_id+'" name="' + pin_id + '"/>');
				
				//if nothing is in the pin field, put whatever is in the visible field into pin field
				the_form.submit(function(){
					if($('input#'+pin_id, the_form).val() == '')
					{
						$('input#'+pin_id, the_form).val($('input#'+new_name_id, the_form).val());
					}//end if
				});
	
				//when the autocomplete has a selected value, put the pin in the pin field
				$('input#'+new_name_id, the_form).autocomplete("result", function(event, data, formatted) {
					$('input#'+pin_id, the_form).val(data[1]);
				});
			});
		});
	};

	/****************************
	 * DJB Dossier Tabs and Events
	 ****************************/
	$.fn.djbDossierTabs = function(){
		return this.each(function(index){
			var selected_tab = $('input[name=selected_tab]').val();
			$(this).tabs({ 
				remote: true, 
				loadingClass: 'progress', 
				fx: {
					opacity: 'toggle',
					height: 'toggle',
					duration: 'normal'
				},
				ajaxOptions: {
					success: function(){
						$('a.tip').cluetip({
							cluetipClass: 'jtip',
							width: 400,
							arrows: true,
							dropShadow: true,
							hoverIntent: false,
							fx: {open: 'fadeIn'},
							activation: 'click',
							sticky: true,
							closePosition: 'title',
							closeText: 'close',
							ajaxCache: false
						});

						$('a.MedalTip').cluetip({
							splitTitle: '|',
							cluetipClass: 'jtip',
							width: 400,
							dropShadow: true,
							hoverIntent: false,
							fx: {open: 'fadeIn'}
						});
					}
				}
			});
			
			$('.relationship-comments-link').livequery('click',function(){
				var intRelationshipID = $(this).attr('id');
				intRelationshipID = intRelationshipID.split('-');
				intRelationshipID = intRelationshipID[3];
				
				$('#relationship-comments-'+intRelationshipID).slideToggle();
				return false;
			});
			
			$('.event-details-link').livequery('click',function(){
				var intEventID = $(this).attr('id');
				intEventID = intEventID.split('-');
				intEventID = intEventID[3];
				
				$('#event-details-'+intEventID).slideToggle();
				
				if ($.trim($('#event-details-'+intEventID).html()) == '')
				{
					$.get('ajax/ajaxEvents.asp', {ajax: 1, ID: intEventID}, 
					function(data){
						$('#event-details-'+intEventID).html(data);
					});
				}
				
				return false;
			});
			
			$('.events-page-link').livequery('click',function(){	
				$.get($(this).attr('href'),function(data){
					$('.events-container').html(data);
				});
				
				return false;
			});
			
			$('.member-details-title').click(function(){
				var objMemberDetails = $(this).parent('.details').children('.member-details');
				var intMember_ID = $(this).attr('id');
				intMember_ID = intMember_ID.split('-');
				var intEventID = intMember_ID[3];
				intMember_ID = intMember_ID[4];
				 
				objMemberDetails.slideToggle();
			 
				if ($.trim(objMemberDetails.html()) == '')
				{
					objMemberDetails.html('<img src="http://www.darkjedibrotherhood.com/' + url_part + '/images/icons/loading.gif">&nbsp;Loading...');
					
					$.get('http://www.darkjedibrotherhood.com/' + url_part + '/ajax/ajaxMembers.asp', {ajax: 1, BasicInformation: 1, Positions: 1, DCCoJComments: 1, PositionHistory: 1, ID: intMember_ID}, 
					function(data){
						$('#member-details-title-'+intEventID+'-'+intMember_ID).parent('.details').children('.member-details').html(data);
						$('#member-details-title-'+intEventID+'-'+intMember_ID).parent('.details').children('.member-details').append("<br><br><a href='#member-details-title-"+intEventID+'-'+intMember_ID+"' class='member-details-close'><small>close member information</small></a>");
					});
				}
				 
				return false;
			});
			
			$('.events-options-link').livequery('click',function(){	
				$.get($(this).attr('href'),function(data){
					$('.events-container').html(data);
				});
				
				return false;
			});
			
			$('.news-page-link').livequery('click',function(){	
				$.get($(this).attr('href'),function(data){
					$('.news-container').html(data);
				});
				
				return false;
			});
			
			$('.reports-page-link').livequery('click',function(){	
				$.get($(this).attr('href'),function(data){
					$('.reports-container').html(data);
				});
				
				return false;
			});
			
			$('.competitions-page-link').livequery('click',function(){	
				$.get($(this).attr('href'),function(data){
					$('.competitions-container').html(data);
				});
				
				return false;
			});
			
			$('#mylink').livequery('hover',function(){		
				$(this).addclass('.betterTip')
				return false;
			});
			
			$(this).tabs('select', '#'+selected_tab);
		});
	};
})(jQuery);