//Image Object
var ImageLoader = function(url)
{
          this.url = url;
          this.image = null;
          this.loadEvent = null;
};

ImageLoader.prototype = 
{
          load:function()
          {
          
            this.image = document.createElement('img');
            var url = this.url;
            var image = this.image;
            var loadEvent = this.loadEvent;
            
            addListener(this.image, 'load', function(e)
            {
                      if(loadEvent != null)
                      {
                                loadEvent(url, image);
                      }
            }, false);
            this.image.src = this.url;
            
          },
          getImage:function()
          {
                return this.image;
          }
};

//Event Listener!!!
function addListener(element, type, expression, bubbling)
{
          bubbling = bubbling || false;
          if(window.addEventListener)	
          { // Standard
                    element.addEventListener(type, expression, bubbling);
                    return true;
          } 
          else if(window.attachEvent) 
          { // IE
                    element.attachEvent('on' + type, expression);
                    return true;
          } 
          else 
          {
                return false;
          }
}
 


$(document).ready(function() {

      //other services button preloader
      var image1 = new ImageLoader("http://www.thethirdeye.org/images/otherservicesbutton.png");
      image1.load();
      
      var image2 = new ImageLoader("http://www.thethirdeye.org/images/otherservicesbutton-hover.png");
      image2.load();
      
      var image3 = new ImageLoader("http://www.thethirdeye.org/images/otherservicesbutton-selected.png");
      image3.load();
      
      //sites button preloader
      var image4 = new ImageLoader("http://www.thethirdeye.org/images/sitesbutton.png");
      image4.load();
      
      var image5 = new ImageLoader("http://www.thethirdeye.org/images/sitesbutton-selected.png");
      image5.load();
      
      var image6 = new ImageLoader("http://www.thethirdeye.org/images/sitesbutton-hover.png");
      image6.load();
      
      //twitter button preloader
      var image7 = new ImageLoader("http://www.thethirdeye.org/images/twitterbutton.png");
      image7.load();
      
      var image8 = new ImageLoader("http://www.thethirdeye.org/images/twitterbutton-hover.png");
      image8.load();
      
      var image9 = new ImageLoader("http://www.thethirdeye.org/images/twitterbutton-selected.png");
      image9.load();

    $('.twitterbutton').hover(
      function () {
        if  (!$(this).hasClass("selected"))
        {
                $(this).addClass("hover");
        }
      },
      function () {
        $(this).removeClass("hover");
      }
    );
    
    $('.twitterbutton').click(function() {
      if ($(this).parent().find('.twitterGroup').is(":hidden")) 
      {
                $(this).parent().find('.twitterGroup').slideDown("slow");
                $(this).removeClass("hover");
                $(this).addClass('selected');
      } else {
                $(this).parent().find('.twitterGroup').slideUp("slow");
                $(this).removeClass("hover");
                $(this).removeClass('selected');
      }
    });
    
    $('.otherservicesbutton').hover(
      function () {
        if  (!$(this).hasClass("selected"))
        {
                $(this).addClass("hover");
        }
      },
      function () {
        $(this).removeClass("hover");
      }
    );
    
    $('.otherservicesbutton').click(function() {
      if ($(this).parent().find('.otherservicesGroup').is(":hidden")) 
      {
                $(this).parent().find('.otherservicesGroup').slideDown("slow");
                $(this).removeClass("hover");
                $(this).addClass('selected');
      } else {
                $(this).parent().find('.otherservicesGroup').slideUp("slow");
                $(this).removeClass("hover");
                $(this).removeClass('selected');
      }
    });
    
    
    $('.sitesbutton').hover(
      function () {
        if  (!$(this).hasClass("selected"))
        {
                $(this).addClass("hover");
        }
      },
      function () {
        $(this).removeClass("hover");
      }
    );
    
    $('.sitesbutton').click(function() {
      if ($(this).parent().find('.sitediscussionsGroup').is(":hidden")) 
      {
                $(this).parent().find('.sitediscussionsGroup').slideDown("slow");
                $(this).removeClass("hover");
                $(this).addClass('selected');
      } else {
                $(this).parent().find('.sitediscussionsGroup').slideUp("slow");
                $(this).removeClass("hover");
                $(this).removeClass('selected');
      }
    });
});
