$(document).ready(function(){
  
  $('ul#leftnav').children('li').each(function(){
    var h = $(this).height();
    if(h<=45)  {
      $(this).addClass('single');
    }else if(h>45 && h<=75)  {
      $(this).addClass('double');
    }else if(h>75)  {
      $(this).addClass('triple');
    }
  });
  
  
  $('ul#leftnav').children('LI').mouseover(function(){
    $(this).addClass('over');
  }).mouseout(function(){
    $(this).removeClass('over');
  });



// --------------------------------------------------------
  // auto embed google map if google map link found in body
  // --------------------------------------------------------

  var sMapSel = 'a[href^="http://maps.google.ie/maps"]:not(.dont-embed)';

  $(sMapSel).each( function() {

       var nEmbedMapWidth  = 578 ;
       var nEmbedMapHeight = 350 ;
       var sEmbedMapHref   = $(this).attr('href') ;
       //swap source=embed to output=embed
       sEmbedMapHref = sEmbedMapHref.replace('source=embed','output=embed');
        // Ok we have the width/height and href
        // transform this into an iFrame embed tag
        // FORMAT is
        //
        // <iframe frameborder="0" height="350" marginheight="0" marginwidth="0" scrolling="no" src="http://maps.google.co.uk/maps?daddr=Market+Street,+Armagh,+Co.+Armagh,+BT61+7BW&amp;q=Market+Place+Theatre+Armagh&amp;hl=en&amp;vpsrc=0&amp;gl=uk&amp;t=m&amp;ie=UTF8&amp;ll=54.348628,-6.653874&amp;spn=0,0&amp;output=embed" width="425" />
        //
      if( sEmbedMapHref !='' && typeof(sEmbedMapHref)!='undefined') {
          var embedMapHtml = '<'+'ifr'+'ame ';
              embedMapHtml+= 'width="'+nEmbedMapWidth+'" height="'+nEmbedMapHeight+'" ';
              embedMapHtml+= ' src="'+sEmbedMapHref+'" ';
              embedMapHtml+= ' frameborder="0" marginheight="0" marginwidth="0" scrolling="no" ';
              embedMapHtml+= ' >'+'<'+'/'+'ifr'+'ame'+'>';

          $(this).css("display","block") ;
          //$(this).css("text-align","center") ;
          $(this).parent().before(embedMapHtml); //assuems the link is in a <p> or <div>
          //$(this).hide();
        }

    });








  // regen via ajx with a delay to allow the server time to set the sessions
  if ($('#olycomptok').length>=1) {
    //olycomp_to = window.setTimeout(regenerateToken,333,'olycomp');
    var olycomp_to = window.setTimeout( function() {regenerateToken('olycomp'); },333);
  }

  if ($('#olyalertstok').length>=1) {
    //olycomp_to = window.setTimeout(regenerateToken,333,'olyalerts');
    var olyalerts_to = window.setTimeout( function() {regenerateToken('olyalerts'); },333);
  }

  // Token Regeneration
  function regenerateToken(tokenName) {
    $.getJSON('/newtok.php',{tik:tokenName},function (oData){ updateTok(oData,tokenName);});
  }

  function updateTok(oData,st) {
    var tokf = '#'+st+'tok';
    var oldt = $(tokf).val();
    $('#'+st+'tok').val(oData.newtoken);
    var tokto = st+'_to';
    window.clearTimeout(tokto);
  }


});
