/*  Jan Hille
 *  File:       janhille.js
 *  Abstract:   javascript for janhille.de
 *  Version:    1.0
 *  (CC):       Marc Wright | Team Tejat Hamburg 2010

    Note:
     $.cookie('the_cookie');                                 // get cookie
     $.cookie('the_cookie', 'the_value');                    // set cookie
     $.cookie('the_cookie', 'the_value', { expires: 7 });    // set cookie with an expiration date seven days in the future
     $.cookie('the_cookie', '', { expires: -1 });            // delete cookie*/

 /* set and restore x/y positions of sections with class name "".drag"" */
 /* some kind of 'thing' to calc wether 16/9 or 4/3 */
 
 function topPadImage(){
    var $wrapper      = $('#inner_images'),
       $slider     = $wrapper.find('> ul'),
       $li_items     = $slider.find('> li'),
       $img_items    = $li_items.find('> img')
       
       n = $li_items.length;
         for(i=0; i<=n+2; i++){
           pWith   = $('a.inner_projects img#img_'+i+'.preview').width()
           pHeight = $('a.inner_projects img#img_'+i+'.preview').height()
           ratio   = pWith / pHeight;
           pTop    = Math.round(ratio * 13);
           //$('img#img_' + i +'.preview').css('marginTop', pTop);
           $('#inner_images').css('visibility', 'visible');
           $('#loading').css('visibility', 'hidden');
           $('.arrow').fadeIn('slow')
         }
 }


(function(){
  $.fn.infiniteCarousel = function (){
    function repeat(str,n){
      return new Array( n + 1).join(str);
    }
    return this.each(function (){
      
      var $outerWrapper = $(this).css('overflow', 'hidden');
      var $wrapper      = $('> div', this),
          
          $slider     = $wrapper.find('> ul'),
          $items      = $slider.find('> li'),
          
          $single     = $items.filter(':first')
          singleWidth = $single.outerWidth()+5;
          $newWidth   = $items.length*singleWidth;
          $('> div', this).css('width', $newWidth );
          
          visible = Math.ceil($outerWrapper.innerWidth() / singleWidth),
          currentPage = 1,
          pages = Math.ceil($items.length / visible );
          
          // Pad the pages with empty elements if required
          if($items.length % visible !=0){
            $slider.append(repeat('<li class="empty media">', visible - ($items.length % visible) ))
            
          //update width of wrapper in cas we pad
            pad = (visible - ($items.length % visible)) * singleWidth;
            $('> div', this).css('width', $newWidth+pad);
          }
          
          function gotoPage(page){
            checkNav(page);
            var dir = page < currentPage ? -1 : 1,
            // number of pages
            n     = Math.abs(currentPage - page),
            left  =singleWidth * dir * visible * n;
            $outerWrapper.filter(':not(:animated)').animate({
            scrollLeft : '+=' + left
            
            }, 500, function (){
              currentPage = page;
            });
          }
          
          // 5) insert back and forward images
          if(pages >=2){
            $outerWrapper.after('<a href="#" title="zurück" class="arrow back" style="visibility: hidden">&lt;&lt;</a><a title="weitere" href="#" class="arrow forward">&gt;&gt;</a>');
          }
          
          function checkNav(thepage){
            switch (thepage) {
              /* not further */
              case pages:
                $('a.forward').css('visibility', 'hidden');
                $('a.back').css('visibility', 'visible');
                break;
              /* show forward */
              case 1:
                $('a.forward').css('visibility', 'visible');
                $('a.back').css('visibility', 'hidden');
                break; 
              default :
                $('a.forward').css('visibility', 'visible');
                $('a.back').css('visibility', 'visible');
              }
          }
          
          $('a.back').click(function (){
            gotoPage(currentPage - 1);
            return false;
          });
          
          $('a.forward').click(function (){
            gotoPage(currentPage + 1);
            return false;
          });
          
          $(this).bind('goto', function (event, page) {
            gotoPage(page)
          })
          
          
          
        });
  };
}) (jQuery);


