$(document).ready(function(){

  //initialise tablesorter
//  $('.sortable').tablesorter();

  //init gallery
  $('.hide_content').click(function(){

    $(this).children('.hidden').toggle();
  });

  $('#show_all').click(function(){

    $('.hidden').toggle();
  });

  $('.evt_row_c').hover(function(){

    $(this).css({background:'#cccccc'});
  },function(){

    $(this).css({background:'#eeeeee'});
  });
/*
  $(".tweet").tweet({
    join_br_text: null ,
    join_text: " ",
    avatar_size: 1,
    count: 1,
    auto_join_text_default: "",
    auto_join_text_ed: "",
    auto_join_text_ing: "",
    auto_join_text_reply: "",
    auto_join_text_url: "",
    loading_text: "loading tweets..."
  });
*/

  $('#gallery').sortable({
    axis: 'y',
    containment: 'document' ,
    cursor: 's-resize' ,
    distance: 10 ,
    helper: 'clone' ,
    items: 'li' ,
    opacity: 0.6 ,
    update: function(event, ui){
      $('#new_gallery_images').attr('value',implode(',' , $('#gallery').sortable('toArray')));
    }
    });
});


function show_booking_c(){

  $('#book_c').slideDown( 1000 , 'swing' );
  return true ;
}


function showDeadlineReminder() {

//  $('#rem_c').animate({opacity:'1',left:'337px'},500,'linear');
  $('#rem_c').animate({opacity:'1',left:'250px'},500,'linear');
  return true ;
}


function passthruGalleryID( gal_id ) {

  $('#GalleryID').attr('value',gal_id) ;
  return true ;
}


function eou_rel_chg_sts( TypeCode , LinkID , StsTxt , RelType ) {

  if ( confirm( 'Are you sure you wish to remove this ' + RelType + '?' ) ) {

    $('#' + TypeCode + '_fld_' + LinkID).attr('value','declined') ;
    $('#' + TypeCode + '_lnk_' + LinkID).hide() ;
  }
  return false ;
}


function toggleEmailForm() {

  $('#lnk_eml_oc').toggle();
  return false ;
}


function ajax_send_mail( ) {

  //grab our message data
  var emltype = $('#lnk_eml_typ').attr('name') ;
  var emltypeid = $('#lnk_eml_typ').attr('value') ;
  var subject = $('#eml_sbj').attr('value') ;
  var message = $('#eml_msg').attr('value') ;
  var c = $('#recaptcha_challenge_field').attr('value') ;
  var r = $('#recaptcha_response_field').attr('value') ;
  var a = $('#lnk_eml_a').attr('value') ;

  //run our ajax to modify mylfa
  $.ajax({
    type: 'POST',
    cache: false,
    url: 'ajax_send_message.php',
    data: 'mailtype='+escape(emltype)+'&id='+escape(emltypeid)+'&subject='+escape(subject)+'&message='+escape(message)+'&a='+escape(a)+'&c='+escape(c)+'&r='+escape(r) ,
    dataType: 'text',
    success: function(data){

      //check returned data is good
      if ( data == 'success' ) {

        //its good, reset fields and inform user
        $('#eml_sbj').attr('value','') ;
        $('#eml_msg').attr('value','') ;

        $('#lnk_eml_oc').hide() ;

        //there was a problem
        alert( 'Your message has been sent.' ) ;
      } else {

        //there was a problem
        alert( data ) ;
      }
      Recaptcha.reload();
    },
    error: function(request,error) {
      //there was a fundamental problem
      if (error = "timeout") {

        alert("The request timed out, please resubmit");
      } else {

        alert("ERROR: " + error);
      }
      Recaptcha.reload();
    }
  });
  return false ;
}


function loadCommunity( com_type ) {

  //clear content
  $('#' + com_type).html('') ;

  //call a random string to ensure we get a new page
	var rnd = new Date().getTime();

  $.get('ajax_populate_community.php?com_type=' + com_type + '&rnd=' + rnd, function(data){

    $('#' + com_type).html(data) ;
  }) ;
  return false ;
}


function addToMyLFA( xvar , zvar , avar ) {

  //run our ajax to modify mylfa
  $.ajax({
    type: 'GET',
    cache: false,
    url: 'ajax_add_to_mylfa.php',
    data: 'x='+escape(xvar)+'&z='+escape(zvar)+'&a='+escape(avar) ,
    dataType: 'text',
    success: function(data){
      //check returned data is good
      if ( data == 'success' ) {

        //its good, inform user
        $('#addEvent_'+xvar).hide() ;

        //write our desired output
        $('#addedEvent_'+xvar).show() ;
      } else {

        //there was a problem
        alert( data ) ;
      }
    },
    error: function(request,error) {
      //there was a fundamental problem
      if (error = "timeout") {

        alert("The request timed out, please resubmit");
      } else {

        alert("ERROR: " + error);
      }
    }
  });
  return false ;
}


function checkRequiredUserFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.FirstName.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your first name' + "\n" ;
  }
  if ( f.LastName.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your last name' + "\n" ;
  }
  if ( f.Email.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your email address' + "\n" ;
  }
  if ( f.Email2.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please confirm your email address' + "\n" ;
  }
  if ( f.Email.value != f.Email2.value ) {
    formError = true ;
    errorMsg += 'Your email addresses do not match' + "\n" ;
  }
  if ( f.Password.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your chosen password' + "\n" ;
  }
  if ( f.Password2.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please confirm your chosen password' + "\n" ;
  }
  if ( f.Password.value != f.Password2.value ) {
    formError = true ;
    errorMsg += 'Your passwords do not match' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}


function checkRequiredEditUserFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.FirstName.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your first name' + "\n" ;
  }
  if ( f.LastName.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your last name' + "\n" ;
  }
  if ( f.Password.value != f.Password2.value ) {
    formError = true ;
    errorMsg += 'Your passwords do not match' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}


function checkRequiredOrgFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.Name.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation name' + "\n" ;
  }
  if ( f.Town.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation town' + "\n" ;
  }
  if ( f.PostCode.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation postcode' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}


function checkRequiredEditOrgFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.Name.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation name' + "\n" ;
  }
  if ( f.Town.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation town' + "\n" ;
  }
  if ( f.PostCode.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your organisation postcode' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}


function checkRequiredEventFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.Name.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your event name' + "\n" ;
  }
  if ( f.Location.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter the event location' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}


function checkRequiredEditEventFields(f) {

  //check fields
  var formError = false ;
  var errorMsg = '' ;
  if ( f.Name.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter your event name' + "\n" ;
  }
  if ( f.Location.value.length < 1 ) {
    formError = true ;
    errorMsg += 'Please enter the event location' + "\n" ;
  }
  if ( formError ) {

    alert( errorMsg ) ;
    return false ;
  }
  return true ;
}

function clr_fld( fld , def ) {

  if ( fld.value == def ) {

    fld.value = '' ;
  }
}


function rst_fld( fld , def ) {

  if ( fld.value == '' ) {

    fld.value = def ;
  }
}


function mark_img_for_del( img_id ) {

  var del = confirm( 'Are you sure you wish to delete this image?' ) ;
  if ( del == true ) {

    $('#edit_img_c'+img_id).hide();
    $('#delImage'+img_id).attr('value','yes') ;
  }
  return false ;
}


var slideIntv = false ;
var supByIntv = false ;

function startSlides( numx , start ) {

  var slideIntv = window.setTimeout( "newSlide("+numx+","+start+")" , 2500 );
}

function newSlide( num , n ) {

  window.clearTimeout( slideIntv );
  nx = n + 1 ;
  if ( nx >= num ) {

    nx = 0;
  }
  $('#sl'+n).animate({opacity:'0'},200,'linear',function(){

//    var nextImg = $("#s"+nx);
    $("#sl"+nx).animate({opacity:'1'},{duration:500,easing:'linear'});
  });
  slideIntv = window.setTimeout( "newSlide("+num+","+nx+")" , 4000 );
}

function startSupByLogoSlide( numx , start ) {

  var supByIntv = window.setTimeout( "newSupBySlide("+numx+","+start+")" , 2500 );
}

function newSupBySlide( num , n ) {

  window.clearTimeout( supByIntv );
  nx = n + 1 ;
  if ( nx >= num ) {

    nx = 0;
  }
  $('#sb'+n).animate({'top':'-10px','opacity':'0'},200,'linear',function(){

    $("#sb"+nx).css({'display':'block'}).animate({'top':'0px','opacity':'1'},300,'swing',function(){

      $('#sb'+n).css({'display':'none','top':'20px'});
    });
  });
  supByIntv = window.setTimeout( "newSupBySlide("+num+","+nx+")" , 4000 );
}

function passthruGalleryID( gal_id ) {

  $('#GalleryID').attr('value',gal_id) ;
  return true ;
}

function remImg( img ) {

  var confirmWithUser = 'Are you sure you wish to remove this image?' ;
  if ( confirm( confirmWithUser ) ) {

    //prepare result array
    newImgs = new Array() ;

    //split string into array
    currentImgs = $('#new_gallery_images').attr('value').split(',') ;

    //calculate size
    numImgs = currentImgs.length ;

    //add all ids to a new array except the chosen image
    for ( i = 0 , j = 0 ; i < numImgs ; i++ ) {

      if ( currentImgs[i] != img ) {

        newImgs[j] = currentImgs[i] ;
        j++ ;
      }
    }

    //hide chosen image
    $('#gallery #'+img).fadeOut(function(){

      //remove chosen image from the parent element
      $('#gallery #'+img).remove() ;
    });

    $('#gallery').sortable('refresh');
    $('#gallery').sortable({
      axis: 'y',
      containment: 'document' ,
      cursor: 's-resize' ,
      distance: 10 ,
      helper: 'clone' ,
      items: 'li' ,
      opacity: 0.6 ,
      update: function(event, ui){
        $('#new_gallery_images').attr('value',implode(',' , $('#gallery').sortable('toArray')));
      }
    });

    //set our new images string
    $('#new_gallery_images').attr('value',implode( ',' , newImgs ) );

    //clean up and return
    newImgs = null ;
    numImgs = null ;
    currentImgs = null ;
    return true ;
  } else {

    return false ;
  }
  return false ;
}

function implode( glue, pieces ) {

  return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}