var products = [
    "amoire",
    "bed",
    "box",
    "brushes",
    "buda",
    "chairs",
    "chess",
    "chest",
    "cornerdesk",
    "decor",
    "desk",
    "dining",
    "dining2",
    "drum",
    "entry_table",
    "figurine",
    "furniture",
    "hall",
    "lanterns",
    "living",
    "painting",
    "painting2",
    "screen",
    "screen2",
    "sculpture",
    "sidetable",
    "textiles",
    "trays",
    "vases",
    "wallhanging"
];

var numProducts = products.length;
var currentProduct = 0;
var timerId = null;
var xPos = 0;

function pvStartScroll ( direction )
{
	var obj = document.getElementById('productViewer');
    var s = "";
    var startIndex;
    
    if (direction > 0)
        startIndex = 0;
    else
        startIndex = -5;
            
    s += "<div style=\"padding: 0px; margin: 0px 0px 3px 0px; display: block;\">";
    for (var i = startIndex; i < startIndex+5; i++) {
        var productName = products[(currentProduct + i + numProducts) % numProducts];
        s += "<img src=\"img/" + productName + "_148sq.jpg\"" + (i > startIndex ? " style=\"margin-left: 5px;\"" : "") + " height=\"148\" width=\"148\" />";
    }
    s += "</div>";
    s += "<div style=\"margin: 0px; padding: 0px; display: block;\">";
    for (var i = startIndex+5; i < startIndex+10; i++) {
        var productName = products[(currentProduct + i + numProducts) % numProducts];
        s += "<img src=\"img/" + productName + "_148sq.jpg\"" + (i > startIndex+5 ? " style=\"margin-left: 5px;\"" : "") + " height=\"148\" width=\"148\" />";
    }
    s += "</div>";

    obj.innerHTML = s;

    xPos = (direction > 0) ? 0 : -151;
    obj.style.top = xPos + "px";
    timerId = window.setTimeout("pvScroller("+direction+")", 20);
}

function pvEndScroll ()
{
	var obj = document.getElementById('productViewer');
    var s = "";
    
    s += "<div style=\"padding: 0px; margin: 0px; display: block;\">";
    for (var i = 0; i < 5; i++) {
        var productName = products[(currentProduct + i) % numProducts];
        s += "<a href=\"img/" + productName + ".jpg\" onclick=\"showLightbox(this); return false;\">" + "<img src=\"img/" + productName + "_148sq.jpg\"" + (i > 0 ? " style=\"margin-left: 5px;\"" : "") + " height=\"148\" width=\"148\" />" + "</a>";
    }
    s += "</div>";

    obj.innerHTML = s;    xPos = 0;
    obj.style.top = xPos + "px";
}

function pvScroller ( direction )
{
    var obj = document.getElementById('productViewer');
    if (direction > 0) {
        xPos = xPos - 5;   
        if (xPos > -151) {
            timerId = window.setTimeout("pvScroller("+direction+")", 20);
            obj.style.top = xPos + "px";
        } else {
            currentProduct = (currentProduct + 5) % numProducts;
            pvEndScroll();
        }
    } else {
        xPos = xPos + 5;
        if (xPos < 0) {
            timerId = window.setTimeout("pvScroller("+direction+")", 20);
            obj.style.top = xPos + "px";
        } else {
            xPos = 0;
            currentProduct = (currentProduct - 5 + numProducts) % numProducts;
            pvEndScroll();
        }
    }
        
}

function pvNextPage ()
{
    pvStartScroll(1);
}

function pvPreviousPage ()
{
    pvStartScroll(-1);
}

function pvInit ()
{
    pvEndScroll();
}

//// addLoadEvent()// Adds event to window.onload without overwriting currently assigned onload functions.// Function found at Simon Willison's weblog - http://simon.incutio.com///function addLoadEvent(func){		var oldonload = window.onload;	if (typeof window.onload != 'function'){    	window.onload = func;	} else {		window.onload = function(){		oldonload();		func();		}	}}addLoadEvent(pvInit);