// Constants
var basedir = '/';
var mediadir = basedir + 'media/';

var font = (function () { // Determines if a font is installed
    var test_string = 'mmmmmmmmmwwwwwww';
    var test_font = '"Comic Sans MS"';
    var notInstalledWidth = 0;
    var testbed = null;
    var guid = 0;
    
    return {
        // must be called when the dom is ready
        setup : function () {
            if ($('#fontInstalledTest').length) return;

            $('head').append('<' + 'style> #fontInstalledTest, #fontTestBed { position: absolute; left: -9999px; top: 0; visibility: hidden; } #fontInstalledTest { font-size: 50px!important; font-family: ' + test_font + ';}</' + 'style>');
            
            
            $('body').append('<div id="fontTestBed"></div>').append('<span id="fontInstalledTest" class="fonttest">' + test_string + '</span>');
            testbed = $('#fontTestBed');
            notInstalledWidth = $('#fontInstalledTest').width();
        },
        
        isInstalled : function(font) {
            guid++;
        
            var style = '<' + 'style id="fonttestStyle"> #fonttest' + guid + ' { font-size: 50px!important; font-family: ' + font + ', ' + test_font + '; } <' + '/style>';
            
            $('head').find('#fonttestStyle').remove().end().append(style);
            testbed.empty().append('<span id="fonttest' + guid + '" class="fonttest">' + test_string + '</span>');
                        
            return (testbed.find('span').width() != notInstalledWidth);
        }
    };
})();

function embedFontBrowser() {
    if ($.browser.safari && $.browser.msie) {
      return true;
    } else if ($.browser.mozilla) {
      if (jQuery.browser.version.substr(0,5).replace(/\./g , '') < "191") {
        return false;
      } else {
      }
    } else {
      return false;
    }
}

// JW Player Functions
function initPlayer(movie) {
  var so = new SWFObject(basedir + 'player/player.swf','whatplayer','100%','100%','9');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen','true');
  so.addVariable('width','100%');
  so.addVariable('height','100%');
  so.addVariable('file', mediadir + 'business_meetings.flv');
  so.addParam('flashvars', 'file=' + mediadir +  movie + '.flv&controlbar=none&autostart=true');
  so.addVariable('javascriptid','whatplayer');
  so.addVariable('enablejs','true');
  so.write('whatvideo');
  return true;
}
function playerReady(obj) {
  var id = obj['id'];
  var version = obj['version'];
  var client = obj['client'];
  player = document.getElementById(id);
};

// Forms
var formFunc = {
    init: function(){
      this.createPseudoLinks();
      this.createDeleteButtons();
    },
    createPseudoLinks: function(){ // Turns elements into links
      $('.employeelink').addClass('pseudolink').click(function(){
        var currid = $(this).attr('id').match(/edit(\d+)/)[1];
        $('form#form' + currid).toggle();
      });
    },
    createDeleteButtons: function(){
      $('form .delete').click(function(){ // Gives delete buttons confirmation dialog and radio button checking
        if (confirm('Are you sure you want to delete?')) {
          $(this).parents('form').contents().find('input[type=radio].delete').attr('checked', 'checked'); 
          $(this).parents('form').submit();
        } else {
          return false;
        }
      });
    }
}

$(document).ready(function(){
  //Current Page
  var classes = $('div#wrapper').attr('class').split(' ');
  for (var i = 0; i < classes.length; i++) {    
    $('ul.nav li').each(function(){ 
      if ($(this).hasClass(classes[i])) {
        $(this).addClass('current');
      }
    });
  }

  //Font replacement
  font.setup(); 
  if (font.isInstalled('Gotham-Book') && font.isInstalled('Gotham-Bold')){ //Font is installed, just adjust margin
    $('div#nav ul li.current a, div.home li.home a, div.whatwedo li.whatwedo a, div.blog li.blog a, div.socialresponsibility li.socialresponsibility a, div.contact li.contact a, div.backstage li.backstage a, div.tspinteractive li.tspinteractive a, div.capabilities li.capabilities a').css({marginTop:'4px', marginBottom:'6px'});
  } else if (!embedFontBrowser() && !font.isInstalled('Gotham-Book') && !font.isInstalled('Gotham-Bold')) { //Font isn't installed and can't be embedded, load it with flash.    
    $('div.secondary div#content h1, div.secondary div#content h3').flash(
        { 
            src: basedir + 'fonts/gotham-bold.swf', 
            flashvars: { 
                css: [
                    '* { color: #3FC4C6; }'
                ].join(' ')
            }
        },
        { version: 7 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML.toUpperCase();
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));						
        }
    );
    $('div.contact div#content h2').flash(
        { 
            src: basedir + 'fonts/gotham-bold.swf', 
            flashvars: { 
                css: [
                    '* { color: #CCC2C2; }'
                ].join(' ')
            }
        },
        { version: 7 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML.toUpperCase();
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));						
        }
    );    
    $(' div#nav ul li a').addClass('imagereplacement');
  }
    //General
    $('a.email').each(function(){ //Finds links with the email class and replaces the text with the email address - helps prevent spam
      var link = $(this).html();  //Links should be formatted just like this: name AT domain DOT com
      link = link.replace(/ AT /, '@'); //Place the text for the link in the title attribute
      link = link.replace(/ DOT /, '.');
      var linktitle = $(this).attr('title');
      if (linktitle != '') {
        $(this).html(linktitle);
      } else {
        $(this).html(link);
      }
      $(this).attr('href', 'mailto:' + link);
    });
    //$('div#nav ul li a').prepend('<span></span>');

    //Home
    $('div.home').each(function(){
      var so = new SWFObject(mediadir + 'audio_home.swf','tsphome_audio_swf','1024','464','9');
      so.write('content');    
    });
    //Contact Page - Scrolls Employees
    $('div.photos').css('overflow', 'hidden');
    $('a[href^=#employee]').click(function(){
      $('a[href^=#employee]').removeClass('current');
      $(this).addClass('current');
      var selecteddiv = 'div' + $(this).attr('href');
      $('div.photos').scrollTo($(selecteddiv), 1100, {easing:'easeOutBack'});
      return false;
    });
    
    //What We Do Page - Controls videos and form 
    $('form#request_backstage').hide(); //Hide the form
    $('a[href="#request_backstage"]').click(function(){
      $('#enterbackstagepass').toggle();
      $('form#request_backstage').slideToggle();    
    });    
    $('div#wrapper.whatwedo').each(function(){ //If we're on the What We Do page only
      $('div.videotext').hide();
      var whatImage = $('div#whatvideo p.image').html();
      $('ul.whatwedo li a').click(function(){
        $('div.whatwedo div#content h1').addClass('pseudolink');
        $('div#whattext div').hide();
        var link = $(this).attr('href').match(/#(\w.+)/)[1];
        if (document.getElementById('whatplayer') == null) {
          initPlayer(link);
        }
        $('ul.whatwedo li a').removeClass('current');
        $(this).addClass('current');
        $('div#' + link).fadeIn();    
        if (typeof player != "undefined") {
          player.sendEvent('LOAD',mediadir + link + '.flv');
        }  
        return false;
      });
      $('div.whatwedo div#content h1').click(function(){ // Restore original state
        $('div#whattext div').hide();
        $('ul.whatwedo li a').removeClass('current');
        $('div#what_default').fadeIn();
        $('div#whatvideo').html(whatImage);     
      });
    });
    //Blog
    $('div.blogs').each(function(){
      $('.add_comment').click(function(){
        $(this).next('.comment_form').slideToggle();
        return false;  
      });    
    });
});  
