var limit = false;
var oldPercent = 0;
var mirrorIntervalCount = 2500;
var mirrorInterval;
var fireMe = 0;

function getIframeUrl()
{
	if( fireMe != 1 )
	{
		fireMe = 1;
		//alert( document.getElementById( 'uploadframe' ) );
	}
	else
	{
		var uid = $( '#upload_id' ).val();
		$.post( 'http://www.gazup.com/get-url/', {u_id:uid}, function(data){
			location.href = 'http://www.gazup.com/' + data; // REMOVE!!!
		});
	}
}
$( document ).ready(function(){
	updateMaxLimit();
	
	mirrorInterval = setInterval( 'updateMirrors()', mirrorIntervalCount );

	if( location.hash == "#debug" )
	{
		$( '#uploadframe' ).css( 'display', 'inline' );
	}


	$( '#uploadform' ).submit(function(){

		if( location.hash != '#debug' ){
			//alert( 'We are doing a complete overhaul of the upload mechanism, uploads are currently disabled!' );
			//return;
		}		
		$(this)[0].target = 'uploadframe';
		if(  $( '.limit:checked' ).length == 0 )
		{
			alert( 'Please select one or more filehosts to upload your file to!' );			
			return false;
		}
		
		if( $( '.adult:checked' ).length == 0 )
		{
			alert( 'Please select whether or not your file is 18+ material!' );
			return false;
		}
		
		if( $( '#fupload' ).val() == '' )
		{
			alert( 'Please select a file to upload!' );
			return false;
		}
		
		$( '#uploadbutton' ).fadeOut( 'slow' );
		$( '#choosehosts' ).animate( { opacity: 0.2 }, 1000 );
		$( ".limit" ).attr( 'readonly', 'readonly' );
		$( '#file_hide' ).fadeOut( 'slow', function(){
			$( '#progress_hide' ).fadeIn( 'slow', function(){
				//setTimeout( "updateProgress()", 3000 )
				var upload_id = $( '#upload_id' ).val();
				$.post( '/progress/', { server: 'http://fritzl.gazup.com', ident:upload_id }, startProgress, 'json' );
			});
		});
		
		//return false;
	});

	$( '.limit' ).change( updateMaxLimit );
	
	var upload_type = 'file';
	var remote_up_id = '#remoteuploadstep1';
	var has_confirmed_rs = 0;
	var can_change = 1;
	
	var upload_type = 'file';
	var remote_up_id = '#remoteuploadstep1';
	var has_confirmed_rs = 0;
	var can_change = 1;
	
	$( '.fupload' ).click(function(){
		if( ( upload_type == 'ftp' || upload_type == 'remote' ) && can_change == 1 )
		{
			$( '#remoteuploadtabon' ).attr( 'id', 'remoteuploadtaboff' );
			$( '#ftpuploadtabon' ).attr( 'id', 'ftpuploadtaboff' );
			$( this ).parent().attr( 'id', 'fileuploadtabon' );
			elem = ( upload_type == 'ftp' ) ? '#ftpuploadtab' : remote_up_id;
			upload_type = 'file';
			
			$( elem ).hide( 'slow', function(){									 
				$( '#file_hide' ).show( 'slow' );
			});
		}
		
		return false;
	});
	
	$( '.ftpupload' ).click(function(){
		if( ( upload_type == 'file' || upload_type == 'remote' ) && can_change == 1 )
		{
			$( '#fileuploadtabon' ).attr( 'id', 'fileuploadtaboff' );
			$( '#remoteuploadtabon' ).attr( 'id', 'remoteuploadtaboff' );
			$( this ).parent().attr( 'id', 'ftpuploadtabon' );
			elem = ( upload_type == 'file' ) ? '#file_hide' : remote_up_id;
			upload_type = 'ftp';
			
			$( elem ).hide( 'slow', function(){									 
				$( '#ftpuploadtab' ).show( 'slow' );
			});
		}
		
		return false;
	});
	
	$( '.rupload' ).click(function(){
		if(( upload_type == 'file' || upload_type == 'ftp' ) && can_change == 1 )
		{
			$( '#fileuploadtabon' ).attr( 'id', 'fileuploadtaboff' );
			$( '#ftpuploadtabon' ).attr( 'id', 'ftpuploadtaboff' );
			$( this ).parent().attr( 'id', 'remoteuploadtabon' );
			elem = ( upload_type == 'file' ) ? '#file_hide' : '#ftpuploadtab';
			upload_type = 'remote';
			
			$( elem ).hide( 'slow', function(){									 
				$( remote_up_id ).show( 'slow' );
			});
		}
		
		return false;
	});
	
	$( '#rslink' ).click(function(){
		$( '#remoteuploadstep1' ).slideUp( 'slow', function(){ 
			if( has_confirmed_rs == 0 )
			{
				remote_up_id = '#rapidshareremoteuploadstep1';
				$( '#rapidshareremoteuploadstep1' ).slideDown( 'slow' );
			}
			else
			{
				remote_up_id = '#rapidshareremoteuploadstep2';
				$( '#rapidshareremoteuploadstep2' ).slideDown( 'slow' );
			}
		});
		
		return false;
	});
	
	$( '.httplink' ).click(function(){
		
		$( remote_up_id ).slideUp( 'slow', function(){ 
			remote_up_id = '#remoteuploadstep1';
			$( '#remoteuploadstep1' ).slideDown( 'slow' );
		});
		
		return false;
	})
	
	$( "#checkrs" ).click(function(){
		$( this ).attr( 'disabled', 'disabled' );
		$( this ).val( 'Checking Account...' );
		rs_username = $( '.rsuser' ).val();
		rs_password = $( '.rspassword' ).val();
		
		if( rs_username == '' || rs_password == '' )
		{
			alert( "Please fill out your RapidShare Premium Account username/password!" )
			$( this ).val( 'Continue &#187;' );
			$( this ).attr( 'disabled', '' );
			return false;
		}
		elem = $( this );
		$.post( "/check-rs/", { username:rs_username, password:rs_password }, function(data){
			if( parseInt( data ) != 1 )
			{
				alert( "Failed to log into your RapidShare Premium Account!" );	
				$( elem ).attr( 'disabled', '' );
				$( elem ).val( 'Continue &#187;' );
			}
			else
			{
				$( '#rapidshareremoteuploadstep1' ).slideUp( 'slow', function(){ 
					has_confirmed_rs = 1;
					remote_up_id = '#rapidshareremoteuploadstep2';
					$( '#rapidshareremoteuploadstep2' ).slideDown( 'slow' );
				});
			}
		});
	});
	
	var has_clicked = 0;
	$( '.ubutton1' ).click(function(){
		if( has_clicked == 1 )
		{
			return false;
		}
		
		if( $( '#normal_urls' ).val() == '' )
		{
			alert( "Please enter one or more URLs to remote files to continue." );
			return false;
		}
		
		if( !$( '.normal_adult:checked' ).val() )
		{
			alert( "Please select whether or not this file is adult material." );
			return false;
		}
		
		parent_elem = $( this );
		$( this ).animate( { "opacity": 0.3 }, 500 );
		has_clicked = 1;
		$.post( '/send-remote/', { urls: $( '#normal_urls' ).val(), normal:'true' }, function(data2){
			eval(data2)
			if( json.has_error == 1 )
			{
				alert( json.error );
				has_clicked = 0;
				can_change = 1;
				
				parent_elem.animate( { "opacity": 1 }, 500 );
				return false;
			}

			errors = '';
			for( i=0; i<json.items.length; i++ )
			{
				if( json.items[i] > limit )
				{
					errors += '    - File #' + ( i+1 ) + ' is too big with current selections (file is ' + json.items[i] + 'mb)' + "\n";
				}
			}
			
			if( errors != '' )
			{
				errors = "The following files will not be uploaded:\n" + errors
				alert( errors )
				has_clicked = 0;
				can_change = 1;
				
				parent_elem.animate( { "opacity": 1 }, 500 );
				return false;
			}
			else
			{
				var filehosts = ''
				$( '.limit:checked' ).each(function(){
					filehosts += ',' + $( this ).attr( 'name' ).substr( 5, 1 )
				});
				
				filehosts = filehosts.substr(1);
				
				// Send the actual uploading AJAX call
				$( '#remoteuploadstep1' ).slideUp( 'slow', function(){
					$( '#rapidshareremoteuploadstep3' ).slideDown( 'slow' );						  
				});
				
				$.post( '/send-remote/', { 
					   hosts:filehosts, urls: $( '#normal_urls' ).val(), send:'true', rapidshare:'false', adult:$( '#normal_adult' ).val() }, function(data){
		   			//alert( data ); return;
					eval(data)
					
					if( json.urls.length > 0 )
					{
						for( i=0; i<json.urls.length; i++ )
						{
							$( '#normal_remote_links' ).html( $( '#normal_remote_links' ).html() + '<li><a href="' + json.urls[i] + '" title="Go to the mirror page">' + json.urls[i] + '</a></li>' );
						}
						
						$( '#rapidshareremoteuploadstep3' ).slideUp( 'slow', function(){
							$( '#remoteuploadstep2' ).slideDown( 'slow' );						  
						});
					}
				});
			}
		});
		return false;
	});
	
	
	$( '.dorsup' ).click(function(){
		if( has_clicked == 1 )
		{
			return false;
		}
		
		if( $( '#rsurls' ).val() == '' )
		{
			alert( "Please enter one or more URLs to remote files to continue." );
			return false;
		}
		
		if( !$( '.rs_adult:checked' ).val() )
		{
			alert( "Please select whether or not this file is adult material." );
			return false;
		}
		
		$( this ).animate( { "opacity": 0.3 }, 500 );
		
		parent_elem = $( this );
		has_clicked = 1;
		can_change = 0;
		$.post( '/send-remote/', { urls: $( '#rsurls' ).val(), rshare:'true' }, function(data2){
			eval(data2)
			if( json.has_error == 1 )
			{
				alert( json.error );
				can_change = 1;
				has_clicked = 0;
				
				parent_elem.animate( { "opacity": 1 }, 500 );
				return false;
			}

			errors = '';
			for( i=0; i<json.items.length; i++ )
			{
				if( json.items[i] > limit )
				{
					errors += '    - File #' + ( i+1 ) + ' is too big with current selections (file is ' + json.items[i] + 'mb)' + "\n";
				}
			}
			
			if( errors != '' )
			{
				errors = "The following files will not be uploaded:\n" + errors
				alert( errors )
				has_clicked = 0;
				can_change = 1;
				
				parent_elem.animate( { "opacity": 1 }, 500 );
				return false;
			}
			else
			{
				var filehosts = ''
				$( '.limit:checked' ).each(function(){
					filehosts += ',' + $( this ).attr( 'name' ).substr( 5, 1 )
				});
				
				filehosts = filehosts.substr(1);
				
				// Send the actual uploading AJAX call
				rs_username = $( '.rsuser' ).val();
				rs_password = $( '.rspassword' ).val();
				$( '#rapidshareremoteuploadstep2' ).slideUp( 'slow', function(){
					$( '#rapidshareremoteuploadstep3' ).slideDown( 'slow' );						  
				});
				
				$.post( '/send-remote/', { 
					   hosts:filehosts, urls: $( '#rsurls' ).val(), rapidshare:'true', send:'true', rsuser:rs_username, rspass:rs_password, adult:$( '#rs_adult' ).val() }, function(data){
					eval(data)
					
					if( json.urls.length > 0 )
					{
						for( i=0; i<json.urls.length; i++ )
						{
							$( '#rs_remote_links' ).html( $( '#rs_remote_links' ).html() + '<li><a href="' + json.urls[i] + '" title="Go to the mirror page">' + json.urls[i] + '</a></li>' );
						}
						
						$( '#rapidshareremoteuploadstep3' ).slideUp( 'slow', function(){
							$( '#rapidshareremoteuploadstep4' ).slideDown( 'slow' );						  
						});
					}
				});
			}
		});
		return false;
	})
});

