// JavaScript Document



$(document).ready(function(){

  /* Home Ages switch On Load */
  if($.cookie('age') == 'age9' || $.cookie('age') == 'age13') {
	$('#subjectshome div a').attr('class','');
	$('#subjectshome ul').hide();

	$('#subjectshome ul.' + $.cookie('age')).show();
	$(this).attr('class','active');
	$('#subjectshome div.'+$.cookie('age')+' a').addClass('active');
  }

	

  /* Arabic attachments */	
  $('table#attachments').attr('dir','ltr');  
  

  /* Home Ages switch */
    $('#subjectshome div a').click( function() {
	  $('#subjectshome div a').attr('class','');
	  $('#subjectshome ul').hide();
	  
	  $('#subjectshome ul.' + $(this).parent().attr('class')).show();
	  $(this).attr('class','active');
	  
	  $.cookie('age', $(this).parent().attr('class'), { path: '/', expires: 10 });
	  
	  $(this).blur();
	  return false;
    });
	
	/* Home Teachers box */
	$('#teachers p a').hover(
      function () {
        $('#teachers h3 a').addClass("active");
      }, 
      function () {
        $('#teachers h3 a').removeClass("active");
      }
    );


  /* Image enlarge */
    $('.page img').parent().addClass('image');
 
 	$('.page img').click( function() {
	  var thisImage = $(this); // So it can be referenced under error()
	  
	  imageSrc = $(this).attr('src');
	  imageSrcPreview = imageSrc.replace('.page','.preview');
	  
	  var newImage = new Image();
	  $(newImage).load( function() {

      	$('#popup-inner-inner').html(this);	
		$('#popup').addClass('image');
		showVale();
	  	$('#popup').fadeIn("slow");	
									
	  }).error( function() {
		$(thisImage).fadeTo('slow', 0.66);
	  }).attr('src', imageSrcPreview);

    });



 
	/* Close popup and vale */
	$('#vale, #close-popup').click( function() {
      $('#vale, #popup').hide();
	  $('#popup-inner-inner').html('');
	  $('#popup').removeClass('image');
	  $(this).blur();
	  return false;
    });
	

	

  /* Load Video Details onto page  */
    $('.page .video').each( function() {
		videoNodeUrl = $(this).find('a').attr('href');
		$(this).load(videoNodeUrl + ' .videobox', '', videoboxSetup());
    });



  /* Test */
  if($('#quiz input').length > 0) {
	$('div.next').hide();
  }
  
  $('#quiz input').change(function() {
		$(this).parent().parent().find('label').removeClass();
		$(this).parent().addClass('checked');					   
  });


  /* Attachments */
  $("#attachments td a[@href$='pdf'], #attachments td a[@href$='PDF']").addClass("pdf").attr('target', '_blank');


});


function showVale() {
      $('#vale').css('opacity',0.01);
	  $('#vale').show();
	  $('#vale').fadeTo("slow", 0.66);
	  closeHover();
}

function closeHover() {
  $("#close-popup").css('opacity',0.5);
  $("#close-popup").hover(
      function () {
        $(this).fadeTo("fast", 1);
      }, 
      function () {
        $(this).fadeTo("fast", 0.5);
      }
    );
}

function videoboxSetup() {
    $('.page .video').css("cursor","pointer");
	
  /* Load 'video'  */
    $('.page .video').click( function() {
	  myUrl = $(this).find('a').attr('href');

    		$.ajax({
        		type: "POST",
        		url: myUrl,
        		data: "",
        		dataType: "html",
        		success: function(data){
				
					var hexColor = '999999';
					if( $('.page.history').length > 0 ) {
						hexColor = 'EE7E33';
					} else if( $('.page.geography').length > 0 ) {
						hexColor = 'CAC032';
					} else if( $('.page.science').length > 0 ) {
						hexColor = 'D3AC36';
					}
					
					var flashPlayer = $(data).find('#flashplayer').html();
					flashPlayer = flashPlayer.replace('c=999999', 'c='+hexColor);
            		$("#popup-inner-inner").html(flashPlayer);

      				showVale();
				    $('#popup').fadeIn("slow");
        		}
    		});	


	  
	  $(this).blur();
	  return false;
    });
	
}

