﻿$(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").datepicker({ 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");
        }
        return false;
    });

    $(".updateLink").click(function() {
        if (!opened) {
            $("#updateForm").slideDown().x($(this).x() - 100)
            opened = !opened;
        }
        else {
            $("body").trigger("close");
        }
        return false;
    });

    $(".caietLink").click(function() {
        if (!opened) {
            $("#caietForm").slideDown().x($(this).x() - 100)
            opened = !opened;
        }
        else {
            $("body").trigger("close");
        }
        return false;
    });
    $("body").bind("close", function() {
        $("#updateForm").slideUp();
        $("#caietForm").slideUp();
        $("#feedbackForm").slideUp();
        opened = !opened;
    });

    //butonul de inchis floaturile
    $(".closeFloatDiv").click(
        function() {
            $("body").trigger("close");
            return false;
        }
    );

    //$(".tabsCaracteristici").tabs();

    //print project page
    $("#printLink").click(function() {
        window.print();
        return false;
    });

    $.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

    $("#pingboxContainer").expose({
        onBeforeLoad: function() {
            this.getExposed().wait(1000, "fx").animate({ top: "50px" }, 1000)
        },
        onBeforeClose: function() {
            if ($.cookies.test()) {
                $.cookies.set("hide-messenger", "1");
            }
            this.getExposed().animate({ top: "-500px" });
        }
    });
    if ($.cookies.get("hide-messenger") != 1) {
        $("#pingboxContainer").expose().load();
    }

    $("#messengerStatus").click(function() {
        $("#pingboxContainer").expose().load();
        return false;
    });
    $("a.minimize").click(function() {
        $("#messengerContainer").hide();
        $("#contactBar").show();
        if ($.cookies.test()) {
            $.cookies.set("hide-contact", "1");
        }
        return false;
    });

    $("a#contactBar").click(function() {
        $(this).hide();
        $("#messengerContainer").show();
        if ($.cookies.test()) {
            $.cookies.set("hide-contact", "0");
        }
        return false;
    });
    if ($.cookies.get("hide-contact") == 1) {
        $("a.minimize").trigger("click");
    }
})
