$(document).ready(function() {
    
    //trick for chrome display
    $('.video_faq, .filmik').click(function(){
        setTimeout(function(){
            $('#facybox').css('position', 'relative');
        }, 100);
        setTimeout(function(){
            $('#facybox').css('position', 'absolute');
        }, 100)
    });
    
    $('#facybox .close').livequery('click', function(){
        if ( $('#facybox object').length > 0 ) {
            ytplayer.stopVideo();
        }
    })
    
    if ( $('#feature_list').length > 0 ) {
        var i = 0;
        $('#tabs li').each(function(){
            i = i + 1;
            $(this).addClass(i.toString());
        })
        
        var j = 0;
        $('#output li').each(function(){
            j = j + 1;
            $(this).addClass(j.toString());
        })
        
        $('#tabs li:first-child a').addClass('current');
        $('#output li').hide();
        $('#output li:first-child').show();

        i = 1;
        var delay = 5000;
        rotator = setInterval(function(){
            if ( i > j) i = 1;
            rotator_f(i, j);
            i = parseInt(i) + 1;
        }, delay)
        
        var get_class;
        $('#tabs li').mouseenter(function(){
            $('.current').removeClass('current');
            $('a', this).addClass('current');
            get_class = $(this).attr('class');
            $('#output li').hide();
            $('#output li.'+get_class).show();
            clearInterval(rotator);
        }).mouseleave(function(){
            clearInterval( rotator );
            $('.current').removeClass('current');
            $('.'+get_class).children('a').addClass('current');
            i = parseInt(get_class) + 1;
            rotator = setInterval(function () {
                if ( i > j)
                    i = 1;
                rotator_f(i, j);
                i = parseInt(i) + 1;
            }, delay);
        });
    }
    
    
    if ( $('.active_m').parent().is('ul') && $('.active_m').parent().attr('class') == '') {
        $('.active_m').parent().prev().children('a').css('color', '#FF9C00');
        $('.active_m').parent('ul').css('display', 'block');
    }
    
    $('.login_to_add').click(function(){
        $('#login_box').animate({
            opacity: 0.2
        }, 250, function(){
            $('#login_box').animate({
                opacity: 1
            }, 250)
        })
    })
    
    $('input#str').click(function(){
        $(this).val('');
    })
    
    $('.blog_short').click(function(){
        window.location.href = $(this).parent().prev().prev().children('a').attr('href');
    });
    
    $('.comments_link').click(function(){
        window.location.href = $(this).parent().prev().children('a').attr('href')+'#comments';
    });
    
    $('.show_all').click(function(){
        $(this).parent().parent().parent().parent().next().children().show();
        $(this).parent().children('.activated').removeClass('activated');
        $(this).addClass('activated');
        return false;
    });

    $('.show_home').click(function(){
        $(this).parent().parent().parent().parent().next().children().show();
        $(this).parent().parent().parent().parent().next().children().not('.played_home').hide();
        $(this).parent().children('.activated').removeClass('activated');
        $(this).addClass('activated');
        return false;
    });

    $('.show_away').click(function(){
        $(this).parent().parent().parent().parent().next().children().show();
        $(this).parent().parent().parent().parent().next().children().not('.played_away').hide();
        $(this).parent().children('.activated').removeClass('activated');
        $(this).addClass('activated');
        return false;
    });
    
    var temp_url;
    $('.more_options_ico p span').livequery(function(){ 
        $(this) 
        .hover(function() { 
            temp_url = $('em', this).attr('title');
            $('em', this).attr('title', '');
        }, function() { 
            $('em', this).attr('title', temp_url);
        }); 
    }, function() { 
        $(this) 
        .unbind('mouseover') 
        .unbind('mouseout'); 
    }); 
    
    $('.more_options_ico p span').livequery('click', function(){
        window.location.href = temp_url;
    })
    
    $('.tab_r_long .home').each(function(){
        var wrapp_href = $(this).parent().prev().children('a').attr('href');
        var wrapper = '<a href="'+wrapp_href+'" />'
        $(this).wrap(wrapper);
    })
     
    $('.tab_r_long .away').each(function(){
        var wrapp_href = $(this).parent().next().children('a').attr('href');
        var wrapper = '<a href="'+wrapp_href+'" />'
        $(this).wrap(wrapper);
    })
    
    if ( $('#login_alert').text() != '' ) {
        $('#login_form').hide();
        $('#login_alert').css('display', 'block');
        $('#login_alert').animate({
            opacity: 1
        }, 1000, function(){
            $('#login_alert').animate({
                opacity: 0
            }, 1000, function(){
                $('#login_alert').hide();
                $('#login_form').fadeIn();
            })
        })
    };
    
    $('.news_a_repl .this_repl').click(function(){
        window.location.href = $(this).parent().parent().children('h4').children('a').attr('href');
    });
    $('ul.expand li ul').hide(); //chowamy podpunkty
    $('ul.expand > li > a').click(function(){
        $(this).parent('li').siblings('li').children('ul').hide(300);
        $(this).parent('li').children('ul').toggle(300);
        return false;

    });
   
    $('ul.expand .zwin ').click(function(){
        $(this).parent().parent().hide(300);
        return false;

    });
 

 

    
    $('.home_repl .comments_nr').click(function(){
        window.location.href = $(this).parent().prev().attr('href');
    });
    
    $('#output .comments_nr').click(function(){
        window.location.href = $(this).parent().prev().attr('href');
    });
    

    $(".table_rank tr.darkh").hover(function() {
        HighLight(this);
    }, function() {
        $(this).removeClass("highlight");
    });
    $(".table_rank tr.lighth").hover(function() {
        HighLight(this);
    }, function() {
        $(this).removeClass("highlight");
    });
	
    function HighLight(el)
    {
        $(el).addClass("highlight");
    }



    $('a[rel*=facybox]').facybox({
        noAutoload: true
    });
    
    //    $('#facybox .close').livequery('click', function(){
    //        $('#facybox').remove();
    //    })



    $(".suggest").autocomplete("http://www.joinfans.com/pl/clubs/team_autocomplete",{
        width: 222,
        max: 30,
        minChars: 3,
        selectFirst: true,
        cacheLength: 0
    });

    //    $("#sample-bar").jixedbar();
    $("#frag2").click(function (event) {
        $(this).addClass('active');
        $('#frag1').removeClass('active');
        $('#fragment2').show();
        $('#fragment1').hide();
        event.preventDefault();
    });
    $("#frag1").click(function (event) {
        $(this).addClass('active');
        $('#frag2').removeClass('active');
        $('#fragment1').show();
        $('#fragment2').hide();
        event.preventDefault();
    });

    $(".checkAll").click(function() {
        $("input[type='checkbox']").not(this).each(
            function(){
                this.checked=!this.checked
            });
    });

    $(".forum tr:odd").css("background-color", "#3F403D");

 
    if ( $(".stars").length ) {
        $(".stars").stars({
            inputType: "select"
        });
    }


    $(function (){
        $(".konkursy").hover(function(){
            $(".konkursy").stop(true, false).animate({
                right:"0"
            },"medium");
        },function(){
            $(".konkursy").stop(true, false).animate({
                right:"-176"
            },"medium");
        },500).click(function(){
            location.href="http://www.joinfans.com/service/how_to";
        });
        return false;
    });

    $('.map_content area').qtip(
    {
        content: {
            text: $(this).attr('.map_content area alt')
        },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topLeft'
            }

        },
        style: {
            name: 'dark',
            fontSize: 10,
            border: {
                width: 0,
                radius:3
            },
            padding: 5,
            textAlign: 'center',
            background: '#cbcbcb',
            color: 'black',
            tip: true // Apply a tip at the default tooltip corner
        }
    });
    
    $('.sign_to_club').qtip(
    {
        content: {
            text: $(this).attr('alt')
        },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topLeft'
            }

        },
        style: {
            name: 'dark',
            fontSize: 10,
            border: {
                width: 0,
                radius:3
            },
            padding: 5,
            textAlign: 'center',
            background: '#cbcbcb',
            color: 'black',
            tip: true // Apply a tip at the default tooltip corner
        }
    });


    $('#content a[title], .distinction, .home_away').qtip(
    {
        content: {
            text: $(this).attr('a title')
        },

        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topLeft'
            }

        },
        style: {
            name: 'dark',
            fontSize: 10,
            border: {
                width: 0,
                radius:3
            },
            padding: 5,
            textAlign: 'center',
            background: '#cbcbcb',
            color: 'black',
            tip: true // Apply a tip at the default tooltip corner
        }
    });

    $('.arr_d').click(function(){
        $(this).parent().next().slideToggle();
    });
    
    var set_id = 1;
    $('.right_select').each(function(){
        $(this).attr('id', 'sel'+set_id);
           
        $(this).selectmenu({
            style:'dropdown',
            maxHeight: 280
        });
       
        $('ul#sel'+set_id+'-menu').qvivoScroll();
        set_id += 1;
    });
    
    
    $('.map_content area').mouseover(function(){
        mouseover_map($(this).attr('class'));
    });

    $('.map_content area').mouseout(function(){
        $('#pl_map_id').css('background-position', '0 0');
    });

    $('.left_menu_select').change( function(){
        window.location.href=$(".left_menu_select option:selected").val();
    });
    
    //pawel przeladowanie captcha
    $('.reload_captcha').bind('click', function(){
        $(this).prev('img').attr('src', $(this).prev('img').attr('src')+'/a');
        return false;
    });
    
    //pawel ajax ozpn_id
    $('.ajax_select').live('change', function(){
        var obj=$(this),
        call=$('#'+obj.attr('data-callback')),
        url=obj.parents('form').attr('action'),
        parent_id= obj.attr('data-callback'),
        select_id= obj.attr('id'),
        id=$('option:selected',obj).attr('data-id');
        if (id!='') {
            $.ajax({
                url: url+'/'+id,
                type: 'POST',
                success: function(d) {
                    call.html(d);
                    $('#'+parent_id+' select').attr('id', select_id);
                    $('#'+select_id).selectmenu({
                        style:'dropdown',
                        maxHeight: 280
                    });
                    $('ul#'+select_id+'-menu').qvivoScroll();
                },
                beforeSend: function() {
                    
                }
            });
            
        } else {
            window.location.href=$('option:selected',obj).val();
        }
     
        return false;
    })
    
    //pawwel club autocompleter
    $('.club_autocompleter').autocomplete({
        source: $('#auto_source').val(),
        minLength: 3,
        autoFocus: true ,
        mustMatch: true ,
        select: function(event, ui) {
            window.location.href='http://www.joinfans.com/pl/'+ui.item.key;
        }
    });
    
    //pawel closest_matches
    $('.closest_matches').live('click', function(){
        var call=$('#'+$(this).attr('data-callback'));
        $.ajax({
            url: $(this).attr('href'),
            type: 'POST',
            success: function(d) {
                call.html(d);

                $('.more_options_ico p span').livequery('click', function(){
                    var temp_url1 = $('em', this).attr('title');
                    window.location.href = temp_url1;
                })
                
            },
            beforeSend: function() {
                    
            }
        });
        return false;
    });
});

function mouseover_map(id)
{
    var x = id*240;
    $("#pl_map_id").css('background-position', '-'+x+'px 0');
}

function clearText(thefield){
    if (thefield.defaultValue==thefield.value)
        thefield.value = ""
}

function rotator_f(i, j){
    if ( i == 1 ) {
        $('.current').removeClass();
        $('#tabs li.'+i).children('a').addClass('current');
        $('#output li.'+j).hide();
        $('#output li.'+i).show();
    } else {
        $('.current').removeClass();
        $('#tabs li.'+i).children('a').addClass('current');
        $('#output li.'+(i-1)).hide();
        $('#output li.'+i).show(); 
    }
    
}
