/**
 * @author Anton Shein
 */

function ClassConsole (sId)
{
	this.dBody = document.getElementById(sId);
	this.dBody.style.border = "1px solid red";
	this.log = function (sMessage)
	{
		this.dBody.innerHTML = sMessage;
	}
}


function registerEventHandler (dObject, sEventName, fHandler)
{
	if (dObject.attachEvent)
	dObject.attachEvent ('on' + sEventName, fHandler);
	else if (dObject.addEventListener)
	dObject.addEventListener (sEventName, fHandler, true);
}

registerEventHandler(window, 'load', slideToEnd);

function slideToEnd()
{
	var percent = (1-scroller_controller.iRollerWidth/scroller_controller.iWindowWidth)*100;
	scroller_controller.startMoveToPercent(percent);
	//scroller_controller.startSlideToPercent(90);
	//if (!//console.log) //console = new Class//console('info');
}

var scroller_window = scroller_controller = null;

function initPortfolioScroller()
{
	scroller_window = new ClassScrollerWindow('scroller_window');
	scroller_window.moveToPercent(0);

	document.getElementById('scroller_controller').style.display = 'block';
	scroller_controller = new ClassScrollerController('scroller_controller', scroller_window);

	var temp_arr = scroller_window.dWindow.getElementsByTagName('div');
	var info_divs = [];
	for (var i in temp_arr)
	{
		if (temp_arr[i].className == 'item')
		{
			var img = temp_arr[i].getElementsByTagName('img')[0];
			img.onmouseover = highliteLink;
			img.onmouseout = dischargeLink;
		}
		if (/item_info/i.test(temp_arr[i].className))
		{
			var str = temp_arr[i].innerHTML;
			var class_name = /class/.test(str)?str.match(/class:(.*?);/)[1]:'t_sites';
			var company = /company/.test(str)?str.match(/company:(.*?);/)[1]:'Упячка!';
			var left = temp_arr[i].parentNode.offsetParent.offsetLeft / scroller_window.iStripeWidth * 100;
			var span = document.createElement('span');
                if (company!='&nbsp') span.innerHTML = '<i><div class="scroller-mark"></div>'+company+'</i>';
				else span.innerHTML = '<i>'+company+'</i>'
                span.className = class_name;
				span.style.left = left+'%';
				scroller_controller.dWindow.appendChild(span);
			//////console.log(temp_arr[i].parentNode.offsetParent.offsetLeft , left);
		}
	}
	scroller_controller.analizeContent();
	registerEventHandler(window, 'resize', redrawPortfolio);
}

function ClassScrollerWindow(sId)
{
	var oThis = this;
	this.dWindow = document.getElementById(sId);
		this.dWindow.style.visibility = "hidden";
		this.dWindow.style.position = 'relative';
		this.dWindow.style.overflow = 'hidden';
	this.dStripe = this.dWindow.firstChild;
	while (this.dStripe.nodeType != 1) this.dStripe = this.dStripe.nextSibling;
		this.dStripe.style.position = 'absolute';
	this.iWindowWidth = this.dWindow.offsetWidth;
	this.iStripeWidth = this.dStripe.offsetWidth;


	this.tMoveTimer = null;
	this.iPercent = null;
	this.iToPercent = null;


	this.redraw = function(iControllPercent)
	{
		this.iWindowWidth = this.dWindow.offsetWidth;
		this.iStripeWidth = this.dStripe.offsetWidth;
		if (iControllPercent) this.iPercent = iControllPercent;
		else return;
		this.startSlideToPercent(this.iPercent);
		//////console.log(this.iWindowWidth + ' - ' +this.iPercent);
	}

	this.moveToPercent = function (iPercent)
	{
		this.iPercent = iPercent;
		this.dStripe.style.left = -	Math.ceil(this.iStripeWidth * iPercent / 100) + 'px';
	}
	this.slideToPercent = function ()
	{
		var flag = 0;
		if (Math.abs(this.iPercent - this.iToPercent) < 0.5 ||  (flag = this.iPercent*this.iStripeWidth/100 + this.iWindowWidth >= this.iStripeWidth) )
		{
			if (this.tMoveTimer)
			{
				clearInterval(this.tMoveTimer);
				this.tMoveTimer = null;
				if (flag) this.iPercent = (this.iStripeWidth - this.iWindowWidth - 1) / this.iStripeWidth * 100;
				//this.moveToPercent(this.iToPercent);
			}

		}
		else
		{
			var distance = this.iPercent - this.iToPercent;
			var dirrection = distance / Math.abs(distance);
			var increase = 0.05*Math.pow(Math.abs(distance), 1.5);
			////console.log(increase, this.iToPercent);
			//if (increase < 0.5) increase = 0.5;
			this.moveToPercent(this.iPercent - increase*dirrection);
		}
		//console.log(this.iPercent*this.iStripeWidth/100 + this.iWindowWidth, this.iStripeWidth+10);
	}

	this.startSlideToPercent = function (iPercent)
	{
		if (!this.tMoveTimer) this.tMoveTimer = setInterval(function() {oThis.slideToPercent();}, 50);
		this.iToPercent = iPercent;
		////console.log(iPercent);
	}

}

