﻿function Product(title, description, image, category) 
{
    this.title = title;
    this.description = description;
    this.image = image;
    this.category = category;
}

var rotateTime=3000;//rotate time interval   
var busy = false
var timer = null;
var Features = new Array();
var loaded = false;

var ProductRotater =
{
    obj: null,
    title: null,
    description: null,
    link: null,
    image: null,
    load: null,

    init: function(object, title, description, image, load, link)
    {
        ProductRotater.obj = object;
        ProductRotater.title = title;
        ProductRotater.description = description;
        ProductRotater.image = image;
        ProductRotater.load = load;
        ProductRotater.link = link;
        ProductRotater.obj.style.visibility = 'hidden';
        ProductRotater.image.onload = function() { loaded = true; }
    },

    initImages: function(index)
    {
        index = index ? index : 0;

        if (Features.length > 0)
        {
            if (index < Features.length)
            {
                var image = new Image();

                image.onload = function() { ProductRotater.initImages(index + 1); }
                image.src = Features[index].image;
            }
            else
            {
                ProductRotater.load.style.visibility = 'hidden';
                rotateProduct(0);
            }
        }
    }
};

function setNew(title, description, image, category)
{    
    if (!busy)
    {
        busy = true;
        ProductRotater.obj.style.zIndex = 20
        ProductRotater.obj.style.position = "absolute";
        var clone = ProductRotater.obj.cloneNode(true)
        clone.id = clone.id + "_clone"
        clone.style.visibility = 'visible';
        ProductRotater.obj.style.visibility = 'hidden';
        ProductRotater.obj.parentNode.insertBefore(clone, ProductRotater.obj)
        ProductRotater.obj.style.zIndex = 0;
        ProductRotater.image.src = image;
        ProductRotater.image.alt = "Gascoigne Leather Furniture Perth";
        ProductRotater.title.innerHTML=title;
        ProductRotater.description.innerHTML = description;

        // This is a hack to make the recliner furniture range text read better
        if (title == "Recliner Furniture") title = "Recliner";
        
        //check if the URL is for a product or a catrgory.
        if (category == "NOURL")
            ProductRotater.link.innerHTML="";
        else
            ProductRotater.link.innerHTML = "<a href='" + category + "'> View the " + title + " Leather Furniture range</a>";
        
        CrossFade(ProductRotater.obj.id, clone.id, 0, endChange, 0.5);
    }
}

function DisplayProduct(origin, newProduct)
{
    if (loaded)
    {
        clearInterval(timer);
        clearTimeout(timer);
        setNew(newProduct.title, newProduct.description, newProduct.image, newProduct.category)
        origin.setAttribute('onmouseout', "timer = setTimeout('rotateProduct(500);', rotateTime); this.removeAttribute('onmouseout');");
    }
}

function endChange(newObject, oldObject)
{
    oldObject.parentNode.removeChild(oldObject);
    busy = false;
}
        
function rotateProduct(index)
{
    clearInterval(timer);
    clearTimeout(timer);

    if (Features.length > 0)
    { 
        if (Features.length > index)
            setNew(Features[index].title, Features[index].description, Features[index].image, Features[index].category)

        index += 1;
        if ((index) < Features.length)
            timer = setTimeout("rotateProduct("+index+");", rotateTime);
        else
            timer = setTimeout("rotateProduct(0);", rotateTime);
    }
    else
        timer = setTimeout("rotateProduct(0);", 1000);
}