function clearMe(oObj,val) {
  if(oObj.value==val) {
    oObj.value=''
  }
}

function fillMe(oObj,val) {
  if(oObj.value=='') {
    oObj.value=val
  }
}

// jquery equiv of above
function qClearMe(oObj,val) {
  if(oObj.val()==val) {
    oObj.val('');
  }
}

function qFillMe(oObj,val) {
  if(oObj.val()=='') {
    oObj.val(val);
  }
}

/**
 *
 * @access public
 * @return void
 **/
 var nLabel = 1;
function SocialPop(sHref){
  sLabel = 'socialpop'+nLabel;
  nLabel +=1;
  window.open(sHref,sLabel,"width=740,height=500,scrollbars=yes,statusbar=yes,resizable=yes");
  return false;
}


/**
* Open External Links In New Window
**/

$(document).ready(function(){
  $('a.newwin, a.external').click(function(){
      window.open($(this).attr('href'));
      return false;
    }).keypress(function(){
      window.open($(this).attr('href'));
      return false;
    })


    //thickbox videos open in new window

    $('a.thickbox').click(function(){

      var sVideoUrl  = $(this).attr('href');
      var aVurlParts = sVideoUrl.split('&');
      var sVideoFile = aVurlParts[2];

      if (sVideoUrl.substr(0,8)=='/tbvideo') {
         //force a google analytics eventTracking event
         _gaq.push(['_trackEvent','Videos','Open In Lightbox',sVideoFile]);
      }

    });

});


/**
* cat list expand/contract
**/
$(document).ready(function(){
  $('div.catlist').mouseover(function(){
    var maxHeight = $(this).children('div.catlistinner').height() + 2;
    if (maxHeight>=30) {
      $(this).css('height',maxHeight+'px');
    }
  }).mouseout(function(){
    //$(this).css('height','18px');
  });
});

$(document).ready(function(){
  $('div.taglist').mouseover(function(){
    var maxHeight = $(this).children('div.taglistinner').height();
    if (maxHeight>=30) {
      $(this).animate({height:( (maxHeight)+'px'),duration:'fast'});
    }
  }).mouseout(function(){
    //$(this).animate({height:'18px',duration:'fast'});
  });
});


/**
* When an LI is moused over then apply the .over class to it since IE wont recognise the :hover pseudo class
**/
$(document).ready(function(){
  $('li').mouseover(function(){
    $(this).addClass('over');
  }).mouseout(function(){
    $(this).removeClass('over');
  });

});


/**
* Top Nav More Links toggle
**/
$(document).ready(function(){

  $('ul#morenav').hide();
  $('#topnav ul li ul').hide();


  $('li.moretn').click(function(){

    //$('ul#morenav').toggle();
    if($('ul#morenav').is(':hidden')) {
      //show the list with a slide effect
      $('ul#morenav').slideDown();
    }else{
     // if they are already visible slide them closed
     $('ul#morenav').slideUp();
    }

    return false;
  });

 // Do not allow the click event on our list layer to propagate
 $('ul#morenav').click(function(e) {
   e.stopPropagation();
 });

 // if we click anywher on the page close the list of days if its visible
 $(document).click(function() {
   $('ul#morenav:visible').slideUp();
   $('#topnav ul li ul:visible').slideUp();
 });


 $('div#topnav ul li').mouseenter(function() {
   if( $(this).children('ul').length>=1) {
     $(this).children('ul:hidden').slideDown();
   }
 }).mouseleave(function(){
   $(this).children('ul:visible').slideUp();
 });


});




/**
* Header Row Togle full and compact mode
**/
$(document).ready(function(){
  $('#hideheader').click(function(){
    $('#pageheader').slideUp();
    $('#compactheader').slideDown();
    return false;
  });

  $('a.hidecompact').click(function(){
    $('#compactheader').slideToggle();
    $('#pageheader').slideToggle();
    return false;
  });

});


/**
* Make right column the same height as teh center column if it's shorter
**/

$(document).ready(function(){
  //fixColHeights();
})

function fixColHeights(){
  var rch =   $('#right-col').height();
  var mch =   $('#page-content').height();
  if (rch < mch) {
    $('#right-col').css('min-height',mch+'px');
    $('#right-col').css('height',mch+'px');
  }
}



