﻿/**************************************
*  ZiLeex JavaScript Library
*  Development Version 0.10
*  (c)2009 Walter M. Soto Reyes
*************************************/
ZiLeex.Extend({
    UI: {
        Change: {
            BorderColor: function (id, newColor) {
                ZiLeex.$(id).style.borderColor = newColor;
            },
            BorderStyle: function (id, newStyle) {
                ZiLeex.$(id).style.borderStyle = newStyle;
            },
            BorderWidth: function (id, newWidth) {
                newWidth = parseInt(newWidth, 10);
                ZiLeex.$(id).style.borderWidth = newWidth + "px";
            },
            Width: function (id, newWidth) {
                newWidth = (newWidth >= 0) ? newWidth : 0;
                ZiLeex.$(id).style.width = newWidth + "px";
            },
            Height: function (id, newHeight) {
                newHeight = (newHeight >= 0) ? newHeight : 0;
                ZiLeex.$(id).style.height = newHeight + "px";
            },
            Left: function (id, newLeft) {
                ZiLeex.$(id).style.left = newLeft + "px";
            },
            Right: function (id, newRight) {
                ZiLeex.$(id).style.right = newRight + "px";
            },
            Top: function (id, newTop) {
                ZiLeex.$(id).style.top = newTop + "px";
            },
            Bottom: function (id, newBottom) {
                ZiLeex.$(id).style.bottom = newBottom + "px";
            },
            Opacity: function (id, opacity) {
                var o = ZiLeex.$(id).style;
                o.opacity = (opacity / 100);
                o.MozOpacity = (opacity / 100);
                o.KhtmlOpacity = (opacity / 100);
                o.filter = "alpha(opacity=" + opacity + ")";
            },
            DisplayOn: function (id) {
                ZiLeex.$(id).style.display = 'block';
            },
            DisplayOff: function (id) {
                ZiLeex.$(id).style.display = 'none';
            },
            Display: function (id, display) {
                ZiLeex.$(id).style.display = display;
            },
            VisibleOn: function (id) {
                ZiLeex.$(id).style.visibility = 'visible';
            },
            VisibleOff: function (id) {
                ZiLeex.$(id).style.visibility = 'hidden';
            },
            Visibility: function (id, visibility) {
                ZiLeex.$(id).style.visibility = visibility;
            }
        },
        Animations: {
            Tween: {
                Acceleration: {
                    Constant: function (pos, time, start, end) {
                        return Math.floor(((end - start) / pos) + pos);
                    }
                },
                Smooth: function (pos) {
                    return pos;
                }
            },
            Animate: function (params, _callback) {
                var speed = Math.round(params.speed / 1000);
                var timer = 0;
                if (params.start > params.end) {
                    for (var i = params.start; i >= params.end; i--) {
                        (function (i, _f, speed, timer) { setTimeout(function (){ _f(i);}, (timer * speed)); })(i, params.func, speed, timer);
                        timer++;
                    }
                } else if (params.start < params.end) {
                    for (var index = params.start; index <= params.end; index++) {
                        (function (i, _f, speed, timer) { setTimeout(function () { _f(i); }, (timer * speed)); })(index, params.func, speed, timer);
                        timer++;
                    }

                }
                if (ZiLeex.Utilities.IsDefined(_callback)) {
                    if (ZiLeex.Utilities.IsFunction(_callback)) {
                        ZiLeex.Page.WaitFor(function () {
                            _callback();
                        }, (speed * timer));
                    }
                }
            },
            Fade: function (id, startOpacity, endOpacity, millisec, _callback) {
                ZiLeex.UI.Animations.Animate({
                    speed: millisec,
                    start: startOpacity,
                    end: endOpacity,
                    func: function (i) { ZiLeex.UI.Change.Opacity(id, i); }
                }, _callback);
            },
            Reposition: {
                Margin: {
                    Top: function (id, startTop, endTop, speed, _callback) {
                        ZiLeex.UI.Animations.Animate({
                            speed: speed,
                            start: startTop,
                            end: endTop,
                            func: function (i) {
                                ZiLeex.$(id).style.marginTop = i + 'px';
                            }
                        }, _callback);
                    },
                    Bottom: function (id, startBottom, endBottom, speed, _callback) {
                        ZiLeex.UI.Animations.Animate({
                            speed: speed,
                            start: startBottom,
                            end: endBottom,
                            func: function (i) { ZiLeex.$(id).style.marginBottom = i + 'px'; }
                        }, _callback);
                    },
                    Left: function (id, startLeft, endLeft, speed, _callback) {
                        ZiLeex.UI.Animations.Animate({
                            speed: speed,
                            start: startLeft,
                            end: endLeft,
                            func: function (i) { ZiLeex.$(id).style.marginLeft = i + 'px'; }
                        }, _callback);
                    },
                    Right: function (id, startRight, endRight, speed, _callback) {
                        ZiLeex.UI.Animations.Animate({
                            speed: speed,
                            start: startRight,
                            end: endRight,
                            func: function (i) { ZiLeex.$(id).style.marginRight = i + 'px'; }
                        }, _callback);
                    }
                },
                Top: function (id, startTop, endTop, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startTop,
                        end: endTop,
                        func: function (i) { ZiLeex.$(id).style.top = i + 'px'; }
                    }, _callback);
                },
                Bottom: function (id, startBottom, endBottom, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startBottom,
                        end: endBottom,
                        func: function (i) { ZiLeex.$(id).style.bottom = i + 'px'; }
                    }, _callback);
                },
                Left: function (id, startLeft, endLeft, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startLeft,
                        end: endLeft,
                        func: function (i) { ZiLeex.$(id).style.left = i + 'px'; }
                    }, _callback);
                },
                Right: function (id, startRight, endRight, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startRight,
                        end: endRight,
                        func: function (i) { ZiLeex.$(id).style.right = i + 'px'; }
                    }, _callback);
                }
            },
            Resize: {
                Width: function (id, startSize, endSize, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startSize,
                        end: endSize,
                        func: function (i) { ZiLeex.UI.Change.Width(id, i); }
                    }, _callback);
                },
                Height: function (id, startSize, endSize, speed, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: speed,
                        start: startSize,
                        end: endSize,
                        func: function (i) { ZiLeex.UI.Change.Height(id, i); }
                    }, _callback);
                },
                BorderWidth: function (id, startWidth, endWidth, millisec, _callback) {
                    ZiLeex.UI.Animations.Animate({
                        speed: millisec,
                        start: startWidth,
                        end: endWidth,
                        func: function (i) { ZiLeex.UI.Change.BorderWidth(id, i); }
                    }, _callback);
                }

            },
            SwapDisplay: function (id) {
                var o = ZiLeex.$(id).style;
                if (o.display != 'none') {
                    o.display = 'none';
                } else {
                    o.display = 'block';
                }
            },
            SwapVisibility: function (id) {
                var o = ZiLeex.$(id).style;
                if (o.visibility != 'hidden') {
                    o.visibility = 'hidden';
                } else {
                    o.visibility = 'visible';
                }
            },
            CenterDiv: function (id, width, height) {
                var o = ZiLeex.$(id).style;
                var uh = ZiLeex.Page.Height();
                var uw = ZiLeex.Page.Width();
                var dh = height;
                var dw = width;
                o.top = (uh / 2 - dh / 2) + "px";
                o.left = (uw / 2 - dw / 2) + "px";
                o.position = "fixed";
            },
            ScrollPage: {
                To: function (x, y) {
                    window.moveTo(x, y);
                },
                Top: function () {
                    window.moveTo(0, 0);
                }
            }
        }
    }
});

