jQuery(document).ready(function() {
 
    init()
});

///////////////////////////////////////////////////////////////////
// PERMISSION LINK STUFF
function RightslinkPopUp( title, date, author, contentID, volumeNum, issueNum, startPage, endPage )
{
    var url = "https://s100.copyright.com/AppDispatchServlet";
    var location = url
        + "?publisherName=" + escape("BMJ")
        + "&publication=" + escape("sbmj")                               
        + "&title=" + escape(title)
        + "&publicationDate=" + escape(date)
        + "&author=" + escape(author)
        + "&contentID=" + escape(contentID)
        + "&volumeNum=" + escape(volumeNum)
        + "&issueNum=" + escape(issueNum)
        + "&startPage=" + escape(startPage)
        + "&endPage=" + escape(endPage)
        + "&orderBeanReset=true";
    PopUp = window.open( location,'Rightslink','location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=650,height=550');
}
///////////////////////////////////////////////////////////////////
// SEARCH STUFF
function populateElement(selector,defaultSelector)
{
    var defvalue = jQuery(defaultSelector).val();
    jQuery(selector).val(defvalue);
       
};
    
function clearSearch(selector,defaultVal,alertVal)
{
    jQuery(selector).css({color:"#777777"});
    searchBox = jQuery.trim(jQuery(selector).val());
    if(searchBox == defaultVal || searchBox == jQuery.trim(jQuery(alertVal).val()))
    {
        jQuery(selector).val("");
    }
}    

function showSearch(selector,defaultSelector)
{
    var defvalue = jQuery(defaultSelector).val();
    if(jQuery.trim(jQuery(selector).val())=="")
    {
        jQuery(selector).val(defvalue);
    }
}   

function liveSearchSubmit(formIdentifier,defaultVal,alertVal)
{
  var searchTerm = jQuery.trim(jQuery("#"+formIdentifier+" .text").fieldValue()[0]);
  if(searchTerm == "" || searchTerm == defaultVal || searchTerm == jQuery.trim(jQuery(alertVal).val()))
  {
        jQuery("#"+formIdentifier+" .text").css({color:"#CC0000"});
        jQuery("#"+formIdentifier+" .text").attr("value",jQuery.trim(jQuery(alertVal).val()));
        return false;
  }
}


///////////////////////////////////////////////////////////////////
var toggleGuidline = function(e)
{
var guidLineDive = $(".guidlines");
    if (guidLineDive.is(':visible'))
    {
        guidLineDive.hide();
    }
    else
    {
        guidLineDive.show();
    }
    
    return false;
}
//////////////////////////////////////////////////////////////////
//email a friend
var toggleRecomendFriend = function() {
    $(".recommendFriend").toggle(400);
    return false;
}
//////////////////////////////////////////////////////////////////
//Navigation Bar
function  showNaviGationBar()
 {
   $("#marketing").hide();
   $("#sub-nav").show();
 }

 function  hideNaviGationBar()
 {
   $(".subNav a").removeClass("active");
   $("#"+sectionName+" a").addClass("active");  
   $("#sub-nav").hide();
   var subNavClass = $("#sub-nav").attr("class");
   $("#"+subNavClass+"-ul").hide();          
   $("#"+sectionName+"-ul").show();
   $("#sub-nav").removeClass(subNavClass).addClass(sectionName).show();
   if(sectionName == 'home')
   $("#marketing").show();
 }
 

 function setNaviGationBar(tabId)
 {
         var subNavClass = $("#sub-nav").attr("class");
         $("#sub-nav").removeClass(subNavClass).addClass(tabId);
         $(".subNav a").removeClass("active");
         var tabLink = "#"+tabId+" a";
         $(tabLink).addClass("active");
         $("#"+subNavClass+"-ul").hide();         
         $("#"+tabId+"-ul").show();
 }

 function resetNaviGationBar(tabId)
 {  
  //$("#marketing").hide();
 }
