function BNI_SetPosition(left, top){
	this.left = left;
	this.top = top;

	this.visElt.style.left = left;
	this.visElt.style.top = top;
}

function BNI_Dump(){
	window.status = "Dumping banner item " + this.idx + " to document.";
	document.write("Position: " + this.left + ","); 
	document.write(this.top);
	document.write(" Dimensions: " + this.width + ",");
	document.write(this.height + "<br>");
}

function BNI_HitTest(x, y){
	var hIn = (x >= this.left && x <= this.left + this.width);
	var vIn = (y >= this.top && y <= this.top + this.height);
	return (hIn && vIn);
}

function BNI_Hilite(){
	var item = this;
	
	var curItem = item.parent.parent.currentItem;
	if(curItem && curItem != item)
		curItem.Restore();

	item.parent.parent.currentItem = item;

	item.elt.style.visibility = "hidden";
	item.eltHlt.style.left = item.left;
	item.eltHlt.style.top = item.top;
	
	item.eltHlt.style.visibility = "visible";
	item.visElt = item.eltHlt;
	item.parent.ShowDescription();
}

function BNI_Restore(){
	var item = this;
	item.eltHlt.style.visibility = "hidden";
	item.elt.style.left = item.left;
	item.elt.style.top = item.top;
	item.elt.style.visibility = "visible";
	item.visElt = item.elt;
	item.parent.parent.currentItem = null;
}

function BNI_Click(){
	var item = this.value;

	//SPECIFIC
	ViewStoreItem(item.parent.url);
	//SPECIFIC
}

function BNI_SetImageSources(defSrc, hltSrc){
	this.imageDefSrc = defSrc;
	this.imageHltSrc = hltSrc;
}

function BannerItem(parent, width, height){
	this.parent = parent;
	this.width = width;
	this.height = height;
	this.left = 0;
	this.top = 0;
	this.idx;
	this.imageDefSrc;
	this.imageHltSrc;

	this.elt = document.createElement("IMG");
	this.elt.style.position = "absolute";
	this.elt.style.width = width;
	this.elt.style.height = height;
	this.elt.value = this;
	this.visElt = this.elt;
	
	this.eltHlt = document.createElement("IMG");
	this.eltHlt.style.position = "absolute";
	this.eltHlt.style.width = width;
	this.eltHlt.style.height = height;
	this.eltHlt.style.visibility = "hidden";
	this.eltHlt.onclick = BNI_Click;
	this.eltHlt.value = this;

	this.prevItem = null;
	this.nextItem = null;
	this.HitTest = BNI_HitTest;
	this.Hilite = BNI_Hilite;
	this.Restore = BNI_Restore;
	this.SetPosition = BNI_SetPosition;
	this.SetImageSources = BNI_SetImageSources;
	this.Dump = BNI_Dump;
}

function BIG_InGroup(item){
	for(var i = 0; i < this.itemCount; i++)
		if(this.items[i] == item)return true;

	return false;
}

function BIG_ShowDescription(){
	if(this.descVisible && this.InGroup(this.parent.currentItem))return;

	for(var i = 0; i < this.parent.itemGroups.length; i++)
	{
		this.parent.itemGroups[i].descVisible = true;
		this.parent.itemGroups[i].HideDescription();
	}

	var descLines = this.description.split("\n");
	for(var i = 0; i < descLines.length; i++)
	{
		if(i > 0)this.descElt.appendChild(document.createElement("BR"));
		this.descElt.appendChild(document.createTextNode(descLines[i]));	
	}

	this.descVisible = true;
}

function BIG_HideDescription(){
	if(!this.descVisible)return;

	if(this.descElt.childNodes.length)
	{
		this.descElt.removeChild(this.descElt.childNodes[0]);
		this.HideDescription();
	}

	this.descVisible = false;
}

function BannerItemGroup(itemCount, width, height, description, url, descElt){
	this.itemCount = itemCount;
	this.description = description;
	this.url = url;
	this.descElt = descElt;
	this.items = [];
	this.parent;
	this.descVisible = false;

	this.InGroup = BIG_InGroup;
	this.ShowDescription = BIG_ShowDescription;
	this.HideDescription = BIG_HideDescription;

	for(var i = 0; i < this.itemCount; i++)
		this.items[i] = new BannerItem(this, width, height);
}


