$(document).ready(function(){ 
	

	$('.asubnav .asubnav').show();
/*	
	$('.sub a').click(function() {
	
		$('.asubnav').hide('slow');
		$('.asubnav .asubnav').show();

		if ($(this).next('[class^=asubnav]').show())	{
			$(this).next('[class^=asubnav]').hide();
		}

		if ($(this).next('[class^=asubnav]').hide())	{
			$(this).nextAll('[class^=asubnav]').show();
		}
		
	});

	$('.subs a').click(function() {
	
		$('.asubnav').hide('slow');
		$('.asubnav .asubnav').show();

		if ($(this).next('[class^=asubnav]').show())	{
			$(this).next('[class^=asubnav]').hide();
		}

		if ($(this).next('[class^=asubnav]').hide())	{
			$(this).nextAll('[class^=asubnav]').show();
		}
		
	});
	
	*/
	//$("div.images").fadeIn(400).delay(800).slideUp();
	
	setInterval( "slideSwitch()", 5000 );
	
	$('.imagetext p').hide();
	$('.newsmore').hide();
	
	$('.first').click(function() {
	$('p#midd').hide();
	$('p#last').hide();
  $('p#first').toggle('slow', function() {
    // Animation complete.
  });
});

	$('.midd').click(function() {
	$('p#first').hide();
	$('p#last').hide();
  $('p#midd').toggle('slow', function() {
    // Animation complete.
  });
});

	$('.last').click(function() {
	$('p#first').hide();
	$('p#midd').hide();
  $('p#last').toggle('slow', function() {
    // Animation complete.
  });
});


	$('.attnbox').hide();
	
	$('a.attn').mouseover(function() { 
		$('.attnbox').show();
	
	}).mouseout(function() { 
		$('.attnbox').hide();
	
	});




	$('p.more').hide();
	
	$('.more').click(function() {
		$(this).parent().next('p').slideToggle();
	});

	// Show More News
	
	$('#clickmore').click(function() {
		$('.newsmore').slideToggle('slow', function() { 
		
		});
	});

        $('.feature ul').bxSlider({
            prevText: '',
            nextText: '&#187; next image' 
        });
        $('.vfeature ul').bxSlider({
            prevText: '',
            nextText: '&#187; next image' 
        });

	$(".body a[rel^='prettyPhoto']").prettyPhoto();  

}); // Document Ready



/* Slide Switch Function */

