(function() {

    $(document).ready(function() {
        prepareOnLoad();


        jQuery("tr.status_lined-up a, tr.status_not-lined-up a").each(function() {

              var  html=jQuery(this).html();
              var self=this;

              jQuery(this).replaceWith("<span style='margin:5px'>" + html + "</span>");

         });


        var renderModule = function(type, fnRender) {

            var type = type,
            fnRender = fnRender;

            return function() {
                $("div." + type + ":first").each(function() {

                    var img = $("img:first", this);
                    var map = $("map:first", this);
                    $("area", map).each(function() {

                        var arr = this.coords.split(",");
                        var t = $(img).offset().top + new Number(arr[1]);
                        var l = $(img).offset().left + new Number(arr[0]);
                        n = $(this).attr("alt");
                        var s="";
                        var el = fnRender(l, t, s, n);
                        $(document.body).append(el);

                    });

                });

            };

        };
		
		
		$(".team_logo img").each(function() {

            var self = this;
            var type = (function() {
                return "neutro.png"
            })();

            $(self).each(function() {

                var img = new Image()
                var self = this;
                img.onload = function() {
                    self.src = this.src;
                }
                var src = this.src;
                this.src = "lib/images/squadre/loghi/large/" + type;
                img.src = src;

            });
        });


        var fnSx=(function() {

            var competitor_image = $("#competitor_0_image").val();

            return function(l, t, s, n) {
                l = l - 20;
                var player = $(modulo[0]).map(function() {
                    return this["ordered-position"] == n ? this: null;
                }).get()[0];

                var alt = player["name"] + " " + player["surname"];

                var html = '<div class="tab_player_cont"><div class="tab_logo" title="' + alt + '"><div class="tab_player_sx">' + player["number"] + '</div><img src="lib/images/squadre/loghi/small/' + player.team_id + '.png"/></div></div>';
                return $(html).css("position", "absolute").css("width", "1px").css("height", "1px").css("top", t + "px").css("left", l + "px");
            };

        })();   
        

        var renderModuleSx = renderModule("sx",fnSx);

				var fnDx=(function() {

            var competitor_image = $("#competitor_1_image").val();

            return function(l, t, s, n) {
                l = l - 5;
                var player = $(modulo[1]).map(function() {
                    return this["ordered-position"] == n ? this: null;
                }).get()[0];
                var alt = player["name"] + " " + player["surname"];
                var html = '<div class="tab_player_cont"><div class="tab_logo" title="' + alt + '"><div class="tab_player_dx">' + player["number"] + '</div><img  src="lib/images/squadre/loghi/small/' + player.team_id + '.png"/></div>';
                return $(html).css("position", "absolute").css("width", "1px").css("height", "1px").css("top", t + "px").css("left", l + "px");
            };

        })();


        var renderModuleDx = renderModule("dx",fnDx);

				//$("div.scrollable").scrollable();
				//$(".items:first").scroll(0,0);


        renderModuleSx();
        renderModuleDx();

    });
})()

