$(document).ready(function(){

   applyAutoAccordions();

   //Put span tag around asterixes in form labels
   applyAsterix();
   faqAccordion();

   $('#eis').prependTo('#right_col');
   $('#scheme-info').prependTo('#right_col');

   //Remove right col if empty
   checkRightCol();

// end document ready function
});


// SF Hover for IE6 top nav hover effects
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



//Function for applying right class to middle col if there is no related links in the right col
function checkRightCol(){
   if($('#right_col')[0] != null){
      if($('#right_col')[0].innerHTML.indexOf('eis') < 0 && $('#right_col')[0].innerHTML.indexOf('see-also') < 0 && $('#right_col')[0].innerHTML.indexOf('image-promo') < 0 && $('#right_col')[0].innerHTML.indexOf('scheme-info') < 0) {
         $('#mid_col').addClass('mid-col-wide');
      }
   }
};


function faqAccordion() {
 // Auto hides open faq panels onload and adds closed class to the li's
      $('.faq ul ul').hide();
     //$('.faq ul ul ul ul').show().addClass("bullet-list");
      $('.faq ul li').addClass("closed");
      $('.faq ul ul ul li').removeClass("closed");




var lis = $('.faq li');
for(var i=lis.length-1;i>-1;i--){
    var textFaq = lis[i].innerHTML;
    var newHtml = lis[i].innerHTML;

 var LIlength = $(lis[i]).siblings("li").length; 
    if (textFaq.indexOf('<ul') > -1 || textFaq.indexOf('<UL') > -1) {
	
	} else {
		if (LIlength > 0)
		{
			$(lis[i]).addClass("bullet-list");	
		} else {
			
		};

	};

// If the li has no ul's as children
   if ($(lis[i]).parents('ul').length > 2 || $(lis[i]).parents('UL').length > 2) {
    lis[i].innerHTML = textFaq;
$(lis[i]).children('ul').addClass("bullet-list").show();
    } else if (textFaq.indexOf('<ul') > -1 || textFaq.indexOf('<UL') > -1 ){
     var textIndex = textFaq.indexOf('<');
     var textStr = textFaq.substr(0,textIndex);
     var replaceStr = '<a href="#" class="anchor">' + textStr + '</a>';
     newHtml = textFaq.replace(textStr, replaceStr);
     lis[i].innerHTML = newHtml;
   };

};

// adds open - close functionality to the faq accordion panels and adds appropriate classes where required
$('.faq ul li a.anchor').click(function(){
  if ($(this).parent().hasClass('closed')){
    $(this).siblings().show('slow');
    $(this).parent("li").addClass("open").removeClass("closed");
    return false;
} else { 
    $(this).siblings().hide('slow');
    $(this).parent("li").addClass("closed").removeClass("open");
    return false;
       }
});


// FAQ - expand all functionality - opens all items and adds appropriate classes for styling
$('#expand-all').click(function (){
      $('.faq ul ul').show('slow');
      $('.faq ul li').addClass("open").removeClass("closed");
      $('.faq ul ul li').addClass("open").removeClass("closed");
          return false;
});
// FAQ - collapse all functionality - closes all items and adds appropriate classes for styling
$('#collapse-all').click(function (){
      $('.faq ul ul').hide('slow');
      $('.faq ul li').addClass("closed").removeClass("open");
      $('.faq ul ul li').addClass("closed").removeClass("open");
          return false;
});

}; // end faqAccordions()


//Functions to put a span tag around asterix symbols in forms
function replaceAsterix(origString){
  var inChar = '*';
  var outChar = '<span class="error">*</span>';
  var newString = origString.split(inChar);
  newString = newString.join(outChar);
  return newString;
};
function applyAsterix(){
   $("#mid_col form td:contains('*')").each(function(){
      var tempString = this.innerHTML;
      this.innerHTML = replaceAsterix(tempString);
   });
};



// IE6 workaround to allow top menu drop downs to hover above select form fields
jQuery.fn.activeXOverlap = function() {   
   
     $(this).each(function(i){  
         var h   = $(this).outerHeight();  
         var w   = $(this).outerWidth();  
         var iframe  = '<!--[if IE 6]>' +  
                       '<iframe src="javascript:false;" style="height: ' +  
                       h +  
                       'px; width: ' +  
                       w +  
                       'px" class="selectOverlap">' +  
                       '</iframe>' +  
                       '<![endif]-->'  
         $(this).prepend(iframe);  
      });  
 };
// tells which elements to hover above the select form fields
$(function() {
  $('#nav ul').activeXOverlap();
});



//Function for automatically create accordion bullet lists from the WYSIWYG, must have class 'accor' on the surrounding div
function applyAutoAccordions(){
$(".accor ul li").click(function(){
   if($(this).next('li').children('ul:eq(0)').is(":visible") == true ){
      $(this).next('li').children('ul:eq(0)').hide('fast'); 
	  $(this).removeClass('shown');
	}else{
	  $(this).next('li').children('ul:eq(0)').show('fast'); 
	  $(this).addClass('shown'); 
	}
});

$(".accor ul ul").hide();
}

