function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

var counter = 0;
var url= "/upload.php";

$(document).ready(function() {
	$('#fileInput').fileUpload ({
	'uploader'  : '/images/uploader.swf',
	'script'    : url,
	'cancelImg' : '/images/cancel.png',
	'auto'      : true,
	'fileExt'	: '*.jpg;*.gif;*.png',
	'fileDesc'	: 'Image files',
	'simUploadLimit': 1,
	'sizeLimit' : 2024 * 1024,
	'multi'		: true,
	onError		: function(event, queueID, fileObj, errorObj){
		alert(errorObj.text);
	},
	onSelect	: function(event, queueID, fileObj){
		$('#uploading').css('display', '');
		$('#finished').css('display', 'none');
		
		$('#fileInput').fileUploadSettings('scriptData', '&user_id='+userId.toString()+'&category='+$('#category').attr('value')+'&session='+sessionId);
	},
	onAllComplete:function(filesUploaded, errors, allbytesLoaded, speed){
		$('div.wallpaper').hover(function(){
			$('div#delete_' + $(this).attr('id')).show();
			$('div#change_' + $(this).attr('id')).show();
		}, function(){
			$('div#delete_' + $(this).attr('id')).hide();
			$('div#change_' + $(this).attr('id')).hide();
		});
		
		$('div.wallpaper_change').click(function(){
			var queueId = $(this).attr('rel');
			
			ShowScreenForEdit(queueId);
		});
		
		$("A[href='#delete_wallpaper']").click(function(){
			var queueId = $(this).attr('rel');
			var id = $("INPUT[name='id_" + queueId + "']").attr('value');
			var value = $("INPUT[name='value_" + queueId + "']").attr('value');
			
			DeleteWallpaper(queueId, id, value);
			
			return false;
		});
		
		$('#uploading').css('display', 'none');
		$('#finished').css('display', '');
	},
	onComplete	: function(event, queueID, fileObj, response, data){
		if(response == '0') {
			alert('Error occured during wallpaper upload!');
			return;
		}
		
		$('#empty_list').css('display', 'none');
		$('#clear_list').css('display', '');
		
		var obj = '<div class="wallpaper" id="' + queueID + '" style="display:none;">';
		obj += '	<div class="wallpaper_image" style="position:relative;">';
		obj += '	<div id="delete_' + queueID + '" class="wallpaper_delete" style="display:none;"><a rel="'+queueID+'" href="#delete_wallpaper" title="Delete wallpaper"><img src="/images/admin/delete.png" alt="Delete wallpaper" /></a></div>';
		obj += '	<div rel=' + queueID + ' id="change_' + queueID + '" class="wallpaper_change" style="display:none;">Click to change</div>';
		obj += '	<a href="/web/wallpapers/detail/id/'+response+'/browse/last" title="'+fileObj.name+'"><img src="/images/wallpapers/index/id/'+response+'/type/fp" alt="'+fileObj.name+'" /></a></div>';
		obj += '	<div class="wallpaper_right_shadow"></div>';
		obj += '	<div class="clear_both"></div>';
		obj += '	<div class="wallpaper_bottom_shadow"></div>';
		obj += '	<div class="wallpaper_title"><a href="/web/wallpapers/detail/id/'+response+'/browse/last" title="'+fileObj.name+'">'+fileObj.name.substring(0,11)+'...</a></div>';
		obj += '	<input type="hidden" name="id_' + queueID + '" value="'+response+'" />';
		obj += '	<input type="hidden" name="value_' + queueID + '" value="'+fileObj.name.substring(0,11)+'" />';
		obj += '</div>';
		
		$('#uploaded_wallpapers').attr('innerHTML', $('#uploaded_wallpapers').attr('innerHTML') + obj);
		$('#' + queueID).show();

 		counter++;
	}
	});
	
	$("A[href='#delete_all']").click(function(){
		$.post(url, {current:'delete',session:sessionId}, function(data){
			ClearUploadWallpapersList()
		});
		return false;
	});
	$("A[href='#clear_list']").click(function(){
		ClearUploadWallpapersList();
		return false;
	});
});

function ShowScreenForEdit(queueId){
	var id = $("INPUT[name='id_" + queueId + "']").attr('value');
	var value = $("INPUT[name='value_" + queueId + "']").attr('value');
	
	var obj = '';
	obj += '	<div style="border: medium none ; margin: 0pt; padding: 0pt; z-index: 1000; cursor: wait; width: 100%; height: 100%; top: 0pt; left: 0pt; background-color: rgb(0, 0, 0); opacity: 0.6; position: fixed;" id="gray_field" style="display:none;">';
	obj += '	</div>';
	obj += '	<div id="data_div" style="margin: 0px; padding: 0px; z-index: 1001; position: fixed; width: 400px; top: ' + ((getClientHeight() - 500) / 2) + 'px; left: ' + ((getClientWidth() - 500) / 2) + 'px; text-align: left; height: 400px;">';
	obj += '		<div class="background-500-top"></div>';
	obj += '		<div class="background-500">';
	obj += '			<div class="sw_title">Change wallpaper\'s title</div><div>&nbsp;</div>';
	obj += '			<div align="center"><img src="/images/wallpapers/index/id/'+id+'/type/cw" /></div><div>&nbsp;</div>';
	obj += '			<div class="change_wall_data">';
	obj += '				<div>&nbsp;</div>';
	obj += '				<label for="value">Wallpaper\'s title:</label>';
	obj += '				<input type="text" name="value" id="value" value="'+value+'" maxlength="255" class="form_input_width" />';
	obj += '			</div>';
	obj += '			<div>&nbsp;</div>';
	obj += '			<div>&nbsp;</div>';
	obj += '			<div class="change_wall_buttons"><img src="/images/save_red_small.png" class="save" alt="Save wallpapers" />&nbsp;<img src="/images/cancel_black_small.png" class="close" alt="Close" /></div>';
	obj += '		</div>';
	obj += '		<div class="background-500-bottom"></div>';
	obj += '	</div>';
	
	$('#add_data_div').attr('innerHTML', obj);
	$('#gray_field').show();
	
	$('#gray_field').click(function(){
		CloseWindow();
	});
	
	$('img.close').click(function(){
		CloseWindow();
	});
	
	$('img.save').click(function(){	
		inSave = true;
		
		$.post(url, {current:'update',session:sessionId,id:id,value:$("INPUT[name='value']").attr('value')}, function(data){
			if(data == '0')
				alert('Error during wallpapers update!');
			else {
				$("INPUT[name='value']").attr('value', data);
				$("INPUT[name='value_" + queueId + "']").attr('value', data);
				CloseWindow();
				$('#add_data_div').attr('innerHTML', '');
				$('div#' + queueId + ' div.wallpaper_title a').attr('innerHTML', data.substring(0,11) + "...");
			}
		});
	});
}

function ClearUploadWallpapersList(){
	$('#uploaded_wallpapers').attr('innerHTML', '');
	
	$('#empty_list').css('display', '');
	$('#clear_list').css('display', 'none');
}

function DeleteWallpaper(queueId, id, name){
	if(!confirm('Are you sure you want to delete wallpaper: ' + name + '?'))
		return false;
	
	$.post(url, {current:'delete',session:sessionId,id:id}, function(data){
		$("div#" + queueId).hide();
		counter--;
		
		if(counter == 0)
			ClearUploadWallpapersList()
	});
}
function CloseWindow(){
	$('#gray_field').fadeOut('def');
	$('#data_div').fadeOut('def');
}	