var MessageBox = {
    CssClass: null,
    Show: function (msg) {
        var newID = "ZiLeex_msgbox_div";
        var bgID = "ZiLeex_msgbox_divBg";
        var msgBox = document.createElement("div");
        var textArea = document.createElement("div");
        msgBox.setAttribute("id", newID);
        if (MessageBox.CssClass === null) {
            msgBox.style.width = MessageBox.Setting.MessageWidth;
            msgBox.style.height = "150px";
            msgBox.style.paddingTop = "20px";
            msgBox.style.textAlign = "center";
            msgBox.style.top = "30%";
            msgBox.style.left = "38%";
            msgBox.style.right = "40%";
            msgBox.style.margin = "auto";
            msgBox.style.position = "fixed";
            msgBox.style.backgroundColor = "#ffffff";
            msgBox.style.border = "1px solid #333333";
            msgBox.style.zIndex = (MessageBox.Setting.ZIndex + 5).toString();
        } else {
            msgBox.className = MessageBox.CssClass;
        }
        textArea.style.width = MessageBox.Setting.TextAreaWidth;
        textArea.style.height = "100px";
        textArea.style.textAlign = "center";
        textArea.style.overflow = "auto";
        textArea.innerHTML = msg;
        msgBox.appendChild(textArea);
        var button = document.createElement("input");
        button.setAttribute("type", "button");
        if (MessageBox.CssClass === null) {
            button.style.width = "80px";
            button.style.border = "1px solid #dedede";
            button.style.borderTop = "1px solid #eee";
            button.style.borderLeft = "1px solid #eee";
            button.style.backgroundColor = "#f5f5f5";
            button.style.cursor = "pointer";
            button.style.cursor = "hand";
        }
        button.value = "OK";
        button.onclick = function () { MessageBox.Hide(newID, bgID); };
        msgBox.appendChild(button);
        var bg = document.createElement("div");
        bg.setAttribute("id", bgID);

        bg.style.backgroundColor = MessageBox.Setting.BackgroundColor;

        if (ZiLeex.Browser.IsIE6()) {
            /* IE6 Best Effort */
            msgBox.style.position = "absolute";
            bg.style.position = "absolute";
            var _documentScrollTop = document.documentElement.scrollTop;
            var _bodyH = document.body.clientHeight;
            var _documentH = document.documentElement.clientHeight;
            if (_documentH > _bodyH) {
                bg.style.height = _documentH + "px";
            } else {
                bg.style.height = _bodyH + "px";
            }
            bg.style.width = document.documentElement.clientWidth + "px";
            msgBox.style.top = 100 + _documentScrollTop + "px";
        } else {
            bg.style.position = "fixed";
            bg.style.height = ZiLeex.Page.Height() + "px";
            bg.style.width = ZiLeex.Page.Width() + "px";
        }
        bg.style.top = "0px";
        bg.style.left = "0px";
        bg.style.zIndex = MessageBox.Setting.ZIndex.toString();
        bg.style.filter = "alpha(opacity=70)";
        bg.style.opacity = "0.7";
        document.body.appendChild(bg);
        document.body.appendChild(msgBox);
    },
    Hide: function (id, bgID) {
        var toRemove = ZiLeex.$(id);
        var toRemoveBg = ZiLeex.$(bgID);
        if (toRemove !== null) {
            document.body.removeChild(toRemove);
        }
        if (toRemoveBg !== null) {
            document.body.removeChild(toRemoveBg);
        }
    },
    Setting: {
        ButtonWidth: "80px",
        BackgroundColor: "#666666",
        ZIndex: 200,
        MessageWidth: "300px",
        TextAreaWidth: "280px"
    }
};

