/*
Author: Łukasz Cholewka
http://downthestairs.com/ueve/
Social Media: @fckueve
*/

(function(){
    $.fn.extend({
        functions: [],
        ueve: function(functions2, options){
            var cStyles = [
            'background-color: #000'
            , 'color: #fff'
            , 'display: block'
            , 'text-align: center'
            , 'width: 100%'
            , 'line-height: 30px'
            , 'font-size: 25px'
        ].join(';');
            console.log('%c Thank You for using http://downthestairs.com/ueve/ You can also follow me on instagram: https://www.instagram.com/fckueve/', cStyles);
            functions = functions2;
            var defaults = {x:5, y: 5};
            options = $.extend(defaults, options);
            click = 0;
            var tmp;
            xy = [];
            start = {x: 0, y: 0};
            end = {x: 0, y: 0};
            timeStart = 0;
            timeEnd = 0;
            time = 0;


            dump = function(obj) {
                console.log(JSON.stringify(obj));
            }

            $(this).addClass('ueve-main');
            $main = $('.ueve-main');
            main = '.ueve-main';
            width = (parseInt(($main.css('width')).replace('px', '')))/options.x;
            height = (parseInt(($main.css('height')).replace('px', '')))/options.y;
            $main.html('');
            for (i = 0; i < options.y; i++) {
                for (ii = 0; ii < options.x; ii++) {
                    $main.append('<div class="ueve-grid-sysyem" data-y="'+i+'" data-x="'+ii+'"></div>');
                }
            }
            $(main + ' .ueve-grid-sysyem').css({
                'width' : width + 'px',
                'height' : height + 'px',
                'display' : 'inline-block',
                'float' : 'left',
                'position' : 'relative'
            });
            $main.bind('touchstart mousedown', function(e){
                e.preventDefault();
                click = 1;
                y = parseInt($(e.target).attr('data-y'));
                x = parseInt($(e.target).attr('data-x'));
                $(e.target).addClass('ueve-active');
                xy.push({y:y, x:[x]});
                start.x = x;
                start.y = y;
                end.x = x;
                end.y = y;
                var d = new Date;
                timeStart = d.getTime();;
            });
            $main.bind('touchmove mouseover', function(e){
                if ( e.originalEvent.type != 'mouseover' ) {
                    var myLocation = e.originalEvent.changedTouches[0];
                    e.target = document.elementFromPoint(myLocation.clientX, myLocation.clientY);
                }
                e.preventDefault();
                if ( click == 1 ) {
                    $(e.target).addClass('ueve-active');
                    yy = parseInt($(e.target).attr('data-y'));
                    xx = parseInt($(e.target).attr('data-x'));
                    if ( (xx && yy) || (xx == 0 || yy == 0) ) {
                        end.x = xx;
                        end.y = yy;
                        togo = {x:1,y:1};
                        xy.forEach(function(y, i){
                            if ( y.y == yy ) {
                                togo.y = 0;
                            }
                        });
                        if (togo.y == 1) {
                            xy.push({y:yy, x:[]});
                        }
                        xy.forEach(function(y, i){
                            if ( y.y == yy ) {
                                y.x.forEach(function(x, ii){
                                    if ( x == xx ) {
                                        togo.x = 0;
                                    }
                                });
                                if ( togo.x == 1 ) {
                                    xy[i].x.push(xx);
                                    xy[i].x.sort(function(a,b){return a-b});
                                }
                            }
                        });
                    }
                }
            });
            $main.bind('touchend mouseup', function (e){
                e.preventDefault();
                var d = new Date;
                timeEnd = d.getTime();
                time = timeEnd - timeStart;
                click = 0;
                $(main + ' .ueve-grid-sysyem').removeClass('ueve-active');

                lowest = {y: options.y, x: options.x};
                xy.forEach(function(y, i){
                    if ( y.y < lowest.y ) {
                        lowest.y = y.y;
                    }
                });
                xy.forEach(function(y, i){
                    y.x.forEach(function(x, ii){
                        if ( x < lowest.x ) {
                            lowest.x = x;
                        }
                    });
                });
                if (lowest.y > 0) {
                    xy.forEach(function(y, i){
                        xy[i].y = xy[i].y - lowest.y;
                    });
                }
                if ( lowest.x > 0 ) {
                    xy.forEach(function(y, i){
                        y.x.forEach(function(x, ii){
                            xy[i].x[ii] = xy[i].x[ii] - lowest.x;
                        });
                    });
                }

                tmpFunctions = [];

                functions.forEach(function(functi, on){
                    tmpFunctions.push(functi.table);
                });

                tmpFunctions.forEach(function(func, tion){
                    go = 1;
                    count = 0;
                    count2 = 0;
                    func.forEach(function(fun, ction){
                        fun.forEach(function(fu, nction){
                            if ( fu == 1 ) {
                                count += 1;
                            }
                        });
                    });
                    xy.forEach(function(y, i){
                        y.x.forEach(function(x, ii){
                            try {
                                if ( func[y.y][x] ) {
                                    count2 += 1;
                                } else {
                                    go = 0;
                                    return false;
                                }
                            } catch (error) {
                                go = 0;
                                return false;
                            }
                        });
                        if ( go == 0 ) {
                            return false;
                        }
                    });
                    if ( go == 1 && count == count2 ) {
                        eval(functions[tion].name);
                        return false;
                    }
                });
                xy = [];
            });
        },
        ueveAdd: function(obj) {
            if ( obj ) {
                functions.push(obj);
            }
        },
        ueveRemove: function(name) {
            functions.forEach(function(func, tion){
                fName = func.name;
                fName = fName.split('(');
                if ( name == fName[0] ) {
                    functions.splice(tion, 1);
                }
            });
        }
    });
}())
