var dirURL = 'http://maxlandscape.com/images/plants2/';
var blnDOMSUPPORT = (document.getElementById) ? true : false;
// Doubly Circular Linked List borrowed from The If Works 
//http://blog.jcoglan.com/2007/07/23/writing-a-linked-list-in-javascript/
function LinkedList() {}
LinkedList.prototype = {
  length: 0,
  first: null,
  last: null
};
LinkedList.Circular = function() {};
LinkedList.Circular.prototype = new LinkedList();
LinkedList.Circular.prototype.append = function(node) {
  if (this.first === null) {
    node.prev = node;
    node.next = node;
    this.first = node;
    this.last = node;
  } else {
    node.prev = this.last;
    node.next = this.first;
    this.first.prev = node;
    this.last.next = node;
    this.last = node;
  }
  this.length++;
};
LinkedList.Circular.prototype.remove = function(node) {
  if (this.length > 1) {
    node.prev.next = node.next;
    node.next.prev = node.prev;
    if (node == this.first) { this.first = node.next; }
    if (node == this.last) { this.last = node.prev; }
  } else {
    this.first = null;
    this.last = null;
  }
  node.prev = null;
  node.next = null;
  this.length--;
};
LinkedList.Node = function(data) {
  this.prev = null; this.next = null;
  this.data = data;
};
// end LL def
var idx = 0;
var lList = new LinkedList.Circular();

function frame(index)
{
	this.img = new Image();
	this.img.src = dirURL+arrayx[index][0];
	this.img.style.position = 'absolute';
	this.img.style.top = "0px";
	this.left =null;
	this.img.style.left = null;
	this.img.style.borderStyle = 'solid';
	this.img.style.borderWidth = '1px';
	this.img.style.borderColor = '#000000';
	if(arrayx[index][1] == 'v')
	{
		this.width = 120;
		this.img.style.height = '160px';
		this.img.style.width =  this.width +'px';
		this.img.style.marginTop = '6px';
	}
	else
	{
		this.width = 160;
		this.img.style.height = '120px';
		this.img.style.width = this.width +'px';
		this.img.style.marginTop = "22px";
	}
	
}

function initialize(elem)
{
	left = 0;
	while(idx <10)
	{
		x= new frame(idx);
		x.left = left;
		x.img.style.left = left+'px';
		y = x.img.style.width.indexOf('p');
		w = Number(x.img.style.width.substr(0,y));
		left = left + w + 16;
		node = new LinkedList.Node(x);
		lList.append(node);
		elem.appendChild(x.img);
		++idx;
	}
}

function pause(elem)
{
	setTimeout(function() {scroller(elem)}, 25);

}

function scroller(elem)
{
	
	for(node = lList.first; node != lList.last; node = node.next)
	{
		if(node == lList.first)
			if((node.data.left+node.data.width+16) <0)
			{
				if(idx == rows)
					idx = 0;
				lList.remove(node);
				elem.removeChild(node.data.img)
				node = lList.last;
				
				l = node.data.left + node.data.width+16;
				f = new frame(idx);
				f.left = l;
				f.img.style.left = l+'px';
				node = node = new LinkedList.Node(f);
				lList.append(node);
				elem.appendChild(f.img);
				++idx;
			}
		node.data.left -= 1;
		node.data.img.style.left = node.data.left + 'px';
	}
	lList.last.data.left -= 1;
	lList.last.data.img.style.left = node.data.left + 'px';
	pause(elem);
}
function externalLinks() 
{

	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) 
 	{
   	var anchor = anchors[i];
   	if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}


window.onload = function()
{
	var strip = document.getElementById("strip");
	externalLinks();
	initialize(strip);
	scroller(strip);
}

