/*  KEITH SCHOFIELD
 *  File: keith-schofield.js
 *  Abstract: window javascript for keith-schofield.com
 
 *  Version: 1.2
 *  (cc) Team Tejat Hamburg 2009/ marcwright.de
 */
 
 
 
 if(window.console) {
    window.console.log("Hello – main js is loaded. Have fun with beautiful pictures… ");
   }

    /* disable contextmenue, uarrghh … sorry for that */
     $(this).bind("contextmenu",function(e){
        return true;
     });

     /* disable draging */
     $(this).bind("dragstart",function(e){
        return false;
     });

     /* disable draging FF */
     $(this).bind("mousedown",function(e){
        return false;
     });


/*  This page utilizes David Flanagan's runOnLoad function as an alternative mechanism to the usual $(document).ready() convention used in jQuery.
    runOnLoad(function(){
      $("#sizeReport").html( $("#saturnID").height());
    });
    
    runOnLoad performs basically the same task as jquery's DOM-ready mechanism, $(function): 
    it queues up all the functions supplied and runs them when the document is available. 
    
    ****
    THE DIFFERENCES IS THAT $(FUNCTION) RUNS BEFORE IMAGES HAVE COMPLETELY 
    DOWNLOADED AND RUNONLOAD WAITS UNTIL THE IMAGE DOWNLOADS ARE COMPLETE.
    ****
    
    I don't recommend that you mix the runOnLoad and $(document).ready() metaphors. 
    Pick one model or the other, depending on whether or not this underspecified <img> thing is a problem for your site.
    
    */
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;


/* some pre-loads */
if (document.images){
    pre = new Image();
    pre_image_url = new Array();
    // insert your preloads here
    pre_image_url[0] = 'art/ajax-loader.gif';
    pre_image_url[1] = 'art/play.png';
    pre_image_url[2] = 'art/story.png';
    pre_image_url[3] = 'art/story_active.png';
    pre_image_url[4] = 'art/growl/one_line.png';
    pre_image_url[5] = 'art/title_top.png';
    pre_image_url[6] = 'art/title_mid.png';
    pre_image_url[7] = 'art/title_bottom.png';
    
    var i = 0;
    for(i=0; i<= 7; i++) 
    pre.src = pre_image_url[i];
}

/* main runOnLoad function 
   runOnLoad makes shure, every image is loaded (other that ready() ) and then executed the script, */