/**
* Social networking Icons
**/
$(document).ready(function(){

  //if there are 2 extrasocial items on page then remove on
  if($('ul#extrasocial').length>1) {
    $('ul#extrasocial:last').remove();
  }

  $('a.togoptional').mouseover(function(e){
    //position relative to item
    var thispos = $(this).position();
    $('ul#extrasocial').css({top: (thispos.top +20) +'px',left:thispos.left+'px',position:'absolute','z-index':'50'});
    $('ul#extrasocial').show();
    return false;
  });

  //on page body click remove the extrasocial layer from view.

   //firstly do not allow a click within our layer to propogate down
   $('#extrasocial').click(function(e) {
    e.stopPropagation();
   });

   $(document).click(function(){
    $('ul#extrasocial').hide();
   });


});






/**
* sendToFriend Functionality
**/

var elX = '';
var elY = '';

jQuery(document).ready(function(){
   $("li.mail-on").click(function(e){
      $('#send-to-friend-form').show();
      $('#send-to-friend-response').hide();
   });
   $("li.comment-on").click(function(e){
      $('#comment-form').show();
      $('#comment-response').hide();
   });
})


/**
* use Ajax to Post The Form to an email script
**/
function sendToFriend() {

  errCnt = 0;
  //validation
  if ($('#stf-your-name').val()=='') {
    errCnt++;
  }

  if ($('#stf-your-email').val()=='') {
    errCnt++;
  }

  if ($('#stf-friend-name').val()=='') {
    errCnt++;
  }

  if ($('#stf-friend-email').val()=='') {
    errCnt++;
  }

  //POST via AJAX
  if (errCnt<=0) {
    $.post("/sendToFriend/",{yourname:$('#stf-your-name').val(),
                                youremail:$('#stf-your-email').val(),
                                friendname:$('#stf-friend-name').val(),
                                friendemail:$('#stf-friend-email').val(),
                                sendlink:$('#stf-url').val(),
                                sendhsh:$('#stf-hsh').val(),
                                message:$('#stf-message').val(),
                                ajax: 'true'}, function(j){

      $('#send-to-friend-form').hide();
      $("#send-to-friend-response").html(j['message']);
      $('#send-to-friend-response').show();

    },"json")
  } else {
      $("#send-to-friend-response").html('Please complete all form fields');
      $('#send-to-friend-response').show();
  }


}
function closeSendToFriend() {

  $('#send-to-friend').hide();

}



function submitComment() {

  errCnt = 0;
  //validation
  if ($('#com-your-name').val()=='') {
    errCnt++;
  }

  if ($('#com-your-email').val()=='') {
    errCnt++;
  }

  if ($('#com-location').val()=='') {
    errCnt++;
  }

  if ($('#com-comment').val()=='') {
    errCnt++;
  }

  //POST via AJAX
  if (errCnt<=0) {
    $.post("/submit-comment/",{user_name:$('#com-your-name').val(),
                                user_email:$('#com-your-email').val(),
                                user_location:$('#com-location').val(),
                                comment:$('#com-comment').val(),
                                sendlink:$('#stf-url').val(),
                                sendhsh:$('#stf-hsh').val(),
                                ajax: 'true'}, function(j){

      $('#comment-form').hide();
      $("#comment-response").html(j['message']);
      $('#comment-response').show();

    },"json")
  } else {
      $("#comment-response").html('Please complete all form fields');
      $('#comment-response').show();
  }
}//end function submitComment

function closeComment() {

  $('#comment-form-container').hide();

}

/** Video / Audio Clips in a facebox **/
 function tbmedia() {
     $('#TB_window').css('background','#000');
     $("#TB_ajaxContent")[0].scrollTop = 0;
     $("#TB_ajaxContent")[0].style.backgroundColor='#000';
     $("#TB_ajaxContent")[0].style.color='#fff';
     $('#TB_window').css({width:'510px !important',height:'398px !important'});
 }


