var st = null;
// anvl-functions.js - requires jquery.js 1.5+
// created by Mindfly Web Studio
// created: May 15, 2011
// modified: December 14, 2011

var anvl = {};

$(document).ready(function () {
  loadMindslide();
  footerPush();
  blurBoxes();
  collapsingBox();
  //if ($('#map').length) bg.Google.map.create('#map', $('.vcard'), 2);
  googleMap();
  trimClientList();
  anvl.func.init();
});

function googleMap() {
  if ($('#map').length) {
    bg.Google.map.load('#map', {zoom: 2}, function() {
      $('.vcard').each(function() {
        var index = $('.vcard').index($(this));
        bg.Google.map.marker.add($(this), {info: $(this).html(), onclick: function(){bg.Google.map.view({zoom: 11});} });
        if (index == 0) bg.Google.map.view({center: $(this), zoom: 2});
      });
      $('.vcard .fn').click(function(){
        var index = $('.vcard .fn').index($(this));
        var marker = bg.Google.map.vars.marker_collection[index];
        var ll = marker.getPosition();
        bg.Google.map.view({center: ll, zoom: 11});
        google.maps.event.trigger(bg.Google.map.vars.marker_collection[index], "click");
      });
    });
  }
}

function loadMindslide(){
  // Configure the homepage banner rotator and run it.
  var settings = {
    source: {
      type: "selector",
      value: "#rotator",
      destination: "#rotator"
    },
    random: false,
    slide: {
      duration: 5000,
      transition: 1000,
      effect: "fade",
      direction: "left",
      autoplay: true
    },
    interface: {
      play: false,
      arrows: false,
      markers: false,
      thumbs: false
    }
  }
  if($('#rotator').length>0){
    mindslide.load(settings);
  }
}

function footerPush(){
  var d = $(window).height() - ($('header').outerHeight(true) + $('footer').outerHeight(true));
  if($('#primary').outerHeight(true) < d){
    $('#primary').css('min-height',(d - ($('#primary').outerHeight(true)-$('#primary').height())) + 'px');
  }
}
      
function blurBoxes() {
    $('#searchform input[type="text"]').focus(function() {
        var clearThis = false;
        if ($(this).attr('name') == "search" && $(this).attr('value') == "Search") clearThis = true;
        if (clearThis) $(this).attr('value', '');
    });
    $('#searchform input[type="text"]').blur(function() {
        if ($(this).attr('value') == '') {
            var insertValue = '';
            if ($(this).attr('name') == "search") insertValue="Search";
            $(this).attr('value', insertValue);
        }
    });
}

function collapsingBox() {
    if ($('.collapsing-box').length > 0) {
        $('.collapsing-box').append('<button type="button">expand +</button>');
        $('.collapsing-box button, .collapsing-box h2, .open-box .collapsing-box h3').click(function() {
            if ($(this).parent('.collapsing-box').hasClass('open')) {
              $(this).parent('.collapsing-box').animate({height: '205px'},function(){
                $(this).removeClass('open').css({height:''});
                $(this).children('button').html('expand +');              
              });
            } else {
              var h = $(this).siblings('aside').height();
              if($(this).siblings('.details').height() > h) {
                h = $(this).siblings('.details').height();
              }
              $(this).parent('.collapsing-box').animate({height: h + 'px'}, function(){
                $(this).addClass('open').css({height:''});
                $(this).children('button').html('collapse -');
              });
            }
        });
    }
}
      
function trimClientList() {
  if ($('body').hasClass('home')) {
    if($('#primary .clients li').length > 7) {
      var r = Math.floor(Math.random() * $('#primary .clients li').length);
      $('#primary .clients li:eq('+r+')').remove();
      trimClientList();
    }
  }
}

anvl.vars = {
  thumb: [],
  scramble: 0
};

