function createCookie(name,value,days) {
  var expires;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires="+date.toGMTString();
  } else {
    expires = "";
  }
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') {c = c.substring(1,c.length);}
    if (c.indexOf(nameEQ) === 0) {return c.substring(nameEQ.length,c.length);}
  }
  return null;
}
function eraseCookie(name) {
  createCookie(name,"",-1);
}
//Scrolling Background
var offset;
readCookie('cloudPos');
if (readCookie('cloudPos') < 0) {
  offset = readCookie('cloudPos');    
} else {
  offset = 0;    
}
function scrollbackground() {
  offset = offset - 1;
  $('body').css("background-position", offset + "px 0");
  createCookie('cloudPos',offset,1);
  setTimeout(function() {scrollbackground();}, 250);
}
//Contact Functions
function intialValue ($jThis,$jThisInput) {
  if ($jThisInput.attr('value') === '' || $jThisInput.attr('value') === $jThis.find('label').text())
  {
    $jThisInput.addClass('blank').attr('value', $jThis.find('label').text());      
  }
  $jThis.find('label').hide();
}
function isBlank($jThis) {
  $jThis.attr('value','');
  $jThis.removeClass('blank');
}
function checkValue($jThis) {
  var $jthisDivLabelText = $jThis.closest('div').find('label').text();
  var $jthisValue = $jThis.attr('value');
  if ($jthisValue.length < 1 || $jthisValue === $jthisDivLabelText)
  {
    $jThis.addClass('blank').attr('value', $jthisDivLabelText);      
  }
}
function showHint ($jThis) {
  $('.help').hide();
  $jThis.fadeIn();
}
function hideHint ($jThis) {
  $jThis.hide();
}


$(document).ready(function() {
  //Frontpage
  $('#frontPageBanner').cycle({
    fx: 'fade',
    timeout: 10000,
    random:1
  });
  
  //news list
  $('.nlItem').click(function() {
    window.location =  $(this).find('a:first').attr("href");
  });
  //FAQ
  $('.answer').hide();
  $('.question').click(function(){
    $(this).next('.answer').slideToggle();
  });
  //Gallery
  $('.fancybox').each(function() {
    $(this).attr('href', $(this).attr('id'));
  });
    
  //Scrolling Background
  //Start the animation
  scrollbackground();
  
  // Main Menu
  $('#topHeader ul li a.section').click(function () {
    $(this).parent().parent().find('ul').hide();
    $(this).parent().parent().find('ul').addClass('hide');
    $(this).parent().find('ul').slideToggle();
//    event.stopPropagation();
    return false;
  });
  $('body').click(function () {
    if ($('#topHeader ul li ul').is(':visible')) {
      $('#topHeader ul li ul').slideUp();
      }
  });
 //Store
  $('.thumbs img').mouseover(function() {
    $(this).closest('#storeItemPics').find('.big img').attr('src',$(this).attr('rel'));
  });
 
  //AGM Registration
  $('#AttendeesList div').children("div:gt(0)").each(function() {
    if ($(this).find('input').attr("value") === "")
    {
      $(this).hide();
    }
  });
  $('#AttendeesList .addAttendee').click(function() {
    $(this).parent().next().slideDown();
  });
  $('#AttendeesList .removeAttendee').click(function() {
    $(this).parent().find('input').attr("value","");
    $(this).parent().slideUp();
  });
  
  /*  Contact Page
  --------------------------------------*/
  //makes label text the starting value for form
  $('#msgFormContainer > div').each(function(){
    intialValue($(this),$(this).find('input[type="text"], textarea'));
    hideHint($(this).find('.help'));
  });
  $("#msgFormContainer > div input[type='text'].blank, #msgFormContainer > div textarea.blank").live('focus',function() {
    isBlank($(this));
  });
  $("#msgFormContainer > div input[type='text'], #msgFormContainer > div textarea").blur(function() {
    checkValue($(this));
    hideHint($(this).find('.help'));
  });
  $("#msgFormContainer > div input[type='text'], #msgFormContainer > div textarea").focus(function() {
    showHint($(this).next(".help"));
  });
});

//FaceBook Application Connect
window.fbAsyncInit = function() {
    FB.init({appId: '140225482687154', status: true, cookie: true, xfbml: true});
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());