//////////////////////////////////////////////////////////////////
//expand-collapsy
function enableExpandables() {
    // Collapse the subsections
    $(".expandable dd").hide();
     // Apply styles
    $(".expandable dt").css("cursor", "pointer");
    $(".expandable dt").addClass("collapsed");
    $(".expandable dt").each( function() {
        setExpandableStyleClass(this);
    });
    // Set the click event handler on subsections.
    $(".expandable dt").click( function(e) {
            // e.target could be the dt or could be a sub-element:
            clickedThing(e, "dt").next("dd").toggle();
            setExpandableStyleClass(this);
        });
    // add buttons to show/hide all:
    jQuery(".expandable").each(
            function() {
                jQuery(this).parents("div.topic-collections").slice(0, 1).click(showall).keydown(function(f){
                 if (f.keyCode == 13) {
                  showall(f)
                  }
                }).end().removeClass("allopenable");              
            });
}


/* 
 * Expands the expandable section based on the anchor in the URL.
 * The anchor id has to be expsec-? where ? can be any number.
 * The anchor has to be a direct child of the expandable dt element.
 */
function expandDefaultExpandable() {
    var hash = location.hash;
    if (hash.indexOf("expsec") != -1) {
        $(hash).parents("dt").click();
    }
}

/* 
 * Checks if any siblings of the 'expandableElement' node are hidden 
 * and sets it's style class accordingly. 
 */
function setExpandableStyleClass(expandableElement) {
    var hiddenElements = $("dd:hidden", expandableElement.parentNode);
    if (hiddenElements.length > 0)      
    {
        $(expandableElement).addClass("collapsed");
        $(expandableElement).removeClass("expanded");
    } else {
        $(expandableElement).addClass("expanded");
        $(expandableElement).removeClass("collapsed");
    }
}

function hoverExpandable()
{    
    $("dt").filter(".collapsed").hover(function() {
        $(this).css({color: "#f0f"});
    }, function() {
        $(this).css({color: "#009AF6"});
    });
}
var showall = function(e) {
    e.stopPropagation();
    var target = clickedThing(e, "span.showall");
    if (target.length == 0)
    {
        return;
    }
    target.addClass("collapse-all");
    target.removeClass("expand-all");
    // Hide the subsections and set the style class on the 'dt' element.
    target.parents("div").slice(0, 1).find(".expandable dd").show();
    target.parents("div").slice(0,1).find(".expandable dt")
        .each(function () { setExpandableStyleClass(this); });
   
    target.unbind("click", showall).text("hide all").click(closeall);  
}

var closeall = function(e) {
    e.stopPropagation();
    var target = clickedThing(e, "span.showall");
    if (target.length == 0)
    {
        return;
    }
    target.addClass("expand-all");
    target.removeClass("collapse-all");
    // Hide the subsections and set the style class on the 'dt' element.
    target.parents("div").slice(0, 1).find(".expandable dd").hide();
    target.parents("div").slice(0,1).find(".expandable dt")
        .each(function () { setExpandableStyleClass(this); });
    
    target.unbind("click", closeall).text("show all").click(showall);
}
//////////////////////////////////////////////////////////////////
function clickedThing(e, type) {
    var el = $(e.target);
    if (!el.is(type)) {
        el = el.parents(type).slice(0, 1);
    }

    return el;
}
//////////////////////////////////////////////////////////////////
function init()
{
    populateElement("#groupSearchGadget","#defaultTextAll");
    populateElement("#search","#defaultText");
    $(".guidesLink").click(toggleGuidline);
    $(".recommend").click(toggleRecomendFriend);
    
     $(".subNav").hover(
      function () {
        setNaviGationBar($(this).attr("id"));
      }, 
      function () {
        resetNaviGationBar($(this).attr("id"));
      });
    
     $("#main-nav-wrap").hover(
      function () {
         showNaviGationBar();
      }, 
      function () {
        hideNaviGationBar();
      });
     $("span").filter(".showall").text("show all").addClass("expand-all").css("cursor", "pointer");
     enableExpandables();
     expandDefaultExpandable();
     hoverExpandable();
     // Set tooltips
     jQuery('.jstooltip').tooltip(
	 { 
		track: true, 
	    delay: 0, 
	    showURL: false,
	    fade: 160,
	    //showBody: "||",
	    fixPNG: true
	 });
      
 }