function ClassScrollerController(sId, oUke)
{
	var oThis = this;
	this.oScroller = oUke;
	this.dWindow = document.getElementById(sId);
		this.dWindow.style.visibility = "hidden";
		this.dWindow.style.position = 'relative';
	this.dRoller = this.dWindow.firstChild;
	while (this.dRoller.nodeType != 1) this.dRoller = this.dRoller.nextSibling;
		this.dRoller.style.position = 'absolute';
    this.dContrStripe = document.getElementById('contr-stripe'); // Полоса, закрывающая собой серую полосу на плашке скроллера. Очень грубо.
	this.dContrStripe.style.display = 'block';
    this.iLeftWindowOffset = this.dWindow.offsetLeft;
	this.iCatchLeftOffset = 0;
	this.iWindowWidth = this.dWindow.offsetWidth;
	this.iRollerWidth = this.dRoller.offsetWidth;
	this.iRollerWidth = Math.ceil ( this.iWindowWidth * this.oScroller.iWindowWidth / this.oScroller.iStripeWidth );
	this.dContrStripe.style.width = this.iRollerWidth - 13 + 'px';
    this.dRoller.style.width = this.iRollerWidth + 'px';

	this.tMoveTimer = null;
	this.iPercent = null;
	this.iToPercent = null;
	this.iTruePercent = null;

	this.redraw = function ()
	{
		this.iWindowWidth = this.dWindow.offsetWidth;
		this.iRollerWidth = this.dRoller.offsetWidth;
		this.iRollerWidth = Math.ceil ( this.iWindowWidth * this.oScroller.iWindowWidth / this.oScroller.iStripeWidth );
		this.dContrStripe.style.width = this.iRollerWidth - 13 + 'px';
        this.dRoller.style.width = this.iRollerWidth + 'px';
		this.iPercentWidth = this.iRollerWidth/this.iWindowWidth*100;
		this.iPercent = this.iEndPercent - this.iPercentWidth;
		this.moveToPosition (this.iPercent);
		this.iPercent = this.dRoller.offsetLeft / this.iWindowWidth * 100;;
	}


	this.dRoller.onmousedown = function (event) {oThis.catchRoller(event);}
	//////console.log('this.iCatchLeftOffset');
	this.catchRoller = function(event)
	{
		if (!event) event = window.event;
		this.iCatchLeftOffset = event.clientX - this.dRoller.offsetLeft - this.iLeftWindowOffset;
		var body = document.getElementsByTagName('body')[0];
		body.onmousemove = function (event) {oThis.moveRoller(event);}
		body.onmouseup = function () {oThis.dropRoller();}

		if (event.preventDefault) event.preventDefault();
		else event.returnValue = false;
		if (this.oScroller.tMoveTimer) this.oScroller.tMoveTimer = clearTimeout(this.oScroller.tMoveTimer);
		//////console.log('!');
	}

	this.moveRoller = function (event)
	{
		if (!event) event = window.event;
		if (this.tMoveTimer)
		{
			this.tMoveTimer = clearTimeout(this.tMoveTimer);
			this.oScroller.tMoveTimer = clearTimeout(this.oScroller.tMoveTimer);
		}
		var iLeft = event.clientX - this.iLeftWindowOffset - this.iCatchLeftOffset;
		if (iLeft < 0) iLeft = 0;
		if (iLeft + this.iRollerWidth > this.iWindowWidth) iLeft = this.iWindowWidth - this.iRollerWidth;
		this.iPercent = iLeft / this.iWindowWidth * 100;
		this.dContrStripe.style.left = this.dRoller.style.left = iLeft + 'px';
		this.oScroller.moveToPercent(this.iPercent);
		this.iEndPercent = (this.dRoller.offsetLeft + this.iRollerWidth)/this.iWindowWidth*100;
		//////console.log(this.iPercent);

	}

	this.dropRoller = function ()
	{
		var body = document.getElementsByTagName('body')[0];
		body.onmousemove = null;
	}

	this.moveToPosition = function (iPercent)
	{
		this.iPercent = iPercent;
		var iLeft = Math.ceil(this.iWindowWidth * iPercent / 100);
		if (iLeft < 0)
		{
			iLeft = 0;
			this.iPercent = 0;
		}
		if (iLeft + this.iRollerWidth > this.iWindowWidth)
		{
			iLeft = this.iWindowWidth - this.iRollerWidth;
			this.iPercent = iLeft / this.iWindowWidth * 100;
		}
		this.dContrStripe.style.left = this.dRoller.style.left = iLeft + 'px';
		this.iEndPercent = (this.dRoller.offsetLeft + this.iRollerWidth)/this.iWindowWidth*100;
		this.oScroller.moveToPercent(this.iPercent);
//		//console.log(this.iEndPercent);
	}



	this.moveToPercent = function (iPercent)
	{
		this.iPercent = iPercent;
		var iLeft = Math.ceil(this.iWindowWidth * iPercent / 100);
		if (iLeft < 0)
		{
			iLeft = 0;
			this.iPercent = this.iToPercent;
			if (this.tMoveTimer) this.tMoveTimer = clearTimeout(this.tMoveTimer);
			this.iPercent = 0;
		}
		if (iLeft + this.iRollerWidth > this.iWindowWidth)
		{
			this.iPercent = this.iToPercent;
			iLeft = this.iWindowWidth - this.iRollerWidth;
			if (this.tMoveTimer) this.tMoveTimer = clearTimeout(this.tMoveTimer);
			this.iPercent = iLeft / this.iWindowWidth * 100;
		}
		this.dContrStripe.style.left = this.dRoller.style.left = iLeft + 'px';
		this.iEndPercent = (this.dRoller.offsetLeft + this.iRollerWidth)/this.iWindowWidth*100;
//		//console.log(this.iEndPercent);
	}
	this.slideToPercent = function ()
	{
		if (Math.abs(this.iPercent - this.iToPercent) < 0.5)
		{
			if (this.tMoveTimer)
			{
				clearInterval(this.tMoveTimer);
				this.tMoveTimer = null;
				//this.moveToPercent(this.iToPercent);
				this.iEndPercent = (this.dRoller.offsetLeft + this.iRollerWidth)/this.iWindowWidth*100;
		////console.log(this.iEndPercent);
			}

		}
		else
		{
			var distance = this.iPercent - this.iToPercent;
			var dirrection = distance / Math.abs(distance);
			var increase = 0.05*Math.pow(Math.abs(distance), 1.5);
			//////console.log(distance, increase*dirrection);
			//if (increase < 0.5) increase = 0.5;
			var iPercent = this.iPercent - increase*dirrection;
			//////console.log(iPercent);
			this.moveToPercent(iPercent);
			this.oScroller.startSlideToPercent(iPercent);
			this.iEndPercent = (this.dRoller.offsetLeft + this.iRollerWidth)/this.iWindowWidth*100;
		}
	}

	this.preventDefault = function(event)
	{
		if (!event) event = window.event;
		if (event.preventDefault) event.preventDefault();
		else event.returnValue = false;
	}

	this.stopPropagation = function (event)
	{
		if (!event) event = window.event;
    	if(event.stopPropagation) event.stopPropagation();
    	else event.cancelBubble = true;
	}

	this.dWindow.onmousedown = function (event) {oThis.preventDefault(event)}

	this.startSlideToPercent = function (iPercent)
	{
		if (!this.tMoveTimer) this.tMoveTimer = setInterval(function() {oThis.slideToPercent();}, 50);
		this.iToPercent = iPercent;
		//////console.log(iPercent, this.iPercent, this.iToPercent, this.iTruePercent);
	}

	this.startMoveToPercent = function (iPercent)
	{
		oThis.iToPercent = iPercent + 1;
		oThis.iPercent = iPercent;
		oThis.moveToPercent(iPercent);
		oThis.redraw();
		scroller_window.dWindow.style.visibility = 'visible';
		scroller_controller.dWindow.style.visibility = 'visible';

//		oThis.slideToPercent();
		//////console.log(iPercent, this.iPercent, this.iToPercent, this.iTruePercent);
	}


	this.analizeContent = function ()
	{
		var elements = this.dWindow.getElementsByTagName('span');
		var memory_w = 0, start_i = 0;
        for (var i in elements)
		{
			var width = false;
			try {
				width = parseFloat(elements[i].style.left) - parseFloat(elements[i-2+1].style.left);
			} catch (err){}
			if (/<i>&nbs/.test(elements[i].innerHTML))
            {
                if (!start_i) start_i = i;
                if (width) memory_w += width;
                elements[i].style.display = 'none';
                continue;
            }
            if (width) {elements[start_i].style.width = memory_w + width+'%'; elements[i].style.width = '10%';}
            memory_w = 0;
            start_i = i;
            elements[i].onclick = function (event) {oThis.getClick(event)};
		}
	}

	this.getClick = function (event)
	{
		if (!event) event = window.event;
		if (!event.target) event.target = event.srcElement;
		var target = event.target;
		while (!/span/i.test(target.nodeName)) target = target.parentNode;
		var left = target.offsetLeft;
		var percent = left / this.iWindowWidth * 100;
		this.startSlideToPercent(percent - 1);
		this.stopPropagation(event);
		//console.log(this.iWindowWidth, percent);
	}
}


function highliteLink (event)
{
	if (!event) event = window.event;
	if (!event.target) event.target = event.srcElement;
	//		//////console.log('!');
	var target = event.target.parentNode.parentNode.getElementsByTagName('a')[1];
	//alert(target.nodeName);
	target.style.color="#bb3300";
}

function dischargeLink (event)
{
	if (!event) event = window.event;
	if (!event.target) event.target = event.srcElement;
	var target = event.target.parentNode.parentNode.getElementsByTagName('a')[1];
	target.style.color="";
}

function redrawPortfolio ()
{
	scroller_window.redraw(scroller_controller.iTruePercent);
	scroller_controller.redraw();
	scroller_window.redraw(scroller_controller.iEndPercent - scroller_controller.iPercentWidth);
}