	// Latest News Scroll

jQuery(function($) {

	function px2num(s) {
		return (s.substr(0, s.length - 2) * 1);
	}
	
	// resize #carousel-offers
	no_offers = $("#carousel-offers .carousel-offerbox").size();
	width_of_single_offer = px2num($(".carousel-offerbox").css("width")) + 
							px2num($(".carousel-offerbox").css("margin-left")) + 
							px2num($(".carousel-offerbox").css("margin-right"));
	
	$("#carousel-offers").css("width", (no_offers * width_of_single_offer)); 
	hide_show_arrows();

	function hide_show_arrows() {
		var current_left = px2num($("#carousel-offers").css("left"));
		var min_left = ( -1 * px2num($("#carousel-offers").css("width")) + 3 * width_of_single_offer );
		var max_left = 0;
		if (current_left >= max_left) {
			$("#arrow-left").hide();
		} else {
			$("#arrow-left").show();
		}
		
		if (current_left <= min_left) {
			$("#arrow-right").hide();
		} else {
			$("#arrow-right").show();
		}
	}
	
	$("#arrow-right").click(function() {
		var current_left = px2num($("#carousel-offers").css("left"));
		var min_left = ( -1 * px2num($("#carousel-offers").css("width")) + 3 * width_of_single_offer );
		if ( current_left >  min_left ) {
			var new_left = current_left - width_of_single_offer;
			new_left = Math.max(new_left, min_left);
			$("#carousel-offers").stop().animate({left: new_left}, 500, function() {hide_show_arrows();});
		} 
		
	});
	
	$("#arrow-left").click(function() {
		var current_left = px2num($("#carousel-offers").css("left"));
		if ( current_left < 0 ) {
			var new_left = px2num($("#carousel-offers").css("left")) + width_of_single_offer;
			new_left = Math.min(new_left, 0);
			$("#carousel-offers").stop().animate({left: new_left}, 500, function() {hide_show_arrows();});
		}
	});
	
	//var newsBox = $('.news-item').height();
	//$('#carousel-container').css({'height': newsBox + 15});
	
	var maxHeight = 0;
	$(".news-item").each(function(){
      if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
      }
	 });
	  
	  $("#carousel-container").css('height', maxHeight+'px');
	  
});