function BNN_HitTest(x, y){
	var hIn = (x >= this.left + this.vBorder && x <= this.left + this.width - this.vBorder);
	var vIn = (y >= this.top + this.hBorder && y <= this.top + this.height - this.vBorder);
	return (hIn && vIn);
}

function BNN_GetFirstVisibleItem(){
	for(var i = 0; i < this.itemCount; i++)
	{
		var item = this.items[i];
		if(item.left <= 0 && item.left + item.width > 0)return item;
	}
}

function BNN_Initialise(firstItemIdx){

	if(!(this.sysInfo))
	{
		window.alert("Cannot initialise banner.  System info and site manager objects not found");
		return false;
	}

	var i;
	for(i = 0; i < this.itemGroups.length; i++)
	{
		var itemGroup = this.itemGroups[i];
		itemGroup.parent = this;
		for(var j = 0; j < itemGroup.items.length; j++)
		{
			var newItem = itemGroup.items[j];
			newItem.idx = this.itemCount;
			this.width += (newItem.width + this.vBorder);
			if(newItem.height + 2*this.hBorder > this.height)this.height = newItem.height + 2*this.hBorder;
			this.items[this.itemCount++] = newItem;
		}
	}
	this.width -= this.vBorder;

	firstItemIdx = firstItemIdx ? firstItemIdx : 0;
	this.firstItem = this.items[firstItemIdx];
	this.lastItem = this.items[(firstItemIdx + this.itemCount - 1) % this.itemCount];
	
	var item;
	for(i = 0; i < this.itemCount; i++)
	{
		item = this.items[i];
		if(i == 0)
		{
			item.prevItem = this.items[this.itemCount - 1];
			item.nextItem = this.items[1];
		}
		else if(i == this.itemCount - 1)
		{
			item.prevItem = this.items[i - 1];
			item.nextItem = this.items[0];
		}
		else
		{
			item.prevItem = this.items[i - 1];
			item.nextItem = this.items[i + 1];
		}	
	}

	item = this.firstItem;
	for(i = 0; i < this.itemCount; i++)
	{
		item.SetPosition(this.left + (i*(this.vBorder + item.width)), this.top + this.vBorder);
		item = item.nextItem;
	}

	this.backgroundElt.style.position = "absolute";
	this.backgroundElt.style.left = this.left;
	this.backgroundElt.style.top = this.top;
	this.backgroundElt.style.width = this.width;
	this.backgroundElt.style.height = this.height;
	this.backgroundElt.style.backgroundColor = this.backgroundColor;

	document.body.appendChild(this.backgroundElt);
	
	this.initialised = true;
	return true;
}

function BNN_Draw(reverseOrder){
	if(!this.initialised)
	{
		window.alert("initialise before drawing");
		return false;
	}

	
	var i, item;
	if(reverseOrder)
	{
		for(i = this.itemCount - 1; i >= 0; i--)
		{
			item = this.items[i];
			item.eltHlt.id = "bih" + item.idx;
			item.eltHlt.src = item.imageHltSrc;
			document.body.appendChild(item.eltHlt);
		}
		for(i = this.itemCount - 1; i >= 0; i--)
		{
			item = this.items[i];
			item.elt.id = "bid" + item.idx;
			item.elt.src = item.imageDefSrc;
			document.body.appendChild(item.elt);
		}
	}
	else
	{
		for(i = 0; i < this.itemCount; i++)
		{
			item = this.items[i];
			item.elt.id = "bid" + item.idx;
			item.elt.src = item.imageDefSrc;
			document.body.appendChild(item.elt);
		}
	
		for(i = 0; i < this.itemCount; i++)
		{
			item = this.items[i];
			item.eltHlt.id = "bih" + item.idx;
			item.eltHlt.src = item.imageHltSrc;
			document.body.appendChild(item.eltHlt);
		}
	}
	document.onmousemove = CaptureMouse;
	document.onmousedown = CaptureMouse;
}

function BNN_HideDescriptions(){
	for(var i = 0; i < this.itemGroups.length; i++)
		this.itemGroups[i].HideDescription();
}

