var RK = {
	opinionform : $('#opinion-form'),
	formbutton: $( '#formbutton' ),
	fileinput: $( '#image' ),
	filecheck: $( '#imageasavatar' ),
	promotionbutton: $( '#morepromotions' ),
	removeopener: $( '#removeopener' ),
	opinionbutton: $( '#moreopinions' ),
	opinionfield: $( '#opinion' ),
	opinioncounter: $( '#counter' ),
	opinionhinter: $( '#hinter' ),
	opinionlist: $( '#opinion-list' ),
	opinioncontent: $( '#opinion-content' ),	
	removekey: 	$( '#removekey' ),
	
	init: function(){
		this.initOpinionlist();
		this.initFileUpload();
		//this.initFormToggle();
		this.initPromotion();
		this.initRemover();
		this.initOpinion();
		this.initCounter();

		$.ifixpng( 'image/pixel.gif' );
		$( '.trans, .button' ).ifixpng();			
	},
	toggleform: function(){
		RK.formbutton.hide();
		this.opinionform.slideToggle( 600 );
		$( 'html' ).animate( {scrollTop: $( 'html' ).outerHeight()}, 600 );
	},
	initRemover: function(){
		if( this.removeopener.length === 0 ) return;
		
		this.removekey.focus( function(){
			if( this.value == this.defaultValue ){
				$( this ).val( '' );					  
			}
		} );
		this.removekey.blur( function(){
			if( this.value == '' ){
				$( this ).val( this.defaultValue );					  
			}						   
		} );		
		
		$( '#remover-container form' ).hide();
		this.removeopener.slideDown( 100 );
		
		this.removeopener.click( function(){
			$( '#remover-container form' ).slideToggle( 100 );
			return false;
		} );
	},
	initFileUpload: function(){
		if( this.fileinput.length === 0 ){ return; }
		if( this.filecheck.length === 0 ){ return; }

		this.fileinput.change( function(){
			RK.filecheck.attr( 'checked', 'checked' );			   
		 } );			
	},
	initFormToggle: function(){
		if( this.opinionform.length === 0 ){ return; }
		if( this.formbutton.length === 0 ){ return; }	

		this.opinionform.hide();
		
		this.formbutton.click( function(){
			RK.toggleform();
			return false;
		} );		
	},
	initCounter: function(){
		if( this.opinionfield.length === 0 ) return;
		if( this.opinioncounter.length === 0 ) return;	
		if( this.opinionhinter.length === 0 ) return;	

		this.opinionfield.keyup( function(){
			res = 800 - ( $( this ).val().length );
			RK.opinioncounter.html( res + ' tecken kvar' );
			if( res < 50 ){
				if( RK.opinionhinter.is( ':hidden' ) ){
					RK.opinionhinter.show();
				}
			}else{
				if( RK.opinionhinter.is( ':visible' ) ){
					RK.opinionhinter.hide();
				}
			}
			if( res < 0 ){	
				$( this ).val( $( this ).val().substr( 0,800 ) );
				RK.opinioncounter.html( 0 + ' tecken kvar' );
			}
		} );
	},
	initPromotion: function(){
		if( this.promotionbutton.length === 0 ){ return; }
		
		this.promotionbutton.click( function(){
			return RK.contentSwitch( 'promotion-box' );
		} );
		
		var i = 0;
		$( '.promotion-box' ).each( function(){
			if( i++ > 0 ){
				$( this ).hide();							 
			}
		} );
	},
	initOpinion: function(){
		if( this.opinionbutton.length === 0 ){ return; }
		
		this.opinionbutton.click( function(){
			return RK.contentSwitch( 'opinion-box' );
		} );
		
		var i = 0;
		$( '.opinion-box' ).each( function(){
			if( i++ > 0 ){
				$( this ).hide();							 
			}
		} );
	},	
	contentSwitch: function( sel ){
		$( '.' + sel ).each( function(){							 
			if( $( this ).css( 'display' ) == 'block' ){
				$( this ).hide();
				if( $( this ).next().hasClass( sel ) ){
					$( this ).next().show();
				}else{
					$( '.' + sel + ':first' ).show();
				}
				return false;
			}
		 } );
		return false;		
	},	
	getData: function( num ){
		$.getJSON( 'opinion.php', {id:num}, function( data ){

			var info = '';
			if( data.sex && data.sex != 'null' ){
				if( data.sex == 'male' ) info += 'Kille  ';
				if( data.sex == 'female' ) info += 'Tjej  ';
				if( data.sex == 'nosex' ) info += '  ';				
			}				
			if( data.age && data.age != 'null' ) info += data.age + ' år, ';
			if( data.zip && data.zip != 'null' ) info += data.zip + '  ';

			$( '#dc-vote' ).attr( 'rel', data.id );
			$( '#dc-image' ).attr( 'rel', data.id ).attr( 'src', data.img );
			$( '#dc-name' ).html( data.alias == '' ? 'Anonym': data.alias );
			$( '#dc-promotions' ).html( data.votes );
			$( '#dc-info' ).html( info.substr( 0, info.length-2 )  );
			$( '#dc-opinion' ).html( data.text );
			
			RK.opinioncontent.slideDown( 600 );
		} );		
	},
	initOpinionlist: function(){
		
		if( this.opinionlist.length === 0 ) return;
		if( this.opinioncontent.length === 0 ) return;
		
		$( '.promotion-content' ).click( function(){
			RK.getData( $( this ).children( '.avatar-image' ).attr( 'rel' ) );
		} );
		
		$( '.large-avatar-image' ).click( function(){
			RK.getData( $( this ).attr( 'rel' ) );
		 } );
		
		$( '#dc-vote' ).click( function(){
			$.getJSON( 'vote.php', {id:$( this ).attr( 'rel' )}, function( data ){
				if( data.result == 'true' ){
					$( '#dc-promotions' ).html( Number( $( '#dc-promotions' ).html() ) + 1 );
				}
			} );															 
		} );
		
		$( '.stang, .kryss' ).click( function(){
			RK.opinioncontent.slideUp( 600 );
			return false;
		 } );	
		$( '.tycktill-liten' ).click( function(){
			RK.toggleform();	
			return false;
		 } );
		
		$( '.large-avatar-image' ).mouseover( function(){
			$( this ).css( {width:'100px', height:'100px', border:'3px solid #3C99B3'} );			  
		 } );
		$( '.large-avatar-image' ).mouseout( function(){
			$( this ).css( {width:'106px', height:'106px', border:'none'} );					  
		} );
		
		$( '.avatar-image' ).mouseover( function(){
			$( this ).css( {border:'1px solid #3C99B3'} );			  
		 } );
		$( '.avatar-image' ).mouseout( function(){
			$( this ).css( {border:'1px solid #E39315'} );					  
		} );			
		
	}
};
	
$( document ).ready( function(){
	RK.init();			  
} );