﻿/// <reference path="~/js/jquery-1.3.2.js" />

jQuery.extend(jQuery.fx, {

    //    _cache: [],
    //    _omaa: {
    //        _counter: 0,
    //        _length: 600
    //    },

    step:
    {
        _default: function (fx) {
            if (fx.elem.style && fx.elem.style[fx.prop] != null)
                fx.elem.style[fx.prop] = fx.now + fx.unit;
            else
                fx.elem[fx.prop] = fx.now;

            if (!jQuery.fx.step._defaultOMAA(fx))
                return;
        },

        _defaultOMAA: function (fx) {
            // *****************************************************************************
            var el = undefined;
            var i = undefined;

            if (fx.options.tollerance == undefined)
                fx.options.tollerance = 0;

            if (fx.options.count != undefined) {
                el = $(window);
                i = el.data("_i");

                if (i == undefined || i == null || isNaN(i)) {
                    i = 0;
                    el.data("_i", 0);
                }
            }

            //            if (fx.options.name) {
            //                console.log(fx.options.name, fx.prop, fx.elem.tagName + "#" + fx.elem.id + "." + fx.elem.className,
            //                    fx.start, fx.end, fx.now, fx.pos,
            //                    i, fx.options.count);
            //            }

            if (fx.pos == 0 && jQuery.isFunction(fx.options.begins)) {

                if (el == undefined)
                    el = $(window);

                if (el.data("_b") == undefined) {
                    el.data("_b", 1);
                    fx.options.begins(fx.now, fx, this);
                }

            }

            var pos = fx.pos;

            //            if (fx.options.name) {
            //                console.log(fx.elem.id, fx.pos, i, fx.options.count);
            //            }

            if (!isNaN(i)) {
                i++;

                if ((i == fx.options.count) && (fx.pos + fx.options.tollerance) >= 1)
                    pos = 1;
            }

            //            if (fx.options.tollerance != undefined && fx.options.tollerance > 0) {
            //            }
            //            if (fx.options.name)
            //                console.log(fx.options.name, ">", fx.pos);

            if (fx.options.tollerance != -1 && pos == 1) { // * fx.options.tollerance

                var complete = (i == undefined) ? true : (i == fx.options.count);

                if (complete) {

                    if (el != undefined) {
                        el.removeData("_i");
                        el.removeData("_b");
                        //                        el.data("_i", null);
                        //                        el.data("_b", null);
                    }

                    fx.options.tollerance = -1;

                    if (jQuery.isFunction(fx.options.group_complete)) {
                        var ret = fx.options.group_complete(fx.now, fx, this);
                        if (ret != undefined) {
                            if (ret == true) {
                                fx.options.blocking = true;
                                fx.options.resetOnBlock = true;
                            }
                            else
                                fx.options.blocking = false;
                        }
                    }
                }
                else if (el != undefined)
                    el.data("_i", i);

            }

            if (fx.options.tollerance == -1 && fx.options.blocking) {
                if (fx.options.resetOnBlock && fx.pos != 1)
                    $(fx.elem).stop(true, true);
                else if (fx.pos != 1)
                    $(fx.elem).stop(true, false);
                return false;
            }

            return true; // IMPORTANT!
            // *****************************************************************************
        },

        //empty: function (fx) {
        //        this.startTime = now();
        //        this.start = from;
        //        this.end = to;
        //        this.unit = unit || this.unit || "px";
        //        this.now = this.start;
        //        this.pos = this.state = 0;
        //},

        opacity: function (fx) {

            //        var o = $(this);
            //        var opacity = parseFloat(o.css("opacity"));

            //        o.css({ "background-color": (opacity == 1) ? "transparent" : "white" });

            jQuery.attr(fx.elem.style, "opacity", fx.now);

            //            if (fx.now == 1 && jQuery.browser.msie) {

            //                fx.elem.style.filter = '';
            //                //jQuery(fx.elem).css("filter", "");
            //            }

            //            if (jQuery.isFunction(fx.options.static)) {

            //                var needed = (fx.now == 1 && fx.pos == 1) || (fx.now == 1 && fx.pos == 0);
            //                if (needed) {
            //                    var state = (fx.now == 1 && fx.pos == 0);
            //                    var ret = fx.options.static(state, fx.now, fx, this);

            //                    if (ret == undefined) {
            //                        if (state) {
            //                            $(fx.elem).css({ "background-color": "white" });
            //                        }
            //                        else {
            //                            $(fx.elem).css({ "background-color": "transparent" });
            //                        }
            //                    }
            //                }
            //            }

            if (!jQuery.fx.step._defaultOMAA(fx))
                return;

        }

    } // step

});


