var WNET = function() {
    $.waitingElements = {};
    $.mouseIsOver = {};
    
    return {
        //function that tests if an element with a given id exists on the page
        exists: function(id) {
            var retVal = false;
            if (document.getElementById(id)) {
                retVal = true;
            }
            return retVal;
        },
        
        //generic function that takes as parameters an element to roll over and an element that slides down on the rollover
        slideDisplay: function(rollElement, displayElement) {
            $('#' + rollElement).hover(
                //mouseover
                function() {
                   $('#' + displayElement + ':hidden').slideDown('fast');
                },
                //mouseout
                function() {
                    $('#' + displayElement).slideUp('fast');
                }
            ) //end hover
        },
        
        //generic function that takes as parameters an element to roll over and an element that fades in/out on the rollover
        fadeDisplay: function(rollElement, displayElement) {
            $('#' + rollElement).hover(
                //mouseover
                function() {
                    $.mouseIsOver[rollElement] = true;
                    if(!$.mouseIsOver[displayElement])
                    {
                        $('#' + displayElement + ':hidden').fadeIn('normal');
                    }
                },
                //mouseout
                function() {
                    $.mouseIsOver[rollElement] = false;
                    if(!$.mouseIsOver[displayElement])
                    {
                        $('#' + displayElement).hide();
                    }
                }
            ) //end hover
            $('#' + displayElement).hover(
                //mouseover
                function() {
                    $.mouseIsOver[displayElement] = true;
                    if(!$.mouseIsOver[rollElement])
                    {
                        $('#' + displayElement).show();
                    }
                },
                //mouseout
                function() {
                    $.mouseIsOver[displayElement] = false;
                    if(!$.mouseIsOver[rollElement])
                    {
                        $('#' + displayElement).hide();
                    }
                }
            ) //end hover
        },
        
        //generic function that does a simple show and a delayed fade
        showDelayFade: function(rollElement, displayElement, startDelay, endDelay) {
            if(!endDelay)
            {
                endDelay = 0;
            }
            $('#' + rollElement).hover(
                //mouseover
                function() {
                    $.waitingElements[displayElement] = false;
                    do_show = function() {
                        $('#' + displayElement + ':animated').fadeIn('fast');
                        $('#' + displayElement + ':hidden').show();
                        $('#' + displayElement + ':animated').show();
                    }
                    if(startDelay)
                    {
                        setTimeout(do_show, startDelay);
                    }
                    else
                    {
                        do_show();
                    }
                },
                //mouseout
                function() {
                    $.waitingElements[displayElement] = true;
                    setTimeout(
                        function()
                        {
                            if($.waitingElements[displayElement])
                            {
                                $('#' + displayElement).hide();
                            }
                        }, 
                        endDelay
                    );
                }
            ) //end hover
        },
        
        //function that prints page given a print class
        printPage: function() {
          
          $('a.print').each(function() {
            
                $(this).click(function() {
                    
                    window.print();
                    
                    return false;  
                });
            
            });
            
        },
        
        //shows a random image on the homepage
        randomShow: function() {
           //find number of images
           var n = $('#home_images img').length;
           //generate random number
           var random = Math.floor(Math.random()*n);
           //show random image in the list
           $('#home_images img:eq(' + random + ')').show();
        },
    
        //activates rollover of theme artwork info on grid, takes  the table id as a parameter
        artworkInfo: function(artTable) {
            $('#' + artTable + ' td').each(function() {
                
                $(this).hover(
                    
                   function() {
                    $(this).find('span.art_info').show();
                      //$(this).find('span.art_info').fadeIn('fast');
                      //add z-index for ie7
                      $(this).css('z-index', '4');
                    },
                    
                    function() {
                        $(this).find('span.art_info').hide();
                       //revert z-index
                       $(this).css('z-index', '0');
                    }
                    
                ); //end hover 
                $(this).bind(
                    'mousemove',
                    function(e)
                    {
                        art_info = $(this).find('span.art_info'); 
                        img = $(this).find('img'); 
                        offset = $(this).offset();
                        if(img.length > 0)
                        {
                            img_position = $(img).position();
                        }
                        else
                        {
                            img_position = {left: 0, top: 0};
                        }
                        mouse_displacement = 10;

                        if(e.pageX + mouse_displacement + 2 + $(art_info).outerWidth() > $(window).width())
                        {
                            art_info.css('left',e.pageX - offset.left + img_position.left - $(art_info).outerWidth() - mouse_displacement);
                        }
                        else
                        {
                            art_info.css('left',e.pageX - offset.left + img_position.left + mouse_displacement);
                        }

                        if(e.pageY + mouse_displacement + 2 + $(art_info).outerHeight() > $(window).height())
                        {
                            art_info.css('top',e.pageY - offset.top + img_position.top - $(art_info).outerHeight() - mouse_displacement);
                        }
                        else 
                        {
                            art_info.css('top',e.pageY - offset.top + img_position.top + mouse_displacement);
                        }
                    }
                );
                    
            }); //end each
        },
        
        //function that switches tabs in artwork browsing
        browseTabs: function() {
           
           $('#filter_nav li').each(function() {
                var myIndex = $('#filter_nav li').index(this);
                $(this).children('a').click(function() {
                    
                    //hide currently visible content
                    if (!$(this).parent('li').hasClass('selected')) {
                        
                        //hide visible content
                        $('#filter .content:visible').hide();
                        
                        //show corresponding content
                        $('#filter .content').eq(myIndex).show();
                        
                        //switch class 'selected' to this menu item
                        $('#filter_nav li.selected').removeClass('selected');
                        $('#filter_nav li').eq(myIndex).addClass('selected');
                    }
                    
                   // $('#filter .content').eq(myIndex).show();
                    
                    
                    return false;
                 }); //end click
            }); //end each
        },
        verticalAlign: function(alignElement) {
            elementHeight = $(alignElement).height();
            parentHeight = alignElement.parent().height();
            var top;

            if(alignElement.offsetParent() == alignElement.parent())
            {
                top = alignElement.position().top;
            }
            else
            {
                top = alignElement.position().top - alignElement.parent().position().top;
            }

            var new_top = ((parentHeight - elementHeight) / 2) - top;

            alignElement.css('position', 'relative');
            alignElement.css('top', new_top + 'px'); 
            
        },
        flashPath: {
            'video_path': 'rtmp://cp83814.edgefcs.net/ondemand/',
            'caption_path': 'http://cdn-origin.peg.tv/cc/T01719/',
            'file_map': '/courses/globalart/flash/episode_urls.xml'
        },
        zoomify: true
    }
    
}();