function slideSwitch() {
    var $active = $('.images img.active');

    if ( $active.length == 0 ) $active = $('.images IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('.images IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

/**
 *    Validation code for BDS CIP Form
 */
 
  function validDate(date, epyt)
  {
    var checkDate = date;
    
    if (checkDate.toLowerCase() == "dd/mm/yyyy") {
      alert("Please enter a " + epyt + " publication date in the specified format");
      return false;      
    }
    
    if(checkDate.length != 10)
    {
      alert("Please enter a " + epyt + " publication date in the specified format");
      return false;
    }
    else
    {
      checkBuffer = checkDate.split("/");
      if (checkBuffer.length != 3)
      {
        alert("Please enter a " + epyt + " publication date in the specified format");
        return false;
      }
      else
      {      
        var today   = new Date();
        var day   = pad(today.getDate());
        var month = pad(today.getMonth() + 1);
        var year  = today.getFullYear();
        
        // Check the year
        if(checkBuffer[2] > year.toString())
        {
          return true;
        }
        else
        {
          if((checkBuffer[2] == year.toString())&&(checkBuffer[1] > month.toString()))
          {
            return true;
          }
          else
          {
            if((checkBuffer[2] == year.toString())&&(checkBuffer[1] == month.toString())&&(checkBuffer[0] > day.toString()))
            {
              return true;
            }
            else
            {
              alert("The " + epyt + " publication date you entered is past. Please enter a valid " + epyt + " publication date");
              return false;
            }
          }
        }
      } 
    }  
  }

  function pad(value)
  {
    if(value < 10)
    {
      value = "0" + value;
    }
    return value;
  }

  function checkEAN(eanToCheck)
  {
    var intEanIntegers;
    var intBarCodeResult;
    var WeightingArray;
    var intResult;
    var CheckDigit;
    var intCheckSum;
    var checkEAN;
    
    checkEAN = false;
    intResult = 0;
    intEANIntegers = [0,1,2,3,4,5,6,7,8,9,10,11,12];
    intBarCodeResult = [0,1,2,3,4,5,6,7,8,9,10,11,12];
    WeightingArray = [1,3,1,3,1,3,1,3,1,3,1,3];
    
    eanToCheck = eanToCheck.replace(/\ /g, "");
    eanToCheck = eanToCheck.replace(/\-/g, "");

    if(eanToCheck.length != 13)
    {
      return false;
    }

    for(i=0;i<12;i++)
    {
      //try{
        intEANIntegers[i] = parseInt(eanToCheck.substr(i,1));   
        intBarCodeResult[i] = intEANIntegers[i] * WeightingArray[i];
        intResult = intResult + intBarCodeResult[i];
      //}catch(err){}
    }

    CheckDigit = eanToCheck.substr(12,1);
    CheckDigit = CheckDigit.toUpperCase();

    if(CheckDigit == "X")
    {
      intResult = intResult + 10
    }
    else
    {
      intResult = intResult + parseInt(CheckDigit);
    }

    intCheckSum = (intResult % 10);

    if(intCheckSum == 0)
    {
      return true;
    }
  }
  
  function baby_validate(feggorm)
  {
    var DoValidate = false;
    
    // Is an ISBN present?
    if(($('#hb_isbn').val() != "" || $('#pb_isbn').val()) != "")
    {
      if($('#hb_isbn').val() != "")
      {
        if(checkEAN($('#hb_isbn').val()) == true)
        {
          if(validDate($('#hb_pub_date').val(),"Hardback"))
          {
            HB = true;
          }
          else
          {
            HB = false;
            $('#hb_pub_date').focus();
          }
        }
        else
        {
          HB = false;
          alert("Please enter a valid Hardback ISBN Number");
          $('#hb_isbn').focus();
        }
      }
      else
      {
        HB = false;
      }
    
      if($('#pb_isbn').val() != "")
      {
        if(checkEAN($('#pb_isbn').val()) == true)
        {
          if(validDate($('#pb_pub_date').val(),"Paperback"))
          {
            PB = true;
          }
          else
          {
            PB = false;
            $('#pb_pub_date').focus();
          }
        }
        else
        {
          PB = false;
          alert("Please enter a valid Paperback ISBN Number");
          $('#pb_isbn').focus();
        }
      }
      else
      {
        PB = false;
      }
    
      if((PB == true)||(HB == true))
      { 
        if((($('#author').val() != "" || $('#editor').val()) != ""))
        {
          aut = true;
        }
        else
        {
          aut = false;
          alert("You must enter either an Author or Editor's name");
          $('#author').focus();
        }
    
        if(document.getElementById("readership_level_children").checked == true)
        {
          if($('#cbmc').val() != "")
          {
            age = true;
          }
          else
          {
            alert("You must enter Key Stage information");
            $('#cbmc').focus();
            age = false;
          }  
        }
        else
        {
          age = true;
        }
    
        if(((HB == true ) || (PB == true)) && (aut == true) && (age == true))
        {
          DoValidate = true;
          $("#"+feggorm).submit();
        }
      }
    }
    else
    {
      alert("You must enter either a hardback or paperback ISBN");
      $('#hb_isbn').focus()
    }    
  }
  
  function checkandclear(theVal,theField)
  {
    
    theVal = theVal.toLowerCase();
    
    if(theVal == "")
    {
      $("#"+theField).val("DD/MM/YYYY");
    }
    
    if(theVal == "dd/mm/yyyy")
    {
      $("#"+theField).val("");
    }
    
  }
