$(document).ready(function() {
   $(".normalimage").fancybox();

   function refreshReady(item) {
     $.jGrowl.defaults.closer = false;
     $('#growlcontainer').jGrowl('close');
     $(".comparecontrol").removeAttr("checked");

     setTimeout(function() { 
       $("#growlcontainer").jGrowl("<input id=\"growlrefresh\" class=\"ready\" type=\"submit\" value=\"REFRESH\"/>", {
         theme: 'skeptugrowl',
         sticky: true,
         open: function(e,m,o) { e.find("#growlrefresh").click(function() { $("#refreshbutton").click(); }); }
       });
     }, 200);

     if(!$("#refreshbutton").hasClass("refreshnowready")) {
       $("#refreshbutton").addClass("refreshnowready");

       $(".sortbutton").each(function() {
         $(this).removeAttr('href');
         $(this).css("cursor", "pointer");
         $(this).click(function() { alert("You must refresh before sorting columns."); });
       });
     }

   }

   /* Query page refresh code */
   // => Only show when on querypage or skeptu_comparison page
   if($("#skequel_query").get(0) != null || $("#skeptu_comparison").get(0) != null) {
     // Make popups appear for (i) links, skequel query column and predicate headers.
     $(".loader").click(function() {
        linktoload = this.href + "/plain";
        setTimeout(loadpopup, 1);
        return false;
     });

     $("input.disabled").click(function() {
       $(this).css("background-color", "white");
       $(this).removeClass("disabled");
       $(this).prev().prev().attr('checked', 'true');
       $(this).select();
     });
     $("input.disabled").css("background-color", "#CECECE");


     // When a text box is changed refresh + check the text box
     $(":input.skequelcontrol").change(function() {
       refreshReady($(this));
       
       if($(this).hasClass("filterNumber")) {
          $(this).prev().prev().attr('checked', 'checked');
       }
     });

     // Collapse all the propertyitems
     $(".propertyitems").each(function() {
       item_number = $(this).children().size();
       if(item_number > 3) {
         $(this).children().hide();
         $(this).children(":first-child").show();
         $(this).after("<input class='morebutton' type='button' value='see more &gt;&gt;' />");
         $(this).next().click(function() {
           $(this).prev().children().show();
           $(this).hide();
         });
       }
     });

     // Collapse all of the sidebar items which do not have at least one checked box.
     $("#skequelHTML li.sks-header").each(function() {
       //item_number = $(this).children().size();
       skequelGroupClassName = $(this).next().attr('className');
       number_of_checked_boxes_in_group = $("." + skequelGroupClassName + " input:checked").size();

       if(number_of_checked_boxes_in_group == 0) {
         skequelGroupSelector = $("." + skequelGroupClassName);
         skequelGroupSelector.hide();
         $(this).children(":first-child").append(" <input class='morebutton' type='button' value='more &gt;&gt;' />");
         $(this).children(":first-child").children("input").click(function() {
           detectedGroupClassName = $(this).parent().parent().next().attr('className');
           $("." + detectedGroupClassName).show();
           $(this).hide();
         });
       }
     });
   }

   function loadpopup() {
     $.fancybox({
         'hideOnOverlayClick': false,
         'hideOnContentClick': false,
         'overlayShow': false,
         'showNavArrows': false,
         'type' : 'ajax',
         'href' : linktoload,
         'onComplete' : function() {
            $("#fancybox-content .loader").click(function() {
              linktoload = $(this).attr('href') + "/plain";
              setTimeout(loadpopup, 1);
              return false;
            });

            $("#fancybox-content .normalimage").fancybox();
            $("#fancybox-content .sit_qi").add(".sit_do").each(function() {
              href = $(this).attr('href');
              text = $(this).text();
              $(this).after(text + " <a target=\"_new\" href=\"" + href + "\">(open in new window)</a>");
              $(this).remove();
            });
          }
        });
   }


   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */

   // Scrolly headers
   if($("#skequel_query").get(0) != null) { $(window).load(function() { 

     var top = $(".coltitleheader").offset().top + 1;
     var left = $(".coltitleheader").offset().left + 1;
     var offset = $(".coltitleheader").offset();
     var headerheight = $(".coltitleheader").height();
     var number_of_columns = $(".coltitleheader th").size();


     var currentHeaderWidths = new Array();
       $(".coltitleheader").children().each(function() {
       currentHeaderWidths.push($(this).width());
     });

     $(".coltitleheader").css('height', $(".coltitleheader").height());
     $(".coltitleheader").css('width', $(".coltitleheader").width() - 1);
     $(".coltitleheader").css({position: 'absolute'});
     $(".coltitleheader").offset({ top: top, left: left }); 


     $(".coltitleheader").before($("<tr id=\"dropinheader\"><th colspan='" + (number_of_columns + 1) + "'></th></tr>"));
     $("#dropinheader").height(headerheight);


     reverseCurrentHeaderWidths = currentHeaderWidths.reverse();

     $(".coltitleheader").children().each(function() {
       $(this).css('width', reverseCurrentHeaderWidths.pop());
     });
    
     $(window).scroll(function() {
       if ($(window).scrollTop() > offset.top) {
         $(".coltitleheader").stop().animate({  marginTop: $(window).scrollTop() - offset.top });
       } else {
         $(".coltitleheader").stop().animate({ marginTop: 0 });
       };
     });
  
   }); } // we need to wait until all the images are loaded

   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */

   $(".datarow").children(".column-in").children("a").children("img").css("opacity", "0.2");

   $(".datarow").mouseover(function() {
     iicon = $(this).children(".column-in").children("a").children("img");
     if(iicon.css("opacity") < 0.7) {
       iicon.stop(); iicon.fadeTo("slow", 0.7);
     }
   });


   $(".datarow").mouseout(function() {
     iicon = $(this).children(".column-in").children("a").children("img");
     iicon.stop();
     iicon.fadeTo("slow", 0.2);
   });

   $(".datarow").children(".column-in").children("a").children("img").mouseover(function() {
     $(this).stop();
     $(this).css("opacity", "1.0");
   });

   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */
   /* ==================================================================== */
});

