// {{{MOD_AXRO}}}

(function ($) {
    window.AXRO = window.AXRO || {};

    /*
     * Utilities
     */

    jQuery.extend({
        random: function (min, max) {
            if (min != null && max != null) {
                return Math.floor(Math.random() * (max - min + 1)) + min;
            }
            return Math.random();
        }
    });

    jQuery.fn.extend({
        random: function () {
            if (this.length == 0) {
                return this;
            }
            return jQuery(this[jQuery.random(0, this.length - 1)]);
        }
    });

    /*
     * Special offer handling
     */

    function OfferDisclaimer(elem) {
        this.elem = $(elem);
        this.init();
    }
    OfferDisclaimer.prototype.init = function () {
        var self = this;
        $(".close a", this.elem).click(function () {
            self.hide();
            return false;
        });
    }
    OfferDisclaimer.prototype.show = function (x, y) {
        x = x ? ('' + x + 'px') : '100px';
        y = y ? ('' + y + 'px') : '100px';
        this.elem.css('position', 'absolute');
        this.elem.css('top', y);
        this.elem.css('left', x);
        this.elem.show();
    }
    OfferDisclaimer.prototype.showAtPos = function (x, y) {
        this.show(x, y);
    }
    OfferDisclaimer.prototype.hide = function () {
        this.elem.hide();
    }

    jQuery(document).ready(function () {
        var offerDisclaimer = new OfferDisclaimer("#offer-disclaimer");
        $("a.offer-disclaimer").click(function (ev) {
            offerDisclaimer.showAtPos(ev.pageX, ev.pageY);
        })
    })

    function OfferRotator(elem) {
        this.elem = $(elem);
        this.container = $(".offerContainer:first", elem);
        this.hovered = false;
        this.init();
    }
    OfferRotator.prototype.init = function () {
        var self = this;
        this.container.css('position', 'relative');
        this.elem.hover(function () {
            self.hovered = true;
        }, function () {
            self.hovered = false;
        });
    };
    OfferRotator.prototype.rotate = function () {
        if (this.hovered) {
            return;
        }
        var self = this;
        $(".offer:hidden", this.elem).random().each(function () {
            var show = $(this);
            var hide = $(".offer:visible", self.elem)

            self.container.height(Math.max(show.height(), hide.height()));

            hide.css('top', 0)
                .css('left', 0)
                .css('position', 'absolute');
            show.css('top', 0)
                .css('left', 0)
                .css('position', 'absolute');

            hide.fadeOut(1000, function () {
                show.fadeIn(1000);
            });

            self.container.height(show.height());
        });
    };

    // TODO: Move this initialization code into the offer box
    jQuery(document).ready(function () {
        var offerRotator = new OfferRotator("#random-offer");
        function rotate() {
            offerRotator.rotate();
            window.setTimeout(rotate, 25000);
        }
        window.setTimeout(rotate, 25000);
    });

    /*
     * Input field decoration
     */


    function InputDeco(input, defaultValue, hasDefaultValue) {
        this.input = $(input);
        this.form = null;
        this.defaultValue = defaultValue;
        this.dirty = !hasDefaultValue;

        this.init();
    }

    $.extend(InputDeco.prototype, {
        init: function () {
            var self = this;
            this.form = this.input.parent("form:first");
            this.input.focus(function () { self.onFocus(); });
            this.input.blur(function () { self.onBlur(); });
            this.input.change(function () { self.onChange(); });
            this.form.submit(function () { self.onSubmit(); });

            if (this.dirty) {
                this.makeChanged(false);
            }
            else {
                this.makeDefault();
            }
        },
        onFocus: function () {
            //this.input.focus();

            if (!this.dirty) {
                this.makeChanged(true);
            }
            else {
                this.input.select();
            }
        },
        onBlur: function () {
            var dirty = this.dirty && this.input.val() != '';
            if (!dirty) {
                this.makeDefault();
                this.dirty = false;
            }
        },
        onChange: function () {
            // NOTE: depends on blur event to be triggered after the change event
            this.dirty = true;
        },
        onSubmit: function () {
            if (!this.dirty) {
                this.makeChanged(true);
            }
            return true;
        },
        makeDefault: function () {
            this.input.val(this.defaultValue);
            this.input.addClass('default');
        },
        makeChanged: function (clear) {
            if (clear) {
                this.input.val('');
            }
            this.input.removeClass('default');
        }
    });

    $.fn.extend({
        inputDeco: function (defaultValue, hasDefaultValue) {
            // argh - jQuery.fn.data was added in 1.2.3, currently we are running 1.2.2, so falling back to DOM expando
            // TODO: upgrade to 1.2.6 or 1.3
            //this.data('AXRO.InputDeco', new InputDeco(this, defaultValue, hasDefaultValue));
            this.each(function () {
                this.axroInputDeco = new InputDeco(this, defaultValue, hasDefaultValue);
                return false;
            });
        }
    });

    /*
     * 3rd party utilities
     */

	var formChecked = {};

	function check_toggle_all(formId, check) {
		var form = document.getElementById(formId);
		var elems = form.elements;

		for (i = 0; i < elems.length; i++) {
			var elem = elems[i];
			if (elem.type=="checkbox") {
				elem.checked=check;
			}
		}
	}

	function check_all(formId, button, checkedValue, uncheckedValue) {
		var check = !formChecked.formId;
		check_toggle_all(formId, check);
		formChecked.formId = check;
		if (button) {
			button.value = check ? uncheckedValue : checkedValue;
		}
	}
	
	window.check_all = check_all;
})(jQuery);
