var menuonhover=false;
var sexymenus=true;
jQuery(document).ready(function(){
    jQuery('#footerleft li:last').css('border','none');
    
    jQuery('#menutop > .menu > li > a').each(function(){
        var width=jQuery(this).width();
        jQuery(this).css('width',width+'px');
    });
    jQuery('#menutop > .menu > li li a').each(function(){
        var width=jQuery(this).width()*.9;
        jQuery(this).css('width',width+'px');
    });
    
    jQuery('#menutop li').hover(function(){
        jQuery(this).addClass('sfhover');
        if(sexymenus){
            jQuery('#menubottom').html('');
            if(jQuery(this).find('ul').length) jQuery('#menubottom').append(jQuery(this).find('ul').clone());
            //Cufon.refresh();
            jQuery('#menubottom li').hover(function(){
                jQuery(this).addClass('sfhover');
            },function(){
                jQuery(this).removeClass('sfhover');
            });       
            menuonhover=true;
            run_reset_menu_timeout();
        }
    },function(){
        jQuery(this).removeClass('sfhover');
        if(sexymenus){
            menuonhover=false;
        }
    });
    
    jQuery('#menubottom').hover(function(){
        menuonhover=true;
    },function(){
        menuonhover=false;
    });
    
    jQuery('#afglinks select').change(function(){
        var id = jQuery(this).val();
        //window.location = 'index.php?option=com_k2&view=item&id='+id;
        window.location = id;
    });
    
    if(jQuery('#slides').length) jQuery('#slides ul').cycle({
            fx: 'fade', 
            timeout: 5000,
            pager: '#slidecut',
            prev: '.slideleft', 
            next: '.slideright'
    });
    
    jQuery('#clipcategory').change(function(){
        var thiscat=jQuery(this).val();
        jQuery('.clipcat').hide();
        jQuery('.clipcat[rel='+thiscat+']').show();
    });
    
    jQuery('#clips .clipcat .page').each(function(){
        var maxheight=0;
        jQuery(this).find('.visiclip').each(function(){
            if(maxheight<jQuery(this).height()) 
                maxheight=jQuery(this).height();
        }).height(maxheight);
        jQuery(this).hide();
    });
    jQuery('#clips .clipcat').each(function(){
        jQuery(this).find('.page:first').addClass('first').show();
        jQuery(this).find('.page:last').addClass('last').find('.nextpage').remove();
        jQuery(this).hide();
    });
    jQuery('#clips .clipcat:first').show();
    
    jQuery('.nextpage').click(function(){
        var page=jQuery(this).parent();
        if(!page.hasClass('last')){
            page.hide();
            page.next().show();
        }
    });
    jQuery('.prevpage').click(function(){
        var page=jQuery(this).parent();
        if(!page.hasClass('first')){
            page.hide();
            page.prev().show();
        }
    });
    
    jQuery('.visiclip .showclip').click(function(){
        kill_active_video();       
        var clipid=jQuery(this).attr('rel');
        jQuery('#viewer .clip').hide().each(function(){ 
            if(jQuery(this).children('.clipviewer').attr('rel')==clipid){
                jQuery('#vidinfo .clipinfo').hide();
                var thisrunner=jQuery(this).attr('rel');
                jQuery('#vidinfo .clipinfo[rel='+thisrunner+']').show();
                jQuery(this).show();
                show_active_video();
            }
        });        
    });
    
    if(jQuery('#afgtv').length || jQuery('#afgtvfeature').length){
        jQuery('.prevclip').click(function(){ 
            var clip=jQuery(this).parent().parent().parent();
            var thisnum=clip.find('.thisclipnum').text();
            if(thisnum>1){
                kill_active_video();       
                var group=clip.attr('rel');
                clip.hide();
                jQuery('#library ul').each(function(){ if(jQuery(this).attr('rel')==group) jQuery(this).hide(); });
                jQuery('#vidinfo .clipinfo').each(function(){ if(jQuery(this).attr('rel')==group) jQuery(this).hide(); });
                
                var nextclip=clip.prev();
                var nextgroup=nextclip.attr('rel');
                nextclip.show();
                jQuery('#library ul').each(function(){ if(jQuery(this).attr('rel')==nextgroup) jQuery(this).show(); });
                jQuery('#vidinfo .clipinfo').each(function(){ if(jQuery(this).attr('rel')==nextgroup) jQuery(this).show(); });

                show_active_video();        
            }
        });
        
        jQuery('.nextclip').click(function(){        
            var clip=jQuery(this).parent().parent().parent();
            var thisnum=clip.find('.thisclipnum').text();
            if(thisnum<jQuery('#viewer .clip').length){
                kill_active_video();       
                var group=clip.attr('rel');
                clip.hide();
                jQuery('#library ul').each(function(){ if(jQuery(this).attr('rel')==group) jQuery(this).hide(); });
                jQuery('#vidinfo .clipinfo').each(function(){ if(jQuery(this).attr('rel')==group) jQuery(this).hide(); });
                
                var nextclip=clip.next();
                var nextgroup=nextclip.attr('rel');
                nextclip.show();
                jQuery('#library ul').each(function(){ if(jQuery(this).attr('rel')==nextgroup) jQuery(this).show(); });
                jQuery('#vidinfo .clipinfo').each(function(){ if(jQuery(this).attr('rel')==nextgroup) jQuery(this).show(); });
                
                show_active_video();
            }
        });
        
        jQuery('#library ul li > a').click(function(){
            kill_active_video();       
            var clipid=jQuery(this).attr('rel');
            var group=jQuery(this).parent().parent().attr('rel');
            var cliplink=jQuery(this).siblings('h5').children('a').attr('href');
            var cliptitle=jQuery(this).siblings('h5').children('a').html();
            jQuery('#viewer .clip').each(function(){ if(jQuery(this).attr('rel')==group){
                jQuery(this).find('.clipviewer').attr('rel',clipid);
                jQuery(this).find('.clipviewer').html(clipid);
                jQuery(this).find('.cliptitle').attr('href',cliplink);
                jQuery(this).find('.cliptitle').html(cliptitle);
            }});
            
            show_active_video();
        });
        
        show_active_video();
    }
    
    jQuery('#afgpicks .her a').click(function(){
        jQuery('.pick').hide();
        jQuery('.otherpicks').hide();        
        jQuery('#herpick').show();
        jQuery('#herother').show();
    });
    
    jQuery('#afgpicks .his a').click(function(){
        jQuery('.pick').hide();
        jQuery('.otherpicks').hide();        
        jQuery('#hispick').show();
        jQuery('#hisother').show();
    });
    
    if(jQuery('#secondcontent').length && jQuery('#secondcontent').children().length==0) jQuery('#secondcontent').remove();
    
    if(jQuery('#staticcontent').not('.nosetheight').length) if(jQuery('#staticcontent').height()<335) jQuery('#staticcontent').css('height','305px');
    
    if(jQuery('#system-message .fade').length){
        window.setTimeout(function(){
            jQuery('#system-message').fadeOut();
        },5000);
    }
    
    if(jQuery('.benefits .benefit').length>1) jQuery('.benefits .benefit:odd').addClass('rowender');
    
    jQuery('#showmorebenefits').click(function(){
        jQuery('#morebenefits').slideDown();
        jQuery('#showfewerbenefits').show();
        jQuery(this).hide();
    });
    
    jQuery('#showfewerbenefits').click(function(){
        jQuery('#morebenefits').slideUp();
        jQuery('#showmorebenefits').show();
        jQuery(this).hide();
    });
    
    jQuery('#registrationTable input.button').before('* Mandatory Fields');
    
    jQuery('#joinsubmitfake').click(function(){
        jQuery('#joinnow .button[type=submit]').click();
    });
    
    jQuery('#acceptfakeyes').change(function(){
        if(jQuery(this).is(':checked')){
            jQuery('#acceptedterms').attr('checked','checked');
        }
    });
    jQuery('#acceptfakeno').change(function(){
        if(jQuery(this).is(':checked')){
            jQuery('#acceptedterms').attr('checked','');
        }
    });
    
    jQuery('#gogocatfilter').click(function(){
        var cat_to_show = jQuery('#catfilter').val();
        if(cat_to_show=='') jQuery('.monthevent').show();
        else{
            jQuery('.monthevent').hide();
            jQuery('.monthevent[rel='+cat_to_show+']').show();
        }
    });
    
    jQuery('.cal_titlelink').hover(function(){
        var id=jQuery(this).attr('rel');
        jQuery(this).addClass('hovering');
        show_calhover(id);
    },function(){
        hide_calhover();
        jQuery(this).removeClass('hovering');
    });
    
    jQuery('.event-hover').hover(function(){
        jQuery(this).addClass('hovering');
    },function(){
        jQuery(this).removeClass('hovering');
    });
       
    jQuery('input[type=hidden]').hide();   
    
    //if(jQuery('#main').height()<=7) jQuery('#main').remove();
    if(jQuery('#secondcontent').children().length==1 && jQuery('#secondcontent').children().find('div').length==0) jQuery('#secondcontent').remove();    
    
    show_default_menu();
});

