﻿jQuery.fn.extend({
    pictureList: function(keysUrl, itemUrl, listUrl) {
        var pictureList = this;
        var keys;

        function _itemIndex() {
            if (arguments.length > 0) {
                _item(keys[arguments[0]]);
            }
            return keys.indexOf(_item());
        } //itemIndex

        function _page() {
            return parseInt(jQuery.bbq.getState('itemPage') || 1);
        } //page

        function _item() {
            return jQuery.bbq.getState('pictureName');
        } //item

        function _load() {
            var item = _item();
            if (item) _loadItem();
            else _loadPage();
        } //load

        function _loadItem() {
            var item = _item();
            jQuery.ajax({
                async: true
                , cache: false
                , url: itemUrl
                , data: {
                    pictureName: item
                }
                , success: function(data) {
                    jQuery(pictureList).html(getMainContent(data));
                }
            });
        } //loadItem

        function _loadPage() {
            jQuery.ajax({
                async: true
                , cache: false
                , url: listUrl
                , data: {
                    pictureNames: keys
                }
                , success: function(data) {
                    jQuery(pictureList).html(data);
                }
            });
        } //loadPage

        function _init() {
            jQuery('a.Previous', pictureList).live('click', function(e) {
                var newItemIndex =
                    (_itemIndex() + keys.length - 1)
                    % keys.length
                    ;
                jQuery.bbq.pushState({ pictureName: keys[newItemIndex] });
                e.preventDefault();
            })

            jQuery('a.Next', pictureList).live('click', function(e) {
                var newItemIndex =
                    (_itemIndex() + keys.length + 1)
                    % keys.length
                    ;
                jQuery.bbq.pushState({ pictureName: keys[newItemIndex] });
                e.preventDefault();
            });

            jQuery('a.Index', pictureList).live('click', function(e) {
                jQuery.bbq.removeState('pictureName');
                e.preventDefault();
            });

            jQuery(window).bind('hashchange', function(e) {
                _load();
            });

            jQuery.ajax({
                async: true
                , cache: false
                , url: keysUrl
                , success: function(data) {
                    keys = data;
                    _load();
                }
            });
        } //init

        _init();
    } //pictureList
});




function addReturnUrl(url) {
    var returnUrl = /^[a-z]+\:\/\/[^\/]*(.*)$/i.exec(document.location.href)[1];
    return jQuery.param.querystring(url, { ReturnUrl: returnUrl })
}

jQuery('form').live('submit', function(e) {
    var $this = jQuery(this);
    if ($this.valid()) {
        this.action = addReturnUrl(this.action);
    }
    else {
        e.preventDefault();
    }
});



jQuery.fn.extend({
    loadFromHref: function() {
        this.each(function() {
            var $this = jQuery(this);
            jQuery.ajax({
                async: true
                , cache: false
                , url: this.href
                , success: function(data) {
                    $this.replaceWith('<span>' + data + '</span>');
                }
            });
            return this;
        });
    }
})











window._loading = 0;
function loading() {
    if (arguments.length > 0) {
        window._loading = arguments[0];

        if (window._loading > 0) {
            jQuery('body').addClass('ShowLoading');
        }
        else {
            jQuery('body').removeClass('ShowLoading');
        }
    }

    return window._loading;
}



jQuery(document).ajaxSend(function(e, xhr, ajaxOptions) {
    loading(loading() + 1);
});
jQuery(document).ajaxComplete(function(e, xhr, ajaxOptions) {
    loading(loading() - 1);
});


jQuery(document).ajaxError(function(e, xhr, ajaxOptions, thrownError) {
    document.clear();
    document.write(xhr.responseText);
    document.close();
});



function openPopup(url) {
    jQuery('body').addClass('ShowPopup');
    jQuery('#popupContent').html('Laden...');
    jQuery.ajax({
        async: true
        , cache: true
        , url: url
        , type: 'GET'
        , success: function(data) {
            jQuery('#popupContent').html(data);
        }
    });
}

function closePopup() {
    jQuery('body').removeClass('ShowPopup');
    jQuery('#popupContent').empty();
}

function getMainContent(html) {
    var match;

    match = /\<\!\-\-main\-\-\>([\s\S]*)\<\!\-\-\/main\-\-\>/i.exec(html);
    if (match) {
        return match[1];
    }
};


jQuery('a.NewWindow').live('click', function(event) {
    window.open(this.href);
    return false;
});
jQuery('.OpenPopup').live('click', function(event) {
    openPopup(this.href);
    return false;
});
jQuery('.ClosePopup').live('click', function(event) {
    closePopup();
    return false;
});

jQuery('.SubmitOnEnter').live('keypress', function(event) {
    if (event.keyCode == 13) {
        jQuery(this.form).submit();
    }
});

jQuery(document).live('keypress', function(event) {
    if (event.keyCode == 27) {
        closePopup();
    }
});




jQuery(document).ready(function() {
    jQuery('.PrettyPrint').prettyprint();
});




jQuery.extend(jQuery.validator.messages, {
    required: "*"
});

jQuery.ajaxSetup({
    traditional: true
});



