
var directions = ["down", "left", "up", "right"];

$.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(){

     if (this.complete || this.complete === undefined){
        var src = this.src;
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }  
  }); 

  return this;
};

function animate( ) { 
    
    var position = 0;
    $("div#main-new-container").hide().fadeIn(3000);
    $("div#left-row-title").show('slide', 1000);
    $("div#images-container a").hide();
    $("div#images-container")
	.find(".bubble")
	.each( function() 
	       {	
		   
		   var bubble = $(this);
		   position++;
		   $(this).show("slide", { direction: directions[ position % 4 ] }, 1000 * position,
				function() {
				    
				    bubble.find("a").hide().fadeIn(777);
				});
	       });
}
   

function bounce(currentA) {
    
    var bouncespeed = 1000;
    var newspeed = bouncespeed;

    var effect = 'bounce'

    var randomDirection = Math.floor(4*Math.random());
    $(currentA).effect( effect, {times: 1, 
		direction: directions[ randomDirection % 4], 
		distance: 3}, bouncespeed,
		      function() { 
			    
			    bounce(currentA);
			});
}

function news() {

    $("div#main-new-body").hide();

    $("div#main-new-container").click( function() { 
	    $(this).find("div#main-new-body").toggle("blind", 1000 );
	});
}

$(document).ready( function() {
    
	animate();
	news();
	$("div.bubble")
	    .each( 
		  function( ) {
		      
		   $(this)
		       .click(
			  function( event ) {
			      		
			      window.location.replace($(this).find("a").attr("href"));
			  });
		   
		   $(this)
		       .hover( 
			   function() { 
			       $(this).animate({opacity:0.9});
			   },
			   function() { 
			       $(this).animate({opacity:1});
			   });
		  });

	Cufon.replace('a.menu',{ textShadow: '0px 0px 1px #ffffff' });
	Cufon.replace('.cattitle a',{ color: "black", textShadow: '0px 1px 2px #fff' });
	Cufon.replace('.cattitle-dark a',{ color: "white", textShadow: '0px 1px 2px #333' });
    });