jQuery(window).ready(function(){
    var maxheight=0;
    jQuery('#featuredstory .featuredstorylist > li').not('.clearList').each(function(){
        if(jQuery(this).height()>maxheight) maxheight=jQuery(this).height();
    });
    jQuery('#featuredstory .featuredstorylist > li').not('.clearList').height(maxheight);
});

function show_calhover(id){
    jQuery('.event-hover').hide().removeClass('hovering').parent().css('z-index',1); 
    jQuery('.event-hover[rel='+id+']').show().parent().css('z-index',10); 
}

function hide_calhover(){
    window.setTimeout(function(){
        var currently=jQuery('.event-hover:visible').attr('rel');
        if(jQuery('.event-hover[rel='+currently+']').length && !jQuery('.event-hover[rel='+currently+']').hasClass('hovering') && !jQuery('.cal_titlelink[rel='+currently+']').hasClass('hovering')) 
            jQuery('.event-hover:visible').removeClass('hovering').fadeOut(500,function(){ jQuery(this).parent().css('z-index',1); });
        else hide_calhover();
    },2000);
}

function kill_active_video(){
    var clip=jQuery('#viewer .clip:visible .clipviewer');
    var clipid=clip.attr('rel');
    clip.html(clipid);
}

function show_active_video(){
    var clip=jQuery('#viewer .clip:visible .clipviewer');
    var clipid=clip.attr('rel');
    var clipcode='<object type="application/x-shockwave-flash" style="width:424px;height:293px;" data="http://www.youtube.com/v/'+clipid+'&amp;hl=en&amp;fs=1"><param name="movie" value="http://www.youtube.com/v/'+clipid+'&amp;hl=en&amp;fs=1" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#000000" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /></object>';
    clip.html(clipcode);
}

function show_default_menu(){
    if(jQuery('#menutop > ul > li.active > ul').length){
        jQuery('#menubottom').html('');
        if(sexymenus) jQuery('#menutop > ul > li.active > ul').clone().appendTo(jQuery('#menubottom'));
        else jQuery('#menutop > ul > li.active > ul').appendTo(jQuery('#menubottom'));
        //Cufon.refresh();
        jQuery('#menubottom li').hover(function(){
            jQuery(this).addClass('sfhover');
        },function(){
            jQuery(this).removeClass('sfhover');
        });    
        jQuery('#menu').addClass('withbottom');
    }
    else{
        jQuery('#menubottom').html(''); 
    }
}

function run_reset_menu_timeout(){
    window.setTimeout(function(){
        if(!menuonhover) show_default_menu();                
        else run_reset_menu_timeout();
    },3000);     
}

