// --------------------------------------------
// High Contrast page preview - change css files
// --------------------------------------------

var $j = jQuery.noConflict();
$j(document).ready(function() {

 // Adds High Contrast icon
 $j('#high_con').html('<span id="hcon" title="switch to high contrast version of the site">High Contrast ON</span> &nbsp; <span id="hcoff" title="switch to normal version of the site">High Contrast OFF</span>'); 
 $j('#top_desc').html('<p>Change font size & colours on this site:</p>').css('width','115px');   
 $j('#top_desc_slim').html('<p>Change colours on this site:</p>').css('width','115px');  
 $j('#hcoff').hide();

 $j.fn.HCPreview = function() {
   
   var elements = this;
   
   return(
     elements.each(function(i) {       
       
       // Default values within scope
       var printLink = $j(this);
       var hcSheet = $j('link[title="high_contrast"]');
       
       printLink.click( function() {
         // Switch to high contrast
         $j('body').fadeOut('fast', function() {
           hcSheet.each(function() {
				this.media = 'all';
           });
           $j(this).fadeIn('slow', function(){
			   $j('#hcoff').show();
			   $j('#hcon').hide();
			   setHighCon("hcon");
           });
           
           $j('html, body').animate({scrollTop:0}, 'fast');

           // Switch Back
           $j('#hcoff').bind('click', function(){
             $j('body').fadeOut('fast', function() {
               hcSheet.each(function() {
				 this.media = 'none';
               });
               $j(this).fadeIn('slow');
               $j('#hcoff').hide();
               $j('#hcon').show();
               setHighCon("hcoff");
             });
             return false;
           });

         });
         
         return false;

       });
     })
   );
 }

 $j('#hcon').HCPreview();
 
 // reads high contrast from cookie
 readHighCon();      

});

// HIGH CONTRAST FUNCTIONS 

// saveCookie 

 function saveCookie(name,value,days) {
 	 if (days) {
		 var date = new Date();
		 date.setTime(date.getTime()+(days*24*60*60*1000))
		 var expires = "; expires="+date.toGMTString()
	 }
	 else expires = ""
	 document.cookie = name+"="+value+expires+"; path=/"
 }

// readCookie 

 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
 }


// saving high contrast to cookie 
 function setHighCon(HighID) {
     saveCookie("HighCon", HighID, "10");
 };

// reading high contrast form cookie 
  function readHighCon() {
     var CookieHighCon = readCookie("HighCon");

     if (CookieHighCon == 'hcon')  { $j('#hcon').click();} 

     if (CookieHighCon == 'hcoff')  {  $j('#hcoff').click();}

  };

// END OF JS FILE