function updateMaxLimit()
{
	limit = false;
	$( '.limit:checked' ).each(function(){
		thisLimit = $( this ).attr( 'rel' );
		limit = ( !limit ) ? thisLimit : ( thisLimit < limit ) ? thisLimit : limit;
		$( '.maxlimit' ).html( limit + 'mb' );	
	});
}

function startProgress( json )
{
	if( parseInt( json.filesize ) > parseInt( limit ) )
	{
		alert( "The file you're trying to upload is bigger than the max\nfilesize based on selections...\n\nPlease change your selections or upload another file!" );
		location.reload();
	}
	
	if( json.completed == 1 )
	{
		//setTimeout( "redirect_me()", 2000 ) 
		//alert( frames['uploadframe'].location.href );
		
		return; 
	}

	newPercent = ( ( json.percent / 100 ) * 455 ) + 'px';
	$( '#progressbarrepeat' ).html( json.percent + '%' );
	$( '#uploadstats' ).html( 'File: <strong>' + json.filename + '</strong><br />Uploaded: ' + json.nice_upl + '/' + json.nice_total + ' <em>|</em> ' + json.speed + 'kbps <em>|</em> ' + json.time_left + ' left' );

	if( json.percent > oldPercent )
	{
		$( '#progressbarrepeat' ).animate( { width: newPercent }, 1000, 'swing', function(){
			oldPercent = json.percent;
			updateProgress();
		});
	}
	else
	{
		setTimeout( "updateProgress()", 1000 );
	}
}

function updateProgress()
{
	var upload_id = $( '#upload_id' ).val();
	$.post( '/progress/', { server: 'http://fritzl.gazup.com', ident:upload_id }, startProgress, 'json' );
}

function updateMirrors()
{
	/*$.get( '/ajax-mirrors/', function(data){
		$( '#mirrorcount' ).html( data + ' mirrors');
	});*/	
}

function redirect_me()
{
	location.href = frames['uploadframe'].location.href;	
}
