$(document).ready(function() {
	
	//declare array to remember data
	var data = new Array();
    
    //hide left arrow if below photos count
    $("div[total_photos!='']").each(function() {
       var total_photos = $(this).attr('total_photos');
       var id = $(this).attr('id');
       var production = id.substr(12, 15);
	   
       	if (total_photos <= 4) {
            $(".left_arrow[production='"+production+"']").hide();
       }
    });
	
	//left arrow
	$(".left_arrow").click(function() {
		var production = $(this).attr('production');
		var div_name = 'list_photos_'+production;
		var total_photos = $('#'+div_name).attr('total_photos');
	
		if (data[div_name] == null || data[div_name] == 0) {
			var start_limit = 5;
			$(".right_arrow[production="+production+"]").show();
		} else {
			var start_limit = data[div_name] + 5;
		}
		    
		start_limit--;
		var url = '/index/ajaxphotos/production/'+production+'/start/'+start_limit;
		$.ajax({
            type: 'GET',
            url: url,
            success: function(html) {
                if (html != 'null') {
            		$('#'+div_name).html(html);        
            		data[div_name] = start_limit;
                }
            }
        });	
        
        
        var end_limit = start_limit + 5;
        if (end_limit >= total_photos) {
            $(this).hide();
        }
                
	});
	
	//right arrow
	$(".right_arrow").click(function() {
		var production = $(this).attr('production');
		var div_name = 'list_photos_'+production;
		
		var start_limit = data[div_name] - 5;
		
		if (start_limit <= 0) {
            $(this).hide();
            start_limit = 0;
        }
		
        var url = '/index/ajaxphotos/production/'+production+'/start/'+start_limit;

		$.ajax({
            type: 'GET',
            url: url,
            success: function(html) {
                if (html != 'null') {
            		$('#'+div_name).html(html);        
            		data[div_name] = start_limit;
                     $('.left_arrow[production='+production+']').show();
              }
            }
        });	             
	});
	
});

