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



/**
 * Hover state of 1-2 Tout buttons on homepage
 * 
 */

$.fn.tout_button_hover = function(elem) {
  $(elem).each(function() {
    $(this).mouseover(function() {

    	if($(this).html() == "1") {
    	  $(this).css('background','#6E82AF');
    	} else if($(this).html() == "2") {
    	  $(this).css('background','#2e4884');
    	}
      });
  });
}



/**
 * 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");
  
/* Open all drawers by default */
  if(accordions.length > 1)
  {
    $.each(accordions, function(i,n) {
        $(this).addClass("accordion-h-open");
        $(this).next(".accordion").addClass("accordion-open").show();
    });
  }  
   
  $(".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>Hide all</span>");
  showHideTag.toggle(
  	function() {$(this).accordion_close(".accordion-h");},
    function() {$(this).accordion_open(".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() {}
    );
  });
}


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

