var poze_cnt = 0;

function listImg_sterge() {
	$('.listImg a').click(function() {
		if(confirm('Esti sigur')) {
			$('.listImg').fadeOut('slow', function() {
				$('.listImg').remove();
			});
		}
		return false;
	});
}

$(function() {
	listImg_sterge();

	function swfupload_control(button, width, height, tip, params)
	{
		$('#swfupload-control').swfupload({
			upload_url: '/assets/js/swfupload/upl.php?action=getUploadSwfImg&tip='+tip,
			file_post_name: 'Filedata',
			file_size_limit : '5120',
			file_types : '*.jpg;*.jpeg',
			file_types_description : 'Image files',
			file_upload_limit : 10,
			flash_url : '/assets/js/swfupload/swfupload.swf',
			flash9_url : '/assets/js/swfupload/swfupload_f9.swf',
			button_image_url : '/assets/js/swfupload/'+button+'.png',
			button_width : width,
			button_height : height,
			button_placeholder : $('#button_upload_swf')[0],
			post_params: {'pic_sis' : $('input[name="pic_sis"]').val() },
			use_query_string : false,
			requeue_on_error : false,
			http_success : [201, 202],
			assume_success_timeout : 0,
			file_types_description: 'Alege imagine de tip',
			file_queue_limit : 1,
			debug : false,	
			prevent_swf_caching : true,
			preserve_relative_urls : true,
			button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
			button_disabled : false,
			button_cursor : SWFUpload.CURSOR.HAND,
			button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT
		})
			.bind('fileQueued', function(event, file) {
				++poze_cnt;
				
				if($('.listImg').size() > 0)
				{
					$('.listImg').remove();
				}

				var listitem='<li id="'+file.id+'">'+
					'Imagine: '+file.name+' ('+Math.round(file.size/1024)+' KB) <span class="progressvalue"></span>'+
					'<div class="progressbar"><div class="progress"></div></div>'+
					'<p class="status">In asteptare</p>'+
					'<span class="cancel">&nbsp;</span>'+
					'</li>';
				
				if(tip == 'poza' || tip == 'poza2' || tip == 'sigla') $('#log').addClass('mic');
				
				$('#log').append(listitem);
				$('li#'+file.id+' .cancel').bind('click', function(){ //Remove from queue on cancel click
					var swfu = $.swfupload.getInstance('#swfupload-control');
					swfu.cancelUpload(file.id);
					$('li#'+file.id).slideUp('fast');
				});
				
				// start the upload since it's queued
				$(this).swfupload('startUpload');
			})
			.bind('fileQueueError', function(event, file, errorCode, message){
				var alert = 'Marimea fisierului '+file.name+' este mai mare decat limita!';
				
				if ( errorCode == -120 ) alert = 'Imagine invalida.';
				if ( errorCode == -110 ) alert = 'Marimea imaginii depaseste limita actuala ( 5MB ).';
				if ( errorCode == -130 ) alert = 'Extensie imaginine incorecta.';
				if ( errorCode == -100 ) alert = 'Numarul de fisiere selectate depaseste maximul permis ( 60 ).';
				
				alert(alert);
			})
			.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
				//$('#queuestatus').text('Imagini selectate: '+numFilesSelected+' / Imagini preluate: '+numFilesQueued);
			})
			.bind('uploadStart', function(event, file){
				$('#log li#'+file.id).find('p.status').text('Se incarca...');
				$('#log li#'+file.id).find('span.progressvalue').text('0%');
				$('#log li#'+file.id).find('span.cancel').hide();
			})
			.bind('uploadProgress', function(event, file, bytesLoaded){
				var percentage=Math.round((bytesLoaded/file.size)*100)
				var item = $('#log li#'+file.id);
				item.find('div.progress').css('width', percentage+'%');
				item.find('span.progressvalue').text(percentage+'%');
				if(percentage == 100) item.addClass('success').find('p.status').html('Asteptati redimensionarea imaginii...');
			})
			.bind('uploadSuccess', function(event, file, serverData){
				serverData = $.parseJSON(serverData);
				
				var item=$('#log li#'+file.id);
				
				if(serverData.validare != 'ok')
				{
					$('#log li#'+file.id).find('span.cancel').show();
					item.find('div.progress').css('width', '0%');
					item.find('span.progressvalue').text('0%');
					item.addClass('fail').find('p.status').html(serverData.validare);
				}
				else
				{
					item.find('div.progress').css('width', '100%');
					item.find('span.progressvalue').text('100%');
					item.addClass('success').find('p.status').html('Imagine salvata cu succes!');
					
					if(tip == 'poza' || tip == 'sigla')
					{
						$('.listImg').remove();
						$('#log').after('<div class="listImg none"><img src="'+serverData.poza.dir+serverData.poza.poza+'" alt="" /><input type="hidden" name="upl_'+tip+'" value="'+serverData.poza.dir+serverData.poza.poza+'" /></div>');
					}
					else if(tip == 'poza2')
					{
						$('.prodoriz img').attr({
							src	: serverData.poza.dir+serverData.poza.poza,
							width : serverData.poza.w,
							height : serverData.poza.h
						});
					}
					else if(tip == 'sigla2')
					{
						$('#logomagazin').attr({
							src	: serverData.poza.dir+serverData.poza.poza,
							width : serverData.poza.w,
							height : serverData.poza.h
						});
					}
					
					var oSelf = '#log li#'+file.id;
					setInterval('fade_complete(\''+oSelf+'\')', 1000);
				}
			})
			.bind('uploadComplete', function(event, file){
				// upload has completed, try the next one in the queue
				$(this).swfupload('startUpload');
				--poze_cnt;

				if(poze_cnt == 0) {
					//setInterval('window.location.href = $(\'#tabs a:eq(1)\').attr(\'href\');', 3000);
				}
			});
	}

	var cls = $('#log').attr('class');
	if(cls == 'upl_poza') {
		swfupload_control('incarcabtn', 88, 28, 'poza', false);
	} else if(cls == 'upl_poza2') {
		swfupload_control('incarcabtn', 88, 28, 'poza2', false);
	} else if(cls == 'upl_sigla') {
		swfupload_control('incarcabtn', 88, 28, 'sigla', false);
	} else if(cls == 'upl_sigla2') {
		swfupload_control('button', 90, 28, 'sigla2', false);
	}
});

function fade_complete(oSelf) {
	jQuery(oSelf).fadeOut('slow', function() {
		$(this).remove();

		if($('.listImg').size() > 0)
		{
			$('.listImg').removeClass('none').addClass('block');
		}
	});
};
