var bars = new Array();
var my_i = 0;
function progressIndicator( elem_id, link_id, link_type )
{
	this.elem = elem_id;
	this.link_id = link_id
		
	this.isTrans = 0;
	this.is_uploading = 0;
	
	this.current_pct = 0;
	this.stopChecking = 0;
	
	this.toggleOpacity = function()
	{
		if( this.isTrans == 0 && this.is_uploading == 1 )
		{
			this.elem.animate( { opacity:0.66 }, { queue:false, duration:1000 } );
			this.isTrans = 1;
		}
		else if( this.isTrans == 1 && this.is_uploading == 1 )
		{
			this.elem.animate( { opacity:1 }, { queue:false, duration:1000 } );
			this.isTrans = 0;
		}
		
		setTimeout( function( obj ){ obj.toggleOpacity() }, 1000, this );
	}
	
	this.setPercentage = function( pct )
	{
		this.current_pct = pct;
		
		if( parseInt( pct ) > 0 )
		{
			this.is_uploading = 1;	
		}
		
		$( '#' + this.link_id +'_progress' ).html( pct );
		this.elem.children( 'div.downloadprogressbar' ).animate( { width:pct + '%' }, 1000 );	
	}
	
	this.update = function( data )
	{
		if( data.done == 'nope' )
		{
			if( this.current_pct == 100 )
			{
				$( '#' + this.link_id +'_progress' ).html( 'Generating link...' );
				$( '#' + this.link_id +'_pct' ).fadeOut();
			}
			
			if( parseInt( data.upload_progress ) > parseInt( this.current_pct ) )
			{
				this.setPercentage( data.upload_progress );	
			}
		}
		else
		{
			// Make it do 100 first
			this.setPercentage( 100 );	
			
			// Show the url after that animation has finised!
			setTimeout( function( data, obj ){ 
				$( '#link_' + obj.link_id + ' div' ).fadeOut( 'slow', function(){
					$( '#link_' + obj.link_id ).html( data.ldata );
					$( '#link_' + obj.link_id ).removeClass();
					
					if( data.error == 0 )
					{
						$( '#link_' + obj.link_id ).addClass( 'downloadonline' );
					}
					else
					{
						$( '#link_' + obj.link_id ).addClass( 'downloaddeleted' );
					}
					$( '#link_' + obj.link_id + ' a' ).fadeIn( 'slow' );
				});					 
			}, 1000, data, this );
			
			this.stopChecking = 1;
			
			// Reset opacity blur thing
			this.is_uploading = 0;
			this.elem.animate( { opacity:1 }, { queue:false, duration:1000 } );
		}
	}
	
	this.checkStatus = function()
	{
		// Get ourselves some data!
		thisObj = this;
		$.get( 'http://www.gazup.com/available/' + this.link_id + '/', function(json){
			eval(json)
			thisObj.update( json )
		
			my_i = ( my_i < ( bars.length - 1 ) ) ? my_i + 1 : 0;
			setTimeout( "updateProgress()", 2000 );
		});
		
		// Do we keep checking?
		/*if( this.stopChecking == 0 )
		{
			setTimeout( function( obj ){ obj.checkStatus() }, 5000, this );
		}*/
	}
	
	// Start this cycle
	this.toggleOpacity();
	
	// Check the upload status
	// this.checkStatus();
}


$( document ).ready(function(){							 
	// Store each mirror's progress into a bar object
	var i =0;
	
	$( '.downloadprogress' ).each(function(){
		var link_id = $( this ).attr( 'id' ).substr(5);
		var link_type = $( this ).attr( 'rel' );
		bars.push( new progressIndicator( $( this ), link_id, link_type ) );
		i++;
	});
	
	updateProgress();
	
$( '.deleteimage' ).click(function(){
		id = $( this ).attr( 'rel' );
		mythis = $( this );
		$.get( 'http://www.gazup.com/delete-screenshot/' + id + '/', function(data){
			if( data == 'ok' )
			{
				mythis.parent().slideUp( 'slow', function(){
					location.reload();										  
				});
			}
		});
		return false;
	});
	

	// AJAX submit 	
	$( '#add_descr' ).submit(function(){

		var id = $( '#page_id' ).val();
		$.post( 'http://www.gazup.com/add-description/' + id + '/', { 'ajax' : 1, 'submit' : 1, 'comments' : $( '#descr' ).val() }, 
		function( data ){
			$( '#add_descr' ).hide( 'slow' );
			$( '#add_result p' ).html( data );
			$( '#add_result' ).show( 'slow' );
		}, "text" );
		return false;							  
	});

	$( '#commentbox' ).focus(function(){
		$( this ).val( ( $( this ).val() == 'Enter your comments here...' ) ? '' : $( this ).val() );
	});

	$( '#commentbox' ).blur(function(){
		$( this ).val( ( $( this ).val() == '' ) ? 'Enter your comments here...' : $( this ).val() );
	});

	$( '#commentform' ).submit(function(){
		var id = $( this ).attr( 'rel' );
		var comment = $( '#commentbox' ).val();
		var myForm = $( this );

		$.post( 'http://www.gazup.com/add-comment/' + id + '/', { 'ajax' : 1, 'comment' : comment }, function(data){
			if( data != 'nope' )
			{
				$( '#nocomments' ).fadeOut( 'slow' );
				myForm.fadeOut( 'slow', function(){
					$( '#comments' ).append( data );
					$( '.hidden_comment' ).fadeIn( 'slow' );
				});
			}
		});

		return false;
	});
});

function updateProgress()
{
	if( bars.length > 0 )
	{
		bars[my_i].checkStatus()
	}
}