/*
** Blätterkatalog Integration Script
** *********************************
**
** 1)   addToCart(artno, shopsessionid)
**  Wird bei Linktyp "Artikel in Warenkorb legen"
**  aufgerufen.
**
** 
** 2)   blaetterkatalogflip(newPage)
**  Blättert auf die gegebene Seite
**  Wird bei Linktyp "Gehe zu Seite" verwendet.
**
**  TODO: Kann optional verwendet werden
**        um mittels 
**  
**  <a href="javascript:blaetterkatalogflip(5)">Gehe zu Seite 5</a>
**
**  den Blätterkatalog umzublättern.
**  (Beispiel: Auf Kapitel-Startseite blättern)
**
** 3)   gotoURL(myURL)
**    Wird bei Linktyp "Externer Link" und
**  von addToCart aufgerufen
**
*/
  
  function recommendPage(currPage)
  {
	
  var recommendURL = "blaetterkatalog/recommendation/recommendation.php?bkPage="+currPage;
	window.open(recommendURL, "bkrecommend", "width=460,height=530,scrollbars=no,resizable=yes");
  }  
  
  /**
  */
    
  function trackPageHit(page) {
  } 
  
 

  
 function addToCart(artno, pagenr, shopsessionid) {
        var destinationURL = shopBase + shopURL + shopArticleIDParameterName + artno + '&pagenr=' + pagenr + shopURLPostfix;	   
	if (shopDebug) alert("ShopSessionID: "+shopsessionid+"\nArtikel "+artno+" in Detailansicht Shop aufrufen\n\nSHOP URL: "+destinationURL);	
	if (!shopDebug) gotoURL(destinationURL);	
  }  
  


  /**
  *  blättert auf die entsprechende seite um
  *  setzt voraus, dass der blätterkatalog in
  *  dem fenster noch geladen ist
  */
  function blaetterkatalogflip(newPage)
  {
    flashCommand('jump_to_id', newPage)
  }

  function bk_refresh_cart()
  {
    flashCommand('refresh_cart', '');
  }

  function bk_refresh_cart_window()
  {
    alert('focus offlineCatalog');
    offlineCatalog.focus();    
  }


  /**
  * sprint zu einer URL
  * Ziel: Shop-Fenster
  */ 
  function gotoURL( myURL ) 
  {
    if (myURL.indexOf("mailto:")!=-1) {
      location.href=myURL;
      return;
    }
    
    if (myURL.indexOf("http://")!=0 && myURL.indexOf("https://")!=0 && myURL.indexOf("mailto:")!=0) {
      myURL = "http://"+myURL;
    }

    try {
      if ( typeof( opener ) == "undefined" || typeof( opener ) == "unknown" ||
            typeof( opener.name ) == "undefined" || typeof( opener.name ) == "unknown" )
      {
        if ( shopFenster.closed ) 
        {
          shopFenster = window.open( myURL, "shop", "" );
          shopFenster.focus();
        }
        else 
        {
          shopFenster.location.href=myURL;
          shopFenster.focus();  
        }
      } 
      else 
      {
        opener.location.href = myURL;
        opener.blatterkatalogPopup = window;
        opener.focus();
      }
    } 
    catch ( Exception )
    {
      if( opener && !opener.closed )
      {
        opener.location.href = myURL;
        opener.focus();
      }
      else 
      {
        try {
          if ( shopFenster.closed ) 
          {
            shopFenster = window.open( myURL, "shop", "" );
            shopFenster.focus();
          }
          else 
          {
            shopFenster.location.href = myURL;
            shopFenster.focus();  
          }
        } 
        catch ( Exception )
        {
            shopFenster = window.open( myURL, "shop", "" );
            shopFenster.focus();
        }
      }
    }
  }

  /**
   * getFlashPath() ermittelt das Flash-Objekt
   */
  function getFlashPath() 
  {
      if ( document.all ) 
      {
          //ie
          return document.bkflash;
      }
      else 
      {
          //safari
          var ffilm = document.getElementById( "bkflash" );
          try 
          {
              flashfilm.SetVariable( "tst", "1" );
          } 
          catch ( Exception ) 
          {
              //firefox
              ffilm = document.embeds["bkflash"];
            }
            return ffilm;
      }
  }
  
  

  /////////////////////////////////////
  // Funktion zum externen Aufruf der Flash-Commands
  // id => 'jump_to_id'
  // par => pageid
  // aufruf mit z.b. flashCommand('jump_to_id', 5)
  /////////////////////////////////////
  function flashCommand( id, par ) 
  {
    try
    {
        var movie = getFlashPath();
        movie.SetVariable( "jscommand", id + "|" + par );
    }   
      catch( Exception )
      {
        document.getElementById("bkflash").SetVariable( "jscommand", id + "|" + par );    
      } 
  }
  
  
  function getURLParam( strParamName, defaultBKValue ) 
  {
  var strReturn = " ";
  var strHref = window.location.href;
  
  var i = strHref.indexOf( strParamName );
  
  if ( i > 0 )
  {
    var j = strHref.indexOf( "=", i + 1 );
    if ( j > 0 )
    {
      var endPos = strHref.length;
      var k = strHref.indexOf( "?", j + 1 );
      var l = strHref.indexOf( "&", j + 1 );
      if ( k > 0 && l > 0 )
      {
          if ( k < l ) 
          {
            endPos = k;
          } 
        else 
        {
          endPos = l;
        } 
      } 
      else if ( k > 0 )
      {
        endPos = k;
      } 
      else if ( l > 0 )
      {
        endPos = l;
      }
      
      strReturn = strHref.substring( j + 1 , endPos );
    } 
  }
  
  if (strReturn == "" || strReturn == " ") 
  {
     if (defaultBKValue)
     {
       return defaultBKValue;
     }
  }
  
  return strReturn;
  } 

  window.onerror = stoperror;

  function stoperror( a )
  {
    return false;
  }

  function openOfflineCatalog(url)
  {
    var offlineCatalog =  window.open(url, "KatalogAnsicht", "width=1150,height=740,scrollbars=no,statusbar=no,status=no,toolbar=no,resizable=1");
    // save reference
    window.name = offlineCatalog; 
    offlineCatalog.focus();
  }
 
  function bkcallback(bkwinref)
  {
    // alert("got initial callback");
    offlineCatalog = bkwinref;
  }

  function callbkrefresh()
  {
    if (offlineCatalog){
      //alert(offlineCatalog);
      try {
        offlineCatalog.bkrefresh();
      } catch(err){}  
    }else{
      //alert("cannot call bk - got no callback");
    }  
  }
