jQuery.fn.highlight = function(pat,tit) {
 function innerHighlight(node, pat, tit) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'keyword-term';
    spannode.setAttribute('title', tit);
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat, tit);
   }
  }
  return skip;
 }
 return this.each(function() {
  innerHighlight(this, pat.toUpperCase(), tit);
 });
};

jQuery.fn.removeHighlight = function() {
 return this.find("span.keyword-term").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};

function processPage(keywords) {
	var docElements = $("#content .node-body, #content .procedure");	
	$.each(keywords, function(name,value) {
		docElements.highlight(name, value);
	});
}


$(function(){
	
	//sectionsTree
	var sectionOpenId = $.cookie('sectionOpen');
	$('span.procedures-list').not('#'+sectionOpenId).hide();
	
	$('a.email-button').button( { icons: { primary: 'ui-icon-mail-closed'} });
	
	$('#sectionsTree ul').treeshow({
		animated: "slow",
		collapsed: true,
		unique: true,
		persist: "cookie",
		toggle: function() {
			$(this).find('>span h4').toggleClass('state-active');
		}
		
	});
	
	$('#'+sectionOpenId).prev('h4.procedures-toggle').addClass('state-active');
			
	$('#sectionsTree h4').hover(
		function(){
			$(this).addClass('state-hover');	
		},
		function() {
			$(this).removeClass('state-hover');
		}
	);
	
	
	
	
	$('#sectionsTree h4.procedures-toggle').bind('click', function(){
			var thisSectionId = $(this).next('span.procedures-list').attr('id');
			
			if( $('#'+thisSectionId).is(':hidden') ) { 
				$('#sectionsTree h4.procedures-toggle.state-active').removeClass('state-active').next('span.procedures-list').slideUp(); 
				
				$(this).addClass('state-active'); $('#'+thisSectionId).slideDown();
				
				$.cookie('sectionOpen', thisSectionId, { path:Croogo.basePath } );
			}
			
			else {
				
				$(this).removeClass('state-active'); $('#'+thisSectionId).slideUp(); 
				
				$.cookie('sectionOpen', null, { path:Croogo.basePath } );
			}
			
			return false; 
		});
	
		
	$('a.section-info').click(function() {
		
		return false;
	})
	
	$('#sectionsTree').fadeIn('slow');
	$('#lcc').hide();
	//sectionsTree end
	
	
    $('#blocknode-marketing a,#blocknode-dentist a').css('opacity','0.75');
    $('#blocknode-marketing a,#blocknode-dentist a').hover(
		function() {
			$(this).animate({opacity:1}, 200);
		}, 
		function() {
			$(this).animate({opacity:0.75}, 300);
		}
	);
    //procedures - left menu   
	/* 
    $('#beauty-procedures-menu').accordion(
    {
    	 collapsible: true,
    	 active: false,
		 autoHeight: false,
    	 navigation: true,
    	 header: "h3"
    });
*/
    
    $('.toggle-panel').click(function(){
       var target = '#' + $(this).attr('rel');
       $(target).toggle('slow');
       return false;
    });
    
    //$("#newsflash .container").after('<div id="news-pager">').cycle({
//    	fx:	'scrollLeft',
//    	timeout:7000,
//    	delay: -2000,
//		pager:  '#news-pager'	  
//    	
//    });
    
    $('#rotator').cycle({
		fx:     'fade', 
		speed:   3000, 
		timeout: 3000,
		next:   '#rotator', 
    	pause:   1 
	}); 
	
    $('.slideshow').cycle({
		fx: 'fade',
		timeout: 2500,
        speed:  1500
	});

    $('#locations-infobox').accordion( {
        autoHeight: false,
        navigation: true
    });
    
//    $('.openPanel').click(function(){
//        var myhref = $(this).attr('href');
//        $("h4 a[href='"+myhref+"']").next('div.address').show();
//        return false;
//    });
    
    $('.xtabs').tabs(
    
    ); 
    
    
    
    
    
    $('#genderTabs').tabs({cookie: { path:Croogo.basePath  }}).fadeIn('slow');    
	$('a.female-positions,a.male-positions').css('opacity','0.5');
    
	$('a.female-positions').hover(
		function(){
			$(this).animate({opacity:1}, 200);
			$('#shownFemaleResults').attr('id','').fadeOut();
			$(this).next('div.map-area').attr('id','shownFemaleResults').fadeIn();
		},
		function(){
			$(this).animate({opacity:0.5}, 100);
		}
	
	);
	$('a.male-positions').hover(
		function(){
			$(this).animate({opacity:1}, 200);
			$('#shownMaleResults').attr('id','').fadeOut();
			var maparea = $(this).next('div.map-area');
			$(maparea).attr('id','shownMaleResults').fadeIn();
		},
		function(){
			$(this).animate({opacity:0.5}, 100);
		}
	
	);
	$('a.map-area-close').click(function(){
		var openedarea = $(this).parent('div.map-area');	
		$(openedarea).attr('id','').fadeOut();
		return false;	
	});
		
	$('#map-procedures-nav').removeClass('loader');
    
		
	
	$('#CommentAddForm input,#CommentAddForm textarea,#GftForm input,#GftForm textarea').tipsy({ trigger: 'focus', gravity: 'w' });
	//$('a.blog.view').colorbox({innerWidth:760, opacity:0.5, close:"X" });
	$('a.mybox').colorbox({ maxWidth:"90%", maxHeight:"90%", scalePhotos:true, close:"X"});
	$('a.youtube-link').colorbox({iframe:true, innerWidth:480, innerHeight:360, close:"X" });
});