$(document).ready(function(){

    $.ajaxSetup({ cache: false });

    //Color LINKS
    $('a').hover(function(){
        $(this).animate({ color : '#FFFF33'  });
    }, function(){
        $(this).animate({ color : '#FFFFFF'  });
    });

    $('.cont').hover(function(){
        $(this).children('h1').animate({ color : '#FFFF33'  });
        $(this).children('h2').animate({ color : '#FFFF33'  });
    }, function(){
        $(this).children('h1').animate({ color : '#FFFFFF'  });
        $(this).children('h2').animate({ color : '#FFFFFF'  });
    });

    //TAB SWITCHY
    var currentTab = 1;
    var nextTab = 0;


    $('#tabs li').hover(function(){
         if( $(this).attr('id') != 'active' ){
             $(this).stop().animate({
                 marginLeft: '20px',
                 color : '#FFFF33'
             }, 250);
         }
    }, function(){
        if( $(this).attr('id') != 'active' ){
             $(this).stop().animate({
                 marginLeft: '0px',
                 color : '#FFFFFF'
             }, 250);
        }
    });

    $('#tabs li').click(function(){
        if( $(this).attr('id') != 'active' ){
             $(this).stop().animate({
                 marginLeft: '0px'
             }, 250);
            $('#active').css('color', '#FFFFFF');
            $('#active').removeAttr('id');

            $(this).attr('id', 'active');

            nextTab = $(this).attr('class');

            //alert($(this).attr('name'));

            _gaq.push(['_trackPageview', $(this).attr('name')]);

            $('#'+currentTab).hide("slide", { direction: "left" }, 500, function(){
                $('#'+nextTab).show("slide", { direction: "left" }, 500);
                currentTab = nextTab;
            });
        }

    });

    //NASTY IE BULLSHIT
    //var noCache = randomString();

    interfader();

    function interfader(){

        var CurrentImage = '';

        $.getJSON(conText + '/imageRotator/getRandomPicture ', function(imageJSON){
            CurrentImage = imageJSON.CurrentImage;
        });

        var imageTimer = setTimeout(function(){

            if(CurrentImage.length > 0){
                var newDrop = new Image();
                newDrop.src = CurrentImage;

                $(newDrop).imagesLoaded(function(){
                    //alert('image loaded');
                    $('#bg').prepend(this);
                    $('#bg img:first').removeAttr('height');
                    $('#bg img:first').removeAttr('width');
                    $('#bg img:last').fadeOut(1000, function(){
                        $(this).removeWithoutLeaking();
                        delete newDrop;
                    });
                });
            }

            interfader();

        }, RefreshInMillis);
    }
});

$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len = elems.length;

  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }
  });

  return this;
};

$.fn.removeWithoutLeaking = function() {
    this.each(function(i,e){
        if( e.parentNode )
            e.parentNode.removeChild(e);
    });
};