function BNN_Scroll(){
	

    var mouseX = this.sysInfo.mouseX;
    var mouseY = this.sysInfo.mouseY;
    var i;
    
    /*var doDec = false;
    if(this.HitTest(mouseX, mouseY))
    {
        if(mouseX >= 0 & mouseX <= this.scrollZone)
        {
	    	this.scrollVel = this.scrollVel + this.scrollAcc;
            this.isScrolling = true;
        }
        else if(mouseX >= this.sysInfo.GetDocWidth() - this.scrollZone)
        {
	   	 	this.scrollVel = this.scrollVel - this.scrollAcc;
	    	this.isScrolling = true;
        }
        else
	    doDec = true;
    }
    else
	this.HideDescriptions();

    if(doDec)
    {
        if(this.scrollVel > 0)this.scrollVel = this.scrollVel - this.scrollDec;
        if(this.scrollVel < 0)this.scrollVel = this.scrollVel + this.scrollDec;
        this.isScrolling = false;
    }*/  

	switch(this.scrollDirection)
	{
		case 'LEFT':
			this.scrollVel = this.scrollVel + this.scrollAcc;
			break;
		case 'RIGHT':
			this.scrollVel = this.scrollVel - this.scrollAcc;
			break;
		default:
			if(this.scrollVel > 0)this.scrollVel = this.scrollVel - this.scrollDec;
        	if(this.scrollVel < 0)this.scrollVel = this.scrollVel + this.scrollDec;
			break;
	}
		
    if(this.scrollVel > 0)this.scrollVel = this.scrollVel > this.scrollMaxSpeed ? this.scrollMaxSpeed : this.scrollVel;
    if(this.scrollVel < 0)this.scrollVel = this.scrollVel < -this.scrollMaxSpeed ? -this.scrollMaxSpeed : this.scrollVel;   
			   
    for(i = 0; i < this.items.length; i++)
    {
        var item = this.items[i];
        var newX = item.left + this.scrollVel;
        item.SetPosition(newX, item.top);

	if(item.HitTest(mouseX, mouseY))
		item.Hilite();
	else
		item.Restore();
    }

    var firstItem = this.firstItem;
    var lastItem = this.lastItem

    if(this.scrollVel > 0 && lastItem.left + this.vBorder > this.sysInfo.GetDocWidth())
    {
        lastItem.SetPosition(firstItem.left - firstItem.width - this.vBorder, lastItem.top);
        this.firstItem = this.lastItem;
        this.lastItem = lastItem.prevItem;
    }
    if(this.scrollVel < 0 && this.firstItem.left + this.firstItem.width + this.vBorder < 0)
    {
        firstItem.SetPosition(lastItem.left + lastItem.width + this.vBorder, firstItem.top);
        this.lastItem = this.firstItem;
        this.firstItem = this.firstItem.nextItem;
    }

    setTimeout("g_banner.Scroll()", 50);
}

function BNN_Stop(){
	this.scrollVel = 0;
	this.isScrolling = false;
}

function BNN_Dump(){
	for(var i = 0; i < this.items.length; i++)
		this.items[i].Dump();
}

function CaptureMouse(e){
	var sysInfo = g_banner.sysInfo;
	sysInfo.CaptureEvent(e);
	e = sysInfo.event;
	if(e.type == "mousedown")
		g_banner.Stop();
}

function Banner(){
	this.sysInfo;
	this.itemGroups;
	this.items = [];
	this.itemCount = 0;
	this.hBorder = 0;
	this.vBorder = 0;
	this.left;
	this.top;
	this.width = 0;
	this.height = 0;
	this.items = [];
	this.scrollZone = 0;
	this.scrollVel = 0;
	this.scrollAcc = 0;
	this.scrollDec = 0;
	this.scrollMaxSpeed = 0;
	this.firstItem = null;
	this.lastItem = null;
	this.backgroundElt = document.createElement("DIV");
	this.backgroundColor;
	this.initialised = false;
	this.isScrolling = false;
	this.scrollDirection = null;
	this.currentItem = null;

	this.HitTest = BNN_HitTest;
	this.Initialise = BNN_Initialise;
	this.Draw = BNN_Draw;
	this.GetFirstVisibleItem = BNN_GetFirstVisibleItem;
	this.HideDescriptions = BNN_HideDescriptions;
	this.Scroll = BNN_Scroll;
	this.Stop = BNN_Stop;
	this.Dump = BNN_Dump;
}

var g_banner = new Banner();