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


/**
* 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(){
  var oTime = new Date();
  var stime = oTime.getTime();

  var aZones = new Array('zlb','advert1','advert2','advert3','advert4','advert5','advert6','advert7',
                         'zfp1','zfp2','banner-zone-l div.zonel','banner-zone-k div.zonek') ;

  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(){

   callAdverts();

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



 });
