// JavaScript Document

if (typeof jQuery.data_creator.player == 'undefined') {jQuery.data_creator.player = {}} ;

jQuery.data_creator.player.request_link = function(jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.player.request_link.settings, options) ;
	this.page = 0 ;
	this.showing = false ;
	this.jElement.hide() ;
	this.request_id = 0 ;
	
	if (this.options.name == '') {
		this.options.name = 'player-request_link#' + Math.random() ;	
	}
	jQuery.data_creator.player.request_link.stack[this.options.name]=this ;
	myself = this ;
	this.jElement.clickable().click(function() {
		myself.action() ;
	}) ;
}

jQuery.data_creator.player.request_link.prototype.action = function() {
	viewer_id = this.options.my_id ;
	request_id = this.request_id ;
	self = this ;
	$.ajax({
		type:'POST',
		url:this.options.data_dir+'player/request_link/set.php',
		data:{viewer:viewer_id, friend:request_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.player.request_link.prototype.update = function(viewer_id, request_id) {
	this.request_id = request_id ;
	if (request_id != viewer_id) {
		var self = this ;
		$.ajax({
			type:'POST',
			url:this.options.data_dir+'player/request_link/check.php',
			data:{viewer:viewer_id, request:request_id},
			success:function(data) {
				var self_ = self ;
				if (data == 'true') {
					$(function() {
						if (self_.jElement.is(':visible')) {
							self_.jElement.hide('blind', {}, 500) ;
						}
					}) ;
				}
				else {
					$(function() {
						if (self_.jElement.is(':hidden')) {
							self_.jElement.show('blind', {}, 500) ;
						}
					}) ;
				}
			}
		}) ;
	}
	else {
			this.jElement.hide('blind', {}, 500) ;
	}
}

jQuery.data_creator.player.request_link.stack = {} ;

jQuery.data_creator.player.request_link.settings = {
	w_index: -1,
	name: '',
	data_dir:'',
	request_link_script_success:'',
	my_id:0
} 