(function($, W, D) {

    jQuery.fn.effectBasket = function(options) {
        var config;

        return $(this).each(function() {
            config = $.extend(true, {},
            $.fn.effectBasket.defaults, options);
            var obj = new effectBasket($(this), config, 0);
            construct.call(obj);
        });
    }

    function construct() {
        var self = this;

        // Create parentTarget
        self.elements.parentTarget = $(self.elements.target).parent().parent();

        // Find product id 
        self.options.productBox.content.productId = $(self.elements.parentTarget).find('input[type="hidden"].productId').val();
        //Fill position basket element
        self.options.flyIn.position = self._getPosition(self.options.flyIn.target);

        // Added events
        assignEvents.call(self);
    }

    /***
    * Create product box
    */
    function fillProductBoxTemplate(self) {
        var linkUrl,
	        linkTitle,
	        linkText;

        var $targetLink = $(self.elements.parentTarget).find('h3 > a');
        
        linkUrl = $targetLink.attr('href');
        linkTitle = $targetLink.attr('title');
        linkText = $targetLink.text();
       
        return $.format(self.options.productBox.content.template, linkUrl, linkTitle, linkText, '');
    }

    function effectBasket(target, config, id) {
        // Declare this reference
        var self = this;

        // Setup class attributes
        self.id = id;
        self.options = config;
        self.elements = {
            target: target,
            parentTarget: null
        };

        // Define exposed API methods
        $.extend(self, self.options.api, {
            showProductBox: function() {
                var windowHeight = $(window).outerHeight();
                var windowWidth = $(window).width();
                var topPosition = parseInt($(window).scrollTop()) + parseInt($(window).height());

                //Fill ProductBox
                self.elements.productBoxInfo = $(fillProductBoxTemplate(self));
                // Append to body
                $(document.body).append(self.elements.productBoxInfo);


                self.elements.productBoxInfo.css({
                    display: 'block',
                    top: parseInt(topPosition) + 'px',
                    left: parseInt(windowWidth - self.elements.productBoxInfo.outerWidth() - 1) + 'px'
                }).animate({
                    top: "-=115" + "px"
                }, {
                    queue: false,
                    duration: 3000,
                    complete: function() {

                        $(this).delay(3000).animate({ top: "+=" + topPosition + "px", opacity: 0 }, {
                            queue: true,
                            duration: 3000
                        });
                    }
                });

                //scroll event
                $(window).bind("scroll" + '.effectBasket', { self: self }, self._moveProductBox)
            },

            _moveProductBox: function(event) {
                var self = event.data.self;
                var windowHeight = $(window).height();
                var windowWidth = $(window).width();
                var topPosition = parseInt($(window).scrollTop()) + parseInt($(window).height());

                self.elements.productBoxInfo.css({
                    top: parseInt(topPosition - 115) + 'px'
                });
            },

            hideProductBox: function() {

            },

            fly: function(event) {
                // Clone image for flaying
                var img = $(self.elements.parentTarget).find('img');
                var imgClone = $(img).clone();
                var imgParent = $(img).parent();


                //Fill position basket element
                //self.options.flyIn.position = self._getPosition(self.options.flyIn.target);

                $(img).effect('shake', {
                    times: 2
                },
                150,
                function() {
                    $(imgClone).appendTo(imgParent);
                    $(imgClone).css({
                        'position': 'absolute',
                        'left': self._getPosition(img).left + 'px',
                        'top': self._getPosition(img).top + 'px'
                    }).animate({
                        opacity: 0.1,
                        top: parseInt(self.options.flyIn.position.top + 20) + 'px',
                        left: parseInt(self.options.flyIn.position.left + 18) + 'px',
                        width: 50 + 'px',
                        height: 50 + 'px'
                    },
                    2000,
                    function() {
                        $(imgClone).fadeOut().remove();

                        // Callback if element fly in element					
                        self.afterFly.call(self, event);

                        // Show basket info box
                        self.showProductBox();
                    });
                });
            },

            _getPosition: function(element) {
                return $(element).offset();
            }
        });
    }

    function assignEvents() {
        var self = this;

        // Setup event target variables
        showTarget = self.elements.target;

        // Define show event method
        function showMethod(event) {
            self.fly(event);
        }

        showTarget.bind("click" + '.effectBasket', showMethod);
    }

    jQuery.fn.effectBasket.defaults = {

        flyIn: {
            target: null,
            position: null,
            effect: {
                type: 'fade',
                length: 100
            }
        },

        productBox: {
            target: null,
            position: null,
            effect: {
                type: 'fade',
                length: 100
            },
            content: {
                template: null,
                basketUrl: null,
                productId: null
            }
        },
        // Callbacks
        api: {
            afterFly: function() { },
            afterScroll: function() { }
        }
    }


})(jQuery, window, document);
