$(function(){
	//attaching animation to trending topics
	function trending_topics(){
          var topics = $(".c_tl a");
                  var number_of_topics = topics.length;
                  var counter = 0;

          topics.each(function(){
              var topic = $(this);
              var h = topic.height();
              var w = topic.width();
              var boundry = {
                  leftMin:20,
                  leftMax:940 - w,
                  topMin: 60,
                  topMax: 200 -h
              }

              topic.css({
                  left:return_left(),
                  top:return_top()
              });

                          function return_top()
                          {
                              return boundry.topMin + (counter%10)*17;
                          }

                          function return_left()
                          {
                              var left = boundry.leftMin + (counter%3)*320 + get_random_number(0,250);
                              if(left > boundry.leftMax)
                              {
                                  return boundry.leftMax;
                              }
                              else
                              {
                                  return left;
                              }
                          }



              topic.hover(show,hide);

              function show(){topic.css("z-index",1000);}
              function hide(){topic.css("z-index",1);}

                          counter++;

          });

          function get_random_number(min,max)
          {
              var done = false;
              while (!done)
              {
                  var number = parseInt(max*Math.random(),10);
                  if (number > min)
                  {
                      return number;
                  }
              }
          }

          var to = setTimeout(show_topics, 1000);

          function show_topics()
          {
              topics.each(function(){
                  var topic = $(this);
                  var to = setTimeout(function(){topic.fadeIn()},get_random_number(100,2000));
              });
          }        
      }
  
	//END:attaching animation to trending topics
	
	//image flipper
	var image_flipper = {};	
	(function(){
		var setting = {
			image_count: 6, //total number of images
			animation_speed: 4500, //how long does it take for the image to animate
			animation_delay: 8000, //delay between two images
			image_path: "/themes/seattlecentral2/images/headers/header#.png"
		};

		//Don't modify stuff below here
		var image_path = setting.image_path;
		var images = {};
		var count = 0;
		var events = {
			ready:function(){}
		};
		
		for (var i=1; i<=setting.image_count; i++)
		{
			var image = $("<img>").attr("src",image_path.replace("#",i));
			images[i] = image;
			image.addClass("header").addClass("h" + i).css({opacity:0});
			image.load(function(){
				count += 1;
				if (count == setting.image_count)
				{
					initialize_header_rotator();
				}
			});
		}
		
		function initialize_header_rotator()
		{
			var cur = 0;
			var container = $("#s_h");
			container.find(".ajax_loader").remove();
			show(1);
			events.ready();
			
			setInterval(swap,setting.animation_delay);
			
			function swap()
			{
				if (cur < setting.image_count)
				{
					hide(cur);
					show(cur+1);
				}
				else
				{
					hide(setting.image_count);
					show(1);
				}
			}
						
			function show(num)
			{
				container.append(images[num]);
				images[num].animate({opacity:1},setting.animation_speed);
				cur = num;		
			}
			
			function hide(num)
			{
				var image = container.find(".h" + (num));
				image.animate({opacity:0},setting.animation_speed);				
			}
		}
		
		//exposing public properties
		image_flipper.events = events;
	})();

	image_flipper.events.ready = function(){trending_topics();}
	//END:image flipper
});