var images;
var cur_index;
var photos_directory;

function getImages(property,callback) {
    var image_sizes = [];
    if (property == 'vertical') {
        image_sizes['photo_inner1'] = '96x156';
        image_sizes['photo_inner2'] = '127x156';
        image_sizes['photo_inner3'] = '148x156';
        image_sizes['photo_inner4'] = '172x155';
        image_sizes['photo_inner5'] = '148x156';
        image_sizes['photo_inner6'] = '127x156';
        image_sizes['photo_inner7'] = '96x156';
    } else {
        image_sizes['photo1'] = '156x98';
        image_sizes['photo2'] = '156x129';
        image_sizes['photo3'] = '156x150';
        image_sizes['photo4'] = '156x174';
        image_sizes['photo5'] = '156x150';
        image_sizes['photo6'] = '156x129';
        image_sizes['photo7'] = '156x98';
    }
    document.image_sizes = image_sizes;

    cur_index = 1;
    $.post('/module/Gallery/Index/get-Gallery-Photos', {property:property}, function(res){
        images = res.photos;
        document.images_data = images;
        var photos_directory = res.photos_directory;
        document.photos_directory = photos_directory;
        var html;
        var el_index;
        var image_dim;
        var image_width;
        var image_height;

        html = '';
        if (property == 'vertical') {
            html+= "<a id='arrow_up' href='#scrollUp'><img src='/public/design/img/arrow_up.jpg' width='41' height='28' alt='Вверх' /></a>";
        }

        for (var i = 0; i < 7; i++) {
            el_index = i + 1;

            if (property == 'vertical') {
                image_dim = new String(document.image_sizes['photo_inner' + el_index]);
            } else {
                image_dim = new String(document.image_sizes['photo' + el_index]);
            }

            image_dim = image_dim.split("x");
            image_width  = image_dim[0];
            image_height = image_dim[1];

            if (property == 'vertical') {
                html+= "<a title = '"+images[i]['description']+"' id='photo_inner"+el_index+"' href='/gallery'><img alt = '"+images[i]['description']+"' title = '"+images[i]['description']+"' image_index = '"+i+"' id = 'gimage"+el_index+"' src='"+photos_directory+"vertical/"+image_width+"_"+image_height+"_"+images[i]['photo_file']+"' width='"+image_width+"'/></a>";
            } else {
                html+= "<a title = '"+images[i]['description']+"' href='/gallery'><img title = '"+images[i]['description']+"' alt = '"+images[i]['description']+"' image_index = '"+i+"' id = 'gimage"+el_index+"' class='photo"+el_index+"' src='"+photos_directory+"gorizontal/"+image_width+"_"+image_height+"_"+images[i]['photo_file']+"' width='"+image_width+"'   /></a>";
            }

            
        }
      //  alert(html);
        if (property == 'vertical') {
            html+= "<a id='arrow_down' href='#scrollDown'><img src='/public/design/img/arrow_bottom.jpg' width='41' height='28' alt='Вниз' /></a>";
            $('#photo_gal_in').html(html);
        } else {
            $('#gallery_in').html(html);
        }
        callback();
    }, 'json');
}




//var count_img = document.images_data.length;

var image_index = 0;

var start_index = null;

function scrollImages(to, property) {
    var image_dim;
    images_count = document.images_data.length;

    if (to == "up") {
        for (var i = 1;i <= 7;i++) {
            if (property == 'vertical') {
                image_dim = new String(document.image_sizes['photo_inner' + i]);
            } else {
                image_dim = new String(document.image_sizes['photo' + i]);
            }
            image_dim = image_dim.split("x");
            image_width  = image_dim[0];
            image_height = image_dim[1];

            var image_index;
            image_index = $('#gimage'+i).attr('image_index');
            image_index++;
            if (!document.images_data[image_index]) {
                image_index = 0;
            }
            var next_img = $('#gimage'+i);
            
            var description = document.images_data[image_index]['description'];
            if (description.length == 0) {
            	description = ' ';
            }
            
            next_img.attr('src', document.photos_directory+property+"/"+image_width+"_"+image_height+"_"+document.images_data[image_index]['photo_file']);
            next_img.attr('alt', description);
            next_img.attr('title', description);  
            next_img.attr('image_index',image_index);
        }
    } else {
        for (var i = 7;i >= 1;i--) {
            if (property == 'vertical') {
                image_dim = new String(document.image_sizes['photo_inner' + i]);
            } else {
                image_dim = new String(document.image_sizes['photo' + i]);
            }
            image_dim = image_dim.split("x");
            image_width  = image_dim[0];
            image_height = image_dim[1];

            var image_index;
            image_index = $('#gimage'+i).attr('image_index');
            image_index--;
            if (image_index < 0) {
                image_index = images_count-1;
            }

            var next_img = $('#gimage'+i);
            
            var description = document.images_data[image_index]['description'];
            if (description.length == 0) {
            	description = ' ';
            }
            
            next_img.attr('src', document.photos_directory+property+"/"+image_width+"_"+image_height+"_"+document.images_data[image_index]['photo_file']);
            next_img.attr('alt', description);
            next_img.attr('title', description);            
            next_img.attr('image_index',image_index);
        }
    }

  

      

  

    


}



