anvl.func = {
  init: function() {
    anvl.func.video_player.init();
    anvl.func.client_rotator.init();
  },
  fire_event: function(element, event) {
    if (document.createEvent) {
       // dispatch for firefox + others
       var evt = document.createEvent("HTMLEvents");
       evt.initEvent(event, true, true ); // event type,bubbling,cancelable
       return !element.dispatchEvent(evt);
    } else {
       // dispatch for IE
       var evt = document.createEventObject();
       return element.fireEvent('on'+event,evt)
    }  
  },
  video_player: {
    init: function() {
      if ($('.video-wrapper').length) {
        VideoJS.setupAllWhenReady();  
        anvl.func.video_player.bind();
      }
    },
    bind: function() {
      $('.video-wrapper').live('click', function() {
        if ($('.video-popup').length) {
          $('.video-popup .video-js-box').remove();
        } else {
          $('body').append('<div class="popup-veil" onclick="">&nbsp;</div><div class="video-popup"><a class="close" onclick="">x</a></div>');
        }
        var mp4url = $(this).find('.mp4-url').html();
        var ogvurl = $(this).find('.ogv-url').html();
        var thumburl = $(this).find('.video-thumb').attr('src');
        var mp4codec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'; //type
        var ogvCodec = 'video/ogg; codecs="theora, vorbis"'; // type
        var flashVars = 'config={"playlist":["' + thumburl + '", {"url": "' + mp4url + '","autoPlay":false,"autoBuffering":true}]}'; // value
        var video = '<div class="video-js-box vim-css"><video id="the-video" class="video-js" width="640" height="360" controls poster="' + thumburl + '"><source class="mp4-source" src="' + mp4url + '" /><source class="ogv-source" src="' + ogvurl + '" /><object id="flash_fallback_1" class="vjs-flash-fallback" width="640" height="360" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf"><param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" /><param name="allowfullscreen" value="true" /><param name="flashvars" /><img src="' + thumburl + '" width="640" height="360" alt="Poster Image" title="No video playback capabilities." /></object></video></div>';
        $('.video-popup').append(video);
        $('.video-popup .video-js-box .mp4-source').attr('type', mp4codec);
        $('.video-popup .video-js-box .ogv-source').attr('type', ogvCodec);
        $('.video-popup .video-js-box .param[name="flashvars"]').attr('value', flashVars);
        VideoJS.setupAllWhenReady();
        $('.video-popup video').attr('style', '');
        $('.popup-veil').css({display:'block',opacity:0}).animate({opacity:1}, function() {
          $('.video-popup').css({display: 'block', opacity: 0}).animate({opacity: 1}, function() {
            $('.vjs-spinner').remove();
            $('#the-video').attr('style', 'width:640px;height:360px');
            try {
              anvl.func.fire_event($('.vjs-big-play-button').get(0), "click");
            } catch (e) {
            }
          });
        });
        $('.popup-veil, .video-popup .close').click(function() {
          $('.video-popup').animate({opacity: 0}, function() {
            $(this).css({display: 'none'});
            $('.popup-veil').animate({opacity: 0}, function () {
              $(this).css({display: 'none'});
              $('.video-popup .video-js-box').remove();
            });
          });
        });
        return false;
      });
    }    
  },
  client_rotator: {
    init: function() {
      if ($('body').hasClass('our-clients')) {
        var thumb = $('.clients li');
        for (i = 0; i < thumb.length; i++) {
          anvl.vars.thumb[i] = thumb.eq(i).children('img').attr('src');
          thumb.eq(i).attr('class', 'icon-' + i);
        }
        $('.clients li + li + li + li + li + li + li').remove();
        var t = setTimeout('anvl.func.client_rotator.rotate();', 3000);
      }
    },
    rotate: function() {
      var tc = anvl.vars.scramble * 2
      for ( c = tc; c < (tc + 2); c ++) {
        var target = $('.clients li:eq(' + c + ')');
        var current_class = target.attr('class');
        while (target.attr('class') == current_class) {
          var r = Math.floor(Math.random() * anvl.vars.thumb.length);
          var good = true;
          for (i = 0; i < 6; i++) {
            if ($('.clients li:eq(' + i + ')').attr('class') == ('icon-' + r)) good = false;
          }
          if (good) {
            target.attr('class', 'icon-' + r);
            if (c == tc) {
              target.children('img').animate({opacity:0}, 500, function(){ 
                $(this).attr('src', anvl.vars.thumb[($(this).parent().attr('class').split('-')[1] * 1)]);
                $(this).animate({opacity:1}, 500);
              });
            } else {
              target.children('img').animate({opacity:0}, 600, function(){ 
                $(this).attr('src', anvl.vars.thumb[($(this).parent().attr('class').split('-')[1] * 1)]);
                $(this).animate({opacity:1}, 400);
                var t = setTimeout('anvl.func.client_rotator.rotate();', 3000);
              });              
            }
          } 
        }
      }
      anvl.vars.scramble += 1;
      if (anvl.vars.scramble > 2) anvl.vars.scramble = 0;
    }
  }
}

