/* ###

    HATSUMATSU www.hatsumatsu.de
    v. 5.0

    Hi there, nice that you take a look around...
    Check out how everything's working ;) HATSUMATSU


### */



    $mp = false;
    $project = "";



    /* ### RESIZE MP ### */

    var resizeTimer = null;
    $(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
       resizeTimer = setTimeout("resize()", 100);
    });

    function resize() {
      for(var j = 1; j <= $("#matsupictu .mp-img").length; j++) {
         $img = $('#item-' + j + ' .mp-img');
            $title = $img.attr("title").split(":");
            $true_height = parseInt($title[0]);
            $true_width = parseInt($title[1]);
            // alert($true_height);
            if(($true_height+60) > $(window).height()) {
               $img_height =  $(window).height() - 60;
               $factor = $true_height / $img_height;
               $img_width = $true_width / $factor;
               $("#item-" + j + " img").css('height',$img_height +  'px');
             } else {
               $img_height = $true_height;
               $img_width = $true_width;
               $("#item-" + j + " img").css('height','auto');
             }
             if( ($true_width + 210 + 60) > $(window).width() ) {
               $img_width =  $(window).width() - 210 - 60;
               $("#item-" + j + " img").css('width',$img_width +  'px');
             } else {
               $img_width = $true_width;
               $("#item-" + j + " img").css('width','auto');
             }
             $i_top = -($img_height / 2);
             $i_left = -(($img_width + 210) / 2);
             $("#item-" + j + " .inner").css('margin',$i_top + 'px 0px 0px ' + $i_left + 'px');
             // $("#item-" + j + " .caption").html("img_height:" + $img_height + " img_width: " + $img_width + " top/left: " + $i_top + "/" + $i_left);
            }
         }


