var $j = jQuery.noConflict();

$j(document).ready(function() {

   $j('div#text_size').html('<p>Text size &nbsp;<a id="small">A</a> | <a id="medium">A</a> | <a     id="large">A</a>');          /* adds Text size menu */

   readTextSize();           /* reads text size from cookie */


   $j('div#text_size a').each(function(){             /* on clicking text size menu */
       $j(this).click(function(){
          var id = $j(this).attr('id');

          resizeTxt(id);                              /*resizing function */
       });
    });
});


/* *************************** FUNCTIONS ******************************************* */

/* resizing function */

function resizeTxt(id) {            
    if(id=='small') { pre_height_refresh(); small_size(); post_height_refresh(); setTextSize("small")}

    if(id=='medium') { pre_height_refresh(); medium_size(); post_height_refresh(); setTextSize("medium")}

    if(id=='large') { pre_height_refresh(); large_size(); post_height_refresh(); setTextSize("large")}
    };

/* this 3 functions changes letter colors and changes body font size */

function small_size(){
   $j('body').css('font-size','11px'); 
   $j('#small').css('color','#5191CD');  
   $j('#medium').css('color','#898989');
   $j('#large').css('color','#898989');
   $j('#lhs_in_top_box').css('height','265px');
   $j('.in6_pink').css('font-size','14px').removeClass('custom');
   $j('.in6_pink').css('font-size','14px').addClass('custom-small');
   $j('.in6_date').css('font-size','20px');
   /*$j('#header').css('width','933px');
   $j('#sub_nav').css('width','950px');*/
   $j('#main_nav ul li a span').removeClass('large_nav');


};


function medium_size(){
   $j('body').css('font-size','13px'); 
   $j('#small').css('color','#898989'); 
   $j('#medium').css('color','#5191CD'); 
   $j('#large').css('color','#898989');
   $j('#lhs_in_top_box').css('height','265px');
   $j('.in6_pink').css('font-size','14px').removeClass('custom');
   $j('.in6_pink').css('font-size','14px').removeClass('custom-small');
   $j('.in6_date').css('font-size','18px');
/*   $j('#header').css('width','933px');
   $j('#sub_nav').css('width','950px'); */
   $j('#main_nav ul li a span').removeClass('large_nav');

};


function large_size(){   
   $j('body').css('font-size','15px');
   $j('#small').css('color','#898989');
   $j('#medium').css('color','#898989');
   $j('#large').css('color','#5191CD');
   $j('#lhs_in_top_box').css('height','auto');
   $j('.in6_pink').css('font-size','14px').addClass('custom');
   $j('.in6_date').css('font-size','20px');
   $j('.in6_pink').css('font-size','14px').removeClass('custom-small');
/*   $j('#header').css('width','1030px');
   $j('#sub_nav').css('width','1050px'); */
   $j('#main_nav ul li a span').addClass('large_nav');
   $j('.home_push_box_content').css('height','auto');
};

/* ******* because of design issues - scripts that changes div's height attribute ****** */

/* reseting it's height */

function pre_height_refresh(){
   $j('div.gen_push_box2_bg').css('height','auto');
   auto_height(); 
   adverts_auto_height();
};


 function auto_height(){
     $j("#box_height1").css('height','auto'); 
     $j("#box_height2").css('height','auto'); 
     $j("#box_height3").css('height','auto');
};

/* tv adverts boxes problem*/

 function adverts_auto_height(){
   $j('.ad_box_content p').each(function(){
      $j(this).css('height','auto')
   }); 
 
   $j('.ad_box_content h2').each(function(){
      $j(this).css('height','auto');
   });
  };

/* setting it's height after changing size */

function post_height_refresh(){
   box_height();
   image_align_2(); 
   header_align_2(); 
   if ($j('.sub_nav_last').text() == 'TV Adverts') {adverts_content_box();};
   m3_height();
   
};

function box_height(){
     var one_height = $j("#box_height1").height();
     var two_height = $j("#box_height2").height();
     var three_height = $j("#box_height3").height();

     if (one_height > two_height && one_height > three_height)
      {
      $j("#box_height2").height(one_height);
      $j("#box_height3").height(one_height);
      $j("#lhs_top_bg").height(one_height + 32);
      }

      else if (two_height > one_height && two_height > three_height)
      {
      $j("#box_height1").height(two_height);
      $j("#box_height3").height(two_height);
      $j("#lhs_top_bg").height(two_height + 32);
       }

      else if (three_height > one_height && three_height > two_height)
      {
      $j("#box_height1").height(three_height);
      $j("#box_height2").height(three_height);
      $j("#lhs_top_bg").height(three_height + 32);
       }
 };

function image_align_2(){
   var max_par_height = 0;

   $j('.ad_box_content p').each(function(){
     if ($j(this).height() > max_par_height) {max_par_height = $j(this).height();}
   });

   $j('.ad_box_content p').each(function(){
      $j(this).height(max_par_height);
   });
  };


 function header_align_2(){
    var max_header_height = 0;

    $j('.ad_box_content h2').each(function(){
       if ($j(this).height() > max_header_height) {max_header_height = $j(this).height();}
    });

    $j('.ad_box_content h2').each(function(){
       $j(this).height(max_header_height);
    });
 };

function adverts_content_box(){
   $j('#in2_center2_content').css('height','auto');
   var new_height = $j('.ad_box').height() * 2.25;
   $j('#in2_center2_content').height(new_height);
};


/* ************************************* Cookies text size scripts ******************** */
/* 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
 }

/* deleteCookie */

 function deleteCookie(name) {
	 saveCookie(name,"",-1)
 }


/* ********** Text size stuff ******* */

/* saving text size to cookie */
 function setTextSize(TextID) {
     saveCookie("TextSize", TextID, "10");
 };


/* reading text size form cookie */
  function readTextSize() {
     var CookieTextSize = readCookie("TextSize");

     if (CookieTextSize == 'small')  { resizeTxt('small');} 

     if (CookieTextSize == 'medium')  { resizeTxt('medium');}

     if (CookieTextSize == 'large')   { resizeTxt('large');}

  };

