﻿/**
* jQuery.ScrollTo - Easy element scrolling using jQuery.
* Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Dual licensed under MIT and GPL.
* Date: 5/25/2009
* @author Ariel Flesler
* @version 1.4.2
*
* http://flesler.blogspot.com/2007/10/jqueryscrollto.html
*/
(function(d) { var k = d.scrollTo = function(a, i, e) { d(window).scrollTo(a, i, e) }; k.defaults = { axis: 'xy', duration: parseFloat(d.fn.jquery) >= 1.3 ? 0 : 1 }; k.window = function(a) { return d(window)._scrollable() }; d.fn._scrollable = function() { return this.map(function() { var a = this, i = !a.nodeName || d.inArray(a.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) != -1; if (!i) return a; var e = (a.contentWindow || a).document || a.ownerDocument || a; return d.browser.safari || e.compatMode == 'BackCompat' ? e.body : e.documentElement }) }; d.fn.scrollTo = function(n, j, b) { if (typeof j == 'object') { b = j; j = 0 } if (typeof b == 'function') b = { onAfter: b }; if (n == 'max') n = 9e9; b = d.extend({}, k.defaults, b); j = j || b.speed || b.duration; b.queue = b.queue && b.axis.length > 1; if (b.queue) j /= 2; b.offset = p(b.offset); b.over = p(b.over); return this._scrollable().each(function() { var q = this, r = d(q), f = n, s, g = {}, u = r.is('html,body'); switch (typeof f) { case 'number': case 'string': if (/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)) { f = p(f); break } f = d(f, this); case 'object': if (f.is || f.style) s = (f = d(f)).offset() } d.each(b.axis.split(''), function(a, i) { var e = i == 'x' ? 'Left' : 'Top', h = e.toLowerCase(), c = 'scroll' + e, l = q[c], m = k.max(q, i); if (s) { g[c] = s[h] + (u ? 0 : l - r.offset()[h]); if (b.margin) { g[c] -= parseInt(f.css('margin' + e)) || 0; g[c] -= parseInt(f.css('border' + e + 'Width')) || 0 } g[c] += b.offset[h] || 0; if (b.over[h]) g[c] += f[i == 'x' ? 'width' : 'height']() * b.over[h] } else { var o = f[h]; g[c] = o.slice && o.slice(-1) == '%' ? parseFloat(o) / 100 * m : o } if (/^\d+$/.test(g[c])) g[c] = g[c] <= 0 ? 0 : Math.min(g[c], m); if (!a && b.queue) { if (l != g[c]) t(b.onAfterFirst); delete g[c] } }); t(b.onAfter); function t(a) { r.animate(g, j, b.easing, a && function() { a.call(this, n, b) }) } }).end() }; k.max = function(a, i) { var e = i == 'x' ? 'Width' : 'Height', h = 'scroll' + e; if (!d(a).is('html,body')) return a[h] - d(a)[e.toLowerCase()](); var c = 'client' + e, l = a.ownerDocument.documentElement, m = a.ownerDocument.body; return Math.max(l[h], m[h]) - Math.min(l[c], m[c]) }; function p(a) { return typeof a == 'object' ? a : { top: a, left: a} } })(jQuery);

function wire(name) {
    var closeOnBack = function() { if (location.hash == '#' || location.hash == '') $find(name).hide(); };    
    var popupOnOpen = function() { location.hash = 'pop'; $(window).hashchange(closeOnBack); };
    var popupOnClosed = function() { location.hash = ''; $(window).unbind("hashchange", closeOnBack); };

    var popup = $find(name);
    if (popup) {
        popup.add_shown(popupOnOpen);
        popup.add_hiding(popupOnClosed);
    }
}

function pageLoad() {
    $(".hover").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });
    wire('modVerse');
    $('.fonter').initFonter('#popFont', '.cboxOverlay');
    $('a.cb').colorbox({ photo: true, rel: 'group1' });
    $('textarea[maxlength]').initMaxLength();
    $('.upload').initUploader();
}

function cb_close() {
    $.fn.colorbox.close();
    return false;
}

this.imagePreview = function() {

    xOffset = 10;
    yOffset = -225;

    $("a.preview").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='" + this.rel + "' alt='Image preview' />" + c + "</p>");
        var left = (e.pageX > ($(document).width() / 2));
        $("#preview")
			.css("top", (e.pageY + yOffset) + "px")
			.css(left ? "left" : "right", "")
			.css(!left ? "left" : "right", left ? $(document).width() - e.pageX + xOffset : e.pageX + xOffset + "px")
			.fadeIn("fast");
    },
	function() {
	    this.title = this.t;
	    $("#preview").remove();
	});
    $("a.preview").mousemove(function(e) {
        var left = (e.pageX > ($(document).width() / 2));
        $("#preview")
			.css("top", (e.pageY + yOffset) + "px")
			.css(left ? "left" : "right", "")
			.css(!left ? "left" : "right", left ? $(document).width() - e.pageX + xOffset : e.pageX + xOffset + "px");
    });
};

