$().ready(function(){
   var top = 25;
   $(".pdf img").hover(
         function()
         {
          this.src = this.src.replace("DownloadPDF","DownloadPDFRollover");
         },
         function()
         {
          this.src = this.src.replace("DownloadPDFRollover","DownloadPDF");
         }
        );
   var icons = new Array();
   var studies = new Array();
   var count = 1;
   $(".icon").each(function(){
       icons.push(new icon(top, this));
       top += 64;
   });
   $(".study").each(function(){
       studies[$(this).attr("id")] = $(this);
   });
   var active_tab = new tab(icons[0].top_pos, $("#tab"));
   var active_icon = $(".activeStudy").attr("id")+"_icon";
   active_tab.set_pos($("#"+active_icon).css("top"));
   inter = setInterval(function(){
       switchTween(icons[count++%icons.length].elem, active_tab);
   },7000);
   $(".icon").click(function(){
       $(".icon").unbind("click");
       clearInterval(inter);
        switchTween($(this), active_tab);
   })
});

function icon(top, ele)
{
    this.top_pos = top;
    this.elem = ele;
    $(this.elem).css("top", this.top_pos);
}

function tab(top, ele)
{
    var top_pos = top-6;
    var elem = ele;
    $(elem).css("top", top_pos);
    this.set_pos = function(pos)
    {
        var new_pos = pos.substring(0,pos.length-2)-6;
        $(elem).animate({top:new_pos, duration:1});
    }
}

function switchTween(mov, active_tab)
{
       var movie_to_load = $(mov).attr("id");
       $(".activeStudy").fadeOut("slow",function(){
           $(this).removeClass("activeStudy");
           $("#"+movie_to_load.substr(0, movie_to_load.length-5)).fadeIn("slow", function(){
               $(this).addClass("activeStudy");
               $(".icon").click(function(){
                   $(".icon").unbind("click");
                   clearInterval(inter);
                    switchTween($(this), active_tab);
               });
           });
       });
       $("#loaded_info").html("<div id='bottom_content'>"+$("."+movie_to_load.substr(0, movie_to_load.length-5)).html()+"</div>");
       $(".pdf img").hover(
         function()
         {
          this.src = this.src.replace("DownloadPDF","DownloadPDFRollover");
         },
         function()
         {
          this.src = this.src.replace("DownloadPDFRollover","DownloadPDF");
         }
        );
       addScroll($("."+movie_to_load.substr(0, movie_to_load.length-5)));
       active_tab.set_pos($(mov).css("top"));
       
       
}

function addScroll(ele)
{
    $("#bottom_content").each(function(){
        if(this.scrollHeight> 350)
        {
            $('#bottom_content')
            .bind('mousewheel', function(event, delta) {
                if(delta < 0)
                {
                    newpos = ($(".bot_handle").position().top) + 30;
                    if(newpos > 370 - ($(".bot_handle").height()))
                        newpos = 370 - ($(".bot_handle").height());
                }else
                {
                    newpos = ($(".bot_handle").position().top) - 30;
                    if(newpos < 0)
                        newpos = 0;
                }

                $(".bot_handle").css("top", newpos);
                var percentage = ((newpos*100)/$(".bot_handle").parent().height())/100;
                $("#bottom_content").scrollTop($("#bottom_content").attr("scrollHeight")*percentage);
                return false;
            });

            $(this).parent().prepend("<div class='scroll_vert height_350'><div class='handle bot_handle'></div></div>");
            $(".bot_handle").css("height",(350*((350*100)/this.scrollHeight)/100));
            $('.bot_handle').draggable({ axis: 'y', containment: 'parent', cursor: 'pointer', scroll: true, drag: function(event, ui){
                            var percentage = ((ui.position.top*100)/$(this).parent().height())/100;
                            $("#bottom_content").scrollTop($("#bottom_content").attr("scrollHeight")*percentage);
                        }
                     });
        }
    })
}