$(document).ready(function(){

   $('#centre-column table:not(.nostyle)').addClass('autostyle').attr({cellspacing:'0',cellpadding:'0',border:'0'});


   $('#centre-column table:not(.nostyle) tr:nth-child(odd)').each( function(){ $(this).children('td').addClass('dark') } );
   // zebra table row hover
   $('#centre-column table:not(.nostyle) tr').mouseenter(function(){$(this).addClass('over')}).mouseleave(function(){$(this).removeClass('over')});


  // wrap any panel special offer links around their corresponding images
  $('li.pnloffer').each(function(){
    var offerlink = $(this).find('h3 a').attr('href');
    $(this).find('img').wrap('<a href="'+offerlink+'"></a>');
  });

  // wrap any list page special offer links around their corresponding images
  $('div#items ul li,div#news ul li').each(function(){
    var itemlink = $(this).children('h3').children('a').attr('href');
    $(this).children('p').children('img').wrap('<a href="'+itemlink+'"></a>');
  });

  // wrap any showlist page  links around their corresponding images
  $('ul#showslist li').each(function(){
    var itemlink = $(this).children('h2').children('a').attr('href');
    $(this).find('img').wrap('<a href="'+itemlink+'"></a>');
  });

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

  var sMapSel = 'a[href^="http://maps.google.co.uk/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');

       // ensure output = embed exists
       if(sEmbedMapHref.indexOf('output=embed')=== -1 ) {
         sEmbedMapHref = sEmbedMapHref + '&amp;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();
        }

    });// end each link


  //tokenizer
  if ($('#mktalertstok').length>=1) {
    //mktplc_to = window.setTimeout(regenerateToken,333,'mktalerts');
    var mktalerts_to = window.setTimeout( function() {regenerateToken('mktalerts'); },333);
  }
  if ($('#mktsmstok').length>=1) {
    //mktplc_to = window.setTimeout(regenerateToken,333,'mktsms');
    var mktsms_to = window.setTimeout( function() {regenerateToken('mktsms'); },333);
  }
  if ($('#mktcontok').length>=1) {
    //mktplc_to = window.setTimeout(regenerateToken,333,'mktcon');
    var mktcon_to = window.setTimeout( function() {regenerateToken('mktcon'); },333);
  }


  // ---------------------------------------
  // ajaxify the alert signup processes
  // ---------------------------------------

  // ----- SMS -----
  $('')


  // ----- EMAIL -----


  // ----- SUBSCRIBE FORM PAGE -----
  $('form.subscribeform input.sbt').click(function(){

    if($(this).parent('p').parent('form').length==1 ) {
      oForm =   $(this).parent('p').parent('form');
    } else if ($(this).parent('form').length==1  ) {
      oForm =  $(this).parent('form');
    } else {
      //default to the first onmatch found
      oForm = $('form.subscribeform:first');
    }

    //what is the action url
    var action = oForm.attr('action');

    // add a "working" ajax loader animation graphic
    var frmid = $(this).attr('id');
    oForm.find('span.resulticon').remove();
    $('p#msg'+frmid).remove();
    $(this).after('<span id="loader'+frmid+'"><img src="/img/icons/ajax-loader.gif" title="working" width="16" height="16"/> working</span>')

    // grab the form vars
    var postvars = oForm.serialize();

    // tag on the ajx identifier
    postvars = postvars + '&ajx=1';

    // post
    $.post(action,postvars,function(oData) {
      // remove animation graphic
      $('#loader'+frmid).remove();
      oForm.find('span.resulticon').remove();
      // add in error message if required
      var sType = oData.ptype;
      if( oData.sType==false || (oData.sms== false && oData.email==false)  ) {
        oForm.find('input.sbt').after('<span class="resulticon"><img src="/img/icons/result-bad.gif" alt="X = failed" /> Failed</span>');
      } else {
        oForm.find('input.sbt').after('<span class="resulticon"><img src="/img/icons/result-good.gif" alt="Tick = success" /> Success!</span>');
      }

      if (oData.error!='') {
        $('p#msg'+frmid).append('<br />'+oData.error);
      }else if(oData.msg!='') {
        oForm.after('<p id="msg'+frmid+'" class="msg">'+oData.msg+'</p>');
      }




    },'JSON');

    // refresh tokens
    if ($('#mktalertstok').length>=1) {
      //mktplc_to = window.setTimeout(regenerateToken,333,'mktalerts');
      var mktalerts_to = window.setTimeout( function() {regenerateToken('mktalerts'); },333);
    }
    if ($('#mktsmstok').length>=1) {
      //mktplc_to = window.setTimeout(regenerateToken,333,'mktsms');
      var mktsms_to = window.setTimeout( function() {regenerateToken('mktsms'); },333);
    }


     return false ;
  });


});



  // 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);
  }