$(document).ready(function() {
    imagePreview();
});

jQuery.fn.log = function(msg) {
    (window.console && console.log) && console.log("%s: %o", msg, this);
    return this;
};

jQuery.fn.initMenu = function() {
    return this.each(function() {
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if ($(parent).hasClass('noaccordion')) {
                    if (theElement[0] === undefined) {
                        window.location.href = this.href;
                    }
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }
                    });
                    return false;
                }
                else {
                    if (theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if ($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal',
                            function() {
                                $(this).prev().removeClass('active');
                            }
                        );
                            return false;
                        }
                        return false;
                    }
                    if (theElement.hasClass('acitem') && !theElement.is(':visible')) {
                        $('.acitem:visible', parent).first().slideUp('normal', function() {
                            $(this).prev().removeClass('active');
                        });
                        theElement.slideDown('normal', function() {
                            $(this).prev().addClass('active');
                        });
                        return false;
                    }
                }
            }
    );
    });
};

jQuery.fn.initMaxLength = function() {
    var regularKey = function(evt) {
        if (typeof evt.which == "undefined")
            return true;
        if (typeof evt.which == "number" && evt.which > 0)
            return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;
        return false;
    };

    return this.each(function() {
        var tb = $(this);
        var charsLeft = tb.siblings('span.charsleft');
        var remain, maxLen = tb.attr('maxlength');
        var update = function() {
            remain = (maxLen - tb.val().length);
            if (remain < 0) {
                tb.val(tb.val().slice(0, maxLen));
                remain = 0;
            };
            charsLeft.text(remain + ' characters remaining.');
        };
        tb.keydown(function(event) { update(); if (remain <= 0 && regularKey(event)) event.preventDefault(); });
        tb.keyup(function() { update(); });
        update();
    });
};

jQuery.fn.initUploader = function() {
    return this.each(function() {
        $(this).colorbox({ inline: true, href: "#uploader", onOpen: function() {
            var rel = $(this).attr("rel");
            var src = $('#uploader iframe').attr("rel");
            $('#uploader iframe').attr("src", src.assign(rel));
        }
        });
    });
};

jQuery.fn.initFonter = function(popID, maskID) {
    return this.each(function() {
        var trigger = $(this);
        var pop = $(popID);
        var popClose = $('.close', pop);
        var mask = $(maskID || '#mask');

        var closeOnBack = function() { if (location.hash == '#' || location.hash == '') close(); };
        var onOpen = function() { location.hash = 'pop'; $(window).hashchange(closeOnBack); };
        var onClosed = function() { location.hash = ''; $(window).unbind("hashchange", closeOnBack); };

        var resize = function() {
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();
            //Set height and width to mask to fill up the whole screen
            mask.css({ 'width': maskWidth, 'height': maskHeight });

            var winH = $(window).height();
            var winW = $(window).width();
            //Set the popup window to center
            var winTop = winH / 2 - pop.height() / 2;
            var winLeft = winW / 2 - pop.width() / 2;
            pop.css({ 'top': winTop, 'left': winLeft });
        };

        var open = function(e) {
            onOpen();
            e.preventDefault();
            $(window).bind('resize', resize);
            resize();

            mask.show();
            pop.show();
            popClose.one('click', close);
            mask.one('click', close);
            var selected = $('input[type=hidden]', trigger).val();
            var scrollTo = null;

            $('.font', pop).each(function() {
                var val = $('input[type=hidden]', this).val();
                $(this).toggleClass("selected", selected == val);
                if (selected == val) scrollTo = this;
            });

            $('.font', pop).click(function() {
                var val = $('input[type=hidden]', this).val();
                $('input[type=hidden]', trigger).val(val);
                close();
                $('input[type=submit][value="Update Preview"]:first').click();
            });

            if (scrollTo) {
                $('#cboxLoadedContent', pop).scrollTo(scrollTo);
            }
        };

        var close = function(e) {
            if (e) e.preventDefault();
            onClosed();
            $(pop).hide();
            $(mask).hide();
            $(window).unbind('resize', resize);
        };

        trigger.click(open);
    });
};

$(document).ready(function() {
    $('.menu').initMenu();
});

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler() {
    $('#cboxLoadedContent').each(function(a) {
        $('body').data("scrollTop:" + a, $(this).scrollTop());
    });
}

function EndRequestHandler() {
    $('#cboxLoadedContent').each(function(a) {
        $(this).scrollTop($('body').data("scrollTop:" + a));
    });
}

function stripSubDomain(hostname) {
    var s = hostname.split(".");
    var o = "";
    var i = s.length-1;
    for (var j = 0; j < 2; j++) {
        if (i >= 0) {
            if (o == "")
                o = s[i--];
            else
                o = s[i--] + "." + o;
        }
    }
    return o;
}
