$(document).ready(function(){

	//alert("Render mode: "+ document.compatMode + "\nUser Agent: " + navigator.userAgent);
	
	
	//First, some css that couldn't be achieved with css selectors
	$("table:not(.ds-includeSet-metadata-table) tr td:has(span[class=bold])").css({ textAlign:"right", verticalAlign:"top" });
	$("table.ds-includeSet-metadata-table tr td:has(span[class=bold])").css({ textAlign:"left", verticalAlign:"top" });
	$("td.ds-table-cell:has(strong)").css({ backgroundColor:"#DDDDDD", padding:"3px" });
	$("form#aspect_submission_Submissions_div_submissions div#aspect_submission_Submissions_div_start-submision p.ds-paragraph a").css({fontWeight:"bold", color:"#374F5D"});
	$("div#aspect_artifactbrowser_CollectionViewer_div_collection-view div.detail-view + p.ds-paragraph a").css({backgroundColor:"#2E6DA2", color:"white", border:"3px outset white", padding:"3px"});
	$("div#aspect_artifactbrowser_CollectionViewer_div_collection-view div.detail-view + p.ds-paragraph").css({marginTop:"15px"});
	
	/* added in order to fix disappearing text in IE8 compatibility-mode */
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view li.ds-artifact-item").css("position", "relative");

	
	//eliminate empty menus; div.menuslider div.ds-option-set whose ul contains no li - was used for the unpopular hover-accordion menu sliders
	/*
	$("div.menuslider:has(div.ds-option-set:has(ul.ds-option-list:not(:has(li))))").remove();
	$("div.menuslider:has(div:has(ul:not(:has(li))))").css({ border:"5px solid red"});
	*/
	
	//eliminate ListPlus and ListMinus for the community/collection hierarchy when there is no ul contained in the li.
	//replace them with spacers to make the hierarchy look flush
	//div#aspect_artifactbrowser_CommunityViewer_div_community-view
	//div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser	
	$("div.ds-static-div li:not(:has(ul))").children("p.ListPlus").after("<p class=\"ListEmpty\"></p>");
	$("div.ds-static-div li:not(:has(ul))").children("p.ListMinus, p.ListPlus").remove();
	
	//close the community/collection hierarchy by default
	$(document).hideAllCommColl();
	//close to second level the community/collection hierarchy on community view page.
	$(document).hideCommCollInCommViewTo2ndLevel();
	
	//show the Expand All link
	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p#expand_all_clicker").show();
	$("div#aspect_artifactbrowser_CommunityViewer_div_community-view p#expand_all_clicker").show();
	
	//eliminate the expansion clickers when they appear in the context of the front page
	$("div#right_content_column div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p#expand_all_clicker, div#right_content_column div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p#collapse_all_clicker").remove();
	
	//open "Browse" menu option by default
	/*
	$("h3#menu_heading_0").removeClass("closed");
	$("h3#menu_heading_0").addClass("open");
	$("h3#menu_heading_0").next().show()
	$("h3#menu_heading_0").find("img.menu_nav_icon_open").show();
	$("h3#menu_heading_0").find("img.menu_nav_icon_closed").hide();*/
	//first, close everything.
	$("h3.ds-option-set-head-clickable").siblings("div.ds-option-set").hide();
	$("h3.ds-option-set-head-clickable").addClass("closed");
	$("h3.ds-option-set-head-clickable").removeClass("open");
	$("h3.ds-option-set-head-clickable").find("img.menu_nav_icon_open").hide();
	$("h3.ds-option-set-head-clickable").find("img.menu_nav_icon_closed").show();
	//then, open just the "Browse" option.	
	$("h3#menu_heading_0").siblings("div.ds-option-set").show();
	$("h3#menu_heading_0").addClass("open");
	$("h3#menu_heading_0").removeClass("closed");
	$("h3#menu_heading_0").find("img.menu_nav_icon_open").show();
	$("h3#menu_heading_0").find("img.menu_nav_icon_closed").hide();
	
	//The unpopular hover-sliding collapsable menus on left navigation bar
	/*
	$("div.menuslider h3.closed").bind("mouseover",function(){
	    //hide any open sub-menu
	    $("h3.open").next().slideUp("slow");
		$("h3.open").find("img.menu_nav_icon_open").hide();
		$("h3.open").find("img.menu_nav_icon_closed").show();
	    $("h3.open").addClass("closed");
	    $("h3.open").removeClass("open");
	    
	    //show this sub-menu
	    $(this).next("div.ds-option-set").slideDown("slow");
	    $(this).addClass("open");
	    $(this).removeClass("closed");
		$(this).find("img.menu_nav_icon_open").show();
		$(this).find("img.menu_nav_icon_closed").hide();
		
		$(document).myReBind();
	});*/
		
	
	

	//Click to open sliders on the left menu
	$("h3.closed").toggle(function(){
	    $(this).addClass("open");
		$(this).removeClass("closed");
		$(this).next().slideDown("fast");
		$(this).find("img.menu_nav_icon_open").show();
		$(this).find("img.menu_nav_icon_closed").hide();
	},function(){					
	    $(this).addClass("closed");
	    $(this).removeClass("open");
		$(this).next().slideUp("fast");
		$(this).find("img.menu_nav_icon_open").hide();
		$(this).find("img.menu_nav_icon_closed").show();
	});
	//Additionally, handle the guys that starts off open.				
	$("h3.open").toggle(function(){
		$(this).addClass("closed");
	    $(this).removeClass("open");
		$(this).next().slideUp("fast");
		$(this).find("img.menu_nav_icon_open").hide();
		$(this).find("img.menu_nav_icon_closed").show();
	},function(){					
	    $(this).addClass("open");
		$(this).removeClass("closed");
		$(this).next().slideDown("fast");
		$(this).find("img.menu_nav_icon_open").show();
		$(this).find("img.menu_nav_icon_closed").hide();
	});
	
	
	//The navigation hover text contextual feedback at the top of the page	- these got removed from the design for aesthetic reasons.
	/*
	$("a.top-icon").hover(function(){
		$(this).find("span").each(function() {
			$(this).addClass("top-icon-text-shown");
		});
	},function(){
		$(this).find("span").each(function() {
			$(this).removeClass("top-icon-text-shown");
		});
	});
	*/
	
	
	
	//community/collection hierarchy
	//expansion with the plus sign (or horizontal arrow)
	$("p.ListPlus").click(function(){
		$(this).hide();
		$(this).next("p.ListMinus").show();
		if(navigator.userAgent.match("MSIE 6")) //slideDown animation doesn't work in IE6.
		{
		    $(this).parent().find("p.ListPlus").hide();
		    $(this).parent().find("p.ListMinus").show();
		    $(this).parent().find("p.ListMinus + span.bold ~ ul").show();
		}
		else
		{
		    $(this).parent().children("ul").slideDown("fast");
		}
		//$(this).parent().find("ul").css("min-height", "2 !important"); //Trying to get things to appear in IE6,7,8!
		//$(this).parent().find("li").css("min-height", "2 !important"); //Trying to get things to appear in IE6,7,8!
		
		// We were unable to get the sub-elements to appear correctly in IE8 compatibility mode.
		//$(this).parent().find("ul").css("display", "block"); //might be necessary for IE8 in compatibility mode which imperfectly pretends to be IE7
		//$(this).parent().find("ul").css("position", "relative");
		//$(this).parent().find("li").css("display", "block"); //might be necessary for IE8 in compatibility mode which imperfectly pretends to be IE7
		//$(this).parent().find("li").css("position", "relative");
		});				
	//contraction with the minus sign (or vertical arrow)
	$("p.ListMinus").click(function(){
		$(this).hide();
		$(this).prev("p.ListPlus").show();
		$(this).prev("p.ListPlus").css("display", "inline");
		if(navigator.userAgent.match("MSIE 6")) //slideUp animation doesn't work in IE6.
		{
		    $(this).parent().find("p.ListPlus").show();
		    $(this).parent().find("p.ListMinus").hide();
		    $(this).parent().find("p.ListMinus + span.bold ~ ul").hide();
		}
		else
		{
		    $(this).parent().children("ul").slideUp("fast");
		}
	});
    $("p#expand_all_clicker").click(function(){
        $(document).showAllCommColl();
        $(document).showSomeCommColl();
        $("p#expand_all_clicker").hide();
        $("p#collapse_all_clicker").show();
        });
    $("p#collapse_all_clicker").click(function(){
        $(document).hideAllCommColl();
        $(document).hideSomeCommColl();
        $("p#collapse_all_clicker").hide();
        $("p#expand_all_clicker").show();
        $("p#expand_all_clicker").css("display", "block");
        $("p.ListPlus").css("display", "inline");
        });
	
	
	
	
	
	//The metadata popups for ds-artifact-item-with-popup's
    $("div.item_metadata_more").toggle(function(){
		$(this).children(".item_more_text").hide();
		$(this).children(".item_less_text").show();
		$(this).next().slideDown();
	},function(){
		$(this).children(".item_more_text").show();
		$(this).children(".item_less_text").hide();
		$(this).next().slideUp();
	});


	
	//slider for filter search
	$("div#collapsible-filter-search-fields div.more_text").click(function(){
		$("div#collapsible-filter-search-fields div.more_text").hide();
		$("div#collapsible-filter-search-fields div.less_text").show();
		$("div#collapsible-filter-search-fields-table-container").slideDown("fast");
		});
	$("div#collapsible-filter-search-fields div.less_text").click(function(){
		$("div#collapsible-filter-search-fields div.more_text").show();
		$("div#collapsible-filter-search-fields div.less_text").hide();
		$("div#collapsible-filter-search-fields-table-container").slideUp("fast");
		});



    //the about text slider	
	$("p#rotating_header_more").click(function(){
		$("p#rotating_header_para").slideUpShow("slow");
		$("p#rotating_header_less").show();
		$(this).hide();
		});
	
	$("p#rotating_header_less").click(function(){
		$("p#rotating_header_para").slideDownHide("slow");
		$("p#rotating_header_more").show();
		$(this).hide();
		});
});



