jQuery(document).ready(function($){
	
	if($('.accordion2').length) {
		 $('.accordion2').accordion({
			 obj:'div', 
			 trigger: ".loner", 
			 event: 'click', 
			 autoHeight: false, 
			 active: false, 
			 head: 'h3', 
			 collapsible: true
		});
	}
	//accordion content functionality
	doToggle();
	
	jQuery("a, h3, h4").focus (function() {
		jQuery(this).blur();
	});
	
	jQuery(".openme").click();

	jQuery("h3").focus(function() {
		jQuery(this).blur();
	});
	
	jQuery("#dnn_ctr420_SearchInput_txtSearch").val("Search");

	jQuery("#dnn_ctr420_SearchInput_txtSearch").focus(function() {
		jQuery(this).val("");
		jQuery(this).css({'color': '#000000'});
	});
	
	jQuery(".arrows").parent("h3").addClass("orange");

	jQuery("li").innerWrap("<div />");
	
	jQuery("#menu div.abscontainer:last").addClass("last");	
	/*jQuery("p.active").hide();
	*/
	
	addArrows();
	
	jQuery('.neuro-immune').flashembed('/portals/_default/skins/siteskin/flash/scope-therapy.swf');
	
	if(jQuery('.ms').length) {
		multiFeatures();
	}
	
	//EXTERNAL LINKS AUTO _BLANK:
 //open external links in new window
  $("a[href^='http']")
  .not("a[href*='" + window.location.host + "'])")
  .each(function() {
   $(this).attr("target", "_blank");
  }); 
  // for pdfs
  jQuery("a[href$='.pdf']").attr("target","_blank");
	
});

jQuery.fn.innerWrap = function() {
    var a, args = arguments;
    return this.each(function() {
        if (!a) a = jQuery.clean(args, this.ownerDocument);
        // Clone the structure that we're using to wrap
        var b = a[0].cloneNode(true),
        c = b; 
        // Find the deepest point in the wrap structure
        while (b.firstChild) b = b.firstChild; 
        // append the child nodes to the wrapper
        jQuery.each(this.childNodes,
        function(i, node) {
            b.appendChild(node);
        });
        jQuery(this) 
        // clear the element
        .empty() 
        // add the new wrapper with the previous child nodes appended
        .append(c);
    });
};


function runSiteScripts(path) {
	//here's the sIfr
	//code for the h1 tags
	var Avenir = {  src: path + 'Avenir.swf' };
	sIFR.activate(Avenir);
	sIFR.replace(Avenir, {
	  selector: 'h2', 
	  wmode: 'transparent', 
	  src:  path +  'Avenir.swf', 
	  css: [ '.sIFR-root {color:#4F2682; }' ]
	});
}


function activeSide(num) {
	jQuery("#side-menu").children().eq(num).addClass('hover');
}

function swapTogglers() {
	jQuery('.toggler.more').click(function() {
		jQuery(this).hide();
		jQuery('.toggler.close').show();
		jQuery(this).parent().next().accordion();
	});
	
	jQuery('.toggler.close').click(function() {
		jQuery(this).hide();
		jQuery('.toggler.more').show();
	});
}


//they want accordion instead of just expand/collapse
function doToggle() {
	jQuery('.accordion .toggler').click(function() {
		//close previously opened accordion and add class to distinguish it (otherwise if statement w/ toggle below becomes 
		jQuery('.revealed').toggle('slow').next().children().toggle();
		if(jQuery('.ui-state-active').length) {
			jQuery('.ui-state-active').click();
		}
		//toggle'learn more' for 'close'
		jQuery(this).children().toggle();

		//if this one was already open then it has been closed above, now just remove the class
		if((jQuery(this).prev().hasClass('revealed')) ) {
			jQuery(this).prev().removeClass('revealed'); }
			
		//otherwise open it on up and add the class
		else {
			jQuery('.revealed').removeClass('revealed');
			jQuery(this).prev().toggle('slow').addClass('revealed');
		}
		jQuery('.loner').click(function() {
			jQuery('.revealed').toggle('slow').next().children().toggle();
		});
		return false;
	});//.parent().toggle('slow');//hide();
}