jQuery.fn.IsolateFromSelectionTo = function (selector, destination, config, callback) {

    var off = (config.off == undefined) ? 250 : config.off;
    var on = (config.on == undefined) ? 500 : config.on;
    var queue = config.queue || "global0";
    selector = selector || ":tassel";

    //    var el = this;
    if (selector.toLowerCase().indexOf(":not(:tassel(a1))") == -1)
        selector += ":not(:tassel(a1))";

    //    console.log(selector);

    //    return null;
    return this.IsolateFromGroupTo($(selector), destination, config, callback);

    //    $(selector).animate(
    //        { opacity: "hide" },
    //        {
    //            queue: queue,
    //            duration: off,
    //            complete: function () { el.moveTasselDirect(destination).animate({ opacity: "show" }, { queue: queue, duration: on, complete: callback }); }
    //        }
    //    );
};

jQuery.fn.IsolateFromGroupTo = function (group, destination, config, callback) {

    var speed_off = (config.off == undefined) ? 250 : config.off;
    var speed_on = (config.on == undefined) ? 500 : config.on;
    //    var queue = config.queue || "global0";

    var el = this;

    //    var count = group.size();
    //    var i = 0;
    //    var first = true;

    return group.animate(
        { opacity: "hide" },
        {
            duration: speed_off,
            tollerance: 0.15, count: group.length,
            begins: function () {
                //console.info("BEGIN");
                group.enabled(false);
            },
            group_complete: function () {
                //                console.warn("COMPLE", destination);
                el.enabled(false).moveTasselDirect(destination).animate(
                    { opacity: "show" },
                    {
                        duration: speed_on,
                        complete: callback
                    }
                );
            }
        }
    );

    //    el.animate(
    //                { opacity: "show" },
    //                {
    //                    queue: "I_L",
    //                    duration: speed_on,
    //                    step: function () {
    //                        if (!first) return;
    //                        first = false;
    //                        el.moveTasselDirect(destination);
    //                    },
    //                    complete: callback
    //                }
    //            );

    //    return this;
};

jQuery.fn.center = function (params) {

    var options = {
        w: -1, h: -1,
        vertical: true,
        horizontal: true
    }
    options = jQuery.extend(options, params);

    return this.each(function () {

        //initializing variables
        var $self = jQuery(this);

        var offsetsX = ($self.outerWidth() - $self.width());
        var offsetsY = ($self.outerHeight() - $self.height());

        //get the dimensions using dimensions plugin
        var width = (options.w > -1) ? options.w : $self.width();
        var height = (options.h > -1) ? options.h : $self.height();

        //get the type of positioning
        var positionType = $self.parent().css("position");

        // get the half minus of width and height
        var halfWidth = -(width + offsetsX) / 2;
        var halfHeight = -(height + offsetsY) / 2;

        // initializing the css properties
        var cssProp = {
            position: 'absolute'
        };

        if (options.vertical) {
            cssProp.height = height;
            cssProp.top = '50%';
            cssProp.marginTop = halfHeight;
        }
        if (options.horizontal) {
            cssProp.width = width;
            cssProp.left = '50%';
            cssProp.marginLeft = halfWidth;
        }
        //check the current position
        if (positionType == 'static') {
            $self.parent().css("position", "relative");
        }
        //aplying the css
        $self.css(cssProp);
    });

};


jQuery.fn.reverse = function () {
    var elements = [];
    for (var i = 0; i < this.length; i++) {
        elements.push(this[i]);
    }
    return $(elements.reverse());
};

