var popupStatus = 0;
var idItem = 0;
var selectitem_params = {userId: '', guid: '', activity: 'userSelectItem',	key:'69a256025f66e4ce5d15c9dd7225d357'}
var piace_params = {userId: '', guid: '', activity: 'piace_a',	key:'69a256025f66e4ce5d15c9dd7225d357'}
var count_params = {userId: '', guid: '', activity: 'comments_count',	key:'69a256025f66e4ce5d15c9dd7225d357'}
var selectItemUrl = '/service/utility/activity.php?';	
var selectedItemId = '';
var selectedItems =  null;


function startSelectItem() {
	setTimeout(function() {
		if($('img[id^="eveselecteditem_"]'))
			initSelectItem();
		else
			startSelectItem();
	}, 250);
}


function initSelectItem() {
	var cookieVals = getCookie('amerg_cookie');
	if (cookieVals!=null && cookieVals !=''){
		var cookie=cookieVals.split('*');
		selectitem_params['userId'] = cookie[1];
	} else {
	}
	
	$('.eveselecteditem').each(function() {
		$(this).click(function() {
			idItem = $(this).attr('id');
			sendActivityCall($(this).attr('id'));
			
		});
	});
	
}

function sendActivityCall() {
	var qIndex = idItem.indexOf('_', 0);
	if (qIndex > 0) {
		var found = false;
		for(var i = 0; i < selectedItems.length; i++) {
			if(selectedItems[i] == idItem) {
				found = true;
				break;
			}
		}
		if(!found) {
			selectedItems.push(idItem);
			selectedItemId = idItem.substring(qIndex + 1,idItem.length);
			callActivity(selectedItemId);
			
		}
	}
}


function callActivity(id){
	selectitem_params['guid'] = id;
	jQuery.ajax({
		type: 'POST',
		url: selectItemUrl,
		dataType: 'html',
		data: selectitem_params,
		success: function (data, textStatus) {
			alert('Aggiunto alla tua selezione');
		},
		error: function (xhr, ajaxOptions, thrownError){
			alert('Server error ' + xhr.status );return(false);
		}
	});
}

function getSimpleUrl(){
	var url = '' + window.location;
	var qIndex = url.indexOf('?',0);
	if (qIndex > 0)
		url = url.substring(0,qIndex);
	return url;
}


function getCookie(NameOfCookie) {
	if (document.cookie.length > 0) {
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return null;
}


function getPiaceA(id,idspan) {
	piace_params['guid'] = id;
	jQuery.ajax({
		type: 'POST',
		url: selectItemUrl,
		dataType: 'html',
		data: piace_params,
		success: function (data, textStatus) {
		    var json_data = eval("("+data+")");
		    if (json_data["data"] == 0) {
		    	$('#'+idspan).html('nessun voto');
		    }
		    else if (json_data["data"] == 1) {
		    	$('#'+idspan).html('piace a '+ json_data["data"] + ' persona');
		    }
		    else {
		    	$('#'+idspan).html('piace a '+ json_data["data"] + ' persone');
		    }
		},
		error: function (xhr, ajaxOptions, thrownError){
			return '0';
		}
	});
	
}


$(document).ready(function(){
		
	selectedItems = new Array();
	startSelectItem();
	
});


