﻿/// <reference path="jquery-1.2.6-vsdoc.js"/>

(function($) {
    $.fn.hideAndFormsOff = function(speed) {
        ///	<summary>
        ///		Chowa element i wyłącza walidację częsci schowanej formularza.
        ///	</summary>
        ///	<returns type="jQuery" />
        return this.each(function() {
            $$ = $(this);
            $$.find(':input').attr('validationOff', 'validationOff');
            $$.hide(speed);
        });
    };
    $.fn.showAndFormsOn = function(speed) {
        ///	<summary>
        ///		Pokazuje element i włącza walidację częsci schowanej formularza.
        ///	</summary>
        ///	<returns type="jQuery" />
        return this.each(function() {
            $$ = $(this);
            $$.find(':input').removeAttr('validationOff');
            $$.show(speed);
        });
    }
    $.fn.toogleObserved = function(offerId) {
        ///	<summary>
        ///		1. Sprawdza czy dany element ma klase 'observed'.
        ///     2. Jak ma 'observed' to wysyla usunięcie obserwowania i usuwa klase 'obseved'.
        ///     3. Jak nie ma 'observed' to wysyła dodanie obserwowania i dodaje klase 'obseved'.
        ///	</summary>
        ///	<returns type="jQuery" />
        ///	<param name="offerId" type="int">
        ///		Identyfikator oferty.
        ///	</param>
        return this.each(function() {
            var $$ = $(this);
            var add = !$$.hasClass('observed');
            if (add)
                $.post('/Observe/Add.rails', { offerId: offerId, ajax: true }, function() { $$.toggleClass('observed') });
            else
                $.post('/Observe/Delete.rails', { offerId: offerId, ajax: true }, function() { $$.toggleClass('observed') });
        });
    };

    $.fn.autoPopulate = function(target, sourceId, targetId) {
        ///	<summary>
        ///		1. Wysyła wybór this do kontrolera
        ///     2. Binduje wyniki do targetu
        ///	</summary>
        ///	<returns type="jQuery" />
        ///	<param name="target" type="string">
        ///		Selektor podrzędnej dropdownlisty.
        ///	</param>
        ///	<param name="sourceId" type="int">
        ///		Identyfikator źródłowego pola w DB
        ///	</param>
        ///	<param name="targetId" type="int">
        ///		Identyfikator podrzędnego pola w DB
        ///	</param>
        return this.each(function() {
            var $$ = $(this);
            $$.selectChain(
                {
                    target: jQuery(target),
                    url: '/AutoPopulate/Index.rails',
                    data: { pid: targetId, id: sourceId, sid: $$.get(0).name }
                }).change();
        });
    };

    $.fn.searchCategoryPopulate = function() {
        ///	<summary>
        ///     1. Nasłuchuje zmiane select
        ///     2. Po zmianie usuwa wszyskie podrzędne selecty (wyszukuje wartości atrybutów lvl)
        ///     3. Wywołuje zapytanie do kontrolera o nowe podrzędne selecty / formularz
        ///	</summary>
        ///	<returns type="jQuery" />
        return this.each(function() {
            var $$ = $(this);
            $$.change(function() {
                $$.attr('kopytko123', 1);
                $('[lvl]').each(function() {
                    var $$$ = $(this);
                    if (($$$.attr('lvl') >= $$.attr('lvl')) && ($$$.attr('kopytko123') != 1))
                        $$$.parent().remove();
                });
                $$.removeAttr('kopytko123');
                $(extednedContainer).html('');
                if ($$.val() > 0) {                    
                    $('#waintingContainer').show();
                    $(":submit").attr('disabled', 'disabled');
                    $('[lvl]').attr('disabled', 'disabled');
                    $.ajax({
                        type: "POST",
                        url: "/Search/PopulateCategory.rails",
                        data: { categoryId: $$.val(), lvl: $$.attr('lvl') },
                        dataType: "script",
                        success: function() {
                            $('#waintingContainer').hide();
                            $(":submit").removeAttr('disabled');
                            $('[lvl]').removeAttr('disabled');
                        }
                    });
                }
            });
        });
    };
})(jQuery);
 
