// JavaScript Document

if (typeof jQuery.data_creator.teams == 'undefined') {jQuery.data_creator.teams = {}} ;

jQuery.data_creator.teams.request_link = function(jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.teams.request_link.settings, options) ;
	this.page = 0 ;
	this.showing = false ;
	this.jElement.hide() ;
	this.team_id = 0 ;
	
	if (this.options.name == '') {
		this.options.name = 'teams-request_link#' + Math.random() ;	
	}
	jQuery.data_creator.teams.request_link.stack[this.options.name]=this ;
	self = this ;
	this.jElement.clickable().click(this.make_click_callback()) ;
}

jQuery.data_creator.teams.request_link.prototype.make_click_callback = function() {
	var self = this ;
	return function() {
		self.action() ;	
	}
}

jQuery.data_creator.teams.request_link.prototype.action = function() {
	viewer_id = this.options.viewer_id ;
	team_id = this.team_id ;
	self = this ;
	$.ajax({
		type:'POST',
		url:this.options.data_dir+'teams/request_link/set.php',
		data:{viewer:viewer_id, team:team_id},
		success:function(data) {
			if (data == '') {
				$.alert({text:'Votre demande &agrave; &eacute;t&eacute; envoy&eacute;e', title:'Succ&egrave;s'}) ;
				eval(self.options.request_link_script_success) ;
			} else if (data =='already requested'){
				$.alert({text:'Votre demande est en cours d\'approbation', title:'En attente'}) ;
			} else {
				$.data_creator.error(data) ;	
			}
		}
	}) ;
}

jQuery.data_creator.teams.request_link.prototype.update = function(viewer_id, team_id) {
	this.team_id = team_id ;
	request_link_update_callback_maker = function(context_) {
		var context = context_ ;
		return function(data) {
			if (data == 'true') {
				if (context.showing == true) {
					context.jElement.hide('blind', {}, 500) ;
					context.showing = false ;
				}
			}
			else {
				if (context.showing == false) {
					context.jElement.show('blind', {}, 500) ;
					context.showing = true ;
				}
			}
		}
	}
	$.ajax({
		type:'POST',
		url:this.options.data_dir+'teams/request_link/check.php',
		data:{viewer:viewer_id, team:team_id},
		success:request_link_update_callback_maker(this)
	}) ;
}

jQuery.data_creator.teams.request_link.stack = {} ;

jQuery.data_creator.teams.request_link.settings = {
	w_index: -1,
	name: '',
	data_dir:'',
	request_link_script_success:'',
	viewer_id:0
} 