function callAdverts(){

  if (isISYS=='undefined') {
    isISYS = false;
  }
  if (isISYS==true) {
    return;
  }

  if (bDFP!='' && bDFP===true) {
    return;
  }

  var oTime = new Date();
  var stime = oTime.getTime();




  var aZones = new Array('zlb','zfp1','zfp2','adtop','azonea','advert1','advert2','advert3','advert4','advert5','advert6',
                         'advert7','advert8','advert9','advert10',
                         'azonec','azoned','azonef1','azonef2','azonef3','azoneh1','azoneh2','azonej1','azonem','azonel',
                         'banner-zone-k div.inner','banner-zone-k div.zonek',
                         'banner-zone-l',
                         'tabbed-zone-1','tabbed-zone-2','tabbed-zone-3','tabbed-zone-4',
                         'tabbed-zone-5','tabbed-zone-6','tabbed-zone-7','tabbed-zone-8','advertise',
                         'adtophp','banner-zone-lhp','banner-zone-khp div.inner','advert1hp','advert2hp') ;


  var sZones = '';
  for (var z in aZones) {
    if( $('#'+aZones[z]).length ) {
      sZones= sZones + aZones[z] +',';
    }
  }

  //N.B. very important to pass the current page URL to the ajax script as some adverts are restricted by page url
  var request_url = '';
  request_url = location.href

  $.getJSON("/get-ads/",{ t:stime,getZones:sZones,pageurl:request_url} ,function(zones) {
    for(sZone in zones) {
      $("#"+sZone).html(zones[sZone]);
    }
  });
}


//Dirty Hack to get columns of equal height
 $(document).ready(function(){

  if (isISYS=='undefined') {
    isISYS = false;
  }
   if (isISYS!=true ) {
     callAdverts();
   }
   window.setTimeout('fixColHeights()',200);

//if the search box has focus OR is not empty then remove the background image
   $('.cseinput').focus(function(){
     $(this).addClass('focus');
   }) ;

   $(".cseinput[value!='']").addClass('nobg');

   $(".cseinput").blur(function(){
     if( $(this).val()!=''){
       $(this).addClass('nobg');
     }else{
       $(this).removeClass('nobg');
     }
     $(this).removeClass('focus');
   });


   // zebra stripes
   //explicitly set cellspacing and cellpadding to 0
   $('table.datatable').attr({cellspacing:'0',cellpadding:'0'});
   //add a class for alternate rows
   $('table.datatable tr:nth-child(odd)').each( function(){ $(this).children('td').addClass('dark') } );
   // zebra table row hover
   $('table.datatable tr').mouseenter(function(){$(this).addClass('over')}).mouseleave(function(){$(this).removeClass('over')});
   //remove border from last TH element
   $('table.datatable tr th:last').addClass('nobrdr');


   //any embedded media links on this page?
   if ($('a.media').length>=1 ) {


     $('#main-col').find('a.media').each(function(){

       if ($(this).parent().attr('id')=='one-large-video')   {
         return;
       }


     //defaults
       var mediaWidth = 250;
       var mediaHeight = 192;
       var mediaImage = '';

       if ($(this).attr('rel') !='') {
         var sMediaOpts = $(this).attr('rel');
         sMediaOpts  = sMediaOpts.replace('{','');
         sMediaOpts  = sMediaOpts.replace('}','');

         if (undefined !== sMediaOpts && sMediaOpts!=='' ) {
            $el = $(this);
          // support metadata plugin (v1.0 and v2.0)
          	var meta = $.metadata ? $el.metadata() : $.meta ? $el.data() : {};
          	meta = meta || {};
          	var w = meta.width	|| parseInt(((sMediaOpts.match(/width:(\d+)/)||[])[1]||0));
          	var h = meta.height || parseInt(((sMediaOpts.match(/height:(\d+)/)||[])[1]||0));
          	var ig = meta.image || ( ( sMediaOpts.match(/image:\'(.*)\'/) ||[] ) [1] || 0 ) ;

          	if (w) meta.width	= w;
          	if (h) meta.height = h;
          	if (ig) meta.image = ig;

           mediaWidth = meta.width;
           mediaHeight = meta.height;
           mediaImage = meta.image;
         }

       }

       $(this).media({ width:mediaWidth,
                          height:mediaHeight,
                          caption:'',
                          bgColor:'#000000',
                          params:{  allowfullscreen: true},
                          flashvars:{allowscriptaccess: 'always',
                                     usefullscreen:true,
                                     autostart: false,
                                     backcolor:'001001001',
                                     screencolor:'#000000',
                                     frontcolor:'900900000'
                                     ,image: mediaImage
                                     }
                        });


     });


   }//end found media links

 });

