/* 
*   DC Comics scripts
*
*
*   addLoadEvent()   = queues an action to window.onload
*   popUp()          = adds a popup function to all a tags with the class name "popup"
*
*/

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}

function popUp() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) 
  {
    if ((links[i].className != null) && (links[i].className == "popup")) 
    {
      links[i].onclick=function() 
      {
          window.open(this.href);
          return false;
      }
    }
  }
}

addLoadEvent(popUp);


/**
 * Accordion effects
 * 
 * Handles the expand/contract functionality of accordion menus 
 * 
 *
 */

$.fn.accordion_close = function(elem) {
	$(elem).removeClass("accordion-h-open").addClass("accordion-h-closed"); 
	$(elem).next(".accordion").removeClass("accordion-open").addClass("accordion-closed");
	$(elem).next(".accordion").animate({ height: 'hide', opacity: 'hide' }, 'fast');
	if(elem == ".accordion-h") {
	  $("#h2-div span").empty().append("Show all");
	}
}

$.fn.accordion_open = function(elem) {
	$(elem).removeClass("accordion-h-closed").addClass("accordion-h-open"); 
	$(elem).next(".accordion").removeClass("accordion-closed").addClass("accordion-open");
	$(elem).next(".accordion").animate({ height: 'show', opacity: 'show' }, 'fast');
	if(elem == ".accordion-h") {
	  $("#h2-div span").empty().append("Hide all");
	}
}

$(document).ready(function() {


  accordions = $("#main_content").children(".accordion-h");

  if(accordions.length > 1)
  {
    $.each(accordions, function(i,n) {

//      if(i==0 && $(this).parent().parent().parent().parent().parent().attr("id")=="graphic_novels")
//      {
//        $(this).addClass("accordion-h-open");
//        $(this).next(".accordion").addClass("accordion-open").show();
//      }
//      else
//      {
        $(this).addClass("accordion-h-closed");
        $(this).next(".accordion").addClass("accordion-closed").hide();
//      }  

    });
  }  
   
  $(".accordion-h").each(function() {
	$(this).click(function() {
      listClass = $(this).next(".accordion").attr("class");
	  if(listClass == "accordion accordion-open") {
		$(this).accordion_close(this);	    
	  } else {
	    $(this).accordion_open(this);
	  }
	})
  });
});

/*
$.fn.insert_show_all_button = function(page) {
  showHideTag = $("<span>Show all</span>");
  showHideTag.toggle(
    function() {$(this).accordion_open(".accordion-h");},
    function() {$(this).accordion_close(".accordion-h");}
  );
  $("#h2-div h2").after(showHideTag);
}
*/

/* 
$(document).ready(function() {
  showHideTag = $("<span>Show all</span>");
  showHideTag.toggle(
    function() {$(this).accordion_open(".accordion-h");},
    function() {$(this).accordion_close(".accordion-h");}
  );
  $("#h2-div.list-page h2").after(showHideTag);
});
*/


/**
 * SINGLE LIST: Book cover/image rollover effects
 *
 */

$.fn.hover_default_image_set = function(elem) {
  $(elem).each(function() {
    href = $(elem).find(".cover_rollover:first").find("ul:first").find(".first a").attr("href");
    imgPath = $(this).find(".first a").attr("name");
    img = $('<img />').attr('src', imgPath).addClass('hoverImgWithLink');
    link = $('<a></a>').attr('href', href).addClass('hoverpic'); 
    $(elem).find(".cover_rollover:first").find("ul:first").before(img);   
    $(this).find(".hoverImgWithLink").wrap(link);
  });
}

$.fn.hover_effect = function(elem) {
  $(elem).each(function() {
    $(this).hover(function() {
        $(document.body).find(".cover_rollover").find(".hoverpic").remove();
        href = $(this).attr("href");
        imgPath = $(this).attr("name");
        newLink = $('<a></a>').attr('href', href).addClass('hoverpic');
        newImg = $('<img />').attr('src',imgPath).addClass('hoverImgWithLink');
        $(document.body).find(".cover_rollover:first").find("ul:first").before(newImg);
        $(document.body).find(".hoverImgWithLink").wrap(newLink);
      },
      function() {}
    );
  });
}



/**
 * MULTI-LIST (ACCORDION): Book cover/image rollover effects
 *
 */

$.fn.hover_default_image_set_accordion = function(elem) {
  $(elem).each(function() {
    href = $(this).next(".accordion").find(".first").find("a").attr("href");
    imgPath = $(this).next(".accordion").find(".first").find("a").attr("name");
    img = $('<img />').attr('src', imgPath).addClass('hoverImgWithLink');
    link = $('<a></a>').attr('href', href).addClass('hoverpic');
    $(this).next(".accordion").find("ul").before(img);
    $(this).next(".accordion").find(".hoverImgWithLink").wrap(link);
  });
}

$.fn.hover_effect_accordion = function(elem) {
  $(elem).each(function() {
    $(this).hover(
      function() {
        $(this).parent().parent().parent().find(".hoverpic").remove();
        href = $(this).attr("href");
        imgPath = $(this).attr("name");			
        newLink = $('<a></a>').attr('href', href).addClass('hoverpic');
        newImg = $('<img />').attr('src',imgPath).addClass('hoverImgWithLink');
        $(this).parent().parent().before(newImg);		$(this).parent().parent().parent().find(".hoverImgWithLink").wrap(newLink);
      },
      function() {}
    );
  });
}


// Left hand Navigation effects

jQuery(document).ready(function() {
	$("#nav-site > li > ul").hide(); 
	// Toggle the selected menu's class and expand or collapse the menu
	$("#nav-site > li > a").click(function() {	
		$(this).parent().find("ul").slideToggle();
		return false;
	});
}); // end jQuery



// Home page tout2 hover effects

jQuery(document).ready(function() {
	$("#home #tout2 #left").hover(function() {	
		$("#tout2").css({background:"transparent url(i/bg-toutgrad.jpg) left top repeat-y" });
	}, 
      function () {
      	$("#tout2").css({background:"none" });
    });
    $("#home #tout2 #right").hover(function() {	
		$("#tout2").css({background:"transparent url(i/bg-toutgrad.jpg) right top repeat-y" });
	}, 
      function () {
      	$("#tout2").css({background:"none" });
    });
}); // end jQuery



$(document).ready(function() {
  $(this).hover_default_image_set("#comics");
  $(this).hover_effect("#comics .cover_rollover ul li a");
  $(this).hover_default_image_set_accordion("#graphic_novels .accordion-h");
  $(this).hover_effect_accordion("#graphic_novels .accordion ul li a");   
});



// This assigns the background images to the tout navigation buttons.
jQuery(document).ready(function() {
  jQuery('#nav-t1 > a').each(function() {
    // Note:  the variables for tout_thumbs[tout_thumbs_i] are created and populated in the JS in the Head of the main index page
    //jQuery(this).css("background-image","url("+tout_thumbs[tout_thumbs_i]+")");
    jQuery(this).html("<img src=\""+tout_thumbs[tout_thumbs_i]+"\" alt=\"\">");

    tout_thumbs_i++;
  });
});




















