﻿
//-----------------спецпредложения
var CountriesTimeOut;
var SpoFilterCountryDiv = document.getElementById('SpoFilterCountryDiv');
SpoFilterCountryDiv.style.display = 'none';
function hideCountries() {
    SpoFilterCountryDiv.style.display = 'none';
    CountriesTimeOut = undefined;
}
function AffectCT(_set) {
    if (_set == 1) {
        if (CountriesTimeOut != undefined) {
            clearTimeout(CountriesTimeOut);
            CountriesTimeOut = undefined;
        }
    }
    else {
        if (CountriesTimeOut == undefined) {
            CountriesTimeOut = setTimeout(hideCountries, 1000);
        }
    }
}
var outerContainer = document.getElementById('outer_div');
var innerContainer = document.getElementById('inner_div');

var objectWidth = 160;
var MovieSeed = 5;

var objectCount = 0;
var count_nodes = 0;
for (i = 0; i < innerContainer.childNodes.length; i++) {
    if (innerContainer.childNodes[i].tagName == "DIV")
        objectCount++;
}

var currentObject = 0;
var oldPosition = 0;
var newPosition = 0;
var Direction = 1;
var MoveIntervalId;
var lastObject = objectCount - Math.floor(parseInt(outerContainer.style.width) / objectWidth);

innerContainer.style.width = objectWidth * objectCount + "px";

if (lastObject < 0) lastObject = 0;
if (outerContainer.addEventListener) {
    outerContainer.addEventListener('DOMMouseScroll', MouseWheelEvent, false);
}
outerContainer.onmousewheel = MouseWheelEvent;
var BlockScroll = false;
function MouseWheelEvent(event) {
    if (!event) {
        event = window.event;
    }
    if (BlockScroll) {
        CancelEvent(event);
        return;
    }
    var WheelDelta = 0;
    if (event.wheelDelta) {
        WheelDelta = event.wheelDelta / 120;
    }
    else if (event.detail) {
        WheelDelta = -event.detail / 3;
    }
    else return;
    if (WheelDelta > 0) Direction = 1; else Direction = -1;
    StartMove(Direction);
    CancelEvent(event);
}

function AutoScroll() {
    if (BlockScroll) return;
    if ((Direction > 0 && currentObject <= 0) || (Direction < 0 && currentObject >= lastObject)) {
        Direction = -Direction;
    }
    StartMove(Direction);
}

function StartMove(new_direction) {
    if (BlockScroll) return;
    BlockScroll = true;
    Direction = new_direction;
    if ((Direction > 0 && currentObject <= 0) || (Direction < 0 && currentObject >= lastObject)) {
        BlockScroll = false;
        return;
    }
    newPosition = oldPosition + objectWidth * Direction;
    MoveIntervalId = window.setInterval("MoveObjects()", 20);
}

function CancelEvent(Event) {
    if (Event.preventDefault) {
        Event.preventDefault();
    }
    Event.returnValue = false;
}

function MoveObjects() {
    var position = parseInt(innerContainer.style.left);
    position += MovieSeed * Direction;
    if ((Direction > 0 && position >= newPosition) || (Direction < 0 && position <= newPosition)) {
        position = newPosition;
        clearInterval(MoveIntervalId);
        oldPosition = newPosition;
        currentObject -= Direction;
        BlockScroll = false;
    }
    innerContainer.style.left = position + "px";
}
Direction = 1;
autoscrollinterval = window.setInterval("AutoScroll()", 10000);



