/**
 * @author Mike De Smet <mike@ratus.nl>
 */
(function($){ 
    var settings = $.extend({}, {
		amount: 3,
        margins: {
            left: 2,
            top: 2
        }
    });
    
    var callbacks = {
    };
    
    var events = {
        init: function (event) {
            
        },
        destroy: function (event) {

        },
        mouseover: function (event) {
			
		},
        mouseout: function (event) {
			
		}
    };
    
    var methods = {
        init: function (options) { 
        	
            return this.each(function(){
                options = $.extend({}, settings, options === undefined ? {} : options);
                settings = options;
                var $this = $(this);
                
                methods.remove.call($this);
//                $this.css({border: 'solid green 1px'});
                
                $this.wrap('<div class="stackWrap" />');
                
        //        if (settings.amount < 1)
          //          return this.stack('remove');
                
                var newEl = null;
                
                for (var i = 0; i <= settings.amount; i++)
                {
                    newEl = $this.eq(0).clone();
                    var pos = $this.position();
                    newEl.css({
                        left: pos.left + settings.margins.left*i + 'px',
                        top: pos.top + settings.margins.top*i + 'px'
                    });

                    $this.parent().append(newEl);
                } 
                
                if (newEl) // we know we stacked if we cloned an element
                {
                    $this.parent().click(function (event) { // delegate event to proper child
                        $(this).find(':first-child').trigger('click', event);
                    });
                }
                
                $this.stack('draw');
            });
        },
        destroy: function () {
            return this.each(function(){
                var $this = $(this);
                var options = methods.settings();
                
                $this.trigger('destroy.stack');
                $.each(events, function (eventName, handler) {
                    $this.unbind(eventName + '.stack');
                });
                
                $this.removeData('stack');
                $this.removeData('stack.settings');
            });
        },
        draw: function () {
            return this.each(function(){
            });
        },        
        data: function (data) { 
            methods.setting('data', data);
        },
        settings: function (settings) {
            if (settings === undefined)
                return $(this).data('stack.settings');
            
            return this.each(function(){
                var orig = $(this).data('stack.settings');
                if (orig === undefined) orig = {};
                orig = $.extend(orig, settings);
                $(this).data('stack.settings', orig);
            });
        },
        setting: function (setting, value) {
            if (value === undefined)
                return $(this).data('stack.settings', setting);
            
            return this.each(function(){
                var orig = $(this).data('stack.settings');
                if (orig === undefined) orig = {};
                orig[setting] = value;
                $(this).data('stack.settings', orig);
            });
        },
        remove: function (isOk) {
            return this.each(function(){
                var $this = $(this);

                if (!$this.parent().hasClass('stackWrap'))
                {
                    //$.error('parent not a stackwrap');
                    
                    return;
                }
                
                
                $this.siblings().remove();
                $this.unwrap();
                $this.data('stack', null);
//                $this.css({border: 'solid ' +(isOk?'red':'yellow') + ' 1px'});//.css({borderSize: $this.css('borderSize') + 5});
            });
        },
        _eoo: false
    };

    $.fn.stack = (function(method){
        if (methods[method])
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        else if (typeof method === 'object' || !method)
            return methods.init.apply(this, arguments);
        
        $.error( 'Method ' +  method + ' does not exist on jQuery.stack' );
        return undefined;
    });
    
    if ($.log === undefined)
    {
    	$.log = function () {if (typeof console != "undefined" && typeof console.log != "undefined") console.log(arguments); };
    }
})(typeof jQueryForExternals === 'undefined' ? jQuery : jQueryForExternals);

