// JavaScript Document
        $(function(){ 
        	 $('.editable').inlineEdit({
        		 save: function(event, data) {
    			 	var t = parseInt($(this).attr('rel'));
    			 	var y = $(this).find('input[type=text]').val();
        		 	var html = $.ajax({
						type: "POST",
						url: "/admin/classifications/correction/"+t,
        		 		data: { 'action': 'save', 'id': t, 'value':y },
						success: function(responseText, responseCode) {
							$('#flash_message').fadeOut();	
							$('#flash_message').html(responseText);
							$('#flash_message').fadeIn();
        		 		}
        		 	});
				 }
        	 });
    	});


	$(document).ready(function(){
		// form encoding results 
		$('.point_element_empty').click(function(){
			$('.point_element').each(function(){
				$(this).val('');
			});
			$('#point_element_1').focus();
		});



		// draggable admin team summary
		
		$('li.headlink').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); });
		$('#module_action a[alt=edit]').click( function() {ButtonAdmin.editAction()});
		$('#module_action a[alt=disable]').click( function() {ButtonAdmin.disableAction()});
		$('#module_action a[alt=delete]').click( function() {ButtonAdmin.deleteAction()});
		$('#module_action a[alt=deleteHard]').click( function() {ButtonAdmin.deleteHardAction()});
		$('#module_action a[alt=enable]').click( function() {ButtonAdmin.enableAction()});
		$('#module_action a[alt=summary]').click( function() {ButtonTeam.summaryAction()});
		$('#module_action a[alt=credit]').click( function() {ButtonTeam.creditAction()});
		$('#module_action a[alt=result]').click( function() {ButtonTeam.resultAction()});
		$('a[alt=flagWe]').click( function() {ButtonTeam.FlagWe()});
		$('a[alt=flagWeVen]').click( function() {ButtonTeam.FlagWeVen()});
		$('a[alt=flagWeRb]').click( function() {ButtonTeam.FlagWeRb()});
		$('a[alt=resultValid]').click( function() {ButtonTeam.resultValid()});
		
		/* autocomplete helper*/
		$('#box_content input[type=text]').val('');
		
		/* autocomplete teams
		$('#teamsName').keyup(function() {AutocompleteSearch.SearchTeams('teamsName','teamsSearchForm','Name')});
		$('#teamsLocalite').keyup(function() {AutocompleteSearch.SearchTeams('teamsLocalite','teamsSearchForm','Localite')});
		$('#teamsUnite').keyup(function() {AutocompleteSearch.SearchTeams('teamsUnite','teamsSearchForm','Unite')});

		
		/* autocomplete payements 
		$('#payementsId').keyup(function() {AutocompleteSearch.SearchPayements('payementsId','payementsSearchForm','Payement.Id')});
		$('#payementsAmount').keyup(function() {AutocompleteSearch.SearchPayements('payementsAmount','payementsSearchForm','Payement.Amount')});
		$('#payementsNumTransaction').keyup(function() {AutocompleteSearch.SearchPayements('payementsNumTransaction','payementsSearchForm','Payement.num_transaction')});
		$('#payementsTeam').keyup(function() {AutocompleteSearch.SearchPayements('payementsTeam','payementsSearchForm','Team.Name')});
		*/
		
		/* autocomplete participants */
		$('#participantsFirstname').keyup(function() {AutocompleteSearch.SearchParticipants('participantsFirstname','participantsSearchForm','Participant.Firstname')});
		$('#participantsLastname').keyup(function() {AutocompleteSearch.SearchParticipants('participantsLastname','participantsSearchForm','Participant.Lastname')});
		$('#participantsTeam').keyup(function() {AutocompleteSearch.SearchParticipants('participantsTeam','participantsSearchForm','Team.name')});

	
		// $('.general_table input[type=radio]').click(function () {ButtonAdmin.radioAction()});
        $("#TeamsTable").tablesorter( 
        	{
        		widgets: ['zebra'],
        		headers: { 
	        			7: {sorter:false},
	        			8: {sorter:false},
	        			9: {sorter:false},
	        			10: {sorter:false}
        		}
        	});
       
        $("#participantsTable").tablesorter( 
            	{
            		widgets: ['zebra'],
            		headers: { 
    	        		4: {sorter:false}
            		}
            	}); 
        $("#payementsTable").tablesorter( 
            	{
            		widgets: ['zebra'],
            		headers: { 
    	        		4: {sorter:false},
    	        		5: {sorter:false}
            		}
            	}); 
        
        $('#membersTable').tableFilter();
        $('#TeamsTable').tableFilter();
        $('#responsablesTable').tableFilter();
        $('#payementsTable').tableFilter(); 
        
       
        /* Inline EDIT */
        
	});
	
	var ButtonAdmin = {

		editAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			t = parseInt(f);
			if(t)
			{
				document.location.href =  "/admin/"+hashes[4]+"/edit/"+t;
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		deleteAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			
			t = parseInt(f);
			
			if(t)
			{
				if($('.general_table img[alt=delete_'+f+']').attr('src') == "/img/accept.png") {
					alert('cannot delete element deleted');
				}
				else{
					if(confirm('Are your Sure ?'))
					{
						$.ajax({
							type: "POST",
							url: "/admin/"+hashes[4]+"/delete/"+t,
							data: 'id='+f,
							success: function(responseText, responseCode) {
							$('#flash_message').fadeOut();	
							$('#flash_message').html(responseText);
							$('#flash_message').fadeIn();
							$('.general_table img[alt=delete_'+f+']').attr("src","/img/accept.png");
							 }
						 });
					}
				}
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		deleteHardAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			
			t = parseInt(f);
			
			if(t)
			{
				if($('.general_table img[alt=delete_'+f+']').attr('src') == "/img/accept.png") {
					alert('cannot delete element deleted');
				}
				else{
					if(confirm('Are your Sure ?'))
					{
						$.ajax({
							type: "POST",
							url: "/admin/"+hashes[4]+"/delete/"+t,
							data: 'id='+f,
							success: function(responseText, responseCode) {
							$('#flash_message').fadeOut();	
							$('#flash_message').html(responseText);
							$('#flash_message').fadeIn();
							document.location.href = "/admin/"+hashes[4]+"/";
							 }
						 });
					}
				}
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		disableAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			
			t = parseInt(f);
			
			if(t)
			{
				if($('.general_table img[alt=disable_'+f+']').attr('src') == "/img/delete.png") {
					alert('cannot disable element disabled');
				}
				else{
					if(confirm('Are your Sure ?'))
					{
						$.ajax({
							type: "POST",
							url: "/admin/"+hashes[4]+"/disable/"+t,
							data: 'id='+f,
							success: function(responseText, responseCode) {
							$('#flash_message').fadeOut();	
							$('#flash_message').html(responseText);
							$('#flash_message').fadeIn();
							$('.general_table img[alt=disable_'+f+']').attr("src","/img/delete.png");
							 }
						 });
					}
				}
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		enableAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			
			t = parseInt(f);
			
			if(t)
			{
				if($('.general_table img[alt=disable_'+f+']').attr('src') == "/img/accept.png") {
					alert('cannot enable element enabled');
				}
				else{
					if(confirm('Are your Sure ?'))
					{
						$.ajax({
							type: "POST",
							url: "/admin/"+hashes[4]+"/enable/"+t,
							data: 'id='+f,
							success: function(responseText, responseCode) {
							$('#flash_message').fadeOut();	
							$('#flash_message').html(responseText);
							$('#flash_message').fadeIn();
							$('.general_table img[alt=disable_'+f+']').attr("src","/img/accept.png");
							 }
						 });
					}
				}
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		}
		/*
		radioAction: function(elem) {
			if($('.general_table img[alt=disable_4]').attr('src') == "/img/accept.png") {
				$('#module_action a[alt=disable]').css({'backgroundColor':'#dddddd','backgroundImage':'none','border':'none'});
				$('#module_action a[alt=disable]').attr('href','');
			}
			return false;
		}
		*/
	}
	
	var ButtonTeam = {
			
		summaryAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			t = parseInt(f);
			if(t)
			{
				document.location.href =  "/admin/"+hashes[4]+"/summary/"+t;
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		creditAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			t = parseInt(f);
			if(t)
			{
				document.location.href =  "/admin/payements/edit/"+t;
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		resultAction: function(elem) {
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/');
			
			var f = $('.general_table input:checked').val();
			t = parseInt(f);
			if(t)
			{
				document.location.href =  "/admin/teams/results/"+t;
			}
			else
			{
				alert('select an element');
				document.location.href = "/admin/"+hashes[4]+"/";
			}
			return false;
		},
		
		FlagWe : function(elem) {
			if(confirm('Are your Sure ?'))
			{
				$.ajax({
					type: "POST",
					url: "/admin/teams/flagWe/",
					data: 'id='+$('#teamsTeam').val(),
					success: function(responseText, responseCode) {
						$('#flash_message').fadeOut();	
						$('#flash_message').html(responseText);
						$('#flash_message').fadeIn();
						$('.flagWe').html('<img src="/img/accept.png" />');
					 }
				 });
			}	
		},
		FlagWeVen : function(elem) {
			if(confirm('Are your Sure ?'))
			{
				$.ajax({
					type: "POST",
					url: "/admin/teams/flagWeVen/",
					data: 'id='+$('#teamsTeam').val(),
					success: function(responseText, responseCode) {
						$('#flash_message').fadeOut();	
						$('#flash_message').html(responseText);
						$('#flash_message').fadeIn();
						$('.flagWeVen').html('<img src="/img/accept.png" />');
					 }
				 });
			}	
		},
		FlagWeRb : function(elem) {
			if(confirm('Are your Sure ?'))
			{
				$.ajax({
					type: "POST",
					url: "/admin/teams/flagWeRb/",
					data: 'id='+$('#teamsTeam').val(),
					success: function(responseText, responseCode) {
						$('#flash_message').fadeOut();	
						$('#flash_message').html(responseText);
						$('#flash_message').fadeIn();
						$('.flagWeRb').html('<img src="/img/accept.png" />');
					 }
				 });
			}	
		}
	}
	
	var AutocompleteSearch = {
		SearchTeams : function(searchInput,seachForm,searchString) {
				$.ajax({
					type: "POST",
					url: "/admin/teams/autocompletesearch/",
					data: {'string':$('#'+searchInput).val(),'searchString':searchString},
					dataType: 'json',
					success: function(responseText, responseCode) {
						$('#suggestions').show();
						$('#autoSuggestionsList').html('');
						$(responseText).each(function(i,item) {
							temp = responseText[i].Team[""+searchString.toLowerCase()+""];
							$('#autoSuggestionsList').append('<li onclick="AutocompleteSearch.fill(\''+escape(responseText[i].Team.id)+'\',\'teamsId\',\''+seachForm+'\')">'+responseText[i].Team.id+' - '+responseText[i].Team.name+' - '+responseText[i].Team.unite+' - '+responseText[i].Team.localite+'</li>');	
						});
						$("#suggestions")
						   .fadeIn( function() 
						   {
						      setTimeout( function()
						      {
						         $("#suggestions").fadeOut("slow");
						      }, 10000);
						   });

					 }
				 });
		},
		SearchPayements : function(searchInput,seachForm,searchString) {
			$.ajax({
				type: "POST",
				url: "/admin/payements/autocompletesearch/",
				data: {'string':$('#'+searchInput).val(),'searchString':searchString},
				dataType: 'json',
				success: function(responseText, responseCode) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html('');
					$(responseText).each(function(i,item) {
						temp = responseText[i].Team[""+searchString.toLowerCase()+""];
						$('#autoSuggestionsList').append('<li onclick="AutocompleteSearch.fill(\''+escape(responseText[i].Payement.id)+'\',\'payementsId\',\''+seachForm+'\')">'+responseText[i].Payement.id+' - '+responseText[i].Payement.amount/100+' € - '+responseText[i].Payement.num_transaction+' - '+responseText[i].Team.name+'</li>');	
					});
					$("#suggestions")
					   .fadeIn( function() 
					   {
					      setTimeout( function()
					      {
					         $("#suggestions").fadeOut("slow");
					      }, 10000);
					   });

				 }
			 });
	},
	SearchParticipants : function(searchInput,seachForm,searchString) {
		$.ajax({
			type: "POST",
			url: "/admin/participants/autocompletesearch/",
			data: {'string':$('#'+searchInput).val(),'searchString':searchString},
			dataType: 'json',
			success: function(responseText, responseCode) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html('');
				$(responseText).each(function(i,item) {
					temp = responseText[i].Team[""+searchString.toLowerCase()+""];
					$('#autoSuggestionsList').append('<li onclick="AutocompleteSearch.fill(\''+escape(responseText[i].Participant.id)+'\',\'participantsId\',\''+seachForm+'\')">'+responseText[i].Participant.lastname+' - '+responseText[i].Participant.firstname+' - '+responseText[i].Team.name+' - '+responseText[i].Participant.id+'</li>');	
				});
				$("#suggestions")
				   .fadeIn( function() 
				   {
				      setTimeout( function()
				      {
				         $("#suggestions").fadeOut("slow");
				      }, 10000);
				   });

			 }
		 });
},
		
		
		
		fill : function(string,searchInput,seachForm) {
			$('#'+searchInput).val(unescape(string));
			$('#'+seachForm).submit();
		}
	}
