/**
 * Custom scripts for vdbc•nl
 * Author: Rutger Laurman
 *
 */

// Document ready
$(function(){

  /**
   * Behavioral improvements
   **************************************************************************/

    /* Make whole service block clickable */
    $("#servicesummary article").click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    })

    /* Make whole caselist clickable */
    $(".caselist figure.item").click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    })

    /* Make whole service blocks on homepage clickable */
    $("#home-services li").click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    })

    /* Make sure misclicks in main navigation do load the right page */
    $('#primary li').click(function(){
      window.location=$(this).find("a").attr("href"); return false;
    })
    // Add down state behavior for IE
    $("#primary li").mousedown(function(){ $(this).addClass('pressed')});

  /**
   * Visual helpers
   **************************************************************************/

    /* Add last class to each third item */
    $(".caselist .item:nth-child(3n)").addClass("last");

    /* Add odd classes to odd elements */
    $("#home-services li:nth-child(even)").addClass("even");

    /* Add last class to last sidebar elelent */
    $("#sidebar nav li:last-child").addClass("last");

	  //be sure about the jQuery object
	  $ = jQuery;

    /* Add tipsy tooltipping */
    if($(".service-icon-small").length)
      $(".service-icon-small").tipsy();

    $("#project-gallery figure:last-child").addClass("last");

    $("#primary li:last-child").addClass("last");

  /**
   * Homepage
   **************************************************************************/

    if($("body").hasClass('home')){

      /*  Testimonial slider on homepage  */
      $('#testimonial-slider').cycle({
        fx: 'fade',
  	  timeout:10000,
        before: function(){
          // Link the arrow to the current project
          var this_index = $(this).attr("id").replace("slide","");
          var this_arrow = $(".project" + this_index);
          // Animate the arrows except for the current one
          $('.arrow').not(this_arrow).each(function(){
            $(this).animate({
              top:20
            },1000);
          });
          // Animate the current arrow
          this_arrow.animate({
            top:0
            },1000)
        }
      });

      /* Twitter stream on homepage */
      var twitterUsername = "vdbc_nl";
      var disableTwitter  = false;

      if(!disableTwitter){
        $.jTwitter(twitterUsername, 1, function(data){

          // Empty current contents
          $('#twitterbox').empty();

          // Load all tweets in and format them properly
          $.each(data, function(i, post){
            $('#twitterbox').append(
              '<p>'+post.text+'</p>'
              +'<em>'+ TwitterDateConverter(post.created_at) + '</em>'
            );
          });

          // Convert all url's to clickable links
          $("#twitterbox p").linkify();
        });
      }


      /* Convert date format to 'hours ago' type */
      function TwitterDateConverter(time){
    	  var date = new Date(time),
    		diff = (((new Date()).getTime() - date.getTime()) / 1000),
    		day_diff = Math.floor(diff / 86400);

    	  if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
    		  return;

    	  return day_diff == 0 && (
    		  diff < 60 && "nu" ||
    		  diff < 120 && "1 minuut geleden" ||
    		  diff < 3600 && Math.floor( diff / 60 ) + " minuten geleden" ||
    		  diff < 7200 && "1 uur geleden" ||
    		  diff < 86400 && Math.floor( diff / 3600 ) + " uren geleden") ||
    	    day_diff == 1 && "gisteren" ||
    	    day_diff < 7 && day_diff + " dagen geleden" ||
    	    day_diff < 31 && Math.ceil( day_diff / 7 ) + " weken geleden";
      }

    }

  /**
   * Styly checkbox and radio styling
   */

   $('#gform_wrapper_3 :radio').styly();
   $('#gform_wrapper_3 :checkbox').styly();

   $('#gform_wrapper_3 :radio:checked').styly('check',true);

   $('#gform_wrapper_3 :checkbox:checked').styly('check',true);

   /* Forms */

    $('#newsletter').html5form({
        allBrowsers : true,
        messages : 'nl',
        colorOn:  '#f1f1f1',
        colorOff: '#B9BBBD',
        responseDiv : '#newslettermessage' // a content div to get the callback function response.
    });
    $('#logincm').html5form({
        allBrowsers : true,

         async : false, // cancels the default submit method.

        messages : 'nl',
        colorOn:  '#222',
        colorOff: '#333',
        responseDiv : '#logincmmsg' // a content div to get the callback function response.
    });
     $('#loginbc').html5form({
         async : false, // cancels the default submit method.

        allBrowsers : true,
        messages : 'nl',
        colorOn:  '#222',
        colorOff: '#333',
        responseDiv : '#loginbcmsg' // a content div to get the callback function response.
    });
    
    
    $("#maparrow")
      .css({ width: 0})
      .animate({ 
        width:"570px"
      }, 3000, function(){ 
    //    marker.setAnimation(google.maps.Animation.DROP) 
      }
    );

});

/**
 * Google Maps
 **************************************************************************/

    var map;

    var vdbcpoint = new google.maps.LatLng(51.798779,5.326526);
    var centerpoint = new google.maps.LatLng(51.761979,5.300396);
    var maplink = "http://maps.google.nl/maps?q=VDBC+BV,+Weteringshoek,+Rossum&hl=nl&sll=51.798973,5.326779&sspn=0.006522,0.011501&oq=vdbc+bv&vpsrc=0&hq=VDBC+BV,&hnear=Weteringshoek,+Rossum,+Maasdriel,+Gelderland&t=m&z=17";
    var infowindow;
    var marker;
    function initializeGoogleMaps() {
      var myOptions = {
         zoom: 12,
         center: centerpoint,
         disableDefaultUI: true,
         draggable: false,
         mapTypeId: google.maps.MapTypeId.HYBRID
      }

      map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
      
      // Info window contents
      var contentString = '<div id="addressWindow" onclick="document.location='+maplink+'">'+
         '<strong>VDBC</strong><br />'+
         'Weteringshoek 19<br />'+
         '5328 BM Rossum (Gld)<br />' +
         '0418 663912<br />'+
          '</div>';

      infowindow = new google.maps.InfoWindow({
        content: contentString
      });

      marker = new google.maps.Marker({
        position: vdbcpoint,
        map: map,
        title:"VDBC",

        setClickable: true
      });


      google.maps.event.addListener(marker, 'click', function(){
        window.open(maplink);
        return false;
      })
//      infowindow.open(marker);

    }

