$(document).ready(function()
{
  
  /* init Events in content and bubbles */
  initEvents();
  
});

/* Init basic events an effects */
  function initEvents()
  {
    if(document.all) // BUTTON RESIZER FOR IE //
    {
      var buttons = document.getElementsByTagName("button");
      for(var i = 0; i < buttons.length; i++)
      {
        thisButton = buttons[i];
        if(thisButton.getElementsByTagName("strong").length > 0)
        { thisButton.style.width = thisButton.getElementsByTagName("strong")[0].offsetWidth + "px"; }
      }
    }

    // Booknavi
    $("#morebooks a").click(function(){ return showBook($(this)); });
    
    // SUBNAVI BUBBLES
    $("#subnavi a:not([onclick])").click(function(){ return showBubble($(this)); });
    $(".metanavi a:not([onclick])").click(function(){ return showBubble($(this)); });

    //imagescroller
    initImgScroller();
    
    //playlists
    $("#playlist a").click(function(){ return playTitle($(this)); });
    
    //videos 
    if($("#videobox").html())
    {
      window.setTimeout(loadVideo, 200);
    }
    
    // ecardlinks
    $("#ecardmotive a").click(function(){ $("#bubble").load($(this).attr("href") + " #bubblecontent", initEvents); return false; });
    
    //links in bubbles
    $("#bubble a.more:not(.download):not([onclick])").click(function()
    { 
      if($(this).attr("href").search(/#/) < 0)
      { $("#bubble").load($(this).attr("href") + " #bubblecontent", initEvents); }
      else
      { $("#bubblecontent").load($(this).attr("href").replace(/#/, " #"), initEvents); }
      
      return false; 
    });
    
    $("#bubble form").submit(function()
    { 
     $(this).parent().load($(this).attr("action") + " #formresult>*", $(this).serializeArray(), initEvents);
      return false; 
    });
  }

/** BUBBLES **/
  var bubbleSrc;
  var thisBubble = new Object();
  function showBubble(callerObj, bubbleType)
  {
    callerObj.blur();
    var bubbleCode = "<div id=\"bubble\" style=\"position: absolute\"><div id=\"bubblecontent\"></div></div>";
    thisBubble.callerObj = callerObj;
    thisBubble.top = (callerObj.offset().top + Math.round(callerObj.height() / 2) - 17) + "px";
    if(!callerObj.parents(".metanavi").html())
    { thisBubble.type = 1; } // subnavi, arrowpos right
    else if(callerObj.parents("#metanavi1").html())
    { thisBubble.type = 2; } // metanavi1, arrowpos top
    else if(callerObj.parents("#metanavi2").html())
    { thisBubble.type = 3; } // metanavi1, arrowpos bottom

    if(thisBubble.type == 1)
    { 
      var newSrc = callerObj.attr("href") + " #bubblecontent>*"; 
      thisBubble.width = "600";
    }
    else
    { 
      var newSrc = callerObj.attr("href") + " #contentbox>*"; 
      thisBubble.width = "775";
    }
    
    if(bubbleSrc != newSrc) // New bubble?
    {
      hideBubble();
      bubbleSrc = newSrc;
      $("#page").append(bubbleCode);
      $("#bubble").hide();
      $("#bubblecontent").load(bubbleSrc, initBubble );
      if(callerObj.parents("#metanavi1").html())
      { $("#metanavi1").css("z-index", 100); }
      else
      { $("#metanavi1").css("z-index", 0); }
      callerObj.parents("li").addClass("bubble on");
    }
    
    return false;
  }
  
  function initBubble()
  {
    $("#bubbleclose a").click(function(){ return hideBubble(); });
    $("#bubble").css("width", thisBubble.width + "px");

    if(thisBubble.type == 1)
    { 
      thisBubble.top = (thisBubble.callerObj.offset().top + Math.round(thisBubble.callerObj.height() / 2) - 17);
      thisBubble.left = 211; //(thisBubble.callerObj.parents("li").offset().left - thisBubble.width + 1);
    }
    else if(thisBubble.type == 2)
    {
      thisBubble.top = (thisBubble.callerObj.parents("li").offset().top + thisBubble.callerObj.parents("li").height() - 1);
      thisBubble.left = (thisBubble.callerObj.offset().left - $("#page").offset().left + Math.round(thisBubble.callerObj.width() / 2) + 27 - thisBubble.width);
    }
    else if(thisBubble.type == 3)
    {
      thisBubble.top = (thisBubble.callerObj.parents("li").offset().top - $("#bubble").height() + 1);
      thisBubble.left = (thisBubble.callerObj.offset().left - $("#page").offset().left + Math.round(thisBubble.callerObj.width() / 2) - 11);
      $("#bubbleclose").css("top", "auto");
      $("#bubbleclose").css("bottom", "8px");
      $("#bubbleclose").css("right", "auto");
      $("#bubbleclose").css("left", "10px");
    }
    
    $("#bubble").css("top", thisBubble.top + "px");
    $("#bubble").css("left", thisBubble.left + "px");

    $("#bubble").show();

    initEvents();
  }

  function hideBubble()
  {
    /* player fix */
    $("#mp3player *").stop();
    $("#mp3player").stop();
    $("#mp3player").remove("*");
    $("#mp3player").remove();

    bubbleSrc = "";
    $("#bubble").remove();
    $("#subnavi li").removeClass("bubble on");
    $(".metanavi li").removeClass("bubble on");
    
    return false;
  }

/** /MOUSEOVER - INFOBUBBLES **/


/** IMGSCROLLER - SCROLLABLE LIST **/
  function initImgScroller()
  {
    if($("#imgscroller").html())
    {
      $("#imgscroller.bookscroller").ready(function(){ $("#imgscroller.bookscroller #images a").click(function(){ return showBook($(this)); }); });
      $("#imgscroller.videoscroller").ready(function(){ $("#imgscroller.videoscroller #images a").click(function(){ return showVideo($(this)); }); });
      $("#imgscroller.ecardscroller").ready(function(){ $("#imgscroller.ecardscroller #images a").click(function(){ return showCard($(this)); }); });
      if($("#imgscroller.bookscroller").html())
      { var imgCount = 6; }
      else
      { var imgCount = 5; }
      
      if($("#imgscroller #images a").length <= imgCount)
      { 
        // to few images, sno scroller, center images
        $("#imgscroller .scroller a").remove();
        var allImgWidth = $("#imgscroller #images a").length * ($("#imgscroller #images a:first").width() + 20) - 20;
        $("#imgscroller #images a").css("left", (Math.round(($("#imgscroller #images").width() - allImgWidth) / 2) + 8) + "px");
      }
      else
      {
        // hide non-visible images, init scroller
        $("#imgscroller #images a:gt(" + (imgCount - 1) + ")").hide();
        $("#imgscroller #images a:lt(" + (imgCount - 1) + ")").css("margin-right", "20px");
        $("#scrollerleft a").click(function()
        {
          if(!($("#imgscroller #images a:animated").is(":animated")))
          {
            $("#imgscroller #images").prepend($("#imgscroller #images a:last"));
            $("#imgscroller #images a:eq(0)").css("margin-right", "20px");
            $("#imgscroller #images a:eq(" + imgCount + ")").css("margin-right", "0");
            $("#imgscroller #images a:eq(" + imgCount + ")").hide(400);
            $("#imgscroller #images a:first").show(400);
          }
          return false;
        });
        $("#scrollerright a").click(function()
        {
          if(!($("#imgscroller #images a:animated").is(":animated")))
          {
            $("#imgscroller #images a:eq(" + (imgCount - 1) + ")").css("margin-right", "20px");
            $("#imgscroller #images a:eq(" + imgCount + ")").css("margin-right", "0");
            $("#imgscroller #images a:eq(" + imgCount + ")").show(400);
            $("#imgscroller #images a:first").hide(400);
            window.setTimeout('$("#imgscroller #images").append($("#imgscroller #images a:first"));', 400);
          }
          return false;
        });
      }
    }
  }

/* BOOKS */
  function showBook(thisLink)
  {
    if(thisLink.attr("class").search(/on/) == -1)
    {
      $("#imgscroller #images a").removeClass("on");
      // trigger link could be extern, filter by url.
      $("#imgscroller #images a[href='" + thisLink.attr("href") + "']").addClass("on");
      $("#bookdetail").load(thisLink.attr("href") + " #bookdetail>*", reloadContent);
      $("#marginal").load(thisLink.attr("href") + " #marginal>*", initEvents);
    }
    thisLink.blur();
    return false;
  }
  
  function reloadContent() 
  {
    hideBubble();
    if(document.all)// IE skaling fix
    { $("#content").prepend($("#contentbox")); }
  }
/* /BOOKS */

/* VIDEOS */
  function showVideo(thisLink)
  {
    if(thisLink.attr("class").search(/on/) == -1)
    {
      $("#imgscroller #images a").removeClass("on");
      thisLink.addClass("on");
      $("#videobox").load(thisLink.attr("href") + " #videobox>*", loadVideo);
    }
    thisLink.blur();
    return false;
  }
/* /VIDEOS */

/* ECARDS */
  function showCard(thisLink)
  {
    if(thisLink.attr("class").search(/on/) == -1)
    {
      $("#imgscroller #images a").removeClass("on");
      thisLink.addClass("on");
      $("#ecardpreview").load(thisLink.attr("href") + " #ecardpreview>*", initEvents);
    }
    thisLink.blur();
    return false;
  }
/* /ECARDS */

/** /IMGSCROLLER - SCROLLABLE LIST **/


/** MP3 Player */
  function playMP3(thisLink)
  {
    var playerHTML = ''
    + '<object type="application/x-shockwave-flash" data="/media/zafon/mediaplayer/player.swf" height="18" width="240">'
    + '  <param name="movie" value="/media/zafon/mediaplayer/player.swf" />'
    + '  <param name="allowScriptAccess" value="always" />'
    + '  <param name="quality" value="high" />'
    + '  <param name="bgcolor" value="#ffffff" />'
    + '  <param name="wmode" value="transparent" />'
    + '  <param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />'
    + '  <param name="flashvars" value="file=' + thisLink.attr("href") + '&autostart=true" />'
    + '</object> ';
    if($("#mp3player a").html())
    { $("#mp3player a").replaceWith(playerHTML); }
    else
    { $("#mp3player").html(playerHTML); }
    return false;   
  }

  function playTitle(thisLink)
  {
    $("#mp3player *").stop();
    $("#mp3player").remove("*");
    $("#mp3player").remove();

    var playerHTML = ""
    + '<div class="hr"><hr /></div>'
    + '<p id="mp3playertitle"><strong>' + thisLink.html() + '</strong></p>'
    + '<div id="mp3player"></div>'
    $("#playerbox").html(playerHTML);
    
    $("#playlist a").removeClass("on");
    thisLink.addClass("on");
    thisLink.blur();
    

    window.setTimeout(function(){ playMP3(thisLink) }, "100");
    
    return false;
  }
  
/** MP3 Player */


/** VIDEOS **/
function loadVideo()
{
  $("#videobox").html($("#videobox").html().replace(/<noscript>/i, "").replace(/<\/noscript>/i, ""));
}
