/**
 * Klasa obsługująca schowek
 *
 * @author Piotr 'pepe' Picheta <piotr.picheta@autentika.pl>
 */
var Tray = (function() {
	var TraySingleton = function() {
	// PUBLIC:
	// PRIVILEGED:
		
        /**
         * Inicjalizacja - bindowanie zdarzeń i sortowanie
         *
         * @access public
         */
		this.init = function() {
            $('a.addToTray:not(.photoAbuse):not(.videoAbuse)').bind('click', _addToTray);
        }
	// PRIVATE:

        /**
         * Dodawanie do schowka
         *
         * @param JQuery.Event e (klasa zdarzenie)
         * @return bool
         *
         * @access private
         */
        var _addToTray = function(e) {
            e.preventDefault();

            var element = e.target;
            var data    = $(element).attr('href').split('_');
            var id      = parseInt(data[1]);
            var type    = data[0].split('#')[1];

            $.ajax({
                type:	 	"POST",
                dataType:	'json',
                url:		"/tray.php",
                data: 		"a_id=" + id + "&type=" + encodeURIComponent(type),
                success: 	function(msg) {
                    if (msg.info == 'ERROR') {
                        jsAlert('Wysłanie zgłoszenia nie powiodło się. Prosimy spróbować później.');
                    } else {
                        jsAlert(msg.info, {title:'Schowek'});
                    }
                    return false;
                }
            });
            return false;
        }
	}, instance = null;

	return {

        /**
         * Metoda zwracająca instancję klasy
         *
         * @return TraySingleton (instancja klasy Tray - wzorzec Singleton)
         *
         * @access public
         */
		getInstance: function() {
			return instance || (instance = new TraySingleton);
		}
	}
})();

$(document).ready(function() {
    var tray = Tray.getInstance();
    tray.init();
});
