function Load()
{
	addScrollers();
	resetScroll();
	FindDLNode();
	CountShowroomVehicles();
	SetShowroomScrollSettings();
	RePositionTabs();
};

var _iVehicles = null;
var _iTotalShowroomWidth = null;
var _iShowroomStartPosition = 0;
var _iShowroomCurrentPosition = 0;
var _iShowroomCurrentScrollDistance = 0;
var _iShowroomTotalScrollDistance = 0;
var _iterator = null;
var _iTimeout1 = null;
var _iTimeout2 = null;
var _oDL = null;

function addScrollers()
{
	if(!document.getElementById('left_scroll') && !document.getElementById('right_scroll'))
	{
		var divContainer = document.getElementById('Launchpad05');
		var scrollLeft = document.createElement('div');
		scrollLeft.setAttribute('id', 'left_scroll');
		var scrollRight = document.createElement('div');
		scrollRight.setAttribute('id', 'right_scroll');
		
		divContainer.appendChild(scrollLeft);
		divContainer.appendChild(scrollRight);
		
		VirtualShowroomControlHandler();
	}
}

function resetScroll()
{
	_iShowroomCurrentPosition = 0;
	_iShowroomCurrentScrollDistance = 0;
}

function VirtualShowroomControlHandler()
{
	var oDiv1 = document.getElementById("left_scroll");
    var oDiv2 = document.getElementById("right_scroll");
    
    if (oDiv1)
    {
        if (oDiv2)
        {
            if (oDiv1.attachEvent)
            {
                oDiv1.attachEvent("onmouseover", ToggleScrollButton);
                oDiv1.attachEvent("onmouseout", ToggleScrollButton);
                oDiv1.attachEvent("onmouseover", StartLeftShowroomScrollControl);
                oDiv1.attachEvent("onmouseout", StopLeftShowroomScrollControl); 
                oDiv2.attachEvent("onmouseover", ToggleScrollButton);
                oDiv2.attachEvent("onmouseout", ToggleScrollButton);
                oDiv2.attachEvent("onmouseover", StartRightShowroomScrollControl);
                oDiv2.attachEvent("onmouseout", StopRightShowroomScrollControl); 
            }
            else if (oDiv1.addEventListener)
            {
                oDiv1.addEventListener("mouseover", ToggleScrollButton, false);
                oDiv1.addEventListener("mouseout", ToggleScrollButton, false);
                oDiv1.addEventListener("mouseover", StartLeftShowroomScrollControl, false);
                oDiv1.addEventListener("mouseout", StopLeftShowroomScrollControl, false);
                oDiv2.addEventListener("mouseover", ToggleScrollButton, false);
                oDiv2.addEventListener("mouseout", ToggleScrollButton, false);
                oDiv2.addEventListener("mouseover", StartRightShowroomScrollControl, false);
                oDiv2.addEventListener("mouseout", StopRightShowroomScrollControl, false);
            }
        }
    }    
}

function GetEventSource(e)
{
	var ev;
	var evSource;

	if (!e)	ev = window.event;
	else		ev = e;

	if (ev.target)						evSource = ev.target;
	else									evSource = ev.srcElement;
	if (evSource.nodeType == 3)	evSource = evSource.parentNode;

	return evSource;
}

function ToggleClassName(element, classname)
{
	if (element.className.indexOf(classname) >= 0)
		RemoveClassName(element, classname);
	else
		AddClassName(element, classname);
}

function AddClassName(ele, className)
{
	if (!ele.className.indexOf(className) >= 0)
	{
		ele.className += " " + className;
	}
}

function RemoveClassName(ele, className)
{
	var classNames = ele.className.split(' ');
	for (var j = 0; j < classNames.length; j++)
	{
		if (classNames[j] == className)
			classNames[j] = "";
	}
	ele.className = classNames.join(" ");
}

function ToggleScrollButton(e)
{
	var div = GetEventSource(e);
	ToggleClassName(div, 'active');
}