runOnLoad(function(){
    $(window).resize( function() {
        $("#image_container").css({ height: $(".panorama").height() });
        $("#pager").css({ top: $(".panorama").height() });
        $("#story").css({ top: $(".panorama").height()-5 });
    });
    
    /* fade out loading */
    $("#loading").fadeOut(1000);
    $("#story").css({ opacity: 0.8});
    
    /* count elemets; >2 init cycle */
    numImages = $('#inner_images').children().size();
    
    if(numImages >= 2 ){
            /* init cycle */
            $('#inner_images').each(function() {
        
                $(this)
                  .cycle({ 
                   fx: 'fade',
                      delay:              10,            // additional delay (in ms) for first transition (hint: can be negative)
                      speed:              1500,
                      easing:             "easeOutCirc", 
                      timeout:            7000,
                      pause:              0,          // true to enable "pause on hover" 
                      pauseOnPagerHover:  0,          // true to pause when hovering over pager link 
                      pager:              '#pager',
                      next:               '.ffw',
                      prev:               '#nav_overlay',
                      fastOnEvent:        0,
                      after:              onAfter,
                      before:             onBefore
              
            });
          })
    } // end if
    
    /* pause cycle */
    $('#inner_images').cycle('pause');
    
    /* append title into div */
    function onAfter() {
      if(this.alt !=''){
        //$('#title_mid').html("").append(' ' + str_replace(this.alt, "\n" , "<br />") );
        $('#inner_text_container').html("").append(' ' + str_replace(this.alt, "\n" , "<br />") );
        STORY = 1;
      }
    }
    
    /*hide description*/
    function onBefore() {
      //$('#title').hide();
      if(this.alt !=''){
        $('#story').fadeIn('slow');
        $('#inner_text_container').html("").append(' ' + str_replace(this.alt, "\n" , "<br />") ); 
      }else{
        $('#text_container').hide();
        $('#nav_overlay').show();
        
        
        $('#inner_text_container').html("");
        $('#story').fadeOut('fast');
        $("#story").css({ background: "url(art/story.png)", backgroundRepeat: "no-repeat"}); 
        $('#title').fadeOut('slow');
        
        STORY = 1
      }
    }
    
    
    
    /* show/ hide Keith's Story */
    var STORY = 1;
    function showhide_story(){
      if(STORY==1){
        //$('#title').fadeIn('slow');
        $('#nav_overlay').hide();
        $('#pager').hide();
        $('#text_container').fadeIn('slow');
        
        $("#story").css({ background: "url(art/story_active.png)", backgroundRepeat: "no-repeat"}); 
        STORY= 0;
      }else{
        //$('#title').fadeOut('slow');
        $('#text_container').hide();
        $('#pager').fadeIn('fast');
        $("#story").css({ background: "url(art/story.png)", backgroundRepeat: "no-repeat"}); 
        $('#nav_overlay').show();
        
        STORY = 1;
        }
    }
    
    /* hover the book */
    $("#story").hover(
        function(){
            $("#story").css({ opacity: 1});
          }, 
        
        function(){
              $("#story").css({ opacity: 0.8});
    });
    
    /* handle the event */
    $("#story").click(function(){
      showhide_story();
    })
    
    $("#pp").click(function(){
      playpauseevent();
    })
    
    /* close infos */
    $(".close").click(function(){
      $("#story").trigger('click');
    })
    
    /* show & hide picture browser */
            window.setTimeout ('$("#image_container").fadeIn(1500);', 900);
            //$("#title_contact").css({ top: '10%' , right: '15%' });
            //window.setTimeout ('$("#title_contact").fadeIn(1500);', 2300);
            
            window.setTimeout ('$("#image_container").css({ height: $(".panorama").height() });  $("#pager").css({ top: $(".panorama").height() }); $("#story").css({ top: $(".panorama").height()-5 }); ', 1100); 
            window.setTimeout ('$("#pager").fadeIn(2000);', 3000); 
            window.setTimeout ('$("#navigation").css({ top: 40 }); ', 1100);
            window.setTimeout ('$("#navigation").fadeIn(1000);', 2000);
                     
    /* end */
    
});

/* play stop slideshow */
var PLAY = 1;
function playpauseevent(){
  if(PLAY==1){
    $('#inner_images').cycle('resume', true);
    growl('', "playing slideshow")
    //$('#pp').html('<img src="art/pause.png" />');
    PLAY= 0;
  }else{
    $('#inner_images').cycle('pause');
    //$('#pp').html('<img src="art/play.png" />');
    growl('', "slideshow paused")
    PLAY = 1;
    }
}


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

/* the key bindings */
$(document).keydown(function (e) {
      
      /*i for information*/
      if (e.which == 73 ) {
        if( $('#inner_text_container').html() !='' ){
          $("#story").trigger('click');
        }
      } 
    
      /* space */
      if (e.which == 32 ) {
        /* slideshow play & pause */
        playpauseevent();
      } 
      
      /* right */
      if (e.which == 39) {
        $(".ffw").trigger('click');
        $('#inner_images').cycle('pause');
        PLAY = 1;
      /* left */
      } 
       if (e.which == 37) {
        $("#nav_overlay").trigger('click');
        $('#inner_images').cycle('pause');
        PLAY = 1;
      }
});

/* setup the growl assistance*/
function growl(title, msg){
    $.growl.settings.displayTimeout = 2000;
    $.growl.settings.noticeTemplate = ''
      + '<div style="background-color: transparent !important;">'
      + '<div style="float: right; background-color: transparent !important; background-image: url(art/growl/one_line.png); position: relative; display: block; height:37px; width: 220px; margin: 0pt;">' 
      + '  <p style="font-family: \'Lucida Grande\'; font-weight: normal; margin: 7px 0px; font-size: 11px; text-align: center;">%message%</p>'
      + '</div>'
      + '</div>';
    $.growl.settings.noticeCss = {
      position: 'absoulte'
    };
    $.growl.settings.dockTemplate = '<div style="background-color: transparent !important;"></div>';
    $.growl.settings.dockCss = {
        position: 'absolute',
        bottom: '3px',
        right: '10px',
        width: '300px'
      };
    $.growl(title,msg);
    
}