jQuery.fn.extend({
  slideDownHide: function(speed){
  	return this.animate({height: "hide", top: "0px"}, speed);
  },
  slideUpShow: function(speed,callback){
  	return this.animate({height: "show", top: "-50px"}, speed);
  },
  hideAllCommColl: function(){
  	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListPlus").show();
  	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListMinus").hide();
  	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListMinus + span.bold ~ ul").hide();
  },
  showAllCommColl: function(){
    $("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListMinus").show();
  	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListPlus").hide();
  	$("div#aspect_artifactbrowser_CommunityBrowser_div_comunity-browser p.ListMinus + span.bold ~ ul").show();
  },
  hideSomeCommColl: function(){
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListPlus").show();
  	$("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListMinus").hide();
  	$("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListMinus + span.bold ~ ul").hide();
  },
  showSomeCommColl: function(){
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListMinus").show();
  	$("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListPlus").hide();
  	$("div#aspect_artifactbrowser_CommunityViewer_div_community-view p.ListMinus + span.bold ~ ul").show();
  },
  hideCommCollInCommViewTo2ndLevel: function(){
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view > ul.ds-artifact-list > li > ul.ds-artifact-list p.ListPlus").show();
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view > ul.ds-artifact-list > li > ul.ds-artifact-list p.ListMinus").hide();
    $("div#aspect_artifactbrowser_CommunityViewer_div_community-view > ul.ds-artifact-list > li > ul.ds-artifact-list p.ListMinus + span.bold ~ ul").hide();
  }/*,
  //was used for the unpopular hover-sliders on the left menu
  myReBind: function(){
      $("div.menuslider h3.open").unbind("mouseover");
      
      $("div.menuslider h3.closed").bind("mouseover",function(){
	    //hide any open sub-menu
	    $("h3.open").next().slideUp("slow");
		$("h3.open").find("img.menu_nav_icon_open").hide();
		$("h3.open").find("img.menu_nav_icon_closed").show();
	    $("h3.open").addClass("closed");
	    $("h3.open").removeClass("open");
	    
	    //show this sub-menu
	    $(this).next("div.ds-option-set").slideDown("slow");
	    $(this).addClass("open");
	    $(this).removeClass("closed");
		$(this).find("img.menu_nav_icon_open").show();
		$(this).find("img.menu_nav_icon_closed").hide();
		
		$(document).myReBind();
	});
  }*/
});