/* some pre-loads */
if (document.images){
    pre = new Image();
    pre_image_url = new Array();
    /* insert your preloads here */
    pre_image_url[0] = 'http://janhille.de/art/mainMenuSearchBoxLGlass.gif';
    pre_image_url[1] = 'http://janhille.de/art/separate.png';
    pre_image_url[2] = 'http://janhille.de/art/separate_white.png';
    pre_image_url[3] = 'http://janhille.de/art/janhille.png';
    
    var i = 0;
    for(i=0; i<= 3; i++) 
    pre.src = pre_image_url[i];
}

function enlargeNum(){
  var tel = $(".tel").html();
  var fax = $(".fax").html();
  
  $(".tel").html('<ul class="gallery clearfix clean" style="margin-top: 25px"><li class="clean"><a href="#tel" rel="prettyPhoto[inline]"><span class="write"><img width="13" height="14" src="art/mainMenuSearchBoxLGlass.gif" alt="" /></span> ' + tel + '<\/a></li></ul> <div id="tel" align="center" class="num"><span class="huge"> ' + tel + ' <\/span><\/div>');
  $(".fax").html('<ul class="gallery clearfix clean"><li class="clean"><a href="#fax" rel="prettyPhoto[inline]"><span class="write"><img width="13" height="14" src="art/mainMenuSearchBoxLGlass.gif"  alt="" /></span> ' + fax + '<\/a></li></ul> <div id="fax" align="center" class="num"><span class="huge"> ' + fax + ' <\/span><\/div>');
}

function sendMail(){
  var mail = $(".mail").html();
  $(".mail").html('<a href="mailto:'+mail+'">' + mail + '</a>');
}



$(function() {
  $('.inner_projects').tipsy({gravity: 'n', fade: true, html: true});
});


$(document).ready(function(){
     //$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook', opacity: 0.70});
     /* login form */
     opac = 0.3;
     $('#login').css('opacity', opac);
     $('#login').click(function(){
        $('#login').val('');
     })
     
     $('#login').hover(function(){
        $(this).fadeTo("slow", 1);
     },
     function(){
       $(this).fadeTo("slow", opac);
     
     })
      
     
     
     $('div.infiniteCarousel').infiniteCarousel();
     $('a').hover(function(){ window.status='⚭'; return false; },function(){ window.status='⚭'; return false; })
     
     enlargeNum(); /* prepare enlages telephone/ mobile/ fax number*/
     sendMail();
     
     $windowHeight    = $(window).height();   // returns height of browser viewport
     $heightContainer = $("#parent").innerHeight();
     $marginTop       = parseInt($windowHeight-$heightContainer)/2+40;
     
     $("body").css({ marginTop: $marginTop });
     $("#parent").css({ visibility: 'visible' });
     
              /* :) change coler scale of image */
              $(".preview").hover(function() {
                
                oldSRC    = this.src
                
                var newSRC    = this.src.replace( /\.jpg$/g, '1.jpg' );
                over = new Image;
                over.src = newSRC;
                this.src = newSRC;
                
                //if(over.complete === true  ){
                //  this.src = newSRC;
                //}
                
                } , function() {
                /* restore to bw and frame 1*/
                  this.src = oldSRC;
              });
              
  
  $(window).resize( function(){
         $("a.pp_close").trigger('click');
         windowHeight    = $(window).height();   // returns height of browser viewport
         heightContainer = $("#parent").innerHeight();
         marginTop       = parseInt(windowHeight-heightContainer)/2+40;
         $("body").css({ marginTop: marginTop });
      });
      
});

$(window).load(function() {
  topPadImage();
  /* enable light-box add flavor */
  $(".gallery a[rel^='prettyPhoto']").prettyPhoto({
    slideshow: false
  });
  
})


