﻿var _images = new Array();

function _PreloadImages() { 
    for (var i=0; i<_images.length; i++) {
        var img = document.createElement('IMG');
        img.src = _images[i].url;
        img.style.visibility = 'hidden';
        img.style.position = 'absolute';
        img.style.top = '0px'; img.style.left = '0px';
        document.body.appendChild(img);
    }
}
function ThumbOver(index) { 
    if (typeof(index) == 'undefined' || typeof(bigImgID) == 'undefined' || !bigImgID) return;
    var big_img = document.getElementById(bigImgID); if (!big_img) return;
    if (!_images[index]) return;
    big_img.src = _images[index].url;
    big_img.style.width = _images[index].width + 'px';
    big_img.style.height = _images[index].height + 'px';
}
function AddImage(url, width, height, imgIndex) {
    _images[imgIndex] = {'width':width, 'height':height, 'url':url};
}
//------------------------------------------------------------------------------------
var dataList = null, div_thumbs = null;
function GetDataListWidth() {
    if (!dataList) dataList = document.getElementById(dataListID); if (!dataList) return null;
    return dataList.offsetWidth;
}
function GetContainerWidth() {
    if (!div_thumbs) div_thumbs = document.getElementById(div_thumbsID); if (!div_thumbs) return null;
    return div_thumbs.offsetWidth;
}
var _scrollInt = null;
var _mObj = null;
function ScrollRight() {
    var contWidth = GetContainerWidth(); if (!contWidth) return;
    var listWidth = GetDataListWidth(); if (!listWidth) return;
    _mObj = new MotionObj(contWidth, listWidth, true);
    ScrollStop();
    _scrollInt = setInterval(MoveScroll, 10);
}
function ScrollLeft() {
    var contWidth = GetContainerWidth(); if (!contWidth) return;
    var listWidth = GetDataListWidth(); if (!listWidth) return;
    _mObj = new MotionObj(contWidth, listWidth, false);
    ScrollStop();
    _scrollInt = setInterval(MoveScroll, 10);
}
function ScrollStop() { if (_scrollInt) { clearInterval(_scrollInt); _scrollInt = null; } step = STEP_MAX; }
var step, STEP_MAX = 14;
step = STEP_MAX;
function MoveScroll() {
    if (!_mObj) return;
    var currLeft = Number(dataList.style.left.replace('px',''));
    var btnWidth = 0;
    if (_mObj.IsLeft) { 
        if (_mObj.contWidht + Math.abs(currLeft) >= _mObj.listWidth + btnWidth) {
            ScrollStop();
        }
        else {
            var remainer = _mObj.listWidth + btnWidth - _mObj.contWidht - Math.abs(currLeft); 
            if (remainer <= 100) { step = Math.round(STEP_MAX*(remainer)/(150)) ; step = (step <= 0) ? 1 : step; }
            dataList.style.left = (currLeft - step) + 'px';
        }
    }
    else {
        if (currLeft >= 0) {
            ScrollStop();
        }
        else {
            var remainer = Math.abs(currLeft); 
            if (remainer <= 100) { step = Math.round(STEP_MAX*(remainer)/150) ; step = (step <= 0) ? 1 : step; }
            dataList.style.left = (currLeft + step) + 'px';
        }
    }
}
function MotionObj(contWidth, listWidth, isLeft) {
    this.contWidht = contWidth;
    this.listWidth = listWidth;
    this.IsLeft = isLeft;
}
function SetThumbScroll() { 
    var contWidth = GetContainerWidth(); if (!contWidth) return;
    var listWidth = GetDataListWidth(); if (!listWidth) return;
    if (listWidth >= contWidth) {
        var d_left = document.getElementById(d_leftID); if (d_left) d_left.className = 'div_scroll';
        var d_right = document.getElementById(d_rightID); if (d_right) d_right.className = 'div_scroll';
        var div_thumbs = document.getElementById(div_thumbsID); if (div_thumbs) div_thumbs.className = 'div_thumbs';
    }
}

var ItemDetails_winOnload = window.onload;
window.onload = function() { 
    setTimeout(_PreloadImages, 10); setTimeout(SetThumbScroll, 10); 
    if (ItemDetails_winOnload) ItemDetails_winOnload();
} 

var dbg = null;
function DebugMessage() {
    var divMsg = document.createElement('DIV');
    divMsg.style.backgroundColor = '#FFEEEE';
    divMsg.style.color = '#000000';
    divMsg.style.border = 'solid 1px #FFAAAA';
    divMsg.style.padding = '3px';
    divMsg.style.position = 'absolute';
    divMsg.style.right = '20px';
    divMsg.style.top = '10px';
    divMsg.style.display = 'none';
    document.body.appendChild(divMsg);
    var _int = null;
    this.print = function(msg) { 
        if (_int) { clearTimeout(_int); _int = null; }
        divMsg.style.display = 'block'; divMsg.innerHTML = msg; 
        clear();
    }
    function clear() { 
        _int = setTimeout(_clear, 1000);
        function _clear() { divMsg.style.display = 'none'; divMsg.innerHTML = ''; }
    }
}

function ScrollDownPage() {
    window.scrollBy(0,100);
}


