/*  STEFAN HOHLOCH
 *  File: stefan_hohloch.js
 *  Abstract: window javascript for stefanhohloch.de
 
 *  Version: 1.2
 *  © Team Tejat Hamburg 2006-2011 / marcwright.de
 */

 function runOnLoad(f) {
     if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
     else runOnLoad.funcs.push(f); // Otherwise, store it for later
 }

 runOnLoad.funcs = []; // The array of functions to call when the document loads
 runOnLoad.loaded = false; // The functions have not been run yet.

 // Run all registered functions in the order in which they were registered.
 // It is safe to call runOnLoad.run() more than once: invocations after the
 // first do nothing. It is safe for an initialization function to call
 // runOnLoad() to register another function.
 runOnLoad.run = function() {
     if (runOnLoad.loaded) return;  // If we've already run, do nothing

     for(var i = 0; i < runOnLoad.funcs.length; i++) {
         try { runOnLoad.funcs[i](); }
         catch(e) { /* An exception in one function shouldn't stop the rest */ }
     }

     runOnLoad.loaded = true; // Remember that we've already run once.
     delete runOnLoad.funcs;  // But don't remember the functions themselves.
     delete runOnLoad.run;    // And forget about this function too!
 };

 // Register runOnLoad.run() as the onload event handler for the window
 if (window.addEventListener)
     window.addEventListener("load", runOnLoad.run, false);
 else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
 else window.onload = runOnLoad.run;


 $(document).ready(function(){
   
       /* scroll init */
             $(function(){
                 $('.scroll-pane').jScrollPane({
                   showArrows: false
                 });
             });
         
   
   
   /* init cycle */
   $('#inner_images')
     .cycle({ 
      fx: 'fade', 
          speed:   1000,
          timeout: 4000,
          pause:             0, // true to enable "pause on hover" 
          pauseOnPagerHover: 0, // true to pause when hovering over pager link 
          pager:  '#pager',
          delay:   1200,
          prev: '#rew',        // picture itself
          next: '#ffw',        // picture itself
          after:   onAfter
   });

   /* pause cycle */
   $('#inner_images').cycle('pause');

   /* append title into div */ 
   function onAfter() { 
       $('#title').html("").append(' ' + $('.maxImgWidth', this).attr('alt') ); 
       //console.log(  );
   }

 })

/* main runOnLoad function 
    runOnLoad makes shure, every image is loaded (other that ready() ) and then executed the script, */


function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}


/* the key bindings */
$(document).keydown(function (e) {
      
      /* right */
      if (e.which == 39) {
        $("#ffw").trigger('click');
        $('#ffw').cycle('pause');
      /* left */
      } 
       if (e.which == 37) {
        $("#rew").trigger('click');
        $('#ffw').cycle('pause');
      }
});
