﻿/// <reference path="jquery.js"/>

jQuery.fn.extend({
    notEmpty: function(callback, removeEmpty)
    {
        return this.each(function()
        {
            var hasChildren = false;
            jQuery.each(this.childNodes, function()
            {
                if (this.tagName)
                {
                    hasChildren = true;
                    return false;
                }
            });
            if (hasChildren)
                callback.call(this);
            else if (removeEmpty)
                jQuery(this).remove();
        });
    },


    edge: function(node, edge, offset)
    {
        offset = offset || 0;
        return this.each(function()
        {
            var postion = jQuery(node).position();
            switch (edge)
            {
                case 'left':
                    jQuery(this)
                        .css('left', postion.left - jQuery(this).outerWidth() - offset);
                    break;
                default:
                    jQuery(this)
                        .css('left', postion.left + jQuery(node).outerWidth() + offset);
                    break;
            }
        });
    },

    align: function(node, direction, offset)
    {
        offset = offset || 0;
        return this.each(function()
        {
            var postion = jQuery(node).position();
            switch (direction)
            {
                case 'left':
                    jQuery(this)
                        .css('left', postion.left);
                    break;
                case 'right':
                    jQuery(this)
                        .css('left', postion.left + jQuery(node).outerWidth() - jQuery(this).outerWidth());
                    break;
                case 'bottom':
                    jQuery(this)
                        .css('bottom', postion.top + jQuery(node).outerHeight() - jQuery(this).outerHeight());
                    break;
                default:
                    jQuery(this)
                        .css('top', postion.top);
                    break;
            }
        });
    },


    dropdown: function(edge, direction, offset)
    {
        if (isNaN(offset)) offset = 1;
        return this.each(function()
        {
            if (this.rel)
            {
                var a = this;
                var hasChildren = false;
                jQuery('#' + this.rel).notEmpty(function()
                {
                    hasChildren = true;
                    jQuery(this).hover(function()
                    {
                        clearTimeout(a.__popupTimer);
                    },
                    function()
                    {
                        a.__popupTimer = setTimeout(function()
                        {
                            jQuery('#' + a.rel)
                                .fadeOut('fast', function() { jQuery(this).removeClass('visible'); });
                        }, 200);
                    });
                }, true);
                if (hasChildren)
                {
                    jQuery(a).addClass('parent');
                    //a.href = 'javascript:;';
                }
            }
        }).hover(function()
        {
            if (this.rel)
            {
                clearTimeout(this.__popupTimer);
                jQuery('#' + this.rel)
                    .addClass('visible')
                    .edge(this, edge, offset)
                    .align(this, direction)
                    .hide()
                    .fadeIn('fast');
            }
        }, function()
        {
            if (this.rel)
            {
                var a = this;
                this.__popupTimer = setTimeout(function()
                {
                    jQuery('#' + a.rel)
                        .fadeOut('fast', function() { jQuery(this).removeClass('visible'); });
                }, 200);
            }
        });
    },

    defaultText: function(focus)
    {
        return this.each(function()
        {
            if (this.__defaultText != undefined) return;
            this.__defaultText = this.value;
            jQuery(this).focus(function()
            {
                if (this.value == this.__defaultText) this.value = '';
                jQuery(this).removeClass('default-text');
            }).blur(function()
            {
                if (this.value == '')
                {
                    this.value = this.__defaultText;
                    jQuery(this).addClass('default-text');
                }
                else
                {
                    jQuery(this).removeClass('default-text');
                }
            });
            if (focus && this.value == this.__defaultText)
            {
                jQuery(this).removeClass('default-text');
                this.value = '';
            }
            else
            {
                jQuery(this).addClass('default-text');
            }
        });
    },

    marquee: function(direction, speed, delay)
    {
        this.each(function()
        {
            var tag = this;
            if (tag.__marquee)
            {
                var marquee = tag.__marquee;
                if (!direction) return;
                direction = direction.toString().toLowerCase();
                switch (direction)
                {
                    case 'pause': marquee.pauseCommand = true; break;
                    case 'play': marquee.pauseCommand = false; break;
                    case 'speed': marquee.speed = speed; break;
                    case 'delay': marquee.delay = speed; break;
                    case 'left':
                    case 'right':
                    case 'up':
                    case 'down':
                        if (marquee.direction == direction) return;
                        if ((direction == 'left' || direction == 'right') && marquee.vertical) return;
                        var ratio = Math.ceil(tag.parentNode[marquee.vertical ? 'clientHeight' : 'clientWidth'] / marquee.size);
                        if (ratio < 1) ratio = 1;
                        var maxSize = ratio * marquee.size;
                        switch (direction)
                        {
                            case 'left': tag.parentNode.scrollLeft = tag.parentNode.scrollLeft - maxSize; break;
                            case 'right': tag.parentNode.scrollLeft = maxSize + (tag.parentNode.scrollLeft - maxSize); break;
                            case 'up': tag.parentNode.scrollTop = tag.parentNode.scrollTop - maxSize; break;
                            case 'down': tag.parentNode.scrollTop = maxSize + (tag.parentNode.scrollTop - maxSize); break;
                        }
                        marquee.direction = direction;
                    default: ;
                }
                return;
            }

            delay = parseInt(delay);
            speed = parseInt(speed);

            if (!delay || delay <= 0) delay = 60;
            if (!speed || speed <= 0) speed = 2;
            direction = (direction || 'left').toLowerCase();
            var marquee = tag.__marquee = {};
            marquee.direction = direction;
            marquee.speed = speed;
            marquee.delay = delay;
            marquee.vertical = marquee.direction == 'up' || marquee.direction == 'down';
            marquee.pause = false;

            marquee.onMouseOver = function() { marquee.pause = true; };
            marquee.onMouseOut = function() { marquee.pause = false; };

            if (tag.attachEvent)
            {
                tag.attachEvent('onmouseover', marquee.onMouseOver, false);
                tag.attachEvent('onmouseout', marquee.onMouseOut, false);
            }
            else
            {
                tag.addEventListener('mouseover', marquee.onMouseOver, false);
                tag.addEventListener('mouseout', marquee.onMouseOut, false);
            }

            var parent = tag.parentNode;
            var empty = false;
            if (marquee.vertical)
            {
                var reverseDirection = 'down';
                var clientProp = 'clientHeight';
                var scrollProp = 'scrollTop';
                marquee.size = tag.offsetHeight;

                if (!tag.rows) return;

                var rows = tag.rows.length;
                if (rows == 0)
                {
                    empty = true;
                }
                else
                {
                    var times = Math.ceil(parent.clientHeight / marquee.size) * 3 - 1;

                    for (var i = 0; i < times; i++)
                    {
                        for (var j = 0; j < rows; j++) tag.tBodies[0].appendChild(tag.rows[j].cloneNode(true));
                    }
                }
            }
            else
            {
                var reverseDirection = 'right';
                var clientProp = 'clientWidth';
                var scrollProp = 'scrollLeft';
                marquee.size = tag.offsetWidth;

                if (!tag.rows || !tag.rows[0]) return;

                var row = tag.rows[0];
                var columns = row.cells.length;
                if (columns == 0)
                {
                    empty = true
                }
                else
                {
                    var times = Math.ceil(screen.availWidth / marquee.size) * 3 - 1;

                    for (var i = 0; i < times; i++)
                    {
                        for (var j = 0; j < columns; j++) row.appendChild(row.cells[j].cloneNode(true));
                    }
                }
            }

            parent[scrollProp] = marquee.direction == reverseDirection ? parent[clientProp] * 2 : 0;
            marquee.handle = function()
            {
                window.clearTimeout(marquee.timer);
                if (!marquee.pause && !marquee.pauseCommand)
                {
                    var ratio = Math.ceil(parent[clientProp] / marquee.size);
                    if (ratio < 1) ratio = 1;
                    var maxSize = ratio * marquee.size;

                    var offset = parent[scrollProp] - maxSize;
                    if (marquee.direction == reverseDirection)
                    {
                        parent[scrollProp] -= marquee.speed;
                        if (parent[scrollProp] <= maxSize)
                        {
                            parent[scrollProp] = maxSize * 2 + offset //- marquee.speed;
                        }
                    }
                    else
                    {
                        parent[scrollProp] += marquee.speed;
                        if (parent[scrollProp] >= maxSize)
                        {
                            parent[scrollProp] = offset + marquee.speed;
                        }
                    }
                }
                marquee.timer = window.setTimeout(arguments.callee, marquee.delay);
            };
            marquee.timer = window.setTimeout(marquee.handle, marquee.delay);
        });
    }
});