$(document).ready(function() {
   
   //activate top navigation theme list rollover
   WNET.fadeDisplay('themeRollover', 'themeList');
   
   //activates print links
   WNET.printPage();
   
   //activates rollover of artwork info on artwork grid
   if (WNET.exists('art_grid')) {
    WNET.artworkInfo('art_grid');
   }
   
   //activates rollover of artwork info on explore grid
   if (WNET.exists('browse_grid')) {
    WNET.artworkInfo('browse_grid');
   }
   
   //activate rollover display of artwork grids
   if (WNET.exists('grid_container')) {
    WNET.fadeDisplay('theme_artwork_link', 'grid_container');
   }
   
   //activate tabs in browse artwork page
   if (WNET.exists('filter_nav')) {
    WNET.browseTabs();
   }

   //align the compare button
   if($('.compare_button .question').get(0))
   {
    WNET.verticalAlign($('.compare_button .question'));
   }

   if($('.print').length)
   {
    $('.print').css('visibility', 'visible');
   }


   $("body").mousemove(
       function(e) {
          target = e.target;
          found_theme_list = false;
          while(target && !found_theme_list)
          {
            if(target.id && target.id == 'themeRollover')
            {
              found_theme_list = true;
              break;
            }
            target = target.parentNode;
          }
          if(!found_theme_list)
          {
            $('#themeList').hide();
          }

          target = e.target;
          found_grid_container = false;
          while(target && !found_grid_container)
          {
            if(target.id && target.id == 'theme_artwork_list')
            {
              found_grid_container = true;
              break;
            }
            target = target.parentNode;
          }
          if(!found_grid_container)
          {
            $('#grid_container').hide();
          }
       }
   );

  $('.compare_link .image img').click(
    function(el)
    {
      window.location.href = $(this).parents('a').get(0).href;
    }
  );
   
});

