(function($){
    //var fadeInProgress=false;
    $.fn.showSport=function() {
        return this.each(function(){
            //console.log('##');
            $(this).bind('click',function(){
                $(this).unbind('click');
                //console.log("**");
                re0 = /(f[\d]+)+/;
                // [s]port[c]omples
                var sc = $(".sportcomplex").attr("class");
                var id = this.id || false;
                if(id) {

                    var re = /(f[\d]+)+/;
                    sc = sc.replace(re, '');
                    $(".sportcomplex").attr('class',sc);                    
                    $(".sportcomplex").addClass(id);//.removeClass("dn");
                    $(".sportcomplex").center();

                    $(".sportcomplex").fadeIn('normal',function(){
                        jQuery('.sportcomplex a').printSport();
                        $(this).bind('click',function(){
                            $(this).hideSport();
                        });
                    });

                }
            });
        });
    };
    
    $.fn.hideSport=function() {
        return this.each(function(){
            if('none'!=$(this).attr('display')) {
                $(this).fadeOut('slow', function(){
                    $(this).unbind('click');
                    jQuery('.sportcomplex a').unbind('click');
                    re = /(f[\d]+)+/;
                    var cls = re.exec($(this).attr('class'));
                    $("#"+cls[0]).showSport();
                });
            }
        });
        
    };

    $.fn.printSport=function(){
        return this.each(function(){
            $(this).bind('click',function(){
                try{
                    var a = $("#printed").css('backgroundImage');
                    if(''==a)return false;                    
                    var tmp = /url\([\'\"]*([\w\/\:\_\-\d\.]+)[\'\"]*\)/.exec(a);
                    var lnk='';
                    if('object' == typeof(tmp)){
                        lnk=tmp[1];
                    }else{}
                    var img = new Image();
                    if(lnk=='')return false;
                    var f=false;
                    if($.browser.mozilla) {
                        var showPrint=function(){
                            var p='menubar=1,resizable=1';
                                p += ',width='+(img.width+20)+',height='+(img.height+20);
                            var b = "<html><head></head><body>";
                            b += "<img src='"+lnk+"' width='"+img.width+"' height='"+img.height+"'>";
                            b += "</body></html>";
                            var wnd = window.open('','_blank',p);
                            if(wnd) {
                                wnd.document.write(b);
                                wnd.document.close();
                                wnd.print();
                            }
                        }
                        img.onload=function(){f=true; showPrint();}
                        img.src=lnk;
                    }
                    else {
                        img.onload=function(){f=true;}
                        img.src=lnk;
                        var fake=function(){
                            var a=1;
                            return !a;
                        }

                        while(img.width<1) {
                            fake();
                            if(f) break;
                        }

                        var p='menubar=1,resizable=1';
                        p += ',width='+(img.width+20)+',height='+(img.height+20);
                        var b = "<html><head></head><body>";
                        b += "<img src='"+lnk+"' width='"+img.width+"' height='"+img.height+"'>";
                        b += "</body></html>";
                        var wnd = window.open('','_blank',p);
                        if(wnd) {
                            wnd.document.write(b);
                            wnd.document.close();
                            wnd.print();
                        }

                    }
                    
                }
                catch(e){
                 
                }
               return false;
            });
        });
    };

})(jQuery);

jQuery(function(){
    jQuery('.fclickable').showSport();
    
});