/**
 * @author Lupatus (michal.kluszewski@firma.o2.pl)
 * @package kumple.pl
 * @subpackage js-script
 * @copyright o2.pl 2005-2007
 *
 * $Revision: 396 $
 * $LastChangedDate: 2007-11-08 20:05:17 +0100 (Thu, 08 Nov 2007) $
 */

var o2_Favourites = Class.create( );

o2_Favourites.prototype = {
	
	busy : false,
	
	pid : 0,
	
	initialize : function( ) {
		Event.observe( window , 'load' , this.Start.bind( this ) );
	},
	
	Start : function( ) {
		if( $( 'favourites-info-box' ) ) {
			User.addRegisterCallBack( this.rcb.bind( this ) );
		}
	},
	
	Add : function( pid ) {
		if( this.busy ) {
			return false;
		} else if( this.favAdded ) {
			Notice.warning( lang_favouriteInfo , 3 );
			return false;
		}
		ajax.clearPost( );
		ajax.addPost( 'cmd' , 'add-to-favourites' );
		ajax.addPost( 'profile-id' , pid );
		ajax.response( URL_CONTACTS_API , this.AddToFavouritesCB.bind( this ) , lang_favouriteAdd , true , 3 );
		return false;
	},
	
	AddToFavouritesCB : function( code , info ) {
		this.busy = false;
		if( code == 1 ) {
			this.favAdded = true;
			Notice.info( lang_favouriteAddInfo , 3 );
		}
	},
	
	RemoveStartup : function( index , pid ) {
		this.busy = true;
		this.pid  = pid;
		ajax.clearPost( );
		ajax.addPost( 'cmd' , 'remove-startup' );
		ajax.addPost( 'startup-id' , this.startup );
		ajax.response( URL_CONTACTS_API , this.reload.bind( this ) );
		return false;
	},
	
	reload : function( code , info ) { 
		if( code < -87 ) {
			Notice.error( lang_favouriteBreak );
		} else {
			window.location.replace( URL_PROFILE_SHOW + this.pid + '/' );
		}
	},
	
	rcb : function(  ) {
		$( 'favourites-info-box' ).hide( );
	}
	
}

var Favourites = new  o2_Favourites( );

