// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$.extend({
  getUrlVars: function (){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function (name){
    return $.getUrlVars()[name];
  },
  getUrlHash: function () {
    return unescape(self.document.location.hash.substring(1));
  },
  setUrlHash: function (hash) {
    var hashStr = hash ? "#" + hash : "";
    window.location.hash = hashStr;
  }
});

$(function () {
  var fadeTimeMs = 800;
  var fadePeriodS = 10;

  var featured = $('.featured');
  var index = 0;
  var last;
  var current = featured[index];
  $(current).fadeIn(fadeTimeMs);

  if(featured.length > 1) {
    setInterval(function () {
      index = index >= featured.length - 1 ? 0 : index + 1;
      last = current;
      current = featured[index];
      $(last).fadeOut(fadeTimeMs, function () {
        $(current).fadeIn(fadeTimeMs);
      });
    }, fadePeriodS*1000);
  }
  
  $('.introduction .play-intro img').hover(function () {
    $(this).attr('src', '/images/mov_over.gif');
  }, function () {
    $(this).attr('src', '/images/mov_default.gif');    
  });
  
  $('.services a.featured img').hover(function () {
    $(this).attr('src', $(this).attr('src').replace('.png', '_over.png'));
  }, function () {
    $(this).attr('src', $(this).attr('src').replace('_over.png', '.png'));    
  });  
});

function canPlay (video) {
  var children = video.children;
  for (var i=0; i<children.length; i++) {
    if (children[i].tagName.toUpperCase() == "SOURCE") {
      var canPlay = video.canPlayType(children[i].type);
      if(canPlay == "probably" || canPlay == "maybe") {
        return true;
      }
    }
  }
  return false;
}

function showPlayer () {
  $('.introduction').replaceWith($('.screencast'));
  $('.screencast').show();
  
  var video = $('.screencast video').get(0);
  if (video.play && canPlay(video)) {
    video.play();
  } else {
    var box = $(video).parent();
    var flash = box.find('#flashContent');
    
    var flashInfo = JSON.parse(flash.attr('data-flash-info'));

    var flashvars = {
      file:       flashInfo.file,
      autostart:  true,
      skin:       '/video/skin.swf',
      icons:      false,
      stretching: 'fill',
      controlbar: 'over'
    };
    
    if (flashInfo.image)
      flashvars['image'] = flashInfo.image;
    
    var params = {
      'allowfullscreen':    'true',
      'allowscriptaccess':  'always',
      'bgcolor':            '#ffffff'
    };
    
    var attributes = {
      'id':                 'player1',
      'name':               'player1'
    };

    $('#flashContent').show();
    box.replaceWith(flash);

    swfobject.embedSWF('/video/player-licensed.swf', 'flashContent', flashInfo.width, flashInfo.height, '9', 'false', flashvars, params, attributes);
  }
}

var colors = [
  //selector         grey       red
  [ '.introduction', '#282820', '' ],
  [ '.services',     '#39393B', '#EF3D4B' ],
  [ '#navigation',   '#39393B', '#EF3D4B' ],
  [ 'body',       '#282829', '#992831' ],
  [ '#page',      '#282829', '#EF3D4B' ]
]

function fadeColors (a, toColor) {
  try {
    var toColorIndex = [ 'grey', 'red' ].indexOf(toColor);

    var curColor = $('body').attr('id');
    if (curColor == toColor) {
      window.location = a.href;
      return;
    }

    var i = 0;
    colors.forEach(function (colorInfo) {
      var selector  = colorInfo[0];
      var destColor = colorInfo[toColorIndex + 1];
      var elements = $(selector);
      if (elements.length > 0) {
        if (i < 3) {
          elements.animate({ backgroundColor: destColor }, 1000);
        } else {
          elements.animate({ backgroundColor: destColor }, 1000, function () {
            console.info('done animating');
            window.location = a.href;
          });
        }
      }
      i++;
    });
  } catch (e) {
    console.info(e);
  }
}

/*
$(function () {
  $('a.to_grey').click(function () {
    fadeColors(this, 'grey');
    return false;
  });
  $('a.to_red').click(function () {
    fadeColors(this, 'red');
    return false;
  });
});
*/