var DEBUGGER = false;

(function ($) {

    $.fn.defaultHover = function () {
        var ots = this.data("ots");
        
        if (ots == "A1")
            return this.hover(Btn_MouseOver, Btn_MouseOut);

        //.hover(Btn_MouseOver, Btn_MouseOut)
        //return $(this).hover(Btn_MouseOver, Btn_MouseOut)
        //return $(this).hover(OMAA.Stage.HoverOn, OMAA.Stage.HoverOff);
        return this.hover(OMAA.Stage.HoverOn, OMAA.Stage.HoverOff);
    },

    $.fn.progress = function (pc) {

        if (pc < 0)
            $(this).hide(10).css({ width: "0px" });
        else if (pc == 0)
            $(this).css({ width: "0px" }).show(10);
        else if (pc > 0)
            $(this).animate({ width: pc + "%" }, 10);
        return $(this);
    },

    $.fn.multiCss = function (styles, debug) {

        var newCss = OMAA.MergeObjects({}, styles);

        if (newCss != null)
            return this.css(newCss);
        else
            return this;

        //        if (styles == undefined)
        //            return $(this);

        //        var empty = styles[0];

        //        var count = styles.length;
        //        
        //        for (var i = 1; i < count; i++) 
        //            jQuery.extend(empty, styles[i]);
        //        }

        //        var count = styles.length;
        //        console.warn (count, styles);
        //        
        //        for (var i = 0; i < count; i++) {
        //            
        //            console.log(i, ": ", styles[i]);
        //            
        //            for (var p in styles[i]) {
        //                var v = styles[i][p];
        //                if (v.length>0)
        //                {
        //                    empty[p] = v;
        //                    console.log("   ", p, "=" , v, " | ", empty);
        //                }
        //            }
        //            
        //        }

        //        for (var i = 0; i < styles.length; i++) {

        //            if (styles[i] == undefined || styles[i] == null)
        //                continue;
        //                
        //            for (var p in styles[i]) {
        //                console.log(i, styles[i][p]);
        //                empty[p] = styles[i][p]
        //            }
        //            
        ////            if (DEBUGGER)
        ////                console.log(i, styles[i]);

        ////            empty = $.extend(empty, empty, styles[i]);
        //            if (DEBUGGER)
        //                console.log("  => ", empty);

        //        }

        //        if (DEBUGGER)
        //            console.log(empty);

        //return $(this).css(empty);
    },

    /* TASSELES */
    $.fn.createTassel = function (config) {
        config.id = "ts" + (config.id || UID.next());
        return $("<div />").attr("id", config.id).addClass("tassel").css(config.css).appendTo(OMAA.Stage.div);
    },

    $.fn.moveTassel = function (callback) {

        if ($(this).attr("animable") && $(this).attr("animable") == "false")
            return $(this).show("normal");

        return $(this).show("slow")
        .animate({ left: '+=200' }, 2000)
        .slideToggle(1000)
        .slideToggle("fast")
        .animate({ left: '-=200' }, 1500)
        .hide("slow")
        .show(1200)
        .slideUp("normal", callback);
    },

    $.fn.loopingTassel = function (styles) {

        if ($(this).attr("animable") && $(this).attr("animable") == "false")
            return $(this).resetStatusTassel().show("normal");

        if ($(this).checkStatusTassel())
            return $(this);

        $(this).moveTassel(function () { $(this).loopingTassel(); });

        //$(this).show("slow");
        //$(this).animate({ left: '+=200' }, 2000);
        //$(this).slideToggle(1000);
        //$(this).slideToggle("fast");
        //$(this).animate({ left: '-=200' }, 1500);
        //$(this).hide("slow");
        //$(this).show(1200);

        //return $(this).slideUp("normal", function() { $(this).loopingTassel(); });

    },

    $.fn.checkStatusTassel = function () {

        return $(this).data("stop") == "stop";
    },

    $.fn.stopLoopingTassel = function () {

        return $(this).data("stop", "stop");
    },

    $.fn.resetStatusTassel = function (callback) {

        return $(this).removeData("stop");
    }

})(jQuery);

