if (typeof (window.Dotfusion) == "undefined")
    window.Dotfusion = {};

Dotfusion.KEYS = {
    UNKNOWN: -1,
    BACKSPACE: 8,
    TAB: 9,
    ENTER: 13,
    SHIFT: 16,
    CTRL: 17,
    ALT: 18,
    PAUSE_BREAK: 19,
    CAPS_LOCK: 20,
    ESCAPE: 27,
    SPACE: 32,
    PAGE_UP: 33,
    PAGE_DOWN: 34,
    END: 35,
    HOME: 36,
    LEFT_ARROW: 37,
    UP_ARROW: 38,
    RIGHT_ARROW: 39,
    DOWN_ARROW: 40,
    INSERT: 45,
    DELETE: 46,
    NUM_0: 48,
    NUM_1: 49,
    NUM_2: 50,
    NUM_3: 51,
    NUM_4: 52,
    NUM_5: 53,
    NUM_6: 54,
    NUM_7: 55,
    NUM_8: 56,
    NUM_9: 57,
    A: 65,
    B: 66,
    C: 67,
    D: 68,
    E: 69,
    F: 70,
    G: 71,
    H: 72,
    I: 73,
    J: 74,
    K: 75,
    L: 76,
    M: 77,
    N: 78,
    O: 79,
    P: 80,
    Q: 81,
    R: 82,
    S: 83,
    T: 84,
    U: 85,
    V: 86,
    W: 87,
    X: 88,
    Y: 89,
    Z: 90,
    NUMPAD_0: 96,
    NUMPAD_1: 97,
    NUMPAD_2: 98,
    NUMPAD_3: 99,
    NUMPAD_4: 100,
    NUMPAD_5: 101,
    NUMPAD_6: 102,
    NUMPAD_7: 103,
    NUMPAD_8: 104,
    NUMPAD_9: 105,
    NUMPAD_MULTIPLY: 106,
    NUMPAD_ADD: 107,
    NUMPAD_SUBTRACT: 109,
    NUMPAD_DECIMAL: 110,
    NUMPAD_DIVIDE: 111,
    F1: 112,
    F2: 113,
    F3: 114,
    F4: 115,
    F5: 116,
    F6: 117,
    F7: 118,
    F8: 119,
    F9: 120,
    F10: 121,
    F11: 122,
    F12: 123,
    NUM_LOCK: 144,
    SCROLL_LOCK: 145,
    SEMI_COLON: 186,
    EQUALS: 187,
    COMMA: 188,
    DASH: 189,
    PERIOD: 190,
    FORWARD_SLASH: 191,
    GRAVE: 192,
    OPEN_BRACKET: 219,
    BACK_SLASH: 220,
    CLOSE_BRACKET: 221,
    SINGLE_QUOTE: 222
};

Dotfusion.MOUSE_BUTTONS = {
    UNKNOWN: 0,
    LEFT: 1,
    RIGHT: 3,
    MIDDLE: 2
};

