﻿function grp(oEl) {
	for (var x = oEl.offsetLeft, y = oEl.offsetTop; oEl.offsetParent != null; x += oEl.offsetLeft, y += oEl.offsetTop)
		oEl = oEl.offsetParent;
	return { x: x, y: y };
}
function getOffset(oEl) {
	return { x: oEl.offsetLeft, y: oEl.offsetTop };
}

$(document).ready(function () {
        $("#scrollContainer .scrollItem:first").addClass("selected");
    
        $("#scrollContainer .scrollItem").click(function () {
        $("#scrollContainer .scrollItem").removeClass("selected");
        $(this).addClass("selected");
        sIRFHelper.setup();
    });
});


var ScrollController =
		{
		    step: 4,

		    currentIndex: 0,
		    maxIndex: 0,
		    indexPositions: [],
		    leftButton: null,
		    rightButton: null,
		    cells: null,
		    scroller: null,

		    Init: function () {
		        var table = document.getElementById("scrollContainerTable");
		        if (table == null)
		            return false;

		        this.scroller = document.getElementById("scrollContainer");
		        this.cells = table.rows[0].cells;

		        this.currentIndex = 0;
		        this.maxIndex = this.cells.length - 1;

		        for (var i = 0; i <= this.maxIndex; i++)
		            this.indexPositions[i] = i * 168;

		        this.leftButton = document.getElementById("scrollButtonLeft");
		        this.rightButton = document.getElementById("scrollButtonRight");

		        this.EnableButtons();

		        this.SetButtonVisibility();
		    },
		    EnableButtons: function () {
		        this.leftButton.onclick = new Function("ScrollController.ScrollLeft()");
		        this.rightButton.onclick = new Function("ScrollController.ScrollRight()");
		    },
		    DisableButtons: function () {
		        this.leftButton.onclick = function () { };
		        this.rightButton.onclick = function () { };
		    },
		    SetButtonVisibility: function () {
		        this.leftButton.style.display = (this.currentIndex > 0) ? "block" : "none"
		        this.rightButton.style.display = ((this.currentIndex + 3) < this.maxIndex) ? "block" : "none";
		    },
		    SetScrollPosition: function (iX) {

		        this.scroller.style.left = iX + "px";
		    },
		    ScrollLeft: function () {

		        var oldPosition = -this.indexPositions[this.currentIndex];
		        var oldIndex = this.currentIndex;

		        this.currentIndex -= this.step;
		        if (this.currentIndex < 0)
		            this.currentIndex = 0;

		        var newPosition = -this.indexPositions[this.currentIndex];

		        var indexChange = Math.abs(Math.abs(oldIndex) - Math.abs(this.currentIndex));

		        var path = Animator.getAccelleratingPath(oldPosition, newPosition, indexChange * 5);

		        this.InitializeScroll(path);
		        this.SetButtonVisibility();
		    },
		    ScrollRight: function () {

		        var oldPosition = -this.indexPositions[this.currentIndex];
		        var oldIndex = this.currentIndex;

		        this.currentIndex += this.step;

		        if ((this.currentIndex + this.step - 1) > this.maxIndex)
		            this.currentIndex = this.maxIndex - this.step + 1;

		        var newPosition = -this.indexPositions[this.currentIndex];

		        var indexChange = Math.abs(Math.abs(oldIndex) - Math.abs(this.currentIndex));

		        var path = Animator.getAccelleratingPath(oldPosition, newPosition, indexChange * 5);

		        this.InitializeScroll(path);
		        this.SetButtonVisibility();
		    },
		    InitializeScroll: function (arPath) {
		        this.DisableButtons();
		        this.path = arPath;
		        this.pathIndex = 0;
		        this.DoScroll();

		    },
		    DoScroll: function () {
		        this.SetScrollPosition(this.path[this.pathIndex++]);
		        if (this.pathIndex < this.path.length)
		            window.setTimeout("ScrollController.DoScroll()", 1);
		        else
		            this.EndScroll();
		    },
		    EndScroll: function () {
		        this.EnableButtons();
		    }
		}
var ContentController =
		{
		    Init: function () {
		        var s = document.getElementById("scrollContainerTable").getElementsByTagName("TD")[0].getElementsByTagName("DIV")[0].innerHTML;
		        this.SetContent(s);
		    },
		    ThumbnailClick: function (elClicked) {

		        var container = elClicked.parentNode.getElementsByTagName("DIV")[0];
		        this.SetContent(container.innerHTML);
		    },
		    SetContent: function (sContent) {
		        document.getElementById("boxcontent").innerHTML = sContent;
		    }
		}
var Animator =
		{
			getPath: function(iStart, iEnd, iSteps) {
				var r = [];
				var diff = iEnd - iStart;

				var step = diff / (iSteps);
				for (var i = 0; i <= iSteps; i++)
					r[i] = iStart + parseInt(step * i, 10);

				return r;
			},
			getAccelleratingPath: function(iStart, iEnd, iSteps) {
				var direction = (iEnd - iStart) > 0 ? 1 : -1;
				var r = [iStart + (2 * direction), iStart + (6 * direction), iStart + (10 * direction)];
				iStart += 15 * direction;
				var gliding = this.getPath(iStart, iEnd, iSteps - 3);

				for (var i = 0; i < gliding.length; i++)
					r[r.length] = gliding[i];
				return r;
			}

		}
$(document).ready(function() {
	ScrollController.Init();
	ContentController.Init();
});