// Aricles Slider with AJAX dynamic loading  
// Milos Dakic, HNS Media, 2011. - www.hnsmedia.net
//

var rcounter=0;	// number of slides to the right (clicks to right)
var loaded=2;	// number of loaeded slides (-1, to start preloading one slide before)


slide_left = function() {
			rcounter--;
			if (rcounter == 0) $("#slide_previous").fadeOut("slow");
			
			if (parseInt($("#inner_container").css("left")) < 0) {
				
				$("#inner_container").animate( 
					{left : "+=980"}, 
					{
						duration: 500,
						step: function (){ if (parseInt($(this).css("left")) >= 0) $(this).stop();  }	// prevent sliding over the left edge */
					}
				);
			}
			
		
}	// slide left ()



load_content = function(rcounter_val){		
			$.ajax({
						url: "ajax/get_next_articles.php",
						data: { skip : rcounter_val },
						type: "GET",
						dataType: "html",
						success: function (html){
							//console.log("loadovan sadrzaj: " + html);
							$("#inner_container").append(html);
							$.getScript("http://platform.twitter.com/widgets.js");	// loads and executes script, twitter button
							$.getScript("http://apis.google.com/js/plusone.js");
							//console.log('load scripptttt');
						}
			 	});					
}	// load_content()



slide_right = function() {
			var rcounter_cur=++rcounter;		
			// take to local variable current rcounter value, and pass it below to load_content, to prevent double click to incremet rcounter global var twice
			//console.log("right click: " + rcounter_cur);
			if (rcounter_cur == 1) $("#slide_previous").fadeIn("slow");
			
			$("#inner_container").animate( 
				{left : "-=980"}, 
				{duration: 500,
				queue: true,
				complete: function() {
						if (rcounter_cur >= loaded) {	// load new content
							
							ic_width+=980;
							$("#inner_container").width(ic_width);
							//console.log("current inner container width: " + ic_width);
							
							load_content(rcounter_cur);
							loaded++;
						}
					}
				}
			);
}	// slide_right()
