﻿Type.registerNamespace("Controls");

/// <summary>
/// V1.2 Adds hover effect whole div tags and makes the whole div tagg clickable, the first link or radio button will be selected.
/// If multiple of links the link with class OverTarget will be followed
/// The plugin should be used on the container div
/// Depends:
/// jquery-1.4.js
///	ui.core.js
/// ExtendJquery.js
/// MicrosoftAjax.js
/// </summary>
Controls.Hover = function() {
}

Controls.Hover.prototype =
{
    _init: function() {
        this.element.hover($.createEventDelegate(this, this.onOver), $.createEventDelegate(this, this.onOut));
        if (this.options.doClickAction) {
            this.element.click($.createEventDelegate(this, this.onClick));
            //find target
            if ($("." + this.options.overTargetClass, this.element).length == 0) {
                if ($("a:first", this.element).length > 0) {
                    $("a:first", this.element).addClass(this.options.overTargetClass);
                }
                else {
                    if ($("input:checkbox:first", this.element).length > 0) {
                        $("input:checkbox:first", this.element).addClass(this.options.overTargetClass);
                        this._fixLabels();
                        this._checkSelected();
                    }
                    else {
                        if ($("input:radio:first", this.element).length > 0) {
                            $("input:radio:first", this.element).addClass(this.options.overTargetClass);
                            this._fixLabels();
                            this._checkSelected();
                        }
                    }
                }
            }
            else {
                if ($("input:radio:first", this.element).length > 0 || $("input:checkbox:first", this.element).length > 0) {
                    this._fixLabels();
                    this._checkSelected();
                }
            }
            $("." + this.options.overTargetClass, this.element).click($.createEventDelegate(this, this._onCheckClick)).bind("refresh", $.createEventDelegate(this, this._onCheckClick))
        }

    },
    _fixLabels: function() {
        //debugger;
        var overTargetClass = this.options.overTargetClass;
        var delegate = $.createEventDelegate(this, this._onCheckClick);
        $("label[for]", this.element).each(function() {
            if ($("#" + $(this).attr("for")).hasClass(overTargetClass)) { //if label points on the target, decouple it to avoid double click
                //$(this).click(delegate);
                $(this).attr("for", "");
            }
        });
    },
    _onCheckClick: function(element, event) {
        //debugger;
        $.log("_onCheckClick");
        event.stopPropagation();
        this._checkSelected();
    },
    _checkSelected: function() {
        var selectedClass = this.options.selectedClass;
        //remove selected class on all radio buttons
        $("." + this.options.selectedClass + " :radio." + this.options.overTargetClass).each(function() {
            if (!$(this).attr("checked")) { $(this).parent("." + selectedClass).removeClass(selectedClass) }
        });
        if ($("." + this.options.overTargetClass, this.element).attr("checked")) {
            this.element.addClass(this.options.selectedClass);
        }
        else {
            this.element.removeClass(this.options.selectedClass);
        }
        if ($("." + this.options.overTargetClass, this.element).attr("disabled")) {
            this.element.addClass(this.options.disabledClass).removeClass(this.options.selectedClass);
        }
        else {
            this.element.removeClass(this.options.disabledClass);
            //            $.log("enable!" + $("." + this.options.overTargetClass, this.element).attr("disabled") + "," + $("." + this.options.overTargetClass, this.element).val() + "," + $("." + this.options.overTargetClass, this.element).attr("checked"));
        }
    },
    onOver: function(element, event) {
        this.element.addClass(this.options.overClass);
        if (this.options.onOver != null)
            this.options.onOver(this.element);
    },
    onOut: function(element, event) {
        this.element.removeClass(this.options.overClass);
        this._checkSelected;
        if (this.options.onOut != null)
            this.options.onOut(this.element);
    },
    onClick: function(element, event) {
        $.log("onClick");

        //debugger;
        var type = $(event.target).attr("type");
        if (type != "radio" && type != "checkbox" && typeof (type) != "undefined") //dont do anything if a input field was clicked
        { return true; }
        if (this.options.ClickAction != null) {
            this.options.ClickAction(this.element);
        }
        event.stopPropagation();
        //$("."+this.options.overTargetClass,this.element).click();
        var el = $("." + this.options.overTargetClass, this.element);

        if (typeof (el.attr("checked")) != "undefined") {
            if (el.attr("disabled")) {
                el.attr('checked', false);

            }
            else {
                el.attr('checked', !el.attr('checked'));
            }
        }
        if (!(typeof (el.attr("disabled")) != "undefined" && el.attr("disabled"))) {

            var returnValue = el.triggerHandler('click');
            if (returnValue != false) {

                if (typeof (el.attr("href")) != "undefined") {
                    window.location = el.attr("href");
                }

            }
        }
        this._checkSelected();

    },
    defaults:
    {
        ClickAction: null,
        doClickAction: true,
        overClass: "Over", //the class to add on hover
        selectedClass: "OverSelected", //the class to add on selected
        disabledClass: "Disabled", //the class to add on disabled
        overTargetClass: "OverTargetBehavior", //the element with this class will be selected or if not found the first a tagg or radio button
        onOver: null,
        onOut: null
    }
}

Controls.Hover.registerClass('Controls.Hover', null, Sys.IDisposable);

$.registerAsWidget(Controls.Hover);




