(function($) {
	$.fn.weekin = function(settings) {
		settings = $.extend({
						params: ["","","","",""]
		},settings);
	
		return this.each(function() {
			var me = $(this);
			var tabs = me.find(".tab");
			resetTabs(tabs, 0);
			addTabActions(tabs);
			me.block({
	        			message: '<img src="img/ajax-loader.gif" />',  
    					css: { border: 'none',backgroundColor: 'transparent'},
    					overlayCSS: {backgroundColor:'#fff', opacity: '0.8'}
	        }); 
			$.ajax({
				url: "offers.php?"+settings.params[0],
				dataType: "json",
				type: "GET",
				success: offersContent
			});
			
			function resetTabs(tabs, index) {
				tabs.find("li").removeClass("current");
				tabs.find("li:eq(" + index + ")").addClass("current");
			}
			function addTabActions(tabs) {
				tabs.find("a").each(function(i){
					$(this).attr("href", "#");
					$(this).bind("click", function() {
						
				        me.block({
				        			message: '<img src="img/ajax-loader.gif" />',  
                					css: { border: 'none',backgroundColor: 'transparent'},
                					overlayCSS: {backgroundColor:'#fff', opacity: '0.8'}
				        }); 
						$.ajax({
							url: "offers.php?"+settings.params[i],
							dataType: "json",
							type: "GET",
							success: offersContent
						});
						resetTabs(tabs,i);
						return false;
					})
				});
			}
			function offersContent(json) {
				var offersHtml = '';
				if(json.count > 0) {
					$.each(json.offers, function(i, n) {
						offersHtml += '<div class="proposta panel">\n';
						offersHtml += '	<div><a href="' + n.link + '"><img src="img/proposta/' + n.image + '" alt="' + n.title + '" /></a></div>';
						offersHtml += '	<div class="desc_proposta">';
						offersHtml += '		<a href="' + n.link + '">';
						offersHtml += '			<span class="titolo">' + n.title + '</span>';
						offersHtml += '			<span class="desc">' + n.description + '</span>';
						offersHtml += '			<span class="prezzo">' + n.price + '</span>';
						offersHtml += '		</a>';
						offersHtml += '	</div>';
						offersHtml += '</div>';
					});
					offersHtml += '<div class="blocca">&nbsp;</div>';
					me.find(".panelContainer").html(offersHtml);
					me.find(".panelContainer").css("left",0);
					me.find(".sx").html(json.count + ' proposte in ' + json.giorni + '<a href="' + json.giorni_link + '">vedi tutte</a>');
					location.hash = "#1";
					me.codaSlider();
					me.unblock();
				}
			}
		});
	};
})(jQuery);