﻿/*
    Featured
*/
// Onload remove the links from the paging links to run as ajax
onloadAdd("initialisefeatured()");
var featured;
function initialisefeatured() {
    // Create the list
    featured = new featuredlist(document.getElementById("featuredpaging"), document.getElementById("featuredlist"));
}

function featuredlist(p, l) {

    this.list = l;
    this.paging = p;
    this.current = null;
    this.timeout = window.setTimeout(this.changefeaturedtimed, 5000)

    var plinks = p.getElementsByTagName("IMG");
    for (var i = 0; i < plinks.length; i++) {
        if (i == 0) {
            this.current = plinks[i];
        }
        plinks[i].featuredlist = this;
        plinks[i].li = l.getElementsByTagName("LI")[i];
        if (isIE) {
            plinks[i].attachEvent("onclick", this.changefeatured);
        }
        else {
            try {
                plinks[i].addEventListener("click", this.changefeatured, false);
            }
            catch (e) {
                alert(e.Description)
            }
        }
    }

}

featuredlist.prototype.changefeatured = function (e) {

    var o;
    var ie_var = "srcElement";
    var moz_var = "target";
    // target for Moz, et al. - srcElement for IE
    e[moz_var] ? o = e[moz_var] : o = e[ie_var];

    if (o.featuredlist.current != o) {
        // hide the current product and show the new one
        o.featuredlist.current.li.className = "hidden";
        o.li.className = "";
        // Set the select paging icon
        o.featuredlist.current.setAttribute("src", "/_resources/files/shop/featured-dot.png");
        o.setAttribute("src", "/_resources/files/shop/featured-dot-on.png");
        // Set the current product
        o.featuredlist.current = o;
        // Reset the timeout
        if (o.featuredlist.timeout != null) {
            window.clearTimeout(o.featuredlist.timeout);
            o.featuredlist.timeout = null;
            o.featuredlist.timeout = window.setTimeout(o.featuredlist.changefeaturedtimed, 10000);
        }
    }
}

featuredlist.prototype.changefeaturedtimed = function (e) {
    featured.timeout = null;
    var ni = featured.current.parentNode.nextSibling;
    while (ni.tagName != "LI" && ni.nextSibling) {
        ni = ni.nextSibling;
    }
    if (ni.tagName != "LI") {
        ni = featured.paging.getElementsByTagName("LI")[0];
    }
    ni = ni.firstChild;
    while (ni.tagName != "IMG") {
        ni = ni.nextSibling;
    }
    if (isIE) {
        ni.click();
    }
    else {
        var myEvt = document.createEvent('MouseEvents');
        myEvt.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
        ni.dispatchEvent(myEvt);
    }

    featured.timeout = window.setTimeout(featured.changefeaturedtimed, 5000);
}
