//
// Scripts for Wright Tanks.
//
// @author Tom Ryder <tom@prodev.co.nz>
// @copyright 2010 Professional Development
//
"use strict";
$(window).addEvent("domready", function () {

	// Focus class setup.
    $$("input,select").addEvent("focus", function () {
        this.addClass("focus");
    }).addEvent("blur", function () {
        this.removeClass("focus");
    });

    // Keep faded grey text in the search box when empty.
    (function () {
        var message = "Enter keywords here",
            active = {"color": "#000000"},
            inactive = {"color": "#959595"},
            empty = "",
            text = $("search-keywords"),
            form = $(text.form);
        if (text && form) {
            text.addEvents({
                "focus": function () {
                    this.setStyles(active);
                    if (this.get("value") === message) {
                        this.set("value", empty);
                    }
                },
                "blur": function () {
                    if (this.get("value") === empty || this.get("value") === message) {
                        this.setStyles(inactive);
                        this.set("value", message);
                    }
                }
            });
            text.fireEvent("blur");
            form.addEvent("submit", function () {
                if (text.get("value") === message) {
                    text.set("value", empty);
                }
            });
        }
    }());

    // Flicker for promotions.
    (function () {
        var show = {"display": "block"},
            hide = {"display": "none"};
        $$("#home").each(function (home) {
            home.getElements(".categories>li").each(function (category) {
                var products = category.getElements(".products>li"),
                    current = 0,
                    left = category.getElements(".left"),
                    right = category.getElements(".right"),
                    redraw;
                redraw = function () {
                    products.each(function (product) {
                        product.setStyles(hide);
                    });
                    products[current].setStyles(show);
                };
                left.addEvent("click", function () {
                    if (current > 0) {
                        current -= 1;
                        redraw();
                    }
                });
                right.addEvent("click", function () {
                    if (current < (products.length - 1)) {
                        current += 1;
                        redraw();
                    }
                });
            });
        });
    }());
});

