﻿(function ($) {
    $.fn.imageBlur = function (options) {
        var opts = $.extend({}, $.fn.imageBlur.defaultOptions, options), $moduleWrap = this;

        $moduleWrap.each(function () {
            var $module = $(this);


            //hover event
            function hoverOver($m, opacity) {
                var width = parseInt($m.find("img").width());
                var height = parseInt($m.find("img").height());
                $m.find("img").addClass("imgblurd");
                if (opts.debug && console) {
                    console.log('Img h:' + height);
                    console.log('Img w:' + width);
                }

                for (i = 0; i < opts.horz.length; i++) {
                    var topMargin = opts.vert[i] + (-height);
                    var leftMargin = opts.horz[i];
                    blur_html = '<div class="songtwo"><span style="position:absolute; left: 0; margin-left:' + leftMargin + 'px; margin-top:' + topMargin + 'px;" >';
                    blur_html += '<img src=' + $m.children().attr('src') + ' width="' + width + '" height="' + height + '" style=" margin-top: ' + opts.vert[i] + 'px; margin-left:' + opts.horz[i] + 'px;" /></span></div>';
                    $m.append(blur_html);
                    if (opts.debug && console) {
                        console.log('blur_html:' + blur_html);
                    }
                }

                $m.find(opts.elementSel).fadeTo(opts.fadeSpeed, opacity, function () {
                    $(".imgblurd").fadeTo(opts.fadeSpeed, opts.selectorFadeToOpacity);
                });
            };

            //hover out event
            function hoverOut($m) {
                if (opts.debug && console) {
                    console.log('imgblurd faded to 1');
                }
                $m.find(".imgblurd").stop().fadeTo(opts.fadeSpeed, 1, function () {
                    $m.find(".imgblurd").removeClass("imgblurd");
                });
                $m.find(".songtwo").stop().fadeTo(opts.fadeSpeed, 0);
                $m.find(".songtwo").remove();                

            }

            $module.hoverIntent({
                sensitivity: opts.hoverSensitivity,
                timeout: opts.hoverTimeout,
                interval: opts.hoverInterval,
                over: function () {
                    hoverOver($(this).data('intentExpressed', true), opts.defaultOpacity);
                    if (opts.debug && console) {
                        console.log('module hover intent over');
                    }
                },
                out: function () {
                    hoverOut($(this).data('intentExpressed', false));
                    if (opts.debug && console) {
                        console.log('module hover intent out');
                    }
                }
            });
        });
        return $moduleWrap;
    };

    // define default options, note, set array values similar to below, makes for a uniform blurrying effect, also set image to be blurred's margin to the highest value (i.e. - margin: 3px;). Array must contain 8 values per margin blurring effect.
    $.fn.imageBlur.defaultOptions = {
        elementSel: '.songtwo span', //elements generated for blurring effect
        defaultOpacity: 0.2, //default opacity of gereated blurring effect elements
        selectorFadeToOpacity: 0.7, //default opacity of original image (on hover)
        fadeSpeed: 'fast', //default fade in/out speed
        hoverSensitivity: 7, //hoverintent hover sensitivity
        hoverTimeout: 100, //hoverintent hover timeout
        hoverInterval: 100, //hoverintent hover interval
        debug: false, //set to true to log to console (FF and Chrome)
        horz: new Array(-1, -1, 0, 1, 1, 1, 0, -1), //horizonal alignment margins and width modification values, be vewy, vewy caweful
        vert: new Array(0, -1, -1, -1, 0, 1, 1, 1) //vertical alignment margins and width modification values, be vewy, vewy caweful
    };    
})(jQuery);
