/**
 * @author Lupatus (michal.kluszewski@firma.o2.pl)
 * @package kumple.pl
 * @subpackage js-script
 * @copyright o2.pl 2005-2007
 *
 * $Revision: 670 $
 * $LastChangedDate: 2008-10-03 11:58:03 +0200 (Fri, 03 Oct 2008) $
 */


var o2_PV = Class.create( );

o2_PV.prototype = {
	
	rated : false,
	
	data : undefined,
	
	isWorking : false,
	
	initialize : function( ) { 
	  Event.observe( window , 'load' , this.hobbiesCheckSize.bind( this ) );
	},
	
	hobbiesCheckSize : function( ){
		if ($('more-about-me') && $('hobbies')){
		   var dim1 = $('more-about-me').getDimensions();
		   var dim2 = $('hobbies').getDimensions();
	
		   if( dim1.height < dim2.height ) {
		       $( 'more-about-me' ).style.height = dim2.height + 'px' ;
		   }
		   else{
		   	   $( 'hobbies' ).style.height = dim1.height + 'px'  ;
		   }
	    }
	},
	
	Rate : function( pid , avg , p_cnt , rate , max ) {
		if( this.isWorking ) {
			return false;
		}
		if( this.rated ) {
			Notice.warning( lang_errors['profile_rate'] , 3 );
			return false;
		}
		try {
		this.data = { avg : 0 , cnt : 0 , max: -1 , avgNum: 0 };
		
		this.isWorking = true;
		avg = ( ( avg * p_cnt ) + rate ) / ( p_cnt + 1 );
		this.p_cnt = p_cnt + 1;
		this.avg   = avg;
		this.data.avgNum = avg;
		
		var avgStr = Math.round( this.avg * 10 );
		avgStr /= 10;
		avgStr += '';
		
		if( ! avgStr.match( /\.[0-9]{1}$/ ) ) {
			avgStr += '.0';
		}
		
		this.data.avg = avgStr;
		this.data.cnt = this.p_cnt;
		if( rate == 15 ) {
			this.data.max = max + 1;
		}
		ajax.clearPost( );
		ajax.addPost( 'cmd'   , 'rate-profile' );
		ajax.addPost( 'grade' , rate );
		ajax.addPost( 'profile-id' , pid );
		ajax.response( URL_PROFILE_API , this.RateEnd.bind( this ) , lang_profileRateP , true );
		} catch( e ) {
			Error( e.toString( ) );
		}
		return false;
	},
	
	RateEnd : function( code , info ) {
		if( code == 1 ) {
			this.rated = true;
			$( 'rates-avg' ).innerHTML = this.data.avg;
			$( 'rates-cnt' ).innerHTML = this.data.cnt;
			var pos = Math.round( (((this.data.avgNum-1)/14)*280)+6 );
			$( 'ratebtns-img' ).style.backgroundPosition = pos+'px 19px';
			if( this.data.max > -1 ) {
				$( 'rates-max' ).show( );
				$( 'rates-maxt' ).show( );
				$( 'rates-max' ).innerHTML = this.data.max;
			}
			if( this.data.avg >= 11 ) {
				$( 'ratebtns-box' ).className = 'flame';
				pos = Math.round( (((this.data.avgNum-11)/4)*80)+195 );
				$( 'ratebtns-box' ).style.backgroundPosition = pos + 'px 1px';
			} else {
				$( 'ratebtns-box' ).className = '';
			}
		}
		this.isWorking = false;
	},
	
	ShowCV : function( ) {
		try {
		if( this.isWorking ) {
			return false;
		}
		this.isWorking = true;
		new Effect.Shrink( $( 'cv-dots' ) , {duration: 0.2} );
		setTimeout( 'new Effect.BlindUp( $("cv-more"), {duration:0.3} );' , 200 );
		setTimeout( 'new Effect.BlindDown( $("my-cv-hidden") , {duration:0.6} );' , 500 );
		setTimeout( 'new Effect.BlindDown( $("cv-hide") , {duration:0.3} );' , 1100 );
		setTimeout( 'PV.isWorking = false;' , 1400 );
		} catch( e ) {
			Error( e.toString( ) );
		}
		return false;
	},
	
	HideCV : function( ) {
		if( this.isWorking ) {
			return false;
		}
		this.isWorking = true;
		new Effect.BlindUp( $( 'cv-hide' ), {duration:0.3} );
		setTimeout( 'new Effect.BlindUp( $("my-cv-hidden") , {duration:0.6} );' , 300 );
		setTimeout( 'new Effect.BlindDown( $("cv-more") , {duration:0.3} );' , 900 );
		setTimeout( 'new Effect.Grow( $("cv-dots"), {duration:0.2} );' , 1200 );
		setTimeout( 'PV.isWorking = false;' , 1400 );
		return false;
	},
	
	ShowMailChooser : function( ) {
		if( $( 'mail-send-chooser' ) ) {
			$( 'mail-send-chooser' ).show( );
		}
		return false;
	}
}

var PV = new o2_PV();
