$(document).ready(function(){
  // Draw fancy text
  Cufon.replace('#slideshow #count li a,#slideshow h2',{ fontFamily: 'bauhaus', hover:true });
  Cufon.replace('h1, #home h2, helvneu',{ fontFamily: 'helvneu' });
  Cufon.replace('h2.bauhaus',{ fontFamily: 'bauhaus', hover:true }); 
  // Style navigation
  $('ul.sf-menu').superfish({ 
    delay:       500,
    animation:   {height:'show','filter':''},
    speed:       'fast', // Has to be fast, IE7 width's are broken.
    autoArrows:  false,
    dropShadows: false
  });

  // Fix mobile device no-hovers
  function isMobile(){
    var mobile = false;
    var agent = navigator.userAgent.toLowerCase();
    var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
    var mobileOS = typeof orientation != 'undefined' ? true : false;
    var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
    var iOS = (navigator.platform.indexOf("iPhone") != -1) ||
      (navigator.platform.indexOf("iPad") != -1) ? true : false;
    var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;

    if(mobileOS || touchOS || iOS || android || otherBrowser) {
      mobile = true;
    }

    return mobile;
  }

  if(isMobile()){

    $('*:not(a)').click(function() {
      $(this).hover();
    });

    $('ul.sf-menu a').click(function() {
      if ($(this).attr('hover') != 'true' && $(this).next('ul').length ) {
        $(this).attr('hover','true').hover();
        return false;
      }
    });
  }

  // Highlight current menu
  var current_d = location.pathname.substr(1).split(/\//g);
  if(current_d[0]){
    $('.sf-menu a[href="/' + current_d[0] + '/' +  current_d[1] + '"]').addClass('selected').parent().addClass('selected');
    $('.sf-menu a[href="' + location.pathname + '"]').addClass('selected');
    $('.sf-menu .selected').parent().parent().parent().addClass('selected');
  } else {
    $('.sf-menu a[href="/"]').addClass('selected').parent().addClass('selected');
  }

  // Animate the search function
  $('#query').focus(function(){$(this).animate({width:'+=150px'}, 300, function(){})});
  $('#query').blur(function(){$(this).animate({width:'-=150px'}, 300, function(){})});

  // Input text box default resets
  $('input#query').each(function(){
    $(this).attr('init', $(this).val());
    $(this).addClass('light-txt');      
  });

  $('input').focus(function(){
    if($(this).val()==$(this).attr('init')) {
      $(this).val('');
      $(this).removeClass('light-txt');
    }
  });

  $('input').blur(function(){
    if($(this).val() == $(this).attr('init') || $(this).val().replace(/^\s*/, '').replace(/\s*$/, '')=='') {
      $(this).val($(this).attr('init'));
      $(this).addClass('light-txt');
    }
  });

  // Overwrite jQuery fades to 'fix' IE display bug
  jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
      if (jQuery.browser.msie)  
      this.style.removeAttribute('filter');  
    if (jQuery.isFunction(callback)) 
      callback();  
    }); 
  }; 

  jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
      if (jQuery.browser.msie)  
      this.style.removeAttribute('filter');  
    if (jQuery.isFunction(callback)) 
      callback();  
    }); 
  }; 

  jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
      if (to == 1 && jQuery.browser.msie)  
      this.style.removeAttribute('filter');  
    if (jQuery.isFunction(callback)) 
      callback();  
    }); 
  }; 

  // Modal box
  $("#dialog-message").dialog({
    bgiframe: true,
  	height: 'auto',
	  width: 'auto',
    autoOpen: false,
    modal: true,
		position: ['center','middle']
  });

  // Modal replace
  $('.modal').click(function() {
    $("#dialog-message img").attr('src','');
    var imgSrc = $(this).attr('alt');
    $("#dialog-message img").attr('src',imgSrc);
    $('#dialog-message img').load(function() {
      $("#dialog-message").dialog("open");
      $("#dialog-message").dialog('option', 'width', $("#dialog-message img").width()+24); 
      $("#dialog-message").dialog('option', 'position', ['center','middle']); 
    });
  });

  // jQuery UI tabs
  if($("#tabs").length) {
    var curr_tab = location.hash.substr(5);
    var tab_ref = location.pathname + '#tab-'+curr_tab; // We lie a bit so that we don't actually move.
      _gaq.push(['_trackPageview', tab_ref]); // Track the clicked tab 
  }

  var $tabs = $( "#tabs" ).tabs({
    show: function(event, ui) {
      var tab_index = $(this).tabs('option', 'selected');
      var tab_num = parseInt(tab_index) + 1;
      var tab_ref = location.pathname + '#tab-'+tab_num;
      //document.location.href = tab_ref; // So people can bookmark it. Can't use. IE7 is too slow...
      $('.sidebox').css('display','none');
      $('#sidebox-'+ tab_index).css('display','block');
      _gaq.push(['_trackPageview', tab_ref]);
      globalResize(1);
      if($('.dock2').length) { // Fix for IE7 since it's a piece of shit
        $('.dock2').Fisheye({
          maxWidth: 80,
          items: 'a',
          itemsText: 'span',
          container: '.dock-container2',
          itemWidth: 100,
          proximity: 130,
          alignment : 'left',
          valign: 'bottom',
          halign : 'center'
        })
      }
    },
    selected: parseInt(curr_tab) - 1
  });

  $('.clicktab a').click(function(){
    var clicked = $(this).attr('href').substr(5);
    $tabs.tabs('select', clicked);
    return false;
  });

  // Customer page img rollovers
  $('#customer-logos img').hover(
    function(){ 
      var toggle = $(this).attr('src').replace(/bw.png/, 'color.png');
      $(this).attr('src',toggle);
    },
    function(){
      var toggle = $(this).attr('src').replace(/color.png/, 'bw.png');
      $(this).attr('src',toggle); 
    }
  );

  // Make sure form is cleared
  $('input.reset').click( function(){
    $('.input input, .input textarea').attr('value','');
  });
});

// Functions that require the page to be fully loaded
$(window).load(function(){
  globalResize();

  // Fade in errors
  $('.error-message').delay('1000').fadeOut('1000').fadeIn('1000').fadeOut('1000').fadeIn('1000').fadeOut('1000').fadeIn('1000');

  // Fade out success
  $('.flash_success').delay(1000).fadeTo(1000,0).hide(500, function(){;
    $('.input input, .input textarea').attr('value','');
  });

});

function globalResize(resetHeight) {
  // Make columns height match
  var colW = $('.secondary .col-wide');
  var colN = $('.secondary .col-narrow');
  if(resetHeight == 1) {
    colN.css('height','auto');
    colW.css('height','auto');
  }
  if(colN.height() > colW.height()) {
    colW.css('height',colN.height() - 11 + 'px');
  } else {
    colN.css('height',colW.height() + 11 + 'px');
  }
}