//--------------------------партнеры
var movieContainer = function(outer, inner, left, right) {
    this.MovieSeed = 20;
    this.outerContainer = document.getElementById(outer);
    this.innerContainer = document.getElementById(inner);
    if (this.outerContainer == undefined || this.innerContainer == undefined) return;
    this.objects = new Array();
    this.commonLength = 0;
    this.leftButton = document.getElementById(left);
    this.rightButton = document.getElementById(right);
    this.direction = 0;
    this.lastObjectIndex = 0;

    this.currentObjectIndex = 0;
    this.nextObjectIndex = 0;
    this.MoveIntervalId;
    this.movieTimes = 0;

    for (i = 0; i < this.innerContainer.childNodes.length; i++) {
        if (this.innerContainer.childNodes[i].tagName == "DIV") {
            this.objects[this.objects.length] = this.innerContainer.childNodes[i];
        }
    }

    for (var i in this.objects) {
        if (this.objects[i].style.cssFloat != undefined)
            this.objects[i].style.cssFloat = "left";
        else
            this.objects[i].style.styleFloat = "left";
        this.objects[i].begin_x = this.commonLength;
        this.commonLength += this.objects[i].clientWidth;
    }

    tmp = 0;
    for (var i in this.objects) {
        if (this.commonLength - tmp <= parseInt(this.outerContainer.style.width)) {
            this.lastObjectIndex = i;
            break;
        }
        tmp += this.objects[i].clientWidth;
    }

    this.innerContainer.style.width = this.commonLength + 'px';
    this.innerContainer.style.left = '0px';
    if (this.leftButton.addEventListener) {
        this.leftButton.addEventListener('click', $createHandler(this, this.Movie), false);
        this.rightButton.addEventListener('click', $createHandler(this, this.Movie), false);
        this.leftButton.addEventListener('dblclick', $createHandler(this, this.Movie), false);
        this.rightButton.addEventListener('dblclick', $createHandler(this, this.Movie), false);
    }
    else {
        this.leftButton.onclick = $createHandler(this, this.Movie);
        this.rightButton.onclick = $createHandler(this, this.Movie);
        this.leftButton.ondblclick = $createHandler(this, this.Movie);
        this.rightButton.ondblclick = $createHandler(this, this.Movie);
    }
}

function $createHandler(instance, method) {
    return function() {
        return method.apply(instance, arguments);
    };
}
movieContainer.prototype.MovieStep = function() {
    currentPosition = -parseInt(this.innerContainer.style.left);
    currentPosition += this.MovieSeed * this.direction;
    if ((currentPosition >= this.objects[this.nextObjectIndex].begin_x && this.direction == 1) ||
    	                (currentPosition <= this.objects[this.nextObjectIndex].begin_x && this.direction == -1)
    	                ) {
        currentPosition = this.objects[this.nextObjectIndex].begin_x;
        this.movieTimes -= this.direction;
        clearInterval(this.MoveIntervalId);
        if (this.movieTimes == 0) {
            this.direction = 0;
        }
        else {
            this.innerContainer.style.left = -currentPosition + 'px';
            this.currentObjectIndex = this.nextObjectIndex;
            this.StartMovie();
            return;
        }
        this.currentObjectIndex = this.nextObjectIndex;
    }
    this.innerContainer.style.left = -currentPosition + 'px';
}
movieContainer.prototype.Movie = function(event) {
    if (!event) {
        event = window.event;
    }
    var target = event.target || event.srcElement;
    if (target === this.leftButton) this.direction = -1;
    if (target === this.rightButton) this.direction = 1;
    if (this.movieTimes * this.direction > 0) {
        this.movieTimes += this.direction;
        return;
    }
    if (this.movieTimes * this.direction < 0) {
        this.movieTimes = 0;
        return;
    }
    this.movieTimes += this.direction;
    this.StartMovie();
}
movieContainer.prototype.StartMovie = function() {
    this.nextObjectIndex = this.currentObjectIndex + this.direction;
    if (this.nextObjectIndex < 0 || this.nextObjectIndex > this.lastObjectIndex) {
        this.nextObjectIndex = 0;
        this.movieTimes = 0;
        return;
    }
    if (this.MoveIntervalId != undefined) clearInterval(this.MoveIntervalId);
    this.MoveIntervalId = setInterval($createHandler(this, this.MovieStep), 50);
}


var partners = new movieContainer("partners_outer_div", "partners_inner_div", "left_arrow", "right_arrow");
