function initiateScrollers(){

	$('.scrollContainer').each(function(){

		var scrollBig = $(this).find('.scrollBig:not(:animated)');

		var scrollContainerWidth = $(this).parent('.scrollWrapper').width() - $(this).siblings().width() * 2; 

		$(this).width(scrollContainerWidth);

		var itemWidth = scrollBig.find('.qtip_aa').width();

		scrollBig.children('.scrollItem').css({'width': itemWidth,'overflow': 'hidden'});

		var itemsPerPage = Math.floor(scrollContainerWidth / scrollBig.children().outerWidth());

		$(this).width(itemsPerPage*scrollBig.children().outerWidth());

	});

}



function lazyLoad(){

	$('.lazyLoadImage').each(function(){

		var curOffset = $(this).parents('.scrollItem').position().left - $(this).parents('.scrollBig').position().left * (-1);

		var containerWidth = $(this).parents('.scrollContainer').width();

		$(this).filter(function(index){

		if(curOffset < containerWidth){

	 		return true;

	 		}else{

	 		return false;

	 		}

		}).each(function(){

			$(this).attr('src', $(this).data('srcReal')).animate({'opacity' : 1}, 500).parent().siblings('.lazyLoadLoader').remove();

		});

	})

}



$(document).ready(function(){

	$('.scrollContainer img.qtip_aa').each(function(){

		var curOffset = $(this).parents('.scrollItem').position().left - $(this).parents('.scrollBig').position().left * (-1);

		var containerWidth = $(this).parents('.scrollContainer').width();

		$(this).filter(function(index){

		if(curOffset > containerWidth){

		 	return true;

		 	}else{

		 	return false;

		 	}

	}).each(function(){

		$(this).data('srcReal', $(this).attr('src')).css('opacity', 0);

		$(this).removeAttr('src').addClass('lazyLoadImage').parent().after('<div class="lazyLoadLoader" style="position:absolute;top:40%;width:60px;left:30%;"><img src="http://www.onelight.tv/styles/onelight/layout/inc/images/lazyLoader.gif" /></div>');

		});

	});

});



$(window).load(function(){

	

	$(window).resize(function(){

		lazyLoad();					  

		initiateScrollers();

			

	});	

	

	initiateScrollers();

	lazyLoad();

	

	$('.scrollLeft').live('click',function(){

		var scrollContainer = $(this).siblings('.scrollContainer').find('.scrollBig:not(:animated)');

		var newOffset = parseInt(scrollContainer.css('left')) + scrollContainer.parent('.scrollContainer').width();

		var maxWidth = '-' + scrollContainer.children().size() * scrollContainer.children().outerWidth();

		var itemsPerPage = scrollContainer.parent('.scrollContainer').width() / scrollContainer.children().outerWidth();

		var pagesNumber = Math.ceil(scrollContainer.children().size() / itemsPerPage);

		var fullWidth = itemsPerPage * (pagesNumber - 1) * scrollContainer.children().outerWidth();

		if(newOffset > 0){

			scrollContainer.animate({ left : '-' + fullWidth }, 500,function(){

				lazyLoad();	

			});

		}else{

			scrollContainer.animate({ left : newOffset }, 500,function(){

				lazyLoad();	

			});

		}

	});

	

	$('.scrollRight').live('click',function(){

		var scrollContainer = $(this).siblings('.scrollContainer').find('.scrollBig:not(:animated)');

		var maxWidth = '-' + scrollContainer.children().size() * scrollContainer.children().outerWidth();

		var newOffset = parseInt(scrollContainer.css('left')) - scrollContainer.parent('.scrollContainer').width();

		if(newOffset <= maxWidth){

			scrollContainer.animate({ left : 0 }, 500,function(){

				lazyLoad();	

			});

		}else{

			scrollContainer.animate({ left : newOffset }, 500,function(){

				lazyLoad();	

			});

		}

	});

		

});

