var lastFavOfferId = 0;

function initJCarousel() {
	jQuery("#JMyCarousel").jMyCarousel({
		visible: '100%',
		start: 0,
		auto: true,
		speed: 1500,
		circular: true
	});
	jQuery('#JMyCarousel > ul').css({left : '0px'});
}

// usuwanie oferty ze schowka
function removeFromClipboard( offerId ) {
	showAjaxIndicator();
	jQuery.ajax({
		type : 'GET',
		url : '/user/removeFromClipboard.html',
		data: 'offer_id='+offerId,
		dataType: 'json',
		success: function(re) {
			hideAjaxIndicator();
			alert(re.msg);
			if (re.refresh) {
				jQuery("#offer"+offerId).remove();
				jQuery(".loginBox").load('/default/loginLinks.html');
			}
		},
		error:  function(){ hideAjaxIndicator(); alert( removeFromClipError? removeFromClipError: 'ERROR' ); }
	})
}

function removeFromFavourites( offer_id ) {
	showAjaxIndicator();
	if (!offer_id) {
		offer_id = lastFavOfferId;
	} else {
		lastFavOfferId = offer_id;
	}
	jQuery.ajax({
		type : 'GET',
		url : '/user/removeFromFavourites.html',
		data: 'offer_id='+offer_id,
		dataType: 'json',
		success: function(re) {
			if (re.msg == 'login') {
				showAjaxLogin('removeFromFavourites', offer_id);
			} else {
				alert(re.msg);
				if (re.refresh) {
					jQuery("#offer"+offer_id).remove();
				}
				tb_remove();
			}
		},
		error:  function(request) { 
			if (request.status == 401) {
				showAjaxLogin('removeFromFavourites', offer_id);
			} else {
				alert(genericAjaxError); 
				tb_remove();
			}
		}
	})
}

function addToClipboard(offer_id) {
	showAjaxIndicator();
	jQuery.ajax({
		type : 'GET',
		url : '/user/addToClipboard/offer_id/'+offer_id+'.html' ,
		dataType: 'json',
		success: function(re){
			hideAjaxIndicator();
			alert(re.msg);
		},
		error:  function(request) { 
			hideAjaxIndicator();
			alert(genericAjaxError); 
		}
	})
	return(false);
}

function addToFavourites(offer_id) {
	if (!offer_id) {
		offer_id = lastFavOfferId;
	} else {
		lastFavOfferId = offer_id;
	}
	showAjaxIndicator();
	
	jQuery.ajax({
		type : 'GET',
		url : '/user/addToFavourites/offer_id/' + offer_id + '.html',
		dataType: 'json',
		success: function(re){
			hideAjaxIndicator();
			if (re.msg == 'login') {
				showAjaxLogin('addToFavourites', offer_id);
			} else {
				alert(re.msg);
				lastFavOfferId = 0;
			}
		},
		error:  function(request) {
			if (request.status == 401) {
				showAjaxLogin('addToFavourites', offer_id);
			} else {
				hideAjaxIndicator();
				alert(genericAjaxError);
			}
		}
	})
	return false;
}

function hideAjaxIndicator() {
	tb_remove();
}

function showAjaxIndicator() {
	if (!jQuery("#ajaxIndicator").length) {
		jQuery("body").append('<div id="ajaxIndicator" style="display: none; color: black;"><span>Przetwarzanie danych</span><br /><img src="/images/ajax-loader-trans.gif" alt="" /> </div>');
	}
	tb_remove();
	tb_open_new('Przetwarzanie danych', '#TB_inline?KeepThis=true&height=80&width=240&inlineId=ajaxIndicator&modal=true', false);
}

