/*
    $Rev: 8403 $
    $Date: 2011-11-18 16:40:03 +0400 (??, 18 но? 2011) $

    Copyright (c) 2011 DevAdmin
 */

/* Zoom plugin */
(function($) {

    $.fn.daZoom = function(params) {

        var defaultParameters = new Object();

        defaultParameters = {
            onzoomin: null,
            onzoomout: null
        };

        $.extend(defaultParameters, params);

        if(typeof defaultParameters.onzoomin !== 'function') {
            defaultParameters.onzoomin = function (){};
        }

        if(typeof defaultParameters.onzoomout !== 'function') {
            defaultParameters.onzoomout = function (){};
        }

        return this.each(function() {
            var $this = $(this);

            /* Create events */
            $this.bind('zoomin.devadmin', function(e) {
                defaultParameters.onzoomin(e);
            });

            $this.bind('zoomout.devadmin', function(e) {
                defaultParameters.onzoomout(e);
            });


            /* mouseover event */
            $this.mouseenter(
                function() {

                    $this.trigger('zoomin.devadmin');

                    /* calculating mouse position */
                    var top = $this.offset().top + parseInt(($this.children('.da-zoom-normal').outerHeight() - $this.children('.da-zoom-hover').outerHeight()) / 2);
                    var left = $this.offset().left + parseInt(($this.children('.da-zoom-normal').outerWidth() - $this.children('.da-zoom-hover').outerWidth()) / 2);


                    var poptop = $(window).scrollTop() + $(window).height() - $this.children('.da-zoom-hover').outerHeight() - top;
                    if (poptop < 0) {
                        top = top + poptop;
                    }
                    if (top < $(window).scrollTop()) {
                        top = $(window).scrollTop();
                    }

                    var popleft = $(window).scrollLeft() + $(window).width() - $this.children('.da-zoom-hover').outerHeight() - left;
                    if (popleft < 0) {
                        left = left + popleft;
                    }
                    if (left < $(window).scrollLeft()) {
                        left = $(window).scrollLeft();
                    }

                    /* positioning tolltip element */
                    $this.addClass('da-zoom-enter').children('.da-zoom-hover').offset({top: top, left: left});
                }
            )
            /* hiding tooltip on mouse out */
            .mouseleave(
                function() {
                    $this.trigger('zoomout.devadmin');
                    $this.removeClass('da-zoom-enter');
                }
            )
        });
    }

    /* Tooltip plugin */
    $.fn.daTooltip = function(params) {

        var defaultParameters = new Object();

        defaultParameters = {
            text: '',
            tooltipElement: 'da-tooltip',
            offset_x: 15,
            offset_y: 17
        };

        $.extend(defaultParameters, params);

        if(!defaultParameters.text || typeof defaultParameters.text !== 'string') {
            return false;
        }

        return this.each(function() {
            /* if not exist create tolltip DOM element */
            if(!$('body > #'+defaultParameters.tooltipElement).length) {
                var tooltipElementId = defaultParameters.tooltipElement + '-' + $(this).attr('id');
                $('body').append('<span id="'+ tooltipElementId +'" class="da-tooltip">' + defaultParameters.text + '</span>').find('#'+tooltipElementId).hide();
            /* else using existing */
            } else {
                $('#'+tooltipElementId).html(defaultParameters.text).hide();
            }

            var tooltipElement = $('#'+tooltipElementId);

            /* attach mouseover event */
            $(this).mouseenter(function(e) {
                tooltipElement.fadeIn(100);
            })
            /* attach mouseleave event */
            .mouseleave(function (e) {
                tooltipElement.fadeOut(100);
            })
            /* attach mousemove event */
            .mousemove(function (e) {
                tooltipElement.offset({top: e.pageY + defaultParameters.offset_y, left: e.pageX + defaultParameters.offset_x});
            });
        });

    };

    /* Multi-state button plugin */
    $.fn.daButton = function(params) {

        var defaultParameters = new Object();

        defaultParameters =
        {
            onClick: null,
            state: 1
        };

        $.extend(defaultParameters, params);

        /* button's states */
        var states = {1: 'normal', 2: 'hover', 3: 'click', 4: 'clicked'};
        if(!$.inArray(defaultParameters.state, states)) {
            defaultParameters.state = 1;
        }

        return this.each(function () {

            var state = defaultParameters.state; /* normal */
            var $this = $(this);
            var counter = $this.find('.counter');

            /* set state class */
            if(!$this.hasClass(states[state])) {
                $this.addClass(states[state]);
            }

            /* set counter */
            function setCounter(val) {
                if(counter && counter.text) {
                    counter.text(val);
                }
            }

            var cnt = isNaN(parseInt(counter.text())) ? 0 : parseInt(counter.text());
            setCounter(cnt);

            /* attach mouse over event */
            $this.mouseenter(
                function() {
                    if(state === 1) {
                        state = 2; /* mouse over */
                        $this.removeClass('normal').addClass('hover');
                    }
                }
            )
            /* attach mouse out event */
            .mouseleave(
                function() {
                    if(state === 2) {
                        state = 1;
                        $this.removeClass('hover').addClass('normal');
                    }
                }
            /* attach click event */
            ).click(
                function(){
                    if(state === 2) {
                        state = 3; /* click */
                        $this.removeClass('hover').addClass('click');
                        /* after 0,5 second set state to 'clicked' from 'click' */
                        setTimeout(
                            function() {
                                state = 4; /* clicked */
                                $this.removeClass('click').addClass('clicked');
                                /* call onclick function if it set */
                                if(defaultParameters.onClick && typeof defaultParameters.onClick === 'function') {
                                    if(defaultParameters.onClick()) {
                                        var cnt = isNaN(parseInt(counter.text())) ? 0 : parseInt(counter.text());
                                        /* increase counter */
                                        setCounter(cnt + 1);
                                    }
                                }
                            }, 500
                        );
                    }
                }
            );

        });
    };

    /* Pop-up element plugin */
    $.fn.daPopup = function(params) {

        var defaultParameters = new Object();

        /* default parameters */
        defaultParameters = {
            timeOut: 1000,
            bodyClickHide: true,
            onpopup: null,
            onhide: null,
            exclusive: true
        };

        $.extend(defaultParameters, params);

        if(typeof defaultParameters.onpopup !== 'function') {
            defaultParameters.onpopup = function () {};
        }

        if(typeof defaultParameters.onhide !== 'function') {
            defaultParameters.onhide = function () {};
        }

        return this.each(function() {

            var timer;
            var $this = $(this);

            /* bind onpopup event */
            $this.bind('popup.devadmin', function(e) {
                defaultParameters.onpopup(e);
            });

            /* bind hide event */
            $this.bind('hide.devadmin', function(e) {
                defaultParameters.onhide(e);
            });

            /* function - hide pop-up and call attached onhide-event */
            function hidePopup() {
                if($this.hasClass('active')) {
                    $this.removeClass('active');
                    $this.trigger('hide.devadmin');
                }
            }

            /* function - show pop-up and call attached onpopup-event */
            function showPopup() {
                if(!$this.hasClass('active')) {
                    /* if parameters 'exclusive' is set then hiding all same elements on the page */
                    if(defaultParameters.exclusive) {
                        $(window).trigger('hidePopup.devadmin');
                    }
                    $this.addClass('active');
                    $this.trigger('popup.devadmin');
                }
            }

            /* set timer for close pop-up after timeout */
            function setTimer() {
                timer = window.setTimeout(hidePopup, defaultParameters.timeOut);
            }

            /* unset timer */
            function clearTimer() {
                if(timer) {
                    window.clearTimeout(timer);
                    timer = null;
                }
            }

            /* stop prpagation for all browsers */
            function stopProp(e) {
                if(!e) e = window.event;
                if(e.stopPropagation) {
                    e.stopPropagation();
                } else {
                    e.cancelBubble = true;
                }
            }

            /* attach events listner to the button */
            $this.children('.button')
                .bind('mouseenter click', function(event) {
                    clearTimer();
                    if(!$this.hasClass('active')) {
                        showPopup();
                    }
                    stopProp(event)
                })
                .mouseleave(function() {
                    setTimer();
                });

            /* attach events listner to the  popup */
            $this.children('.popup')
                .mouseenter(function() {
                    clearTimer();
                })
                .mouseleave(function() {
                    if(!$this.find('.popup input[type=text]:focus, .popup textarea:focus').length) {
                        setTimer();
                    }
                })
                .click(function(event) {
                    stopProp(event);
                    event.preventDefault();
                });

            /* if set parameter bodyClickHide then attach event listner to the body */
            if(defaultParameters.bodyClickHide) {
                $(document).click(function() {
                    clearTimer();
                    hidePopup();
                });
            }

            /* if exists close pop-up element then attach event listner to it */
            $this.find('.popup .popup-close').click(function(event){
                hidePopup();
                stopProp(event);
            });

            /* attach event listner to body for hiding all same element on the page */
            $(window).bind('hidePopup.devadmin', function(e) {hidePopup()});


        });


    }

})(jQuery)