function StartLeftShowroomScrollControl()
{
    if (_iVehicles > 2)
    {
        _iTimeout1 = setTimeout("ScrollShowroom(-3,3)",30);
        _iTimeout2 = setTimeout(StartLeftShowroomScrollControl,10);
    }
}

function StopLeftShowroomScrollControl()
{
    if (_iVehicles > 2)
    {
        clearTimeout(_iTimeout1);
        clearTimeout(_iTimeout2);
    }
}

function StartRightShowroomScrollControl()
{   
    if (_iVehicles > 2)
    {
        _iTimeout1 = setTimeout("ScrollShowroom(3,-3)", 30);
        _iTimeout2 = setTimeout(StartRightShowroomScrollControl,10);
    }
}

function StopRightShowroomScrollControl()
{
    if (_iVehicles > 2)
    {
        clearTimeout(_iTimeout1);
        clearTimeout(_iTimeout2);
    }
}

function ScrollShowroom(positionChange, scrollDistance)
{
    if (_oDL)
    {
        var odl = _oDL;
        var newPosition = 0;
        
        
        if (_iShowroomCurrentScrollDistance < 0)
        {
				_iShowroomCurrentScrollDistance = 0;
				_iShowroomCurrentPosition = 0;
        }
        
        if (_iShowroomCurrentScrollDistance > _iShowroomTotalScrollDistance)
        {
				_iShowroomCurrentScrollDistance = _iShowroomTotalScrollDistance;
				_iShowroomCurrentPosition = -_iShowroomTotalScrollDistance;
        }
        
        if (_iShowroomCurrentScrollDistance <= _iShowroomTotalScrollDistance && _iShowroomCurrentScrollDistance >= 0)
        {
            _iShowroomCurrentPosition += positionChange;
            _iShowroomCurrentScrollDistance += scrollDistance;
            newPosition = _iShowroomCurrentPosition + "px";
            odl.style.left = newPosition;
        }
        
    }
}

function CountShowroomVehicles()
{
    if (_oDL)
    {
        var odl = _oDL;
        var odls = _oDL.getElementsByTagName("div");
        _iVehicles = odls.length;
        for(i=0; i < odls.length; i++)
        {
			var showroomAnchor = odls[i].getElementsByTagName('a');
		}
    }
}

function SetShowroomScrollSettings()
{
    if (_oDL)
    {
        var iNumOfVehicles = 0;
        var iWidthPerVehicle = 175;
        var odl = _oDL;
        var paddingLeft = 9;
        var paddingRight = 9;
        iNumOfVehicles  = _iVehicles;
        _iTotalShowroomWidth = (iNumOfVehicles + 1) * (iWidthPerVehicle + paddingLeft + paddingRight);
    
        if (_iVehicles > 2)
        {
            odl.style.width = _iTotalShowroomWidth + "px";
            _iShowroomTotalScrollDistance = _iTotalShowroomWidth - 1100;
        }
    }
}

function FindDLNode()
{
    var oDiv = document.getElementById("home_showroom");
    var odls = oDiv.getElementsByTagName("div");
    var sTargetCN = "evoxitems";
    var sTempCN = new String();
    if (odls.length > 0)
    {
        for (var i=0; i < odls.length; i++)
        {
            if (odls[i].hasAttribute)
            {
                sTemp  = odls[i].getAttribute("class");
                if (sTemp)
                {
                    if (sTemp.indexOf(sTargetCN) > -1)
                    {
                        _oDL = odls[i];
                        break;
                    }
                }
            }
            else // is IE
            {
                sTemp = odls[i].attributes.getNamedItem("class").value;
                if (sTemp)
                {
                    if (sTemp.indexOf(sTargetCN) > -1)
                    {
                        _oDL = odls[i];
                    }    
                }
            }
        }        
    }
}

function RePositionTabs()
{
	$('Launchpad05').insert($('Launchpad05').down('.evoxtabs'));
}