(function ($) {

    $.trackPageview = function(url) {

        if (window._gaq) {

            window._gaq.push(['_trackPageview', url]);

        }

    };

    $.hashQuery = function (obj, value) {

        var createHashQuery = function () {

            var hashQuery = {};
            var hash = window.location.hash.substring(1);

            if (hash.indexOf("!") == 0) {

                var nextIdx = hash.indexOf("&");
                hashQuery.ajaxUrl = nextIdx >= 0 ? hash.substring(1, nextIdx) : hash.substring(1);

                hash = nextIdx >= 0 ? hash.substring(nextIdx + 1) : hash;

            }

            var hashes = hash.split("&");

            for (var i = 0; i < hashes.length; i++) {

                var hash = hashes[i].split("=");

                if (hash[0] && hash[1])
                    hashQuery[hash[0]] = hash[1] ? hash[1] : true;

            }

            return hashQuery;

        };

        if (obj) {

            var currentQuery = createHashQuery();

            currentQuery[obj] = value;

            var newHash = "#!";

            if (currentQuery.ajaxUrl)
                newHash += currentQuery.ajaxUrl;

            for (var p in currentQuery) {

                if (p != "ajaxUrl")
                    newHash += "&" + p + "=" + currentQuery[p];

            }

            window.location.hash = newHash;

        }

        return createHashQuery();

    };

    $.clearHashQuery = function () {

        window.location.hash = "#!";

        return $.hashQuery();

    };

    $.pageTitle = function(title) {

        document.title = $("body").data("base-title") + " \u203A " + title;

    };

    $.fn.loadImage = function(callback) {

        this.filter(".iph").each(function() {

            var $iph = $(this);

            if (!$iph.is(".loaded")) {

                if ($iph.data("src")) {

                    if ($iph.find("img[src='" + $iph.data("src") + "']").length) {

                        $iph.addClass("loaded clear");

                        if (callback)
                            callback();

                    } else {

                        $iph.addClass("loading");
                
                        if ($iph.children("a").length)
                            $iph.children("a").first().append("<img />");
                        else
                            $iph.append("<img />");

                        var $img = $("img", $iph);

                        $img.css("visibility", "hidden");

                        $img.one("load", function () {

                            //$img.attr("width", $img[0].width ? $img[0].width : "");
                            //$img.attr("height", $img[0].height ? $img[0].height : "");

                            $img.css("-webkit-transform", "translate3d(0,0,0)").css({

                                opacity: 0,
                                visibility: ""

                            }).stop(true).animate({

                                opacity: 1

                            }, {

                                duration: 350,
                                complete: function () {

                                    $img.css("opacity", "").css("-webkit-transform", "");
                                    $iph.removeClass("loading").addClass("clear");

                                }

                            });

                            $iph.addClass("loaded");

                            if (callback)
                                callback();

                        });

                        $img.attr("src", $iph.data("src")/* + "?" + new Date().getTime()*/);

                    }

                } else {

                    $iph.addClass("loaded empty");

                }

            }

        });

    };

    $.fn.loadImages = function (callback) {

        this.find(".iph[data-src]").loadImage(callback);

    };

    $.fn.panTo = function (addressIdx) {

        return this.filter(".google-map").each(function () {

            var $this = $(this);

            var domMap = $this[0];

            if (domMap._addresses[addressIdx]) {

                domMap._map.panTo(domMap._addresses[addressIdx]);
                domMap._map.setZoom(15);

            }

        });

    };

    $.fn.googleMap = function (options, markerOptions, addresses) {

        options = $.extend({}, options);
        markerOptions = $.extend({}, markerOptions);

        return this.filter(".google-map").each(function () {

            var $this = $(this);

            var domMap = $this[0];

            domMap._addresses = [];

            domMap._map = new google.maps.Map(domMap, options);

            var getAndSetAddress = function (idx, address) {

                $.getJSON("/proxy.h?_url=" + encodeURIComponent("http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true"), function (data) {

                    var results = data.results;
                    var point = new google.maps.LatLng(results[0].geometry.location.lat, results[0].geometry.location.lng);
                    var options = $.extend({

                        map: domMap._map,
                        position: point

                    }, markerOptions);

                    domMap._addresses[idx] = point;

                    var marker = new google.maps.Marker(options);

                    google.maps.event.addListener(marker, 'click', function () {

                        $this.panTo(idx);

                    });

                });

            };

            for (var i = 0; i < addresses.length; i++) {

                getAndSetAddress(i, addresses[i]);

            }

        });

    };

    $.fn.inner = function () {

        return this.children(".inner");

    };

    $(document).bind("mousedown touchstart", function (evt) {
        
        var $target = $(evt.target);
        var $this = $target.closest(".drag-container");

        if ($this.length && ((evt.type == "touchstart" /*&& !$target.is(".default-touch")*/) || (evt.type == "mousedown" && evt.which == Dotfusion.MOUSE_BUTTONS.LEFT))) {

            evt.preventDefault();

            $this[0]._drag = {

                target: $target[0],
                isDragging: false,
                isMouseDown: true,
                startX: evt.type == "touchstart" ? evt.originalEvent.touches[0].pageX : evt.pageX,
                startY: evt.type == "touchstart" ? evt.originalEvent.touches[0].pageY : evt.pageY,
                offsetX: 0,
                offsetY: 0

            };

            $this.addClass("dragready");

        }

        //if (evt.type == "touchstart" && !$target.is(".default-touch"))
        //    $target.removeClass(".default-touch");

    }).bind("mousemove touchmove", function (evt) {

        var $this = $(".drag-container").filter(".dragready, .dragging");

        if ($this.length) {

            var drag = $this[0]._drag;

            if (drag && drag.isMouseDown) {

                evt.preventDefault();

                drag.offsetX = (evt.type == "touchmove" ? evt.originalEvent.touches[0].pageX : evt.pageX) - drag.startX;
                drag.offsetY = (evt.type == "touchmove" ? evt.originalEvent.touches[0].pageY : evt.pageY) - drag.startY;

                if (!drag.isDragging && (Math.abs(drag.offsetX) > 5 || Math.abs(drag.offsetY) > 5)) {

                    drag.isDragging = true;

                    evt = $.extend(evt, {

                        type: "dragstart",
                        drag: drag

                    });

                    $this.trigger(evt);

                }

                if (drag.isDragging) {

                    evt = $.extend(evt, {

                        type: "dragmove",
                        drag: drag

                    });

                    $this.removeClass("dragready").addClass("dragging").trigger(evt);

                }

                $this[0]._drag = drag;

            }

        }

    }).bind("mouseup touchend", function (evt) {

        if (evt.type == "touchend" || (evt.type == "mouseup" && evt.which == Dotfusion.MOUSE_BUTTONS.LEFT)) {

            $(".drag-container").filter(".dragready, .dragging").each(function() {

                var $this = $(this);
                var drag = $this[0]._drag;

                if (drag && drag.isMouseDown && drag.isDragging) {

                    drag.offsetX = (evt.type == "touchmove" ? evt.originalEvent.touches[0].pageX : evt.pageX) - drag.startX;
                    drag.offsetY = (evt.type == "touchmove" ? evt.originalEvent.touches[0].pageY : evt.pageY) - drag.startY;

                    evt = $.extend(evt, {

                        type: "dragend",
                        drag: drag

                    });

                    $this.trigger(evt);

                } else if (evt.type == "touchend" && drag && drag.target && !drag.isDragging/* && !$(drag.target).is(".default-touch")*/) {

                    $(drag.target).addClass("default-touch").click();

                }/* else if (evt.type == "touchend" && drag && drag.target && !drag.isDragging && $(drag.target).is(".default-touch")) {

                    $(drag.target).removeClass("default-touch");

                }*/

                if (drag) {

                    drag.isMouseDown = false;
                    drag.isDragging = false;
                    drag.target = null;
                    $this.removeClass("dragready dragging");
                    
                }

                $this[0]._drag = drag;

            });

        }

    });

})(jQuery);
