
(function(angular,window){
    "use strict";
    var _directives = angular.module("potfolio.directives", []);

    _directives.directive("cover", ["$injector",function($injector){
        return {
            restrict: "A",
            compile: function(scope, elem){

                return {
                    pre: function($scope, elem, attr){
                        var elem = $(elem[0]);
                        var reg = $scope.$watch(function(){
                            return +attr.child === elem.siblings();
                        }, function(){
                            var calc = !!(+elem.width() <= +elem.height());
                            var css = calc ? {"width": "100%"} : {"height": "100%"};
                            Velocity(elem.css(css),{"opacity": [1,0]}, {
                                duration: 1000,
                                delay: 20
                            });
                        })              
                    }
                }
            }
        }
    }]);
    
    _directives.directive("skills", ["$injector",function($injector){
        return {
            restrict: "E",
            scope: {
                data: "="
            },
            template: "<div ng-repeat='skill in data track by $index'>"
                            +"<div class='progress' ng-init=''>"
                                +"<div class='bar' data-per='{{skill.percentage}}' ng-style='timed ? {width: (skill.percentage + symbol)} : {width: 0}'><h5>{{skill.name}}</h5></div>"
                            +"</div>"
                        +"</div>",
            compile: function(elem,attr){
                return {
                    pre: function(scope,elem,attr){
                        scope.symbol = "%";
                        scope.timed = false;
                        $injector.get("$timeout")(function(){
                            scope.timed = true;
                        }, 1000);
                    }
                }
            }
        }
    }]);
    
    _directives.directive("calcHeightToAjust", ["$injector",function($injector){
        return {
            restrict: "A",
            compile: function(elem,attr){
                return {
                    pre: function(scope,elem,attr){
                        $(window).ready(function(){
                            $injector.get("$timeout")(function(){ //CHROME FIX
                                var img = $(elem[0]),
                                    container = img.parent();

                                var calc = function(){
                                    !!(img.height() < container.height()) && img.css("bottom", 0);
                                }

                                $(window).resize(calc);

                                calc();
                            }, 1); //CHROME FIX
                        });
                    }
                }
            }
        }
    }]);
    
}(angular,window))