$.noConflict();
jQuery(document).ready(function($){
    if ($('.floaty').length > 0) {
        $(window).scroll(function() {
            $('.floaty').each(function() {
                var bottom = $('.c .subwrapper').position().top + $('.c .subwrapper').height() - $(this).height() - 40;
                if ($(window).scrollTop() > $('.c .subwrapper').position().top) {
                    if ($(window).scrollTop() < bottom) {
                        $(this).css('top', $(window).scrollTop() - $('.c .subwrapper').position().top);
                    }
                } else {
                    $(this).css('top', 0);
                }
            });
        });
    }
    
    $('.article-list li a').mouseover(function() {
        var finder = jQuery(this).attr('rel')
        $('.article-list a').removeClass("selected");
        $(this).addClass("selected");
        $('.article-carousel .carousel').hide();
        $('.article-carousel .carousel[rel="'+finder+'"]').show();
        return false;
    });
    
    $('.region-selector a').hover(function(){
        $('.region-selector span').hide();
    });
    
    $('a.reg1').hover(function(){
        $('span.reg1').show();
    });
    $('a.reg2').hover(function(){
        $('span.reg2').show();
    });
    $('a.reg3').hover(function(){
        $('span.reg3').show();
    });
    $('a.reg4').hover(function(){
        $('span.reg4').show();
    });
    $('a.reg5').hover(function(){
        $('span.reg5').show();
    });
    $('a.reg6').hover(function(){
        $('span.reg6').show();
    });
    $('a.reg7').hover(function(){
        $('span.reg7').show();
    });
    $('a.reg8').hover(function(){
        $('span.reg8').show();
    });
    $('a.reg9').hover(function(){
        $('span.reg9').show();
    });
    
    $("a[rel='colorbox']").colorbox();
    
    $('a').qtip();
    
    $('img').qtip();
    
    $('.pay input').qtip({
        position: {
              my: 'center left',  // Position my top left...
              at: 'center right'
           }
    });

    $("ul#ticker01").liScroll();
    
    $('.twitter .carousel ul').jcarousel({
      scroll: 1
    });
    /*
    $('.article-carousel ul').jcarousel({
      scroll: 1
    });
    */
    $(".n .trigger, .controls a").click(function(){    
        return false;
    });
    
    $('.member-module .more span').click(function() {
        if ($('.'+$(this).attr('rel')+':visible').length > 0) {
            $('.'+$(this).attr('rel')).slideUp();
        }
        else    {
            $('.'+$(this).attr('rel')).slideDown();
        }
    });
    
    $('.n .trigger').click(function() {
        $('.n li').removeClass('selected');        
        if ($('.'+$(this).attr('rel')+':visible').length > 0 && $('.n form:visible').length > 0) {
            $('.n form').hide();
        } else {
            $(this).parents("li").addClass('selected')
            $('.n form').show();        
            $('.'+$(this).attr('rel')).show(); 
        }     
    });
    
    var page = 1;
    $('.ico31').click(function() {
       $this = $(this);
       page++;
       $.ajax({
          url: $this.attr('href') + '&page=' + page,
          success: function(data) {
              $this.before(data);
          },
          dataType: 'html'
        }); 
        return false;
    });
    
    $(".nav-popout").hide();
    
    $('.subnav a.hero').mouseover(function() {
        $('.subnav li').removeClass('selected');
        
        if ($('.'+$(this).attr('rel')+':visible').length > 0 && $('.nav-popout:visible').length > 0) {
            $('.nav-popout').hide();
            //if ($('body.home').length == 0) {
                if ($('body.results').length == 1) {
                    $('.h, .h .subwrapper').animate({height: 341});                    
                } else {
                    $('.h, .h .subwrapper').animate({height: 341});
                }
            //}
        } else {
            //if ($('body.home').length == 0) {
                if ($('body.results').length == 1) {
                    $('.h, .h .subwrapper').animate({height: 510}); 
                } else {
                    $('.h, .h .subwrapper').animate({height: 510});
                }
            //}
            $(this).parents("li").addClass('selected')
            $('.nav-popout').show();        
            $('.nav-popout > div').hide()
            $('.'+$(this).attr('rel')).show(); 
        }     
    });
    
    $('.img_1 a').click(function(){
        $('.image img, .preview p').removeClass('selected');
        $('.preview .img_1').addClass('selected');
        $('.image span').removeClass('active');
        $('.img_1 span').addClass('active');
    });
    
    $('.img_2 a').click(function(){
        $('.image img, .preview p').removeClass('selected');
        $('.preview .img_2').addClass('selected');
        $('.image span').removeClass('active');
        $('.img_2 span').addClass('active');
    });
    
    $('.img_3 a').click(function(){
        $('.image img, .preview p').removeClass('selected');
        $('.preview .img_3').addClass('selected');
        $('.image span').removeClass('active');
        $('.img_3 span').addClass('active');
    });
    
    $('.img_4 a').click(function(){
        $('.image img, .preview p').removeClass('selected');
        $('.preview .img_4').addClass('selected');
        $('.image span').removeClass('active');
        $('.img_4 span').addClass('active');
    });
    
    $(function(){
        //$('.mem-trig>ul').hide();
        $('.mem-trig').click(function(){
            if ($('.mem-trig ul:animated').size() == 0) {
                $heading = $(this);
                $expandedSiblings = $heading.siblings().find('ul:visible');
                if ($expandedSiblings.size() > 0) {
                    $expandedSiblings.hide(function(){
                        $heading.find('ul').slideDown(300);
                    });
                }
                else {
                    $heading.find('ul').slideDown(300);
                }
            }
        });
    });
    
    $('.temp-message').delay(4000).slideUp().fadeOut();
    $.getJSON('/ajax/check-login', function(data) {
        if (data.loggedIn)  {
            $('#bar-login-button').hide();
            $('#bar-logout-button').fadeIn();
            $('#nav-login-form').hide();
            $('#nav-login-message span').html(data.message);
            $('#nav-login-message').fadeIn();
        }
    });
});

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}