var ModalWindow = {
    Show: function (mainID, bgID) {
        ZiLeex.$(mainID).style.display = 'block';
        ZiLeex.$(bgID).style.display = 'block';
    },
    Hide: function (mainID, bgID) {
        ZiLeex.$(mainID).style.display = 'none';
        ZiLeex.$(bgID).style.display = 'none';
    }
};

//*******************
//Extend ZiLeex.Fn
//*******************
    ZiLeex.Fn.prototype.Toggle = function (visibility) {
        visibility = ((typeof (visibility) == 'undefined') ? false : true);
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            if (visibility) {
                ZiLeex.UI.SwapVisibility(ZiLeex.FnElements[i]);
            } else {
                ZiLeex.UI.SwapDisplay(ZiLeex.FnElements[i]);
            } return this;
        }
    };
    ZiLeex.Fn.prototype.Fade = function (startOpacity, endOpacity, speed) {
        if (!ZiLeex.Utilities.IsDefined(speed)) {
            speed = 500;
        }
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Fade(ZiLeex.FnElements[i], startOpacity, endOpacity, speed);
        }
        return this;
    };
    ZiLeex.Fn.prototype.FadeIn = function (speed) {
        if (!ZiLeex.Utilities.IsDefined(speed)) {
            speed = 500;
        }
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Fade(ZiLeex.FnElements[i], 0, 100, speed);
        }
        return this;
    };
    ZiLeex.Fn.prototype.FadeOut = function (speed) {
        if (!ZiLeex.Utilities.IsDefined(speed)) {
            speed = 500;
        }
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Fade(ZiLeex.FnElements[i], 100, 0, speed);
        }
        return this;
    };
    ZiLeex.Fn.prototype.Border = function (style, width, color) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.BorderColor(ZiLeex.FnElements[i], color);
            ZiLeex.UI.Change.BorderStyle(ZiLeex.FnElements[i], style);
            ZiLeex.UI.Change.BorderWidth(ZiLeex.FnElements[i], width);
        }
        return this;
    };
    ZiLeex.Fn.prototype.Width = function (newWidth) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.Width(ZiLeex.FnElements[i], newWidth);
        }
        return this;
    };
    ZiLeex.Fn.prototype.Height = function (newHeight) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.Height(ZiLeex.FnElements[i], newHeight);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateBorderWidth = function (startWidth, endWidth, speed) {
        if (!ZiLeex.Utilities.IsDefined(speed)) {
            speed = 1500;
        }
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Resize.BorderWidth(ZiLeex.FnElements[i], startWidth, endWidth, speed);
        }
        return this;
    };
    ZiLeex.Fn.prototype.ResizeWidth = function (startSize, endSize, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Resize.Width(ZiLeex.FnElements[i], startSize, endSize, speed, _callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.ResizeHeight = function (startSize, endSize, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Resize.Height(ZiLeex.FnElements[i], startSize, endSize, speed, _callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.ResizeWidthTo = function (endSize, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            var w = parseInt(ZiLeex.$(ZiLeex.FnElements[i]).style.width, 10);
            if (!ZiLeex.Utilities.IsDefined(speed)) {
                speed = 500;
            }
            ZiLeex.UI.Animations.Resize.Width(ZiLeex.FnElements[i], w, endSize, speed, _callback);
        }
        return this;
    };

    ZiLeex.Fn.prototype.ResizeHeightTo = function (endSize, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            var h = parseInt(ZiLeex.$(ZiLeex.FnElements[i]).style.height, 10);
            if (!ZiLeex.Utilities.IsDefined(speed)) {
                speed = 500;
            }

            ZiLeex.UI.Animations.Resize.Height(ZiLeex.FnElements[i], h, endSize, speed, _callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateLeft = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Left(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateTop = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
         ZiLeex.UI.Animations.Reposition.Top(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateBottom = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Bottom(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateRight = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Right(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateLeftMargin = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Margin.Left(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateBottomMargin = function (start, end, speed, _callback) {
        ZiLeex.UI.Animations.Reposition.Margin.Bottom(ZiLeex.FnElements[i], start, end, speed,_callback);
        return this;
    };
    ZiLeex.Fn.prototype.AnimateTopMargin = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Margin.Top(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.AnimateRightMargin = function (start, end, speed, _callback) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Animations.Reposition.Margin.Right(ZiLeex.FnElements[i], start, end, speed,_callback);
        }
        return this;
    };
    ZiLeex.Fn.prototype.DisplayOn = function () {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.DisplayOn(ZiLeex.FnElements[i]);
        }
        return this;
    };
    ZiLeex.Fn.prototype.DisplayOff = function () {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.DisplayOff(ZiLeex.FnElements[i]);
        }
        return this;
    };
    ZiLeex.Fn.prototype.Display = function (display) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.Display(ZiLeex.FnElements[i], display);
        }
        return this;
    };
    ZiLeex.Fn.prototype.VisibleOn = function () {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.VisibleOn(ZiLeex.FnElements[i]);
        }
        return this;
    };
    ZiLeex.Fn.prototype.Visibility = function (visibility) {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.Visibility(ZiLeex.FnElements[i], visibility);
        }
        return this;
    };
    ZiLeex.Fn.prototype.VisibleOff = function () {
        for (var i = 0; i < ZiLeex.FnElements.length; i++) {
            ZiLeex.UI.Change.VisibleOff(ZiLeex.FnElements[i]);
        }
        return this;
    };
  
 
