
function showImg(thId){
	for(var i = 0; i < arr.length; i++){
		if(oThumb = document.getElementById('th' + arr[i])){
			if(arr[i] == thId){
				oThumb.className = 'sel';
			} else {
				oThumb.className = '';
			}
		}
	}
	if(oThumbs){
		//checkSelectedPosition();
		resizeThumbs();
	}
}

function showPreview(iThumbId, aIds, sNewSrc){
	var oThumb;
	var oPreview;
	for(var i = 0; i < aIds.length; i++){
		if(oThumb = document.getElementById('ph_thumb' + aIds[i])){
			if(aIds[i] == iThumbId){
				if(oPreview = document.getElementById('ph_preview')){
					oPreview.src = sNewSrc;
				}
				oThumb.className = 'sel';
			} else {
				oThumb.className = '';
			}
		}
	}
}

var bSS = false;
var iScrollTimeout = 1;
var iTimeOut = window.addEventListener ? iScrollTimeout * 2 : iScrollTimeout;
var iScrollKoef = 15;
var iCW, iTW, iTL, iTR, iDiff;
var oContainer = null;
var oThumbs = null;
var oLeftArr = null;
var oRightArr = null;


function initScrollVars(){
	iCW = oContainer.offsetWidth;
	iTW = oThumbs.offsetWidth;
	iTL = oThumbs.offsetLeft;
	iDiff = iTW - iCW;
	iTR = -(iTL + iDiff);
}

function scrollInit(){
	if(oContainer = document.getElementById('gn_container')){
	if(oThumbs = document.getElementById('gn_thumbs')){
	if(oLeftArr = document.getElementById('gn_arr_left')){
	if(oRightArr = document.getElementById('gn_arr_right')){
		if(oContainer.attachEvent){
			oContainer.attachEvent("onmousewheel", mouseScrollThumbs);
		} else {
			oContainer.addEventListener("DOMMouseScroll", mouseScrollThumbs, true);
		}
		if(window.addEventListener){
			window.addEventListener('resize', resizeThumbs, false);
		} else {
			window.attachEvent('onresize', resizeThumbs);
		}
		checkScrollButtons();
		resizeThumbs();
	}
	}
	}
	}
}



function checkScrollButtons(){
	initScrollVars();
	if(iDiff > 0){
		bRA = iTR < 0 ? true : false;
		bLA = iTL < 0 ? true : false;
	} else {
		bLA = false; bRA = false;
	}

	oLeftArr.style.visibility = bLA ? '' : 'hidden';
	oRightArr.style.visibility = bRA ? '' : 'hidden';
}

function resizeThumbs(){
	if(oThumbs){
		initScrollVars();
		if(iTL < 0){
			if(iDiff < 0) {
				oThumbs.style.left = '0px';
			} else if(iDiff > 0) {
				if(iTR >= 0){
					oThumbs.style.left = -iDiff + 'px';
				}
			}
		} else {
			oThumbs.style.left = '0px';
		}
		checkScrollButtons();
	}
}

function scrollThumbs(sSS, sDirection){

	bSS = sSS == 'start' ? true : false;
	iDirection = sDirection == 'left' ? -1 : 1;
	_scroll(iDirection);
	
	
}


function mouseScrollThumbs(evt){
	if(!evt) evt = window.event;
	if(evt.wheelDelta) {
		delta = evt.wheelDelta / 120;
		if(window.opera) delta = -delta;
	} else if (evt.detail) {
		delta = -evt.detail / 3;
	}
	iDirection = delta > 0 ? -1 : 1;
	_scroll(iDirection, delta * 5);

	//reload_img_ajax()

	if (evt.preventDefault) evt.preventDefault();
	evt.returnValue = false;

	__scroll()
	return false;
}


var _schet = false;
var o_left1 = null;
var o_left2 = null;
var _item = 0;

function _scroll(iDirection, iKoef){
	var iMultiplier = !isNaN(iKoef) ? Math.abs(iKoef) : 1;
	var iShift = iScrollKoef * iMultiplier * iDirection;

	initScrollVars();
	if(oThumbs && iCW > 0){
		if(iDiff > 0) {
			if(iTL > iShift) {
				oThumbs.style.left = '0px';
			} else { 
				if((iDiff + iTL) < iShift) {
					oThumbs.style.left = -iDiff + 'px';	
				} else {
					oThumbs.style.left = (iTL - iShift) + 'px';
					if(bSS){
						setTimeout('_scroll(' + iDirection + ')', iTimeOut);
						__scroll();
					}
				}
			}
		}
	}
	checkScrollButtons();
}


function add_img() {
	var k=0;


	for (var nam in elem)
	{
		var obj = document.createElement('td');
		obj.innerHTML = "<div class='thumb'>&nbsp;asdfsad</div>";
		tr.appendChild(obj);
		if (typeof elem[nam]['cur'] == 'boolean')
			{
				cur = elem[nam]['id'];
				kk = k;
				tr.getElementsByTagName('td')[kk].className = 'sel';
				
			}
		k++;
	}
	reload_img();
	document.getElementById('gn_thumbs').style.left = -((kk*117))+300 + 'px';
}
		
		
window.onload = function() {
	if (typeof elem != 'undefined')
	{
		add_img();
		scrollInit();
		showImg(cur);
	} else {
		setTimeout(function() {
			add_img();
			scrollInit();
			showImg(cur);
		},200);
	}
}

function __scroll() {
	
	if (_schet == false) {
		o_left1 = oThumbs.offsetLeft; 
	} else {
		o_left2 = oThumbs.offsetLeft;
		_item += Math.abs(o_left1 - o_left2); 

		if (iDirection == 1)
		{
			reload_img();
		} else {
			reload_img();
		}

		pos1++

		if (pos1 == 1)
		{
			left0 = document.getElementById('gn_thumbs').offsetLeft;
		}
		col_img = Math.floor((left0 - document.getElementById('gn_thumbs').offsetLeft)/117);

		if (_item > 120)
		
		{
			_item=0;
			o_left1 = o_left2;
		}
	}
	_schet = true;
}