// JavaScript Document
var scroller = {};
scroller.item_width=130; //ширина одного элемента, задается в стилях
scroller.arrow_width=24; //ширина стрелок прокрутки влево\вправо
scroller.items_count = 0;

scroller.getNumProds = function(){
    var items_count = 0;
    var scroll_items = document.getElementsByTagName('DIV', document.getElementById('scroller_items'))
    for(var i=0;i<scroll_items.length;i++){
        if(scroll_items[i].className == 'scroll_item'){
            items_count++;
        }
    }
    return items_count;
}
scroller.jump = function(step){    
    scroller.scrollBy((scroller.item_width * -step))
}
scroller.scrollBy = function(distance){
    var scroller_items = document.getElementById('scroller_items');
    var curr_pos = this.getCurrentScrollerPosition();
    var galleryWidth = this.getGalleryWidth();
	var max_items_on_screen = Math.floor(scroller_items.parentNode.clientWidth / this.item_width);

    if( curr_pos < 0 && distance > 0 )
	{
        if( -curr_pos < distance ) curr_pos = distance = 0;
        scroller_items.style.left = ( curr_pos + distance ) + 'px';
    }
    else
	{
        if( curr_pos > ( ( max_items_on_screen * this.item_width ) - galleryWidth ) && distance < 0 )
		{
            if( ( curr_pos + distance ) < ( ( max_items_on_screen * this.item_width ) - galleryWidth ) )
			{
                curr_pos = ( max_items_on_screen * this.item_width ) - galleryWidth;
                distance = 0;
            }
            scroller_items.style.left = ( curr_pos + distance ) + 'px';
        }
    }	
}
scroller.getCurrentScrollerPosition = function(){
    var scroller_items = document.getElementById('scroller_items');
    var currPos = scroller_items.style.left;
    if(currPos=='') currPos = scroller_items.scrollLeft;
    return parseInt(currPos);
}
scroller.getGalleryWidth = function(){
    var galleryWidth = 0;
    if( this.items_count == 0 ) this.items_count = this.getNumProds();
    if( this.items_count > 0 )
        galleryWidth = this.item_width * this.items_count;
    return galleryWidth;
}
scroller.resize_scroll = function(){
	var scroller_items = document.getElementById('scroller_items');
	
	scroller_items.parentNode.style.margin = '0 ' + scroller.arrow_width + 'px';
	scroller_items.parentNode.style.width = '';	
	var max_items_on_screen = Math.floor(scroller_items.parentNode.clientWidth / scroller.item_width);
	scroller_items.parentNode.style.margin = '0 auto';
	if( scroller.items_count == 0 ) scroller.items_count = scroller.getNumProds();
	if( scroller.items_count <= max_items_on_screen )
	{
		document.getElementById('scroll_prev').style.display='none';
		document.getElementById('scroll_next').style.display='none';
		scroller_items.parentNode.style.width = scroller.items_count*scroller.item_width+'px';
		scroller_items.style.left='0px';
	}
	else
	{
		document.getElementById('scroll_prev').style.display='block';
		document.getElementById('scroll_next').style.display='block';
		scroller_items.parentNode.style.width = max_items_on_screen*scroller.item_width+'px';
	}
	
}



window.onresize = scroller.resize_scroll;
window.onload = scroller.resize_scroll;