$(document).ready(function () {

    /* ### MESSAGE BOXES ### */


    $("#browser-list a").mouseenter( function() {
    $("#browser-overlay .tooltip").text($(this).text());
    } );
    $("#browser-list li").mouseleave( function() {
    $("#browser-overlay .tooltip").text(".");
    } );


    /* ### NAVIGATION ### */

     function contact() {
         $page = "contact";
         $('#wrapper-0').animate({top: '0%'},700);
         $('body').animate({backgroundColor: '#fff'},700);
    }

    function portfolio() {
        $mp = false;
        $page = "portfolio";
        $('body').animate({backgroundColor: '#fff'},700);
        $('#wrapper-0').animate({'top': '-100%'},700);
        $('#matsupictu').css("left","0px");
        $('#viewer').fadeOut();
    }


    /* ### VIEWER AKA MATSUPICTU ### */

    function mp($url,$c) {                   /* $url ist z.B. /projects/rabiat/   $c ist z.B. 3  */
      $c = parseInt($c);
      // alert("mp starts");
      $mp = true;
      $mp_i = 1;   /* was war das doch gleich?  */
      // alert($c);
      if($url) {
        $('#viewer').fadeOut(function() {$('#viewer').empty(); });

        $url = $url + ' #wrapper-mp';
        // $url = $url.replace('#/','');     /* pseudo history */

        // alert($url);

         $('body').animate({backgroundColor: '#3a3a3a'},800,function() { $('#loader').fadeIn(); });
         $('#wrapper-0').animate({top: '-200%'},700,function() {
           $('#viewer').load($url,function(a,status,c) {
             if(status == "error") {
              alert("Oouups! Diese Seite gibt es nicht...");
              $('#loader').hide();
              window.location.href = "#/";

             }

             $('#viewer .mp-img.first').bind("load",function() {
              $mp_count = $('#count').val();
              $('#loader').fadeOut('slow');
              $('#viewer').fadeIn('slow');

              resize();

          if($c) {
            if($mp /*&& !$mp_scrolling*/ && $mp_i > 0 && $mp_i < $mp_count) {
               // $mp_scrolling = true;
               $t_left = ($c - 1) * -100;
               $("#matsupictu").animate({ "left": $t_left + "%" },500,function() { $mp_scrolling = false; });
               $mp_i = $c;
              }
             }
             });
          refresh_mp_links();
          });

         });
       } else {
         if($mp /*&& !$mp_scrolling*/ && $mp_i > 0 && $mp_i <= $mp_count) {
          // $mp_scrolling = true;
          $t_left = ($c - 1) * -100;
          // alert($t_left);
          // alert($("#matsupictu").css("left"));
          $("#matsupictu").animate({ "left": $t_left + "%" },500,function() { $mp_scrolling = false; });
          $mp_i = $c;
         }


       }
    }


      $('#loader').hide();
    /* ### MP SCROLLING ### */
    $mp_scrolling = false;
    $('#matsupictu').bind('mousewheel', function(event, delta) {
            var dir = delta > 0 ? 'Up' : 'Down',
                vel = Math.abs(delta);
            if(dir == 'Down') { if(!$mp_scrolling && $mp_i < $mp_count) window.location.href = "#/" + $project + ($mp_i + 1);  }
            if(dir == 'Up') { if(!$mp_scrolling && $mp_i > 1) window.location.href = "#/" + $project + ($mp_i - 1); }
            return false;
        });


    $(document).bind('keydown', function(e) {
      if(e.keyCode == '39') {
      e.preventDefault();
       if($mp && !$mp_scrolling && $mp_i < $mp_count) window.location.href = "#/" + $project + ($mp_i + 1);
      }
      if(e.keyCode == '37') {
      e.preventDefault();
       if($mp && !$mp_scrolling && $mp_i > 1) window.location.href = "#/" + $project + ($mp_i - 1);
      }
      if(e.keyCode == '27') {
      e.preventDefault();
       window.location.href = "#";
      }

    });

   /* ### CATEGORY MARKER ### */

    $('#cat-tree li a').hover( function(e) {
       e.preventDefault;
       $(this).parent("li").siblings("li").css("width","48px");
       $(this).parents("ul").css({width: "100%"});
       $(".entry:not(." + $(this).parent("li").attr("title") + ")").animate({opacity:0.1},300);
    },
    function(e) {
       e.preventDefault;
       $(this).parent("li").siblings("li").css("width","auto");
       $(this).parents("ul").css({width: "48px"});
       $(".entry:not(." + $(this).parent("li").attr("title") + ")").animate({opacity:1},300);
    });

   /* ### SCROLLING IN PORTFOLIO ### */

    $scrolling = false;
    $('#page-projects, #helper').bind('mousewheel', function(event, delta) {
            var dir = delta > 0 ? 'Up' : 'Down',
                vel = Math.abs(delta);
            if(dir == 'Down') { ppnext(); }
            if(dir == 'Up') { ppprev(); }
            return false;
        });

    $(document).bind('keydown', function(e) {
      if(e.keyCode == '37') {
      e.preventDefault();
       if(!$mp) { ppprev() }
      }
      if(e.keyCode == '39') {
      e.preventDefault();
       if(!$mp) { ppnext() }
      }

    });

    function ppnext() {
        $winwidth =  $("#page-contact").width();
        $left = parseInt($('#page-projects').css("left"));
        if(!$left) $left = 0;
        $end = $("#page-projects .entry:last").offset();
        $end = parseInt($end.left) + (-1 * $left);
        if(!$scrolling && $end > (-1 * $left)) {
        $scrolling = true;
        $('#page-projects').animate({'left': '-=' + $winwidth*2/3 + 'px' },400,function() { $scrolling = false });
        }
    }
    function ppprev() {
        $winwidth =  $("#page-contact").width();    // Breite des browser-fensters
        if(!$scrolling) {                           // wenn grad nicht gescrollt wird
        $scrolling = true;
        $left = parseInt($('#page-projects').css("left"));
        if(!$left) $left = 0;
        if($left + $winwidth*2/3 <= 0) {            // wenn man noch weit genug zurück scrollen kann
        $('#page-projects').animate({'left': '+=' + $winwidth*2/3 + 'px' },400,function() { $scrolling = false });
            } else {  // jetzt würde man zu weit nach links scrollen, deswegen geht's nach 0px
        $('#page-projects').animate({'left': '0px' },400,function() { $scrolling = false });
            }
        }
    }

 /* ### VISUAL  FX ### */

    $(".entry.print").hover(function() {
      $("#cat-tree li[title=print]").stop().animate({opacity:1},700);
    },function() {
      $("#cat-tree li[title=print]").stop().animate({opacity:0.3},700);
    });

     $(".entry.digital").hover(function() {
      $("#cat-tree li[title=digital]").stop().animate({opacity:1},700);
    },function() {
      $("#cat-tree li[title=digital]").stop().animate({opacity:0.3},700);
    });

     $(".entry.typo").hover(function() {
      $("#cat-tree li[title=typo]").stop().animate({opacity:1},700);
    },function() {
      $("#cat-tree li[title=typo]").stop().animate({opacity:0.3},700);
    });

     $("a#button-freefonts").hover(function() {
       if($page == "contact") {
         $(this).animate({bottom: '10px'},100);
       } else {
         $(this).animate({bottom: '-140px'},100);
       }
     },function() {
       $(this).animate({bottom: '-70px'},100);
     });

  /* ### PSEUDO HISTORY ### */

   $(".a-thumb, #nav-contact").not(".redirect a").each( function() {
     $permalink = $(this).attr("href").replace($.address.baseURL(),"");
     $(this).attr("href","#" + $permalink);
   });
   $("#page-projects .a-thumb").not(".redirect a").each( function() {
     $(this).attr("href",$(this).attr("href") + "1");
   });


   function refresh_mp_links() {
       $(".a-mp-prev, .a-mp-next").each(function() {
         // alert($project);
         $(this).attr("href","#/" + $project + $(this).attr("href"));
       });
   }

   $.address.change(function(e) {
        e.preventDefault();
        // alert("change");
        // alert($.address.baseURL());
        // alert($.address.value());
        // alert($.address.value());
        $p_project = $project;
        $parts = $.address.value().split("/");
        $project = $parts[1] + "/" + $parts[2] + "/";
        // alert($parts[3]);
        // alert($p_project + ":" + $project);
     if($parts[1] == "projects") {
       if($project == $p_project) {
          $mp_scrolling = true; mp(null,$parts[3]);
         } else {
           $mp_scrolling = true; mp($project,$parts[3]);
           }
        }

     if($.address.value() == "/") {
             // alert("portfolio");
             portfolio();
        }
     if($.address.value().substr(0,8) == "/kontakt") {
             // alert("contact");
             contact();
        }
       $.getScript('http://hatsumatsu.de/wp/wp-content/themes/hatsumatsu/js/piwik.ajax.pagetrack.js');
    });


    /* ### HELPER ### */


     $help = false;
     $helper = $("#helper");
     $("#page-projects").mousemove(function(e) {
      if(e.pageX > $(window).width() - 50) {
        if(!$help) {
         $helper.fadeIn();
         $help = true;
        }
         $helper.css({ marginTop: e.pageY });
      }
      if(e.pageX < $(window).width() - 50) {
          $helper.fadeOut();
          $help = false;

           }
        });


});


