﻿$(document).ready(function() {
    $.fn.x = function(n) {
        var result = null;
        this.each(function() {
            var o = this;
            if (n === undefined) {
                var x = 0;
                if (o.offsetParent) {
                    while (o.offsetParent) {
                        x += o.offsetLeft;
                        o = o.offsetParent;
                    }
                }
                if (result === null) {
                    result = x;
                } else {
                    result = Math.min(result, x);
                }
            } else {
                o.style.left = n + 'px';
            }
        });
        return result;
    };

    $.fn.y = function(n) {
        var result = null;
        this.each(function() {
            var o = this;
            if (n === undefined) {
                var y = 0;
                if (o.offsetParent) {
                    while (o.offsetParent) {
                        y += o.offsetTop;
                        o = o.offsetParent;
                    }
                }
                if (result === null) {
                    result = y;
                } else {
                    result = Math.min(result, y);
                }
            } else {
                o.style.top = n + 'px';
            }
        });
        return result;
    };

    //functie generala pentru calendar
    $(".calendar").removeClass('hasDatepicker').datepicker({ dateFormat: 'dd.mm.yy' });
    $(".calendarMinToday").removeClass('hasDatepicker').datepicker({ minDate: 1, dateFormat: 'dd.mm.yy' });


    //pagina de proiecte
    //floatFormurile

    //variabila prin care verificam daca e deschis
    var opened = false;
    $(".feedbackLink").click(function() {
        if (!opened) {
            $("#feedbackForm").slideDown().x($(this).x() - 100);
            opened = !opened;
        }
        else {
            $("body").trigger("close");
            $(this).trigger("click");
        }
        return false;
    });

    $(".updateLink").click(function() {
        if (!opened) {
            $("#updateForm").slideDown().x($(this).x() - 100)
            opened = !opened;
        }
        else {
            $("body").trigger("close");
            $(this).trigger("click");
        }
        return false;
    });

    $(".caietLink").click(function() {
        if (!opened) {
            $("#caietForm").slideDown().x($(this).x() - 100)
            opened = !opened;
        }
        else {
            $("body").trigger("close");
            $(this).trigger("click");
        }
        return false;
    });

    $(".alertLink").click(function() {
        if (!opened) {
            $("#alertForm").slideDown().x($(this).x() - 100);
            opened = !opened;
        }
        else {
            $("body").trigger("close");
            $(this).trigger("click");
        }
        return false;
    });


    $("body").bind("close", function() {
        $("#updateForm").slideUp();
        $("#caietForm").slideUp();
        $("#feedbackForm").slideUp();
        $("#alertForm").slideUp();
        opened = !opened;
    });

    //butonul de inchis floaturile
    $(".closeFloatDiv").click(
        function() {
            $("body").trigger("close");
            return false;
        }
    );

    //print project page
    $("#printLink").click(function() {
        window.print();
        return false;
    });

    $.fn.qtip.styles.bGreenTooltip = {
        border: {
            width: 1,
            radius: 5,
            color: '#9AD81B'
        },
        background: 'white',
        padding: 3,
        textAlign: 'center',
        color: '#575757'
    };


    $('a[title].tip').qtip({
        position: {
            corner: {
                tooltip: 'topMiddle', // Use the corner...
                target: 'bottomMiddle' // ...and opposite corner
            }
        },
        style: { name: 'bGreenTooltip', tip: 'topMiddle' }
    });

    $('.bTip').qtip({
        position: {
            corner: {
                tooltip: 'leftTop', // Use the corner...
                target: 'rightMiddle' // ...and opposite corner
            }
        },
        content: { text: $(".betaDiv").text() },
        style: { name: 'bGreenTooltip', tip: 'topLeft' }
    });

    $('td.withTip label').each(function() {
        $this = $(this);
        $text = $this.find("span").text();
        $this.qtip({
            position: {
                corner: {
                    tooltip: 'leftTop', // Use the corner...
                    target: 'rightMiddle' // ...and opposite corner
                }
            },
            content: { text: $text },
            style: { name: 'bGreenTooltip', tip: 'topLeft' }
        });
    });

    $(".modal").click(function() {
        $("#ajaxModal").remove();
        $("body").append("<div id='ajaxModal'></div>");
        $model = $("#ajaxModal");
        $source = $(this).attr("href");
        $content = "";
        $.ajax({
            url: $source,
            cache: false,
            success: function(html) {
                $content = html;
                $model.html($content);
                $model.dialog({
                    modal: true,
                    width: 820,
                    height: 600
                });
            }
        })
        return false;
    })

});            //jQuery end     