/* 
*   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);
	  }
	})
  });
});


$(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);
});



/**
 * Book cover/image rollover effects
 *
 */

$.fn.hover_default_image_set = function(elem) {
    $(elem).each(function() {
    imageStatus = $(this).next(".accordion").find(".first").find("a").attr("class");

    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 = 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() {}
	  );
	  });
}


// DEFAULT CASE
/* jQuery(document).ready(function() {
  $(document).accordion_open("#green");
}); */


/*

$(document).ready(function() {
  $(this).hover_default_image_set("#comics .accordion-h");
  $(this).hover_default_image_set("#heroes_and_villains .accordion-h");
  $(this).hover_effect("#comics .accordion ul li a");
  $(this).hover_effect("#heroes_and_villains .accordion ul li a");   
});

*/
