var errorMessages = '';
var swfUploadCount = 0;
var allowedFilesNumber = 4;

function previewImage(serverData){
	var response = eval("(" + serverData + ")");
	var imgUrl = '';
	var innerHtml = '';
	var idGallery = $('#gimsGalleryId').val();
	$.ajax({
		type: "POST",
		url: baseURL + "upload_contest/ajaxCreateThumbnail",
		data: "id=" + response.data.id + "&galleryId=" + idGallery,
		success: function(data){
			var returnData = eval("(" + data + ")");
			if(returnData.error == '0'){
				imgUrl = returnData.error_message+'/operation=resize&amp;resize_type=surface&amp;width=200&amp;height=150&amp;operation=crop&amp;crop_x=middle&amp;width=200&amp;crop_y=middle&amp;height=150';
				innerHtml = '<li id="list_'+response.data.id+'"><div><img src="'+imgUrl+'" alt="" border="0" /></div><a href="javascript://" title="sterge poza" id="del_'+response.data.id+'">şterge poza</a></li>';
				$('#thumbnails').append(innerHtml);
				applyDeleteAction();
			}else{
				errorMessages = returnData.error_message;
				showErrors();
			}
		}
	});
}

function applyDeleteAction(){
	$('[id^=del_]').unbind('click');
	$('[id^=del_]').click(function(){
		deleteImage($(this).attr('id'));
	});
}

function start_upload_handler(uploader, file){
	var filesCount = countSubmittedFiles();

	var stats = uploader.getStats();
	var total_files = filesCount + stats.files_queued;

	if (allowedFilesNumber < total_files){
		uploader.cancelUpload();
		errorMessages = "Puteţi uploada maxim "+allowedFilesNumber+" fişiere";
		showErrors();
		return false;
	}
	
	return true;
}

function countSubmittedFiles(uploader, file){
	var idGallery = $('#gimsGalleryId').val();
	var filesCount = 0;
	$.ajax({
		type: "POST",
		url: baseURL + "upload_contest/ajaxCountUploadedFiles",
		async: false,
		data: "galleryId=" + idGallery,
		success: function(data){
			filesCount = parseInt(data);
		}
	});
	return filesCount;
}

function deleteImage(str){
	var id = str.slice(4);
	var idGallery = $('#gimsGalleryId').val();
	$.ajax({
		type: "POST",
		url: baseURL + "upload_contest/ajaxDeleteImage",
		data: "id=" + id + "&galleryId=" + idGallery,
		success: function(data){
			var response = eval("(" + data + ")");
			if(response.error == '0'){
				$('#list_'+id).remove();
			}else{
				errorMessages = 'Eroare. Fişierul nu a putut fi şters';
				showErrors();
			}
		}
	});
}

function showErrors(){
	$('#error_messages').html(errorMessages);
	$('#error_messages').css('display', 'block');
}

function hideErrors(){
	if($('#error_messages').css('display') == 'block'){
		$('#error_messages').html('');
		$('#error_messages').css('display', 'none');
	}
}

function applyMagnifyAction(){
	$(".magnify_img").click(function(){
		var full_url = $(this).children('img').attr('src');
		var full_index = $(this).children('img').attr('id');
		var index = full_index.slice(6);
		var position = full_url.indexOf('operation=resize');
		var url = full_url.slice(0, position);
		$("#current_picture").children('img').attr('src', url+"operation=resize&width=570&height=450");
		applyFullSizeAction(index);
	});
}
function applyFullSizeAction(index){
	$("#current_picture a, #current_picture img").unbind('click');
	$("#current_picture a, #current_picture img").click(function(){
		window.location.assign(basic_url+index);
//		window.open(basic_url+index);
	});
}

$(document).ready(function(){ 
	$('#subscribe-form').click(function(){
		hideErrors();
	});
	$('a[id^=contest_rules_]').click(function (){
		var contest_id = $(this).attr('id').split('_');
		contest_id = contest_id[(contest_id.length - 1)];
		tb_show('Regulament concurs', baseURL + 'contest/ajax_contest_rules/'+contest_id, '');
	});
	$('#completeSubscribe').click(function(){
		hideErrors();
		$('#subscribe-form').submit();
	});
});

$(document).ready(function (){
	$('.vote_button span').click(function (){
		vote_participation(this);
	});
});

function vote_participation (el) {
	var participation_id = $(el).attr('id').split('_');

	participation_id = participation_id.length > 1 ? participation_id[1] : null;

	if (participation_id == null) return null;

	var response = null;

	$.ajax({
		url			: baseURL + 'contest_participants/vote_participation',
		async		: false,
		data		: {'participation_id' : participation_id},
		dataType	: 'json',
		type		: 'POST',
		success		: function (data){
			response = data;
		}
	});

	if (response != null) {
		alert(response.message);
		if (response.status != undefined && response.status == true) {
			location.reload();
		}
	} else {
		alert('Eroare aplicatie !');
	}
	return null;
}