function doTour() {
    //get first and last labeled w/ class; use later to remove appropriate arrow
    jQuery('.slides').children(":first").addClass('first');
    jQuery('.slides').children(":last").addClass('last');
    
    //fade the first image in
    jQuery('.slides .first').fadeIn(450, 'easeOutExpo').addClass('currentSlide');
    checkArrows();
    
    //get forward motion functional
    jQuery('.arrows .next a').bind('click', function() {
    jQuery('.currentSlide').stop().fadeOut(400, 'easeOutExpo').removeClass('currentSlide').next().stop().fadeIn(500, 'easeOutExpo').addClass('currentSlide');
    growTourArea();
    checkArrows();
    });
    
    //get backward motion functional
    jQuery('.arrows .prev a').bind('click', function() {
    jQuery('.currentSlide').stop().fadeOut(400, 'easeOutExpo').removeClass('currentSlide').prev().stop().fadeIn(500, 'easeOutExpo').addClass('currentSlide');
    growTourArea();
    checkArrows();
    
    });
}


function slideshow() {
	jQuery('.slides').cycle({
		timeout: 6000,
		speed: 800,
		next: '.tour .next',
		prev: '.tour .prev'
	});

}

function seeTour() {
	if(jQuery('.tour').parent().hasClass('revealed')) {
		return;
	}
	else {
		jQuery('.tour').parent().next().click();
	}
}

var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition=0;TopPosition=20
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}


function addArrows() {
	jQuery('.ui-accordion-header, .federal').each(function() {
		if(jQuery(this).hasClass('faker')) {
			null;
		} else {
		var _html = jQuery(this).html();
		jQuery(this).html(_html+"<span class='arrows'>&nbsp;&nbsp;</span>");
		}
	});
}



function research() {
	jQuery('.study').hover(function() {
		jQuery(this).addClass('hover');
		jQuery('.study').each(function() {
			if(jQuery(this).hasClass('hover')) {
				jQuery(this).stop().animate({
					opacity: 1
				}, 500, 'easeOutExpo');
			}else{
				jQuery(this).stop().animate({
					opacity: .70
				}, 400, 'easeOutExpo');
			}
		});
	}, function() {
		jQuery(this).removeClass('hover');
		jQuery('.study').each(function() {
			jQuery(this).stop().animate({
				opacity: 1
			}, 400, 'easeInSine');
		});
	});
}

function featured() {
	var _name
	jQuery('.study').bind('click', function() {
		_name = jQuery(this).children(0).attr('name');
		if(jQuery('.'+_name).hasClass('currentFeature')) {
			return;
		} else {
			moveUp(_name);
		}
	});
}

function moveUp(_name) {
	_target = (jQuery(".featured").offset().top)-100;
	jQuery("html, body").animate({scrollTop: _target}, 800, 'easeOutExpo', swapFeatures(_name));
}

function swapFeatures(_name) {
	jQuery('.currentFeature').stop().fadeOut(375, 'easeInExpo' ).removeClass('currentFeature');
	var _height = jQuery(".featured").find('.'+_name).height();
	jQuery('.featured').stop().animate({height: _height}, 700, 'easeOutExpo');
	jQuery(".featured").find('.'+_name).fadeIn(500, 'easeInExpo').addClass('currentFeature');
}

function multiFeatures() {
	$('.blind').css("opacity", "0");
	$('.ms .featured-pic').hover(function() {
		$('.blind').animate({
			opacity: .9
			}, 450);
	}, function() {
		$('.blind').animate({
			opacity: 0
			}, 250);
	});
}

function mapHover() {
	jQuery("#map-map").children().each(function() {
		jQuery(this).hover(function(e) {
			var _x = e.pageX;
			var _y = e.pageY;
			toolTipShow(jQuery(this), jQuery(this).attr('class'), _x, _y);
		},
		function() {
			return false;
		});
	});
}

function toolTipShow(el, txt, x, y) {
	x -= (jQuery(el).offset().left)-15;
	y -= jQuery(el).offset().top;
	jQuery(el).parent().prepend("<div class='tooltipholder'><div class='tooltip'/></div>");
	jQuery('.tooltip').css('opacity', '0').html("<p class='instructions'>Click for information on our "+txt+" location.</p>");
	jQuery('.tooltipholder').css("left", x).css("top",y);
	jQuery('.tooltip').animate({opacity: 0.9}, 300, "easeOutExpo");
	
	//call killing function/listener
	killTip();
}

function killTip(_hovered) {
	jQuery('.mappy').mouseout(function() {
		_hovered = false;
	/*});
	if(!_hovered) {*/
		jQuery('.tooltipholder').animate({opacity: 0}, 450, "easeOutExpo",
			function() {
				jQuery(this).remove();
		});
	});
}

function triggerForm() {
	jQuery('.formtrig').click(function() { jQuery(this).parent().parent().next().click(); }); 
}
